blob: 20b24572cef49f677f5db947ee30f8b70fbfefd5 [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 Moolenaar05159a02005-02-26 23:04:13 +0000362#ifndef FEAT_PROFILE
363# define ex_profile ex_ni
364#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200365#ifndef FEAT_TERMINAL
366# define ex_terminal ex_ni
367#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200368#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
369# define ex_xrestore ex_ni
370#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100371#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200372# define ex_popupclear ex_ni
373#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000374
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375/*
376 * Declare cmdnames[].
377 */
378#define DO_DECLARE_EXCMD
379#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200380#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100381
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382static char_u dollar_command[2] = {'$', 0};
383
384
385#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100386// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387typedef struct
388{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100389 char_u *line; // command line
390 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391} wcmd_T;
392
393/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000394 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000396 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000398struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100400 garray_T *lines_gap; // growarray with line info
401 int current_line; // last read line from growarray
402 int repeating; // TRUE when looping a second time
403 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200404 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 void *cookie;
406};
407
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200408static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100409static int store_loop_line(garray_T *gap, char_u *line);
410static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000411
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100412// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000413struct dbg_stuff
414{
415 int trylevel;
416 int force_abort;
417 except_T *caught_stack;
418 char_u *vv_exception;
419 char_u *vv_throwpoint;
420 int did_emsg;
421 int got_int;
422 int did_throw;
423 int need_rethrow;
424 int check_cstack;
425 except_T *current_exception;
426};
427
Bram Moolenaared203462004-06-16 11:19:22 +0000428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100429save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000430{
431 dsp->trylevel = trylevel; trylevel = 0;
432 dsp->force_abort = force_abort; force_abort = FALSE;
433 dsp->caught_stack = caught_stack; caught_stack = NULL;
434 dsp->vv_exception = v_exception(NULL);
435 dsp->vv_throwpoint = v_throwpoint(NULL);
436
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100437 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000438 dsp->did_emsg = did_emsg; did_emsg = FALSE;
439 dsp->got_int = got_int; got_int = FALSE;
440 dsp->did_throw = did_throw; did_throw = FALSE;
441 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
442 dsp->check_cstack = check_cstack; check_cstack = FALSE;
443 dsp->current_exception = current_exception; current_exception = NULL;
444}
445
446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100447restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000448{
449 suppress_errthrow = FALSE;
450 trylevel = dsp->trylevel;
451 force_abort = dsp->force_abort;
452 caught_stack = dsp->caught_stack;
453 (void)v_exception(dsp->vv_exception);
454 (void)v_throwpoint(dsp->vv_throwpoint);
455 did_emsg = dsp->did_emsg;
456 got_int = dsp->got_int;
457 did_throw = dsp->did_throw;
458 need_rethrow = dsp->need_rethrow;
459 check_cstack = dsp->check_cstack;
460 current_exception = dsp->current_exception;
461}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462#endif
463
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464/*
465 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
466 * command is given.
467 */
468 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100469do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100470 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471{
472 int save_msg_scroll;
473 int prev_msg_row;
474 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100475 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000476
477 if (improved)
478 exmode_active = EXMODE_VIM;
479 else
480 exmode_active = EXMODE_NORMAL;
481 State = NORMAL;
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +0200482 trigger_modechanged();
Bram Moolenaardf177f62005-02-22 08:39:57 +0000483
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100484 // When using ":global /pat/ visual" and then "Q" we return to continue
485 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000486 if (global_busy)
487 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488
489 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100490 ++RedrawingDisabled; // don't redisplay the window
491 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100493 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 ++hold_gui_events;
495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496
Bram Moolenaar32526b32019-01-19 17:43:09 +0100497 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 while (exmode_active)
499 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100500 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000501 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
502 {
503 exmode_active = FALSE;
504 break;
505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 msg_scroll = TRUE;
507 need_wait_return = FALSE;
508 ex_pressedreturn = FALSE;
509 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100510 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 prev_msg_row = msg_row;
512 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 if (improved)
514 {
515 cmdline_row = msg_row;
516 do_cmdline(NULL, getexline, NULL, 0);
517 }
518 else
519 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
520 lines_left = Rows - 1;
521
Bram Moolenaardf177f62005-02-22 08:39:57 +0000522 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100523 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000525 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100526 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000527 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000529 if (ex_pressedreturn)
530 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100531 // go up one line, to overwrite the ":<CR>" line, so the
532 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000533 msg_row = prev_msg_row;
534 if (prev_msg_row == Rows - 1)
535 msg_row--;
536 }
537 msg_col = 0;
538 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
539 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100542 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000543 {
544 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100545 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000546 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100547 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 }
550
551#ifdef FEAT_GUI
552 --hold_gui_events;
553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 --RedrawingDisabled;
555 --no_wait_return;
556 update_screen(CLEAR);
557 need_wait_return = FALSE;
558 msg_scroll = save_msg_scroll;
559}
560
561/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200562 * Print the executed command for when 'verbose' is set.
563 * When "lnum" is 0 only print the command.
564 */
565 static void
566msg_verbose_cmd(linenr_T lnum, char_u *cmd)
567{
568 ++no_wait_return;
569 verbose_enter_scroll();
570
571 if (lnum == 0)
572 smsg(_("Executing: %s"), cmd);
573 else
574 smsg(_("line %ld: %s"), (long)lnum, cmd);
575 if (msg_silent == 0)
576 msg_puts("\n"); // don't overwrite this
577
578 verbose_leave_scroll();
579 --no_wait_return;
580}
581
582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 * Execute a simple command line. Used for translated commands like "*".
584 */
585 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100586do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 return do_cmdline(cmd, NULL, NULL,
589 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
590}
591
592/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100593 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
594 * This allows for using a range without ":" in Vim9 script.
595 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200596 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100597do_cmd_argument(char_u *cmd)
598{
599 return do_cmdline(cmd, NULL, NULL,
600 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
601}
602
603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 * do_cmdline(): execute one Ex command line
605 *
606 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100607 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 * 2. Split up in parts separated with '|'.
609 *
610 * This function can be called recursively!
611 *
612 * flags:
613 * DOCMD_VERBOSE - The command will be included in the error message.
614 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100615 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 * DOCMD_KEYTYPED - Don't reset KeyTyped.
617 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
618 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
619 *
620 * return FAIL if cmdline could not be executed, OK otherwise
621 */
622 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100623do_cmdline(
624 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200625 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100626 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100627 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100629 char_u *next_cmdline; // next cmd to execute
630 char_u *cmdline_copy = NULL; // copy of cmd line
631 int used_getline = FALSE; // used "fgetline" to obtain command
632 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100634 int count = 0; // line number count
635 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 int retval = OK;
637#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100638 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100639 garray_T lines_ga; // keep lines for ":while"/":for"
640 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200641 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100642 char_u *fname = NULL; // function or script name
643 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
644 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
645 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200647 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200648 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100650 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200651 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000653 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000655 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100657# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658# define cmd_cookie cookie
659#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100660 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200661#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100662 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
663 // location for storing error messages to be converted to an exception.
664 // This ensures that the do_errthrow() call in do_one_cmd() does not
665 // combine the messages stored by an earlier invocation of do_one_cmd()
666 // with the command name of the later one. This would happen when
667 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 saved_msg_list = msg_list;
669 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670#endif
671
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100672 // It's possible to create an endless loop with ":execute", catch that
673 // here. The value of 200 allows nested function calls, ":source", etc.
674 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100675 if (call_depth >= 200
676#ifdef FEAT_EVAL
677 && call_depth >= p_mfd
678#endif
679 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100681 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100683 // When converting to an exception, we do not include the command name
684 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100685 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 msg_list = saved_msg_list;
687#endif
688 return FAIL;
689 }
690 ++call_depth;
691
692#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100693 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 cstack.cs_idx = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
696
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100697 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100699 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100700 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000701 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 ++ex_nesting_level;
703
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100704 // Get the function or script name and the address where the next breakpoint
705 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000706 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {
708 fname = func_name(real_cookie);
709 breakpoint = func_breakpoint(real_cookie);
710 dbg_tick = func_dbg_tick(real_cookie);
711 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100712 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100714 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 breakpoint = source_breakpoint(real_cookie);
716 dbg_tick = source_dbg_tick(real_cookie);
717 }
718
719 /*
720 * Initialize "force_abort" and "suppress_errthrow" at the top level.
721 */
722 if (!recursive)
723 {
724 force_abort = FALSE;
725 suppress_errthrow = FALSE;
726 }
727
728 /*
729 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000730 * exception handling (used when debugging). Otherwise clear it to avoid
731 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000733 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000734 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000735 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200736 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
738 initial_trylevel = trylevel;
739
740 /*
741 * "did_throw" will be set to TRUE when an exception is being thrown.
742 */
743 did_throw = FALSE;
744#endif
745 /*
746 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000747 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 * If force_abort is set, we cancel everything.
749 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100750#ifdef FEAT_EVAL
751 did_emsg_cumul += did_emsg;
752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 did_emsg = FALSE;
754
755 /*
756 * KeyTyped is only set when calling vgetc(). Reset it here when not
757 * calling vgetc() (sourced command lines).
758 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100759 if (!(flags & DOCMD_KEYTYPED)
760 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 KeyTyped = FALSE;
762
763 /*
764 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000765 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 * - for multiple commands on one line, separated with '|'
767 * - when repeating until there are no more lines (for ":source")
768 */
769 next_cmdline = cmdline;
770 do
771 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000772#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100773 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000774#endif
775
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100776 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 if (next_cmdline == NULL
778#ifdef FEAT_EVAL
779 && !force_abort
780 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000781 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782#endif
783 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100784 {
785#ifdef FEAT_EVAL
786 did_emsg_cumul += did_emsg;
787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
791 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000792 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100793 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 * 3. If a line is given: Make a copy, so we can mess with it.
795 */
796
797#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100798 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000799 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100801 // Each '|' separated command is stored separately in lines_ga, to
802 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100803 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100805 // Check if a function has returned or, unless it has an unclosed
806 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000809# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000810 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000811 func_line_end(real_cookie);
812# endif
813 if (func_has_ended(real_cookie))
814 {
815 retval = FAIL;
816 break;
817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000820 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100821 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000822 script_line_end();
823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100825 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100826 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 {
828 retval = FAIL;
829 break;
830 }
831
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100832 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 if (breakpoint != NULL && dbg_tick != NULL
834 && *dbg_tick != debug_tick)
835 {
836 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100837 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100838 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 *dbg_tick = debug_tick;
840 }
841
842 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100843 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100845 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100847 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100849 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100850 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100852 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100853 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 *dbg_tick = debug_tick;
855 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000856# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000857 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000858 {
859 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100860 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100861 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000862 script_line_start();
863 }
864# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866#endif
867
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100868 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 if (next_cmdline == NULL)
870 {
871 /*
872 * Need to set msg_didout for the first line after an ":if",
873 * otherwise the ":if" will be overwritten.
874 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100875 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100877 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878#ifdef FEAT_EVAL
879 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
880#else
881 0
882#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100883 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
884 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100886 // Don't call wait_return for aborted command line. The NULL
887 // returned for the end of a sourced file or executed function
888 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (KeyTyped && !(flags & DOCMD_REPEAT))
890 need_wait_return = FALSE;
891 retval = FAIL;
892 break;
893 }
894 used_getline = TRUE;
895
896 /*
897 * Keep the first typed line. Clear it when more lines are typed.
898 */
899 if (flags & DOCMD_KEEPLINE)
900 {
901 vim_free(repeat_cmdline);
902 if (count == 0)
903 repeat_cmdline = vim_strsave(next_cmdline);
904 else
905 repeat_cmdline = NULL;
906 }
907 }
908
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100909 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 else if (cmdline_copy == NULL)
911 {
912 next_cmdline = vim_strsave(next_cmdline);
913 if (next_cmdline == NULL)
914 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200915 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 retval = FAIL;
917 break;
918 }
919 }
920 cmdline_copy = next_cmdline;
921
922#ifdef FEAT_EVAL
923 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200924 * Inside a while/for loop, and when the command looks like a ":while"
925 * or ":for", the line is stored, because we may need it later when
926 * looping.
927 *
928 * When there is a '|' and another command, it is stored separately,
929 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000930 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200931 *
932 * Pass a different "fgetline" function to do_one_cmd() below,
933 * that it stores lines in or reads them from "lines_ga". Makes it
934 * possible to define a function inside a while/for loop and handles
935 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200937 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200939 cmd_getline = get_loop_line;
940 cmd_cookie = (void *)&cmd_loop_cookie;
941 cmd_loop_cookie.lines_gap = &lines_ga;
942 cmd_loop_cookie.current_line = current_line;
943 cmd_loop_cookie.getline = fgetline;
944 cmd_loop_cookie.cookie = cookie;
945 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
946
947 // Save the current line when encountering it the first time.
948 if (current_line == lines_ga.ga_len
949 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 {
951 retval = FAIL;
952 break;
953 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200954 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200956 else
957 {
958 cmd_getline = fgetline;
959 cmd_cookie = cookie;
960 }
961
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 did_endif = FALSE;
963#endif
964
965 if (count++ == 0)
966 {
967 /*
968 * All output from the commands is put below each other, without
969 * waiting for a return. Don't do this when executing commands
970 * from a script or when being called recursive (e.g. for ":e
971 * +command file").
972 */
973 if (!(flags & DOCMD_NOWAIT) && !recursive)
974 {
975 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100976 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100978 msg_scroll = TRUE; // put messages below each other
979 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 ++RedrawingDisabled;
981 did_inc = TRUE;
982 }
983 }
984
Bram Moolenaar823654b2020-05-29 23:03:09 +0200985 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100986 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
988 /*
989 * 2. Execute one '|' separated command.
990 * do_one_cmd() will return NULL if there is no trailing '|'.
991 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
992 */
993 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100994 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995#ifdef FEAT_EVAL
996 &cstack,
997#endif
998 cmd_getline, cmd_cookie);
999 --recursive;
1000
1001#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001002 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001003 // Use "current_line" from "cmd_loop_cookie", it may have been
1004 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001005 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006#endif
1007
1008 if (next_cmdline == NULL)
1009 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001010 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001011
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 /*
1013 * If the command was typed, remember it for the ':' register.
1014 * Do this AFTER executing the command to make :@: work.
1015 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001016 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 && new_last_cmdline != NULL)
1018 {
1019 vim_free(last_cmdline);
1020 last_cmdline = new_last_cmdline;
1021 new_last_cmdline = NULL;
1022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 }
1024 else
1025 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001026 // need to copy the command after the '|' to cmdline_copy, for the
1027 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001028 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 next_cmdline = cmdline_copy;
1030 }
1031
1032
1033#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001034 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001036 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001038 {
1039 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001043 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 {
1045 ++current_line;
1046
1047 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001048 * An ":endwhile", ":endfor" and ":continue" is handled here.
1049 * If we were executing commands, jump back to the ":while" or
1050 * ":for".
1051 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001053 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001055 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001057 // Jump back to the matching ":while" or ":for". Be careful
1058 // not to use a cs_line[] from an entry that isn't a ":while"
1059 // or ":for": It would make "current_line" invalid and can
1060 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 if (!did_emsg && !got_int && !did_throw
1062 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001063 && (cstack.cs_flags[cstack.cs_idx]
1064 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 && cstack.cs_line[cstack.cs_idx] >= 0
1066 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1067 {
1068 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001069 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001070 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001071 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001073 // Check for the next breakpoint at or after the ":while"
1074 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 if (breakpoint != NULL)
1076 {
1077 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001078 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 fname,
1080 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1081 *dbg_tick = debug_tick;
1082 }
1083 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001084 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001086 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001088 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1089 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 }
1091 }
1092
1093 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001096 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001098 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001099 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 }
1101 }
1102
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001103 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001104 if (breakpoint != NULL && has_watchexpr())
1105 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001106 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001107 *dbg_tick = debug_tick;
1108 }
1109
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 /*
1111 * When not inside any ":while" loop, clear remembered lines.
1112 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001113 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 {
1115 if (lines_ga.ga_len > 0)
1116 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001117 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1119 free_cmdlines(&lines_ga);
1120 }
1121 current_line = 0;
1122 }
1123
1124 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001125 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1126 * being restored at the ":endtry". Reset them here and set the
1127 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1128 * This includes the case where a missing ":endif", ":endwhile" or
1129 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001133 cstack.cs_lflags &= ~CSL_HAD_FINA;
1134 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1135 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 did_throw ? (void *)current_exception : NULL);
1137 did_emsg = got_int = did_throw = FALSE;
1138 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1139 }
1140
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001141 // Update global "trylevel" for recursive calls to do_cmdline() from
1142 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 trylevel = initial_trylevel + cstack.cs_trylevel;
1144
1145 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001146 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 * files) is aborted because of an error, an interrupt, or an uncaught
1148 * exception, cancel everything. If it is left normally, reset
1149 * force_abort to get the non-EH compatible abortion behavior for
1150 * the rest of the script.
1151 */
1152 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1153 force_abort = FALSE;
1154
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001155 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001157#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158
1159 }
1160 /*
1161 * Continue executing command lines when:
1162 * - no CTRL-C typed, no aborting error, no exception thrown or try
1163 * conditionals need to be checked for executing finally clauses or
1164 * catching an interrupt exception
1165 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001166 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 * looping for ":source" command or function call.
1168 */
1169 while (!((got_int
1170#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001171 || (did_emsg && (force_abort || in_vim9script()))
1172 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173#endif
1174 )
1175#ifdef FEAT_EVAL
1176 && cstack.cs_trylevel == 0
1177#endif
1178 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001179 && !(did_emsg
1180#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001181 // Keep going when inside try/catch, so that the error can be
1182 // deal with, except when it is a syntax error, it may cause
1183 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001184 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1185#endif
1186 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001187 && (getline_equal(fgetline, cookie, getexmodeline)
1188 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 && (next_cmdline != NULL
1190#ifdef FEAT_EVAL
1191 || cstack.cs_idx >= 0
1192#endif
1193 || (flags & DOCMD_REPEAT)));
1194
1195 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001196 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197#ifdef FEAT_EVAL
1198 free_cmdlines(&lines_ga);
1199 ga_clear(&lines_ga);
1200
1201 if (cstack.cs_idx >= 0)
1202 {
1203 /*
1204 * If a sourced file or executed function ran to its end, report the
1205 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001206 * In Vim9 script do not give a second error, executing aborts after
1207 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 */
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001209 if (!got_int && !did_throw && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001210 && ((getline_equal(fgetline, cookie, getsourceline)
1211 && !source_finished(fgetline, cookie))
1212 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 && !func_has_ended(real_cookie))))
1214 {
1215 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001216 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001218 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001219 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001220 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001222 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 }
1224
1225 /*
1226 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1227 * ":endtry" in a sourced file or executed function. If the try
1228 * conditional is in its finally clause, ignore anything pending.
1229 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001230 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 */
1232 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001233 {
1234 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1235
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001236 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001237 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001238 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1239 &cstack.cs_looplevel);
1240 }
1241 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 trylevel = initial_trylevel;
1243 }
1244
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001245 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1246 // lack was reported above and the error message is to be converted to an
1247 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001248 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 ? (char_u *)"endfunction" : (char_u *)NULL);
1250
1251 if (trylevel == 0)
1252 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001253 // Just in case did_throw got set but current_exception wasn't.
1254 if (current_exception == NULL)
1255 did_throw = FALSE;
1256
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 /*
1258 * When an exception is being thrown out of the outermost try
1259 * conditional, discard the uncaught exception, disable the conversion
1260 * of interrupts or errors to exceptions, and ensure that no more
1261 * commands are executed.
1262 */
1263 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001264 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
1266 /*
1267 * On an interrupt or an aborting error not converted to an exception,
1268 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001269 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 * when force_abort is set and did_emsg unset in case of an interrupt
1271 * from a finally clause after an error.
1272 */
1273 else if (got_int || (did_emsg && force_abort))
1274 suppress_errthrow = TRUE;
1275 }
1276
1277 /*
1278 * The current cstack will be freed when do_cmdline() returns. An uncaught
1279 * exception will have to be rethrown in the previous cstack. If a function
1280 * has just returned or a script file was just finished and the previous
1281 * cstack belongs to the same function or, respectively, script file, it
1282 * will have to be checked for finally clauses to be executed due to the
1283 * ":return" or ":finish". This is done in do_one_cmd().
1284 */
1285 if (did_throw)
1286 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001287 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001289 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 && ex_nesting_level > func_level(real_cookie) + 1))
1291 {
1292 if (!did_throw)
1293 check_cstack = TRUE;
1294 }
1295 else
1296 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001297 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001298 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 --ex_nesting_level;
1300 /*
1301 * Go to debug mode when returning from a function in which we are
1302 * single-stepping.
1303 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001304 if ((getline_equal(fgetline, cookie, getsourceline)
1305 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001307 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 ? (char_u *)_("End of sourced file")
1309 : (char_u *)_("End of function"));
1310 }
1311
1312 /*
1313 * Restore the exception environment (done after returning from the
1314 * debugger).
1315 */
1316 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001317 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318
1319 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001320
1321 // Cleanup if "cs_emsg_silent_list" remains.
1322 if (cstack.cs_emsg_silent_list != NULL)
1323 {
1324 eslist_T *elem, *temp;
1325
1326 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1327 {
1328 temp = elem->next;
1329 vim_free(elem);
1330 }
1331 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001332#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333
1334 /*
1335 * If there was too much output to fit on the command line, ask the user to
1336 * hit return before redrawing the screen. With the ":global" command we do
1337 * this only once after the command is finished.
1338 */
1339 if (did_inc)
1340 {
1341 --RedrawingDisabled;
1342 --no_wait_return;
1343 msg_scroll = FALSE;
1344
1345 /*
1346 * When just finished an ":if"-":else" which was typed, no need to
1347 * wait for hit-return. Also for an error situation.
1348 */
1349 if (retval == FAIL
1350#ifdef FEAT_EVAL
1351 || (did_endif && KeyTyped && !did_emsg)
1352#endif
1353 )
1354 {
1355 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001356 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 }
1358 else if (need_wait_return)
1359 {
1360 /*
1361 * The msg_start() above clears msg_didout. The wait_return we do
1362 * here should not overwrite the command that may be shown before
1363 * doing that.
1364 */
1365 msg_didout |= msg_didout_before_start;
1366 wait_return(FALSE);
1367 }
1368 }
1369
Bram Moolenaar2a942252012-11-28 23:03:07 +01001370#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001371 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001372#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 /*
1374 * Reset if_level, in case a sourced script file contains more ":if" than
1375 * ":endif" (could be ":if x | foo | endif").
1376 */
1377 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001378#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001379
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 --call_depth;
1381 return retval;
1382}
1383
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001384#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001385/*
1386 * Handle when "did_throw" is set after executing commands.
1387 */
1388 void
1389handle_did_throw()
1390{
1391 char *p = NULL;
1392 msglist_T *messages = NULL;
Bram Moolenaar9e0ee592021-07-31 22:17:28 +02001393 ESTACK_CHECK_DECLARATION
Bram Moolenaar620c9592021-07-31 21:32:31 +02001394
1395 /*
1396 * If the uncaught exception is a user exception, report it as an
1397 * error. If it is an error exception, display the saved error
1398 * message now. For an interrupt exception, do nothing; the
1399 * interrupt message is given elsewhere.
1400 */
1401 switch (current_exception->type)
1402 {
1403 case ET_USER:
1404 vim_snprintf((char *)IObuff, IOSIZE,
1405 _("E605: Exception not caught: %s"),
1406 current_exception->value);
1407 p = (char *)vim_strsave(IObuff);
1408 break;
1409 case ET_ERROR:
1410 messages = current_exception->messages;
1411 current_exception->messages = NULL;
1412 break;
1413 case ET_INTERRUPT:
1414 break;
1415 }
1416
1417 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1418 current_exception->throw_lnum);
1419 ESTACK_CHECK_SETUP
1420 current_exception->throw_name = NULL;
1421
1422 discard_current_exception(); // uses IObuff if 'verbose'
1423 suppress_errthrow = TRUE;
1424 force_abort = TRUE;
1425
1426 if (messages != NULL)
1427 {
1428 do
1429 {
1430 msglist_T *next = messages->next;
1431 int save_compiling = estack_compiling;
1432
1433 estack_compiling = messages->msg_compiling;
1434 emsg(messages->msg);
1435 vim_free(messages->msg);
1436 vim_free(messages->sfile);
1437 vim_free(messages);
1438 messages = next;
1439 estack_compiling = save_compiling;
1440 }
1441 while (messages != NULL);
1442 }
1443 else if (p != NULL)
1444 {
1445 emsg(p);
1446 vim_free(p);
1447 }
1448 vim_free(SOURCING_NAME);
1449 ESTACK_CHECK_NOW
1450 estack_pop();
1451}
1452
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001454 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 */
1456 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001457get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001459 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 wcmd_T *wp;
1461 char_u *line;
1462
1463 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1464 {
1465 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001466 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001468 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001470 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001472 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001473 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 ++cp->current_line;
1475
1476 return line;
1477 }
1478
1479 KeyTyped = FALSE;
1480 ++cp->current_line;
1481 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001482 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 return vim_strsave(wp->line);
1484}
1485
1486/*
1487 * Store a line in "gap" so that a ":while" loop can execute it again.
1488 */
1489 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001490store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491{
1492 if (ga_grow(gap, 1) == FAIL)
1493 return FAIL;
1494 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001495 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 return OK;
1498}
1499
1500/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001501 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 */
1503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001504free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505{
1506 while (gap->ga_len > 0)
1507 {
1508 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1509 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 }
1511}
1512#endif
1513
1514/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001515 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1516 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001519getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001520 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001521 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001522 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523{
1524#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001525 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001526 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001528 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1529 // function that's originally used to obtain the lines. This may be
1530 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001531 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001532 cp = (struct loop_cookie *)cookie;
1533 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 {
1535 gp = cp->getline;
1536 cp = cp->cookie;
1537 }
1538 return gp == func;
1539#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001540 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541#endif
1542}
1543
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001545 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 * getline function. Otherwise return "cookie".
1547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001549getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001550 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001551 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
Bram Moolenaar13505972019-01-24 15:04:48 +01001553#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001554 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001555 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001557 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1558 // cookie that's originally used to obtain the lines. This may be nested
1559 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001560 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001561 cp = (struct loop_cookie *)cookie;
1562 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 {
1564 gp = cp->getline;
1565 cp = cp->cookie;
1566 }
1567 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001568#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001571}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572
Bram Moolenaard5053d02020-06-28 15:51:16 +02001573#if defined(FEAT_EVAL) || defined(PROT)
1574/*
1575 * Get the next line source line without advancing.
1576 */
1577 char_u *
1578getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001579 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001580 void *cookie) // argument for fgetline()
1581{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001582 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001583 struct loop_cookie *cp;
1584 wcmd_T *wp;
1585
1586 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1587 // cookie that's originally used to obtain the lines. This may be nested
1588 // several levels.
1589 gp = fgetline;
1590 cp = (struct loop_cookie *)cookie;
1591 while (gp == get_loop_line)
1592 {
1593 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1594 {
1595 // executing lines a second time, use the stored copy
1596 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1597 return wp->line;
1598 }
1599 gp = cp->getline;
1600 cp = cp->cookie;
1601 }
1602 if (gp == getsourceline)
1603 return source_nextline(cp);
1604 return NULL;
1605}
1606#endif
1607
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001608
1609/*
1610 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1611 * ":bwipeout", etc.
1612 * Returns the buffer number.
1613 */
1614 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001615compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001616{
1617 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001618 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001619 int count = offset;
1620
1621 buf = firstbuf;
1622 while (buf->b_next != NULL && buf->b_fnum < lnum)
1623 buf = buf->b_next;
1624 while (count != 0)
1625 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001626 count += (offset < 0) ? 1 : -1;
1627 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1628 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001629 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001630 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001631 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001632 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001633 while (buf->b_ml.ml_mfp == NULL)
1634 {
1635 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1636 if (nextbuf == NULL)
1637 break;
1638 buf = nextbuf;
1639 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001640 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001641 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001642 if (addr_type == ADDR_LOADED_BUFFERS)
1643 while (buf->b_ml.ml_mfp == NULL)
1644 {
1645 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1646 if (nextbuf == NULL)
1647 break;
1648 buf = nextbuf;
1649 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001650 return buf->b_fnum;
1651}
1652
Bram Moolenaarb7316892019-05-01 18:08:42 +02001653/*
1654 * Return the window number of "win".
1655 * When "win" is NULL return the number of windows.
1656 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001657 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001658current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001659{
1660 win_T *wp;
1661 int nr = 0;
1662
Bram Moolenaar29323592016-07-24 22:04:11 +02001663 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001664 {
1665 ++nr;
1666 if (wp == win)
1667 break;
1668 }
1669 return nr;
1670}
1671
1672 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001673current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001674{
1675 tabpage_T *tp;
1676 int nr = 0;
1677
Bram Moolenaar29323592016-07-24 22:04:11 +02001678 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001679 {
1680 ++nr;
1681 if (tp == tab)
1682 break;
1683 }
1684 return nr;
1685}
1686
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001687 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001688comment_start(char_u *p, int starts_with_colon UNUSED)
1689{
1690#ifdef FEAT_EVAL
1691 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001692 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001693#endif
1694 return *p == '"';
1695}
1696
Bram Moolenaarf240e182014-11-27 18:33:02 +01001697# define CURRENT_WIN_NR current_win_nr(curwin)
1698# define LAST_WIN_NR current_win_nr(NULL)
1699# define CURRENT_TAB_NR current_tab_nr(curtab)
1700# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001701
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702/*
1703 * Execute one Ex command.
1704 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001705 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1706 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 *
1708 * 1. skip comment lines and leading space
1709 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001710 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001711 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001712 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001713 * 6. parse arguments
1714 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001716 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 *
1718 * This function may be called recursively!
1719 */
1720#if (_MSC_VER == 1200)
1721/*
Bram Moolenaared203462004-06-16 11:19:22 +00001722 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001724 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725#endif
1726 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001727do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001728 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001729 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001731 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001733 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001734 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001736 char_u *p;
1737 linenr_T lnum;
1738 long n;
1739 char *errormsg = NULL; // error message
1740 char_u *after_modifier = NULL;
1741 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001742 cmdmod_T save_cmdmod;
1743 int save_reg_executing = reg_executing;
1744 int ni; // set when Not Implemented
1745 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001746 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001747#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001748 int may_have_range;
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001749 int vim9script;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001750 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001751#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001752 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
Bram Moolenaara80faa82020-04-12 19:37:17 +02001754 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 ea.line1 = 1;
1756 ea.line2 = 1;
1757#ifdef FEAT_EVAL
1758 ++ex_nesting_level;
1759#endif
1760
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001761 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 if (quitmore
1763#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001764 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001765 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001766#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001767 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001768 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 --quitmore;
1770
1771 /*
1772 * Reset browse, confirm, etc.. They are restored when returning, for
1773 * recursive calls.
1774 */
1775 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001777 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001778 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1779 goto doend;
1780
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001781/*
1782 * 1. Skip comment lines and leading white space and colons.
1783 * 2. Handle command modifiers.
1784 */
1785 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001787 ea.cmdlinep = cmdlinep;
1788 ea.getline = fgetline;
1789 ea.cookie = cookie;
1790#ifdef FEAT_EVAL
1791 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001792 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001794 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001795 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001796 apply_cmdmod(&cmdmod);
Bram Moolenaar2379f872021-02-14 14:07:34 +01001797#ifdef FEAT_EVAL
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001798 vim9script = in_vim9script();
Bram Moolenaar2379f872021-02-14 14:07:34 +01001799#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001800 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
1802#ifdef FEAT_EVAL
1803 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1804 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1805#else
1806 ea.skip = (if_level > 0);
1807#endif
1808
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001810 * 3. Skip over the range to find the command. Let "p" point to after it.
1811 *
1812 * We need the command to know what kind of range it uses.
1813 */
1814 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001815#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001816 // In Vim9 script a colon is required before the range. This may also be
1817 // after command modifiers.
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001818 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001819 {
1820 may_have_range = FALSE;
1821 for (p = ea.cmd; p >= *cmdlinep; --p)
1822 {
1823 if (*p == ':')
1824 may_have_range = TRUE;
1825 if (p < ea.cmd && !VIM_ISWHITE(*p))
1826 break;
1827 }
1828 }
1829 else
1830 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001831 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001832#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001833 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834
1835#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001836 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001837 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001838 if (ea.cmd == cmd + 1 && *cmd == '$')
1839 // should be "$VAR = val"
1840 --ea.cmd;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001841 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001842 if (ea.cmdidx == CMD_SIZE)
1843 {
1844 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1845
1846 // If a ':' before the range is missing, give a clearer error
1847 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001848 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001849 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001850 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001851 goto doend;
1852 }
1853 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001854 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001855 else
1856#endif
1857 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001858
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001859#ifdef FEAT_EVAL
1860# ifdef FEAT_PROFILE
1861 // Count this line for profiling if skip is TRUE.
1862 if (do_profiling == PROF_YES
1863 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1864 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1865 {
1866 int skip = did_emsg || got_int || did_throw;
1867
1868 if (ea.cmdidx == CMD_catch)
1869 skip = !skip && !(cstack->cs_idx >= 0
1870 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1871 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1872 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1873 skip = skip || !(cstack->cs_idx >= 0
1874 && !(cstack->cs_flags[cstack->cs_idx]
1875 & (CSF_ACTIVE | CSF_TRUE)));
1876 else if (ea.cmdidx == CMD_finally)
1877 skip = FALSE;
1878 else if (ea.cmdidx != CMD_endif
1879 && ea.cmdidx != CMD_endfor
1880 && ea.cmdidx != CMD_endtry
1881 && ea.cmdidx != CMD_endwhile)
1882 skip = ea.skip;
1883
1884 if (!skip)
1885 {
1886 if (getline_equal(fgetline, cookie, get_func_line))
1887 func_line_exec(getline_cookie(fgetline, cookie));
1888 else if (getline_equal(fgetline, cookie, getsourceline))
1889 script_line_exec();
1890 }
1891 }
1892# endif
1893
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001894 // May go to debug mode. If this happens and the ">quit" debug command is
1895 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001896 dbg_check_breakpoint(&ea);
1897 if (!ea.skip && got_int)
1898 {
1899 ea.skip = TRUE;
1900 (void)do_intthrow(cstack);
1901 }
1902#endif
1903
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001904/*
1905 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 *
1907 * where 'addr' is:
1908 *
1909 * % (entire file)
1910 * $ [+-NUM]
1911 * 'x [+-NUM] (where x denotes a currently defined mark)
1912 * . [+-NUM]
1913 * [+-NUM]..
1914 * NUM
1915 *
1916 * The ea.cmd pointer is updated to point to the first character following the
1917 * range spec. If an initial address is found, but no second, the upper bound
1918 * is equal to the lower.
1919 */
1920
Bram Moolenaar25190db2019-05-04 15:05:28 +02001921 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001922 if (!IS_USER_CMDIDX(ea.cmdidx))
1923 {
1924 if (ea.cmdidx != CMD_SIZE)
1925 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1926 else
1927 ea.addr_type = ADDR_LINES;
1928
Bram Moolenaar25190db2019-05-04 15:05:28 +02001929 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001930 if (ea.cmdidx == CMD_wincmd && p != NULL)
1931 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001932#ifdef FEAT_QUICKFIX
1933 // :.cc in quickfix window uses line number
1934 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1935 ea.addr_type = ADDR_OTHER;
1936#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001937 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001938
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001939 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001940#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001941 if (!may_have_range)
1942 ea.line1 = ea.line2 = default_address(&ea);
1943 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001944#endif
1945 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1946 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001949 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 */
1951
1952 /*
1953 * Skip ':' and any white space
1954 */
1955 ea.cmd = skipwhite(ea.cmd);
1956 while (*ea.cmd == ':')
1957 ea.cmd = skipwhite(ea.cmd + 1);
1958
1959 /*
1960 * If we got a line, but no command, then go to the line.
1961 * If we find a '|' or '\n' we set ea.nextcmd.
1962 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001963 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1964 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 {
1966 /*
1967 * strange vi behaviour:
1968 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001969 * ":3|..." prints line 3 (not in Vim9 script)
1970 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001972 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001974 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 goto doend;
1976 }
1977
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001978 // If this looks like an undefined user command and there are CmdUndefined
1979 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001980 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1981 && ASCII_ISUPPER(*ea.cmd)
1982 && has_cmdundefined())
1983 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001984 int ret;
1985
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001986 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001987 while (ASCII_ISALNUM(*p))
1988 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001989 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001990 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1991 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001992 // If the autocommands did something and didn't cause an error, try
1993 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001994 p = (ret
1995#ifdef FEAT_EVAL
1996 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001997#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001999 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 if (p == NULL)
2002 {
2003 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002004 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 goto doend;
2006 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002007 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002008 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2009 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 {
2011 errormsg = uc_fun_cmd();
2012 goto doend;
2013 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002014
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 if (ea.cmdidx == CMD_SIZE)
2016 {
2017 if (!ea.skip)
2018 {
2019 STRCPY(IObuff, _("E492: Not an editor command"));
2020 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002021 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002022 // If the modifier was parsed OK the error must be in the
2023 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002024 if (after_modifier != NULL)
2025 append_command(after_modifier);
2026 else
2027 append_command(*cmdlinep);
2028 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002029 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002030 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 }
2032 goto doend;
2033 }
2034
Bram Moolenaar958636c2014-10-21 20:01:58 +02002035 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2036 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002037#ifdef HAVE_EX_SCRIPT_NI
2038 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2039#endif
2040 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
2042#ifndef FEAT_EVAL
2043 /*
2044 * When the expression evaluation is disabled, recognize the ":if" and
2045 * ":endif" commands and ignore everything in between it.
2046 */
2047 if (ea.cmdidx == CMD_if)
2048 ++if_level;
2049 if (if_level)
2050 {
2051 if (ea.cmdidx == CMD_endif)
2052 --if_level;
2053 goto doend;
2054 }
2055
2056#endif
2057
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002058 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002059 if (*p == '!' && ea.cmdidx != CMD_substitute
2060 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 {
2062 ++p;
2063 ea.forceit = TRUE;
2064 }
2065 else
2066 ea.forceit = FALSE;
2067
2068/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002069 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002071 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002072 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
2074 if (!ea.skip)
2075 {
2076#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002077 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002079 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002080 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 goto doend;
2082 }
2083#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002084 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002085 {
2086 errormsg = _("E981: Command not allowed in rvim");
2087 goto doend;
2088 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002089 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002091 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002092 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 goto doend;
2094 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002095
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002096 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002098#ifdef FEAT_CMDWIN
2099 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2100 {
2101 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002102 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002103 goto doend;
2104 }
2105#endif
2106 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2107 {
2108 // Command not allowed when text is locked
2109 errormsg = _(get_text_locked_msg());
2110 goto doend;
2111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002113
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002114 // Disallow editing another buffer when "curbuf_lock" is set.
2115 // Do allow ":checktime" (it is postponed).
2116 // Do allow ":edit" (check for an argument later).
2117 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002118 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002119 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002120 && ea.cmdidx != CMD_edit
2121 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002122 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002123 && curbuf_locked())
2124 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002126 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002128 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002129 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 goto doend;
2131 }
2132 }
2133
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002134 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002136 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 goto doend;
2138 }
2139
2140 /*
2141 * Don't complain about the range if it is not used
2142 * (could happen if line_count is accidentally set to 0).
2143 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002144 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 {
2146 /*
2147 * If the range is backwards, ask for confirmation and, if given, swap
2148 * ea.line1 & ea.line2 so it's forwards again.
2149 * When global command is busy, don't ask, will fail below.
2150 */
2151 if (!global_busy && ea.line1 > ea.line2)
2152 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002153 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002155 if (sourcing || exmode_active)
2156 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002157 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002158 goto doend;
2159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 if (ask_yesno((char_u *)
2161 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002162 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
2164 lnum = ea.line1;
2165 ea.line1 = ea.line2;
2166 ea.line2 = lnum;
2167 }
2168 if ((errormsg = invalid_range(&ea)) != NULL)
2169 goto doend;
2170 }
2171
Bram Moolenaarb7316892019-05-01 18:08:42 +02002172 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2173 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 ea.line2 = 1;
2175
2176 correct_range(&ea);
2177
2178#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002179 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002180 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002182 // Put the first line at the start of a closed fold, put the last line
2183 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 (void)hasFolding(ea.line1, &ea.line1, NULL);
2185 (void)hasFolding(ea.line2, NULL, &ea.line2);
2186 }
2187#endif
2188
2189#ifdef FEAT_QUICKFIX
2190 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002191 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 * option here, so things like % get expanded.
2193 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002194 p = replace_makeprg(&ea, p, cmdlinep);
2195 if (p == NULL)
2196 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197#endif
2198
2199 /*
2200 * Skip to start of argument.
2201 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2202 */
2203 if (ea.cmdidx == CMD_bang)
2204 ea.arg = p;
2205 else
2206 ea.arg = skipwhite(p);
2207
Bram Moolenaar379fb762018-08-30 15:58:28 +02002208 // ":file" cannot be run with an argument when "curbuf_lock" is set
2209 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2210 goto doend;
2211
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 /*
2213 * Check for "++opt=val" argument.
2214 * Must be first, allow ":w ++enc=utf8 !cmd"
2215 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002216 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2218 if (getargopt(&ea) == FAIL && !ni)
2219 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002220 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 goto doend;
2222 }
2223
2224 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2225 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002226 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002228 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002230 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 goto doend;
2232 }
2233 ea.arg = skipwhite(ea.arg + 1);
2234 ea.append = TRUE;
2235 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002236 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 {
2238 ++ea.arg;
2239 ea.usefilter = TRUE;
2240 }
2241 }
2242
2243 if (ea.cmdidx == CMD_read)
2244 {
2245 if (ea.forceit)
2246 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002247 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 ea.forceit = FALSE;
2249 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002250 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 {
2252 ++ea.arg;
2253 ea.usefilter = TRUE;
2254 }
2255 }
2256
2257 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2258 {
2259 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002260 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 {
2262 ++ea.arg;
2263 ++ea.amount;
2264 }
2265 ea.arg = skipwhite(ea.arg);
2266 }
2267
2268 /*
2269 * Check for "+command" argument, before checking for next command.
2270 * Don't do this for ":read !cmd" and ":write !cmd".
2271 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002272 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2274
2275 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002276 * For commands that do not use '|' inside their argument: Check for '|' to
2277 * separate commands and '"' or '#' to start comments.
2278 *
2279 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002280 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002281 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002282 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002284 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2285 {
2286 separate_nextcmd(&ea);
2287 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002288 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002289 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002290 || ea.cmdidx == CMD_global
2291 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002292 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002293#ifdef FEAT_EVAL
2294 || inside_block(&ea)
2295#endif
2296 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
2298 for (p = ea.arg; *p; ++p)
2299 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002300 // Remove one backslash before a newline, so that it's possible to
2301 // pass a newline to the shell and also a newline that is preceded
2302 // with a backslash. This makes it impossible to end a shell
2303 // command in a backslash, but that doesn't appear useful.
2304 // Halving the number of backslashes is incompatible with previous
2305 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002307 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002308 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
2310 ea.nextcmd = p + 1;
2311 *p = NUL;
2312 break;
2313 }
2314 }
2315 }
2316
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002317 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002318 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002320 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002321 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002323 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002324 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002325 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002327#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002328 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002329 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002331 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002332 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 }
2334#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002335 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2336 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002337 {
2338 ea.regname = *ea.arg++;
2339#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002340 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002341 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2342 {
kuuote08d7b1c2021-10-04 22:17:36 +01002343 if (!ea.skip)
2344 {
2345 set_expr_line(vim_strsave(ea.arg), &ea);
2346 did_set_expr_line = TRUE;
2347 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002348 ea.arg += STRLEN(ea.arg);
2349 }
2350#endif
2351 ea.arg = skipwhite(ea.arg);
2352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 }
2354
2355 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002356 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 * count, it's a buffer name.
2358 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002359 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002360 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002361 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002363 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002365 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002367 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 goto doend;
2369 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002370 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
2372 ea.line2 = n;
2373 if (ea.addr_count == 0)
2374 ea.addr_count = 1;
2375 }
2376 else
2377 {
2378 ea.line1 = ea.line2;
2379 ea.line2 += n - 1;
2380 ++ea.addr_count;
2381 /*
2382 * Be vi compatible: no error message for out of range.
2383 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002384 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 ea.line2 = curbuf->b_ml.ml_line_count;
2386 }
2387 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002388
2389 /*
2390 * Check for flags: 'l', 'p' and '#'.
2391 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002392 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002393 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002394 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2395 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002397 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002398 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 goto doend;
2400 }
2401
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002402 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002404 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 goto doend;
2406 }
2407
2408#ifdef FEAT_EVAL
2409 /*
2410 * Skip the command when it's not going to be executed.
2411 * The commands like :if, :endif, etc. always need to be executed.
2412 * Also make an exception for commands that handle a trailing command
2413 * themselves.
2414 */
2415 if (ea.skip)
2416 {
2417 switch (ea.cmdidx)
2418 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002419 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 case CMD_while:
2421 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002422 case CMD_for:
2423 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 case CMD_if:
2425 case CMD_elseif:
2426 case CMD_else:
2427 case CMD_endif:
2428 case CMD_try:
2429 case CMD_catch:
2430 case CMD_finally:
2431 case CMD_endtry:
2432 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002433 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 break;
2435
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002436 // Commands that handle '|' themselves. Check: A command should
2437 // either have the EX_TRLBAR flag, appear in this list or appear in
2438 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 case CMD_aboveleft:
2440 case CMD_and:
2441 case CMD_belowright:
2442 case CMD_botright:
2443 case CMD_browse:
2444 case CMD_call:
2445 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002446 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 case CMD_delfunction:
2448 case CMD_djump:
2449 case CMD_dlist:
2450 case CMD_dsearch:
2451 case CMD_dsplit:
2452 case CMD_echo:
2453 case CMD_echoerr:
2454 case CMD_echomsg:
2455 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002456 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002458 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002459 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 case CMD_help:
2461 case CMD_hide:
2462 case CMD_ijump:
2463 case CMD_ilist:
2464 case CMD_isearch:
2465 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002466 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 case CMD_keepjumps:
2468 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002469 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 case CMD_leftabove:
2471 case CMD_let:
2472 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002473 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002474 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002476 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002477 case CMD_noautocmd:
2478 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 case CMD_perl:
2480 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002481 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002482 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002483 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 case CMD_return:
2485 case CMD_rightbelow:
2486 case CMD_ruby:
2487 case CMD_silent:
2488 case CMD_smagic:
2489 case CMD_snomagic:
2490 case CMD_substitute:
2491 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002492 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 case CMD_tcl:
2494 case CMD_throw:
2495 case CMD_tilde:
2496 case CMD_topleft:
2497 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002498 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002499 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 case CMD_verbose:
2501 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002502 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 break;
2504
2505 default: goto doend;
2506 }
2507 }
2508#endif
2509
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002510 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 {
2512 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2513 goto doend;
2514 }
2515
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 /*
2517 * Accept buffer name. Cannot be used at the same time with a buffer
2518 * number. Don't do this for a user command.
2519 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002520 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002521 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 {
2523 /*
2524 * :bdelete, :bwipeout and :bunload take several arguments, separated
2525 * by spaces: find next space (skipping over escaped characters).
2526 * The others take one argument: ignore trailing spaces.
2527 */
2528 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2529 || ea.cmdidx == CMD_bunload)
2530 p = skiptowhite_esc(ea.arg);
2531 else
2532 {
2533 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002534 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 --p;
2536 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002537 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002538 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002539 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 goto doend;
2541 ea.addr_count = 1;
2542 ea.arg = skipwhite(p);
2543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002545 // The :try command saves the emsg_silent flag, reset it here when
2546 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002547 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002548 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002549 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002550 if (emsg_silent < 0)
2551 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002552 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002553 }
2554
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002556 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
Bram Moolenaar958636c2014-10-21 20:01:58 +02002559 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 {
2561 /*
2562 * Execute a user-defined command.
2563 */
2564 do_ucmd(&ea);
2565 }
2566 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 {
2568 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002569 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 */
2571 ea.errmsg = NULL;
2572 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2573 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002574 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 }
2576
2577#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002579 // Not if this was a command that wasn't executed or :endif.
Bram Moolenaar16531552020-02-08 16:00:46 +01002580 if (getline_equal(ea.getline, ea.cookie, getsourceline)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002581 && current_sctx.sc_sid > 0
2582 && ea.cmdidx != CMD_endif
2583 && (cstack->cs_idx < 0
2584 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002585 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 /*
2588 * If the command just executed called do_cmdline(), any throw or ":return"
2589 * or ":finish" encountered there must also check the cstack of the still
2590 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2591 * exception, or reanimate a returned function or finished script file and
2592 * return or finish it again.
2593 */
2594 if (need_rethrow)
2595 do_throw(cstack);
2596 else if (check_cstack)
2597 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002598 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002600 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 && current_func_returned())
2602 do_return(&ea, TRUE, FALSE, NULL);
2603 }
2604 need_rethrow = check_cstack = FALSE;
2605#endif
2606
2607doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002608 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002609 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002611 curwin->w_cursor.col = 0;
2612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613
2614 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2615 {
2616 if (sourcing)
2617 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002618 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 {
2620 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002621 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002623 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 }
2625 emsg(errormsg);
2626 }
2627#ifdef FEAT_EVAL
2628 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002629 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2630 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002631
2632 if (did_set_expr_line)
2633 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634#endif
2635
Bram Moolenaare1004402020-10-24 20:49:43 +02002636 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002638 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002640 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 ea.nextcmd = NULL;
2642
2643#ifdef FEAT_EVAL
2644 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002645 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646#endif
2647
2648 return ea.nextcmd;
2649}
2650#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002651 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652#endif
2653
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002654static char ex_error_buf[MSG_BUF_LEN];
2655
2656/*
2657 * Return an error message with argument included.
2658 * Uses a static buffer, only the last error will be kept.
2659 * "msg" will be translated, caller should use N_().
2660 */
2661 char *
2662ex_errmsg(char *msg, char_u *arg)
2663{
2664 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2665 return ex_error_buf;
2666}
2667
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002669 * Handle a range without a command.
2670 * Returns an error message on failure.
2671 */
2672 char *
2673ex_range_without_command(exarg_T *eap)
2674{
2675 char *errormsg = NULL;
2676
2677 if ((*eap->cmd == '|' || (exmode_active && eap->line1 != eap->line2))
2678#ifdef FEAT_EVAL
2679 && !in_vim9script()
2680#endif
2681 )
2682 {
2683 eap->cmdidx = CMD_print;
2684 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2685 if ((errormsg = invalid_range(eap)) == NULL)
2686 {
2687 correct_range(eap);
2688 ex_print(eap);
2689 }
2690 }
2691 else if (eap->addr_count != 0)
2692 {
2693 if (eap->line2 > curbuf->b_ml.ml_line_count)
2694 {
2695 // With '-' in 'cpoptions' a line number past the file is an
2696 // error, otherwise put it at the end of the file.
2697 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2698 eap->line2 = -1;
2699 else
2700 eap->line2 = curbuf->b_ml.ml_line_count;
2701 }
2702
2703 if (eap->line2 < 0)
2704 errormsg = _(e_invalid_range);
2705 else
2706 {
2707 if (eap->line2 == 0)
2708 curwin->w_cursor.lnum = 1;
2709 else
2710 curwin->w_cursor.lnum = eap->line2;
2711 beginline(BL_SOL | BL_FIX);
2712 }
2713 }
2714 return errormsg;
2715}
2716
2717/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002718 * Check for an Ex command with optional tail.
2719 * If there is a match advance "pp" to the argument and return TRUE.
2720 * If "noparen" is TRUE do not recognize the command followed by "(".
2721 */
2722 static int
2723checkforcmd_opt(
2724 char_u **pp, // start of command
2725 char *cmd, // name of command
2726 int len, // required length
2727 int noparen)
2728{
2729 int i;
2730
2731 for (i = 0; cmd[i] != NUL; ++i)
2732 if (((char_u *)cmd)[i] != (*pp)[i])
2733 break;
2734 if (i >= len && !isalpha((*pp)[i])
2735 && (*pp)[i] != '_' && (!noparen || (*pp)[i] != '('))
2736 {
2737 *pp = skipwhite(*pp + i);
2738 return TRUE;
2739 }
2740 return FALSE;
2741}
2742
2743/*
2744 * Check for an Ex command with optional tail.
2745 * If there is a match advance "pp" to the argument and return TRUE.
2746 */
2747 int
2748checkforcmd(
2749 char_u **pp, // start of command
2750 char *cmd, // name of command
2751 int len) // required length
2752{
2753 return checkforcmd_opt(pp, cmd, len, FALSE);
2754}
2755
2756/*
2757 * Check for an Ex command with optional tail, not followed by "(".
2758 * If there is a match advance "pp" to the argument and return TRUE.
2759 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002760 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002761checkforcmd_noparen(
2762 char_u **pp, // start of command
2763 char *cmd, // name of command
2764 int len) // required length
2765{
2766 return checkforcmd_opt(pp, cmd, len, TRUE);
2767}
2768
2769/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002770 * Parse and skip over command modifiers:
2771 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002772 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002773 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002774 * When "skip_only" is TRUE the global variables are not changed, except for
2775 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002776 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2777 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002778 * Call apply_cmdmod() to get the side effects of the modifiers:
2779 * - Increment "sandbox" for ":sandbox"
2780 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002781 * - set msg_silent for ":silent"
2782 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002783 * Return FAIL when the command is not to be executed.
2784 * May set "errormsg" to an error message.
2785 */
2786 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002787parse_command_modifiers(
2788 exarg_T *eap,
2789 char **errormsg,
2790 cmdmod_T *cmod,
2791 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002792{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002793 char_u *p;
2794 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002795
Bram Moolenaare1004402020-10-24 20:49:43 +02002796 CLEAR_POINTER(cmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002797
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002798 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002799 for (;;)
2800 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002801 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002802 {
2803 if (*eap->cmd == ':')
2804 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002805 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002806 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002807
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002808 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002809 if (*eap->cmd == NUL && exmode_active
2810 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2811 || getline_equal(eap->getline, eap->cookie, getexline))
2812 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2813 {
2814 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002815 if (!skip_only)
2816 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002817 }
2818
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002819 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002820 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002821 {
2822 // a comment ends at a NL
2823 if (eap->nextcmd == NULL)
2824 {
2825 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2826 if (eap->nextcmd != NULL)
2827 ++eap->nextcmd;
2828 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002829 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002830 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002831 if (*eap->cmd == NUL)
2832 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002833 if (!skip_only)
2834 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002835 return FAIL;
2836 }
2837
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002838 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002839
2840 // In Vim9 script a variable can shadow a command modifier:
2841 // verbose = 123
2842 // verbose += 123
2843 // silent! verbose = func()
2844 // verbose.member = 2
2845 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002846 // But not:
2847 // verbose [a, b] = list
Bram Moolenaar17126b12021-01-07 22:03:02 +01002848 if (in_vim9script())
2849 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002850 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002851
2852 for (s = p; ASCII_ISALPHA(*s); ++s)
2853 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002854 n = skipwhite(s);
2855 if (vim_strchr((char_u *)".=", *n) != NULL
2856 || *s == '['
2857 || (*n != NUL && n[1] == '='))
Bram Moolenaar17126b12021-01-07 22:03:02 +01002858 break;
2859 }
2860
Bram Moolenaareffed932018-08-14 13:38:17 +02002861 switch (*p)
2862 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002863 // When adding an entry, also modify cmd_exists().
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002864 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002865 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002866 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002867 continue;
2868
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002869 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002870 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002871 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002872 continue;
2873 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002874 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002875 {
2876#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002877 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002878#endif
2879 continue;
2880 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002881 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002882 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002883 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002884 continue;
2885
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002886 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002887 break;
2888#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002889 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002890#endif
2891 continue;
2892
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002893 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002894 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002895 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002896 continue;
2897 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002898 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002899 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002900 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002901 continue;
2902 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002903 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002904 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002905 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002906 continue;
2907 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002908 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002909 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002910 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002911 continue;
2912
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002913 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002914 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02002915 char_u *reg_pat;
2916 char_u *nulp = NULL;
2917 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002918
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002919 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002920 || *p == NUL
2921 || (ends_excmd(*p)
2922#ifdef FEAT_EVAL
2923 // in ":filter #pat# cmd" # does not
2924 // start a comment
2925 && (!in_vim9script() || VIM_ISWHITE(p[1]))
2926#endif
2927 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02002928 break;
2929 if (*p == '!')
2930 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002931 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002932 p = skipwhite(p + 1);
2933 if (*p == NUL || ends_excmd(*p))
2934 break;
2935 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002936#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002937 // Avoid that "filter(arg)" is recognized.
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +01002938 if (in_vim9script() && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002939 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002940#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002941 if (skip_only)
2942 p = skip_vimgrep_pat(p, NULL, NULL);
2943 else
2944 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02002945 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
2946 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02002947 if (p == NULL || *p == NUL)
2948 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002949 if (!skip_only)
2950 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002951 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002952 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002953 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002954 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02002955 // restore the character overwritten by NUL
2956 if (nulp != NULL)
2957 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002958 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002959 eap->cmd = p;
2960 continue;
2961 }
2962
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002963 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002964 case 'h': if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02002965 || *p == NUL || ends_excmd(*p))
2966 break;
2967 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002968 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002969 continue;
2970
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002971 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002972 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002973 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002974 continue;
2975 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002976 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
2977 {
2978 if (ends_excmd2(p, eap->cmd))
2979 {
2980 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02002981 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002982 return FAIL;
2983 }
2984 cmod->cmod_flags |= CMOD_LEGACY;
2985 continue;
2986 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002987
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002988 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002989 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002990 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002991 continue;
2992
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002993 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002994 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002995 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002996 continue;
2997 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002998 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002999 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003000 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003001 continue;
3002
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003003 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003004 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003005 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003006 continue;
3007
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003008 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003009 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003010 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003011 continue;
3012 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003013 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003014 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003015 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003016 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3017 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003018 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003019 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003020 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003021 }
3022 continue;
3023
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003024 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003025 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003026 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003027 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003028 long tabnr = get_address(eap, &eap->cmd,
3029 ADDR_TABS, eap->skip,
3030 skip_only, FALSE, 1);
3031 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003032 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003033 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003034 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003035 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3036 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003037 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003038 return FAIL;
3039 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003040 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003041 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003042 }
3043 eap->cmd = p;
3044 continue;
3045 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003046 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003047 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003048 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003049 continue;
3050
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003051 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003052 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003053 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003054 continue;
3055
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003056 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003057 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003058 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003059 continue;
3060 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003061 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003062 {
3063 if (ends_excmd2(p, eap->cmd))
3064 {
3065 *errormsg =
3066 _(e_vim9cmd_must_be_followed_by_command);
3067 return FAIL;
3068 }
3069 cmod->cmod_flags |= CMOD_VIM9CMD;
3070 continue;
3071 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003072 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003073 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003074 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02003075 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003076 else
Bram Moolenaare1004402020-10-24 20:49:43 +02003077 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003078 eap->cmd = p;
3079 continue;
3080 }
3081 break;
3082 }
3083
3084 return OK;
3085}
3086
3087/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003088 * Return TRUE if "cmod" has anything set.
3089 */
3090 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003091has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003092{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003093 return (cmod->cmod_flags != 0 && (!ignore_silent
3094 || (cmod->cmod_flags
3095 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003096 || cmod->cmod_split != 0
3097 || cmod->cmod_verbose != 0
3098 || cmod->cmod_tab != 0
3099 || cmod->cmod_filter_regmatch.regprog != NULL;
3100}
3101
3102/*
3103 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3104 */
3105 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003106cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003107{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003108 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003109 {
3110 emsg(_(e_misplaced_command_modifier));
3111 return TRUE;
3112 }
3113 return FALSE;
3114}
3115
3116/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003117 * Apply the command modifiers. Saves current state in "cmdmod", call
3118 * undo_cmdmod() later.
3119 */
3120 void
3121apply_cmdmod(cmdmod_T *cmod)
3122{
3123#ifdef HAVE_SANDBOX
3124 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3125 {
3126 ++sandbox;
3127 cmod->cmod_did_sandbox = TRUE;
3128 }
3129#endif
3130 if (cmod->cmod_verbose > 0)
3131 {
3132 if (cmod->cmod_verbose_save == 0)
3133 cmod->cmod_verbose_save = p_verbose + 1;
3134 p_verbose = cmod->cmod_verbose;
3135 }
3136
3137 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3138 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003139 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003140 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003141 cmod->cmod_save_msg_scroll = msg_scroll;
3142 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003143 if (cmod->cmod_flags & CMOD_SILENT)
3144 ++msg_silent;
3145 if (cmod->cmod_flags & CMOD_UNSILENT)
3146 msg_silent = 0;
3147
3148 if (cmod->cmod_flags & CMOD_ERRSILENT)
3149 {
3150 ++emsg_silent;
3151 ++cmod->cmod_did_esilent;
3152 }
3153
Bram Moolenaare1004402020-10-24 20:49:43 +02003154 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003155 {
3156 // Set 'eventignore' to "all".
3157 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003158 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003159 set_string_option_direct((char_u *)"ei", -1,
3160 (char_u *)"all", OPT_FREE, SID_NONE);
3161 }
3162}
3163
3164/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003165 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003166 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003167 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003168undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003169{
Bram Moolenaare1004402020-10-24 20:49:43 +02003170 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003171 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003172 p_verbose = cmod->cmod_verbose_save - 1;
3173 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003174 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003175
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003176#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003177 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003178 {
3179 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003180 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003181 }
3182#endif
3183
Bram Moolenaare1004402020-10-24 20:49:43 +02003184 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003185 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003186 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003187 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3188 OPT_FREE, SID_NONE);
3189 free_string_option(cmod->cmod_save_ei);
3190 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003191 }
3192
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003193 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003194
Bram Moolenaare1004402020-10-24 20:49:43 +02003195 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003196 {
3197 // messages could be enabled for a serious error, need to check if the
3198 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003199 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3200 msg_silent = cmod->cmod_save_msg_silent - 1;
3201 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003202 if (emsg_silent < 0)
3203 emsg_silent = 0;
3204 // Restore msg_scroll, it's set by file I/O commands, even when no
3205 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003206 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003207
3208 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3209 // somewhere in the line. Put it back in the first column.
3210 if (redirecting())
3211 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003212
Bram Moolenaare1004402020-10-24 20:49:43 +02003213 cmod->cmod_save_msg_silent = 0;
3214 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003215 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003216}
3217
3218/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003219 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003220 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003221 * Return FAIL and set "errormsg" or return OK.
3222 */
3223 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003224parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003225{
3226 int address_count = 1;
3227 linenr_T lnum;
3228
3229 // Repeat for all ',' or ';' separated addresses.
3230 for (;;)
3231 {
3232 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003233 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003234 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003235 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003236 eap->addr_count == 0, address_count++);
3237 if (eap->cmd == NULL) // error detected
3238 return FAIL;
3239 if (lnum == MAXLNUM)
3240 {
3241 if (*eap->cmd == '%') // '%' - all lines
3242 {
3243 ++eap->cmd;
3244 switch (eap->addr_type)
3245 {
3246 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003247 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003248 eap->line1 = 1;
3249 eap->line2 = curbuf->b_ml.ml_line_count;
3250 break;
3251 case ADDR_LOADED_BUFFERS:
3252 {
3253 buf_T *buf = firstbuf;
3254
3255 while (buf->b_next != NULL
3256 && buf->b_ml.ml_mfp == NULL)
3257 buf = buf->b_next;
3258 eap->line1 = buf->b_fnum;
3259 buf = lastbuf;
3260 while (buf->b_prev != NULL
3261 && buf->b_ml.ml_mfp == NULL)
3262 buf = buf->b_prev;
3263 eap->line2 = buf->b_fnum;
3264 break;
3265 }
3266 case ADDR_BUFFERS:
3267 eap->line1 = firstbuf->b_fnum;
3268 eap->line2 = lastbuf->b_fnum;
3269 break;
3270 case ADDR_WINDOWS:
3271 case ADDR_TABS:
3272 if (IS_USER_CMDIDX(eap->cmdidx))
3273 {
3274 eap->line1 = 1;
3275 eap->line2 = eap->addr_type == ADDR_WINDOWS
3276 ? LAST_WIN_NR : LAST_TAB_NR;
3277 }
3278 else
3279 {
3280 // there is no Vim command which uses '%' and
3281 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003282 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003283 return FAIL;
3284 }
3285 break;
3286 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003287 case ADDR_UNSIGNED:
3288 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003289 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003290 return FAIL;
3291 case ADDR_ARGUMENTS:
3292 if (ARGCOUNT == 0)
3293 eap->line1 = eap->line2 = 0;
3294 else
3295 {
3296 eap->line1 = 1;
3297 eap->line2 = ARGCOUNT;
3298 }
3299 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003300 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003301#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003302 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003303 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003304 if (eap->line2 == 0)
3305 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003306#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003307 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003308 case ADDR_NONE:
3309 // Will give an error later if a range is found.
3310 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003311 }
3312 ++eap->addr_count;
3313 }
3314 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3315 {
3316 pos_T *fp;
3317
3318 // '*' - visual area
3319 if (eap->addr_type != ADDR_LINES)
3320 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003321 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003322 return FAIL;
3323 }
3324
3325 ++eap->cmd;
3326 if (!eap->skip)
3327 {
3328 fp = getmark('<', FALSE);
3329 if (check_mark(fp) == FAIL)
3330 return FAIL;
3331 eap->line1 = fp->lnum;
3332 fp = getmark('>', FALSE);
3333 if (check_mark(fp) == FAIL)
3334 return FAIL;
3335 eap->line2 = fp->lnum;
3336 ++eap->addr_count;
3337 }
3338 }
3339 }
3340 else
3341 eap->line2 = lnum;
3342 eap->addr_count++;
3343
3344 if (*eap->cmd == ';')
3345 {
3346 if (!eap->skip)
3347 {
3348 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003349 // Don't leave the cursor on an illegal line or column, but do
3350 // accept zero as address, so 0;/PATTERN/ works correctly.
3351 if (eap->line2 > 0)
3352 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003353 }
3354 }
3355 else if (*eap->cmd != ',')
3356 break;
3357 ++eap->cmd;
3358 }
3359
3360 // One address given: set start and end lines.
3361 if (eap->addr_count == 1)
3362 {
3363 eap->line1 = eap->line2;
3364 // ... but only implicit: really no address given
3365 if (lnum == MAXLNUM)
3366 eap->addr_count = 0;
3367 }
3368 return OK;
3369}
3370
3371/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003372 * Append "cmd" to the error message in IObuff.
3373 * Takes care of limiting the length and handling 0xa0, which would be
3374 * invisible otherwise.
3375 */
3376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003377append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003378{
3379 char_u *s = cmd;
3380 char_u *d;
3381
3382 STRCAT(IObuff, ": ");
3383 d = IObuff + STRLEN(IObuff);
3384 while (*s != NUL && d - IObuff < IOSIZE - 7)
3385 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003386 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003387 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003388 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003389 STRCPY(d, "<a0>");
3390 d += 4;
3391 }
3392 else
3393 MB_COPY_CHAR(s, d);
3394 }
3395 *d = NUL;
3396}
3397
3398/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003399 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3400 * the name. Otherwise just return "start".
3401 */
3402 char_u *
3403skip_option_env_lead(char_u *start)
3404{
3405 char_u *name = start;
3406
3407 if (*start == '&')
3408 {
3409 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3410 name += 3;
3411 else
3412 name += 1;
3413 }
3414 else if (*start == '$')
3415 name += 1;
3416 return name;
3417}
3418
3419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003421 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003422 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003423 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424 *
3425 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3426 * assignment without "let". Sets eap->cmdidx to the command while returning
3427 * "eap->cmd".
3428 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 * Returns NULL for an ambiguous user command.
3430 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 char_u *
3432find_ex_command(
3433 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003434 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003435 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003436 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437{
3438 int len;
3439 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003440 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003441#ifndef FEAT_EVAL
3442 int vim9 = FALSE;
3443#else
3444 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 /*
3447 * Recognize a Vim9 script function/method call and assignment:
3448 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3449 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003450 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003451 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003453 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003455 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003456 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003457 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003458 int oplen;
3459 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003460 char_u *swp;
3461
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003462 if (*eap->cmd == '&'
3463 || *eap->cmd == '$'
3464 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003465 && (valid_yank_reg(eap->cmd[1], FALSE)
3466 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003467 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003468 if (*eap->cmd == '&')
3469 {
3470 p = eap->cmd + 1;
3471 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3472 p += 2;
3473 p = to_name_end(p, FALSE);
3474 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003475 else if (*eap->cmd == '$')
3476 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003477 else
3478 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003479 if (ends_excmd(*skipwhite(p)))
3480 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003481 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3482 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003483 eap->cmdidx = CMD_eval;
3484 return eap->cmd;
3485 }
3486 // "&option" can be followed by "->" or "=", check below
3487 }
3488
3489 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003491 if (
3492 // "(..." is an expression.
3493 // "funcname(" is always a function call.
3494 *p == '('
3495 || (p == eap->cmd
3496 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003497 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003498 *eap->cmd == '{'
3499 // "'string'->func()" is an expression.
3500 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003501 // '"string"->func()' is an expression.
3502 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003503 // "g:varname" is an expression.
3504 || eap->cmd[1] == ':'
3505 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003506 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003507 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003508 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003509 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3510 {
3511 // "{" by itself is the start of a block.
3512 eap->cmdidx = CMD_block;
3513 return eap->cmd + 1;
3514 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003515 eap->cmdidx = CMD_eval;
3516 return eap->cmd;
3517 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003518
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003519 if (p != eap->cmd && (
3520 // "varname[]" is an expression.
3521 *p == '['
3522 // "varname.key" is an expression.
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02003523 || (*p == '.' && (ASCII_ISALPHA(p[1])
3524 || p[1] == '_'))))
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003525 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003526 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003527
3528 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003529 // Skip over the whole thing, it can be:
3530 // name.member = val
3531 // name[a : b] = val
3532 // name[idx] = val
3533 // name[idx].member = val
3534 // etc.
3535 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003536 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003537 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003538 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003539 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003540 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003541 || (after[0] == '.' && after[1] == '.'
3542 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003543 eap->cmdidx = CMD_var;
3544 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003545 --emsg_silent;
3546 return eap->cmd;
3547 }
3548
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003549 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3550 // an assignment.
3551 // If there is no line break inside the "[...]" then "p" is
3552 // advanced to after the "]" by to_name_const_end(): check if a "="
3553 // follows.
3554 // If "[...]" has a line break "p" still points at the "[" and it
3555 // can't be an assignment.
3556 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003557 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003558 char_u *eq;
3559
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003560 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003561 if (p == eap->cmd && *p == '[')
3562 {
3563 int count = 0;
3564 int semicolon = FALSE;
3565
3566 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3567 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003568 eq = p;
3569 if (eq != NULL)
3570 {
3571 eq = skipwhite(eq);
3572 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3573 ++eq;
3574 }
3575 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003576 {
3577 eap->cmdidx = CMD_eval;
3578 return eap->cmd;
3579 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003580 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003581 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003582 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003583 return eap->cmd;
3584 }
3585 }
3586
3587 // Recognize an assignment if we recognize the variable name:
3588 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003589 // "@r = expr"
3590 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003591 // "var = expr" where "var" is a variable name or we are skipping
3592 // (variable declaration might have been skipped).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003593 oplen = assignment_len(skipwhite(p), &heredoc);
3594 if (oplen > 0)
3595 {
3596 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3597 || *eap->cmd == '&'
3598 || *eap->cmd == '$'
3599 || *eap->cmd == '@'
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003600 || eap->skip
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003601 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003602 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003603 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003604 return eap->cmd;
3605 }
3606 }
3607
3608 // Recognize using a type for a w:, b:, t: or g: variable:
3609 // "w:varname: number = 123".
3610 if (eap->cmd[1] == ':' && *p == ':')
3611 {
3612 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003613 return eap->cmd;
3614 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003615 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003616
Bram Moolenaar7b829262021-10-13 15:04:34 +01003617 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3618 // an expression. A global/substitute/list command needs to use a
3619 // longer name.
3620 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3621 {
3622 eap->cmdidx = CMD_eval;
3623 return eap->cmd;
3624 }
3625
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003626 // If it is an ID it might be a variable with an operator on the next
3627 // line, if the variable exists it can't be an Ex command.
3628 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003629 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003630 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3631 {
3632 eap->cmdidx = CMD_eval;
3633 return eap->cmd;
3634 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003635
3636 // Check for "++nr" and "--nr".
3637 if (p == eap->cmd && p[0] == p[1] && (*p == '+' || *p == '-'))
3638 {
3639 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3640 return eap->cmd + 2;
3641 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 }
3643#endif
3644
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 /*
3646 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003647 * Exceptions:
Bram Moolenaar10b94212021-02-19 21:42:57 +01003648 * - The 'k' command can directly be followed by any character.
3649 * But it is not used in Vim9 script.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003651 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003653 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 */
3655 p = eap->cmd;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003656 if (!vim9 && *p == 'k')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
3658 eap->cmdidx = CMD_k;
3659 ++p;
3660 }
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003661 else if (!vim9
3662 && p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003663 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3664 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 || p[1] == 'g'
3666 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3667 || p[1] == 'I'
3668 || (p[1] == 'r' && p[2] != 'e')))
3669 {
3670 eap->cmdidx = CMD_substitute;
3671 ++p;
3672 }
3673 else
3674 {
3675 while (ASCII_ISALPHA(*p))
3676 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003677 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003678 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003680 while (ASCII_ISALNUM(*p))
3681 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 }
3683 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3684 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003685 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686 ++p;
3687 while (ASCII_ISALPHA(*p))
3688 ++p;
3689 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003690
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003691 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003692 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 ++p;
3694 len = (int)(p - eap->cmd);
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003695 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003696 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003697 // Check for ":dl", ":dell", etc. to ":deletel": that's
3698 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003699 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003700 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003701 break;
3702 if (i == len - 1)
3703 {
3704 --len;
3705 if (p[-1] == 'l')
3706 eap->flags |= EXFLAG_LIST;
3707 else
3708 eap->flags |= EXFLAG_PRINT;
3709 }
3710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003712 if (ASCII_ISLOWER(eap->cmd[0]))
3713 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003714 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003715 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003716
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003717 if (command_count != (int)CMD_SIZE)
3718 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003719 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003720 getout(1);
3721 }
3722
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003723 // Use a precomputed index for fast look-up in cmdnames[]
3724 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003725 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3726 if (ASCII_ISLOWER(c2))
3727 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3728 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003729 else if (ASCII_ISUPPER(eap->cmd[0]))
3730 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003732 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733
3734 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3735 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3736 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3737 (size_t)len) == 0)
3738 {
3739#ifdef FEAT_EVAL
3740 if (full != NULL
3741 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3742 *full = TRUE;
3743#endif
3744 break;
3745 }
3746
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003747 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003748 // 'cpoptions'.
3749 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3750 p = eap->cmd;
3751
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003752 // Look for a user defined command as a last resort. Let ":Print" be
3753 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003754 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3755 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003757 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 while (ASCII_ISALNUM(*p))
3759 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003760 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003762 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 eap->cmdidx = CMD_SIZE;
3764 }
3765
Bram Moolenaar373863e2020-09-26 17:20:53 +02003766 // ":fina" means ":finally" for backwards compatibility.
3767 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3768 eap->cmdidx = CMD_finally;
3769
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003770#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003771 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003772 && vim9
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +02003773 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003774 && (eap->cmdidx < 0 ||
3775 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003776 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003777 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3778
3779 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003780 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003781 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003782 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003783#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003784
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 return p;
3786}
3787
3788#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003789static struct cmdmod
3790{
3791 char *name;
3792 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003793 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003794} cmdmods[] = {
3795 {"aboveleft", 3, FALSE},
3796 {"belowright", 3, FALSE},
3797 {"botright", 2, FALSE},
3798 {"browse", 3, FALSE},
3799 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003800 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003801 {"hide", 3, FALSE},
3802 {"keepalt", 5, FALSE},
3803 {"keepjumps", 5, FALSE},
3804 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003805 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003806 {"leftabove", 5, FALSE},
3807 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003808 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003809 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003810 {"rightbelow", 6, FALSE},
3811 {"sandbox", 3, FALSE},
3812 {"silent", 3, FALSE},
3813 {"tab", 3, TRUE},
3814 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003815 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003816 {"verbose", 4, TRUE},
3817 {"vertical", 4, FALSE},
3818};
3819
3820/*
3821 * Return length of a command modifier (including optional count).
3822 * Return zero when it's not a modifier.
3823 */
3824 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003825modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003826{
3827 int i, j;
3828 char_u *p = cmd;
3829
3830 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02003831 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02003832 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003833 {
3834 for (j = 0; p[j] != NUL; ++j)
3835 if (p[j] != cmdmods[i].name[j])
3836 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003837 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003838 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003839 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003840 }
3841 return 0;
3842}
3843
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844/*
3845 * Return > 0 if an Ex command "name" exists.
3846 * Return 2 if there is an exact match.
3847 * Return 3 if there is an ambiguous match.
3848 */
3849 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003850cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851{
3852 exarg_T ea;
3853 int full = FALSE;
3854 int i;
3855 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003856 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003858 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02003859 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 {
3861 for (j = 0; name[j] != NUL; ++j)
3862 if (name[j] != cmdmods[i].name[j])
3863 break;
3864 if (name[j] == NUL && j >= cmdmods[i].minlen)
3865 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3866 }
3867
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003868 // Check built-in commands and user defined commands.
3869 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003870 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003873 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003875 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3876 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003877 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003878 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3880}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003881
3882/*
3883 * "fullcommand" function
3884 */
3885 void
3886f_fullcommand(typval_T *argvars, typval_T *rettv)
3887{
3888 exarg_T ea;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003889 char_u *name;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003890 char_u *p;
3891
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003892 rettv->v_type = VAR_STRING;
3893 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003894
3895 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
3896 return;
3897
3898 name = argvars[0].vval.v_string;
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003899 if (name == NULL)
3900 return;
3901
3902 while (*name != NUL && *name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003903 name++;
3904 name = skip_range(name, TRUE, NULL);
3905
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003906 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
3907 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003908 ea.addr_count = 0;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003909 p = find_ex_command(&ea, NULL, NULL, NULL);
3910 if (p == NULL || ea.cmdidx == CMD_SIZE)
3911 return;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003912 if (in_vim9script())
3913 {
3914 int res;
3915
3916 ++emsg_silent;
3917 res = not_in_vim9(&ea);
3918 --emsg_silent;
3919
3920 if (res == FAIL)
3921 return;
3922 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003923
3924 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02003925 ? get_user_command_name(ea.useridx, ea.cmdidx)
3926 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003927}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928#endif
3929
Bram Moolenaard0190392019-08-23 21:17:35 +02003930 cmdidx_T
3931excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932{
Bram Moolenaard0190392019-08-23 21:17:35 +02003933 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934
Bram Moolenaard0190392019-08-23 21:17:35 +02003935 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3936 idx = (cmdidx_T)((int)idx + 1))
3937 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 break;
3939
Bram Moolenaard0190392019-08-23 21:17:35 +02003940 return idx;
3941}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942
Bram Moolenaard0190392019-08-23 21:17:35 +02003943 long
3944excmd_get_argt(cmdidx_T idx)
3945{
3946 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947}
3948
3949/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003950 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 *
3952 * Backslashed delimiters after / or ? will be skipped, and commands will
3953 * not be expanded between /'s and ?'s or after "'".
3954 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003955 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 * Returns the "cmd" pointer advanced to beyond the range.
3957 */
3958 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003959skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00003960 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003961 int skip_star, // skip "*" used for Visual range
3962 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00003964 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003965 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003967 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003969 if (*cmd == '\\')
3970 {
3971 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3972 ++cmd;
3973 else
3974 break;
3975 }
3976 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00003978 char_u *p = cmd;
3979
3980 // a quote is only valid at the start or after a separator
3981 while (p > cmd_start)
3982 {
3983 --p;
3984 if (!VIM_ISWHITE(*p))
3985 break;
3986 }
3987 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
3988 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 if (*++cmd == NUL && ctx != NULL)
3990 *ctx = EXPAND_NOTHING;
3991 }
3992 else if (*cmd == '/' || *cmd == '?')
3993 {
3994 delim = *cmd++;
3995 while (*cmd != NUL && *cmd != delim)
3996 if (*cmd++ == '\\' && *cmd != NUL)
3997 ++cmd;
3998 if (*cmd == NUL && ctx != NULL)
3999 *ctx = EXPAND_NOTHING;
4000 }
4001 if (*cmd != NUL)
4002 ++cmd;
4003 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004004
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004005 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004006 while (*cmd == ':')
4007 cmd = skipwhite(cmd + 1);
4008
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004009 // Skip "*" used for Visual range.
4010 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4011 cmd = skipwhite(cmd + 1);
4012
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 return cmd;
4014}
4015
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004016 static void
4017addr_error(cmd_addr_T addr_type)
4018{
4019 if (addr_type == ADDR_NONE)
4020 emsg(_(e_norange));
4021 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004022 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004023}
4024
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004026 * Return the default address for an address type.
4027 */
4028 static linenr_T
4029default_address(exarg_T *eap)
4030{
4031 linenr_T lnum = 0;
4032
4033 switch (eap->addr_type)
4034 {
4035 case ADDR_LINES:
4036 case ADDR_OTHER:
4037 // Default is the cursor line number. Avoid using an invalid
4038 // line number though.
4039 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4040 lnum = curbuf->b_ml.ml_line_count;
4041 else
4042 lnum = curwin->w_cursor.lnum;
4043 break;
4044 case ADDR_WINDOWS:
4045 lnum = CURRENT_WIN_NR;
4046 break;
4047 case ADDR_ARGUMENTS:
4048 lnum = curwin->w_arg_idx + 1;
4049 if (lnum > ARGCOUNT)
4050 lnum = ARGCOUNT;
4051 break;
4052 case ADDR_LOADED_BUFFERS:
4053 case ADDR_BUFFERS:
4054 lnum = curbuf->b_fnum;
4055 break;
4056 case ADDR_TABS:
4057 lnum = CURRENT_TAB_NR;
4058 break;
4059 case ADDR_TABS_RELATIVE:
4060 case ADDR_UNSIGNED:
4061 lnum = 1;
4062 break;
4063 case ADDR_QUICKFIX:
4064#ifdef FEAT_QUICKFIX
4065 lnum = qf_get_cur_idx(eap);
4066#endif
4067 break;
4068 case ADDR_QUICKFIX_VALID:
4069#ifdef FEAT_QUICKFIX
4070 lnum = qf_get_cur_valid_idx(eap);
4071#endif
4072 break;
4073 case ADDR_NONE:
4074 // Will give an error later if a range is found.
4075 break;
4076 }
4077 return lnum;
4078}
4079
4080/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004081 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 *
4083 * Set ptr to the next character after the part that was interpreted.
4084 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004085 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 *
4087 * Return MAXLNUM when no Ex address was found.
4088 */
4089 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004090get_address(
4091 exarg_T *eap UNUSED,
4092 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004093 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004094 int skip, // only skip the address, don't use it
4095 int silent, // no errors or side effects
4096 int to_other_file, // flag: may jump to other file
4097 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098{
4099 int c;
4100 int i;
4101 long n;
4102 char_u *cmd;
4103 pos_T pos;
4104 pos_T *fp;
4105 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004106 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107
4108 cmd = skipwhite(*ptr);
4109 lnum = MAXLNUM;
4110 do
4111 {
4112 switch (*cmd)
4113 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004114 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004115 ++cmd;
4116 switch (addr_type)
4117 {
4118 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004119 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 lnum = curwin->w_cursor.lnum;
4121 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004122 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004123 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004124 break;
4125 case ADDR_ARGUMENTS:
4126 lnum = curwin->w_arg_idx + 1;
4127 break;
4128 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004129 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004130 lnum = curbuf->b_fnum;
4131 break;
4132 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004133 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004134 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004135 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004136 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004137 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004138 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004139 cmd = NULL;
4140 goto error;
4141 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004142 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004143#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004144 lnum = qf_get_cur_idx(eap);
4145#endif
4146 break;
4147 case ADDR_QUICKFIX_VALID:
4148#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004149 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004150#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004151 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004152 }
4153 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004155 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004156 ++cmd;
4157 switch (addr_type)
4158 {
4159 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004160 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 lnum = curbuf->b_ml.ml_line_count;
4162 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004163 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004164 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004165 break;
4166 case ADDR_ARGUMENTS:
4167 lnum = ARGCOUNT;
4168 break;
4169 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004170 buf = lastbuf;
4171 while (buf->b_ml.ml_mfp == NULL)
4172 {
4173 if (buf->b_prev == NULL)
4174 break;
4175 buf = buf->b_prev;
4176 }
4177 lnum = buf->b_fnum;
4178 break;
4179 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004180 lnum = lastbuf->b_fnum;
4181 break;
4182 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004183 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004184 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004185 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004186 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004187 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004188 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004189 cmd = NULL;
4190 goto error;
4191 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004192 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004193#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004194 lnum = qf_get_size(eap);
4195 if (lnum == 0)
4196 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004197#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004198 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004199 case ADDR_QUICKFIX_VALID:
4200#ifdef FEAT_QUICKFIX
4201 lnum = qf_get_valid_size(eap);
4202 if (lnum == 0)
4203 lnum = 1;
4204#endif
4205 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004206 }
4207 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004209 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004210 if (*++cmd == NUL)
4211 {
4212 cmd = NULL;
4213 goto error;
4214 }
4215 if (addr_type != ADDR_LINES)
4216 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004217 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004218 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004219 goto error;
4220 }
4221 if (skip)
4222 ++cmd;
4223 else
4224 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004225 // Only accept a mark in another file when it is
4226 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004227 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4228 ++cmd;
4229 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004230 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004231 lnum = curwin->w_cursor.lnum;
4232 else
4233 {
4234 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 {
4236 cmd = NULL;
4237 goto error;
4238 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004239 lnum = fp->lnum;
4240 }
4241 }
4242 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
4244 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004245 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004246 c = *cmd++;
4247 if (addr_type != ADDR_LINES)
4248 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004249 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004250 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004251 goto error;
4252 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004253 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004254 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004255 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004256 if (*cmd == c)
4257 ++cmd;
4258 }
4259 else
4260 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004261 int flags;
4262
4263 pos = curwin->w_cursor; // save curwin->w_cursor
4264
4265 // When '/' or '?' follows another address, start from
4266 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004267 if (lnum > 0 && lnum != MAXLNUM)
4268 curwin->w_cursor.lnum =
4269 lnum > curbuf->b_ml.ml_line_count
4270 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004271
4272 // Start a forward search at the end of the line (unless
4273 // before the first line).
4274 // Start a backward search at the start of the line.
4275 // This makes sure we never match in the current
4276 // line, and can match anywhere in the
4277 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004278 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004279 curwin->w_cursor.col = MAXCOL;
4280 else
4281 curwin->w_cursor.col = 0;
4282 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004283 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004284 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004285 {
4286 curwin->w_cursor = pos;
4287 cmd = NULL;
4288 goto error;
4289 }
4290 lnum = curwin->w_cursor.lnum;
4291 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004292 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004293 cmd += searchcmdlen;
4294 }
4295 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004297 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004298 ++cmd;
4299 if (addr_type != ADDR_LINES)
4300 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004301 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004302 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004303 goto error;
4304 }
4305 if (*cmd == '&')
4306 i = RE_SUBST;
4307 else if (*cmd == '?' || *cmd == '/')
4308 i = RE_SEARCH;
4309 else
4310 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004311 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004312 cmd = NULL;
4313 goto error;
4314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004316 if (!skip)
4317 {
4318 /*
4319 * When search follows another address, start from
4320 * there.
4321 */
4322 if (lnum != MAXLNUM)
4323 pos.lnum = lnum;
4324 else
4325 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004327 /*
4328 * Start the search just like for the above
4329 * do_search().
4330 */
4331 if (*cmd != '?')
4332 pos.col = MAXCOL;
4333 else
4334 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004335 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004336 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004337 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004338 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004339 lnum = pos.lnum;
4340 else
4341 {
4342 cmd = NULL;
4343 goto error;
4344 }
4345 }
4346 ++cmd;
4347 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348
4349 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004350 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004351 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
4353
4354 for (;;)
4355 {
4356 cmd = skipwhite(cmd);
4357 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4358 break;
4359
4360 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004361 {
4362 switch (addr_type)
4363 {
4364 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004365 case ADDR_OTHER:
4366 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004367 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004368 break;
4369 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004370 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004371 break;
4372 case ADDR_ARGUMENTS:
4373 lnum = curwin->w_arg_idx + 1;
4374 break;
4375 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004376 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004377 lnum = curbuf->b_fnum;
4378 break;
4379 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004380 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004381 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004382 case ADDR_TABS_RELATIVE:
4383 lnum = 1;
4384 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004385 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004386#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004387 lnum = qf_get_cur_idx(eap);
4388#endif
4389 break;
4390 case ADDR_QUICKFIX_VALID:
4391#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004392 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004393#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004394 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004395 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004396 case ADDR_UNSIGNED:
4397 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004398 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004399 }
4400 }
4401
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004403 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 else
4405 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004406 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 n = 1;
4408 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004409 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004411 if (n == MAXLNUM)
4412 {
4413 emsg(_(e_line_number_out_of_range));
4414 goto error;
4415 }
4416 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004417
4418 if (addr_type == ADDR_TABS_RELATIVE)
4419 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004420 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004421 cmd = NULL;
4422 goto error;
4423 }
4424 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004425 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004426 lnum = compute_buffer_local_count(
4427 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004429 {
4430#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004431 // Relative line addressing, need to adjust for folded lines
4432 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004433 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
Bram Moolenaar03725c52021-11-24 12:17:53 +00004434 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004435 (void)hasFolding(lnum, NULL, &lnum);
4436#endif
4437 if (i == '-')
4438 lnum -= n;
4439 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004440 {
4441 if (n >= LONG_MAX - lnum)
4442 {
4443 emsg(_(e_line_number_out_of_range));
4444 goto error;
4445 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004446 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004447 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 }
4450 } while (*cmd == '/' || *cmd == '?');
4451
4452error:
4453 *ptr = cmd;
4454 return lnum;
4455}
4456
4457/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004458 * Set eap->line1 and eap->line2 to the whole range.
4459 * Used for commands with the EX_DFLALL flag and no range given.
4460 */
4461 static void
4462address_default_all(exarg_T *eap)
4463{
4464 eap->line1 = 1;
4465 switch (eap->addr_type)
4466 {
4467 case ADDR_LINES:
4468 case ADDR_OTHER:
4469 eap->line2 = curbuf->b_ml.ml_line_count;
4470 break;
4471 case ADDR_LOADED_BUFFERS:
4472 {
4473 buf_T *buf = firstbuf;
4474
4475 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4476 buf = buf->b_next;
4477 eap->line1 = buf->b_fnum;
4478 buf = lastbuf;
4479 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4480 buf = buf->b_prev;
4481 eap->line2 = buf->b_fnum;
4482 }
4483 break;
4484 case ADDR_BUFFERS:
4485 eap->line1 = firstbuf->b_fnum;
4486 eap->line2 = lastbuf->b_fnum;
4487 break;
4488 case ADDR_WINDOWS:
4489 eap->line2 = LAST_WIN_NR;
4490 break;
4491 case ADDR_TABS:
4492 eap->line2 = LAST_TAB_NR;
4493 break;
4494 case ADDR_TABS_RELATIVE:
4495 eap->line2 = 1;
4496 break;
4497 case ADDR_ARGUMENTS:
4498 if (ARGCOUNT == 0)
4499 eap->line1 = eap->line2 = 0;
4500 else
4501 eap->line2 = ARGCOUNT;
4502 break;
4503 case ADDR_QUICKFIX_VALID:
4504#ifdef FEAT_QUICKFIX
4505 eap->line2 = qf_get_valid_size(eap);
4506 if (eap->line2 == 0)
4507 eap->line2 = 1;
4508#endif
4509 break;
4510 case ADDR_NONE:
4511 case ADDR_UNSIGNED:
4512 case ADDR_QUICKFIX:
4513 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4514 break;
4515 }
4516}
4517
4518
4519/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004520 * Get flags from an Ex command argument.
4521 */
4522 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004523get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004524{
4525 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4526 {
4527 if (*eap->arg == 'l')
4528 eap->flags |= EXFLAG_LIST;
4529 else if (*eap->arg == 'p')
4530 eap->flags |= EXFLAG_PRINT;
4531 else
4532 eap->flags |= EXFLAG_NR;
4533 eap->arg = skipwhite(eap->arg + 1);
4534 }
4535}
4536
4537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 * Function called for command which is Not Implemented. NI!
4539 */
4540 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004541ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542{
4543 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004544 eap->errmsg =
4545 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546}
4547
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004548#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549/*
4550 * Function called for script command which is Not Implemented. NI!
4551 * Skips over ":perl <<EOF" constructs.
4552 */
4553 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004554ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555{
4556 if (!eap->skip)
4557 ex_ni(eap);
4558 else
4559 vim_free(script_get(eap, eap->arg));
4560}
4561#endif
4562
4563/*
4564 * Check range in Ex command for validity.
4565 * Return NULL when valid, error message when invalid.
4566 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004567 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004568invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004570 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004571
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 if ( eap->line1 < 0
4573 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004574 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004575 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004576
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004577 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004578 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004579 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004580 {
4581 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004582 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004583#ifdef FEAT_DIFF
4584 + (eap->cmdidx == CMD_diffget)
4585#endif
4586 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004587 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004588 break;
4589 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004590 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004591 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004592 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004593 break;
4594 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004595 // Only a boundary check, not whether the buffers actually
4596 // exist.
4597 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004598 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004599 break;
4600 case ADDR_LOADED_BUFFERS:
4601 buf = firstbuf;
4602 while (buf->b_ml.ml_mfp == NULL)
4603 {
4604 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004605 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004606 buf = buf->b_next;
4607 }
4608 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004609 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004610 buf = lastbuf;
4611 while (buf->b_ml.ml_mfp == NULL)
4612 {
4613 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004614 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004615 buf = buf->b_prev;
4616 }
4617 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004618 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004619 break;
4620 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004621 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004622 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004623 break;
4624 case ADDR_TABS:
4625 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004626 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004627 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004628 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004629 case ADDR_OTHER:
4630 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004631 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004632 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004633#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004634 // No error for value that is too big, will use the last entry.
4635 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004636 {
4637 if (eap->addr_count == 0)
4638 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004639 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004640 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004641#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004642 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004643 case ADDR_QUICKFIX_VALID:
4644#ifdef FEAT_QUICKFIX
4645 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4646 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004647 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004648#endif
4649 break;
4650 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004651 case ADDR_NONE:
4652 // Will give an error elsewhere.
4653 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004654 }
4655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 return NULL;
4657}
4658
4659/*
4660 * Correct the range for zero line number, if required.
4661 */
4662 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004663correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004665 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 {
4667 if (eap->line1 == 0)
4668 eap->line1 = 1;
4669 if (eap->line2 == 0)
4670 eap->line2 = 1;
4671 }
4672}
4673
Bram Moolenaar748bf032005-02-02 23:04:36 +00004674#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004675/*
4676 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4677 * pattern. Otherwise return eap->arg.
4678 */
4679 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004680skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004681{
4682 char_u *p = eap->arg;
4683
Bram Moolenaara37420f2006-02-04 22:37:47 +00004684 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4685 || eap->cmdidx == CMD_vimgrepadd
4686 || eap->cmdidx == CMD_lvimgrepadd
4687 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004688 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004689 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004690 if (p == NULL)
4691 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004692 }
4693 return p;
4694}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004695
4696/*
4697 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4698 * in the command line, so that things like % get expanded.
4699 */
4700 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004701replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004702{
4703 char_u *new_cmdline;
4704 char_u *program;
4705 char_u *pos;
4706 char_u *ptr;
4707 int len;
4708 int i;
4709
4710 /*
4711 * Don't do it when ":vimgrep" is used for ":grep".
4712 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004713 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4714 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4715 || eap->cmdidx == CMD_grepadd
4716 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004717 && !grep_internal(eap->cmdidx))
4718 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004719 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4720 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004721 {
4722 if (*curbuf->b_p_gp == NUL)
4723 program = p_gp;
4724 else
4725 program = curbuf->b_p_gp;
4726 }
4727 else
4728 {
4729 if (*curbuf->b_p_mp == NUL)
4730 program = p_mp;
4731 else
4732 program = curbuf->b_p_mp;
4733 }
4734
4735 p = skipwhite(p);
4736
4737 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4738 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004739 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004740 i = 1;
4741 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4742 ++i;
4743 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004744 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004745 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004746 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004747 ptr = new_cmdline;
4748 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4749 {
4750 i = (int)(pos - program);
4751 STRNCPY(ptr, program, i);
4752 STRCPY(ptr += i, p);
4753 ptr += len;
4754 program = pos + 2;
4755 }
4756 STRCPY(ptr, program);
4757 }
4758 else
4759 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004760 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004761 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004762 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004763 STRCPY(new_cmdline, program);
4764 STRCAT(new_cmdline, " ");
4765 STRCAT(new_cmdline, p);
4766 }
4767 msg_make(p);
4768
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004769 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004770 vim_free(*cmdlinep);
4771 *cmdlinep = new_cmdline;
4772 p = new_cmdline;
4773 }
4774 return p;
4775}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004776#endif
4777
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778/*
4779 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004780 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 * Return FAIL for failure, OK otherwise.
4782 */
4783 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004784expand_filename(
4785 exarg_T *eap,
4786 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004787 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004789 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 char_u *repl;
4791 int srclen;
4792 char_u *p;
4793 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004794 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795
Bram Moolenaar748bf032005-02-02 23:04:36 +00004796#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004797 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004798 p = skip_grep_pat(eap);
4799#else
4800 p = eap->arg;
4801#endif
4802
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 /*
4804 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4805 * the file name contains a wildcard it should not cause expanding.
4806 * (it will be expanded anyway if there is a wildcard before replacing).
4807 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004808 has_wildcards = mch_has_wildcard(p);
4809 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004811#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004812 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004813 if (p[0] == '`' && p[1] == '=')
4814 {
4815 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004816 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004817 if (*p == '`')
4818 ++p;
4819 continue;
4820 }
4821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 /*
4823 * Quick check if this cannot be the start of a special string.
4824 * Also removes backslash before '%', '#' and '<'.
4825 */
4826 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4827 {
4828 ++p;
4829 continue;
4830 }
4831
4832 /*
4833 * Try to find a match at this position.
4834 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004835 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4836 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004837 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004839 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 {
4841 p += srclen;
4842 continue;
4843 }
4844
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004845 // Wildcards won't be expanded below, the replacement is taken
4846 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004847 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4848 {
4849 char_u *l = repl;
4850
4851 repl = expand_env_save(repl);
4852 vim_free(l);
4853 }
4854
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004855 // Need to escape white space et al. with a backslash.
4856 // Don't do this for:
4857 // - replacement that already has been escaped: "##"
4858 // - shell commands (may have to use quotes instead).
4859 // - non-unix systems when there is a single argument (spaces don't
4860 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004862 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 && eap->cmdidx != CMD_grep
4865 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004866 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004867 && eap->cmdidx != CMD_lgrep
4868 && eap->cmdidx != CMD_lgrepadd
4869 && eap->cmdidx != CMD_lmake
4870 && eap->cmdidx != CMD_make
4871 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004873 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874#endif
4875 )
4876 {
4877 char_u *l;
4878#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004879 // Don't escape a backslash here, because rem_backslash() doesn't
4880 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 static char_u *nobslash = (char_u *)" \t\"|";
4882# define ESCAPE_CHARS nobslash
4883#else
4884# define ESCAPE_CHARS escape_chars
4885#endif
4886
4887 for (l = repl; *l; ++l)
4888 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4889 {
4890 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4891 if (l != NULL)
4892 {
4893 vim_free(repl);
4894 repl = l;
4895 }
4896 break;
4897 }
4898 }
4899
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004900 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004901 if ((eap->usefilter || eap->cmdidx == CMD_bang
4902 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004903 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 {
4905 char_u *l;
4906
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004907 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 if (l != NULL)
4909 {
4910 vim_free(repl);
4911 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 }
4913 }
4914
4915 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4916 vim_free(repl);
4917 if (p == NULL)
4918 return FAIL;
4919 }
4920
4921 /*
4922 * One file argument: Expand wildcards.
4923 * Don't do this with ":r !command" or ":w !command".
4924 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004925 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 {
4927 /*
4928 * May do this twice:
4929 * 1. Replace environment variables.
4930 * 2. Replace any other wildcards, remove backslashes.
4931 */
4932 for (n = 1; n <= 2; ++n)
4933 {
4934 if (n == 2)
4935 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 /*
4937 * Halve the number of backslashes (this is Vi compatible).
4938 * For Unix and OS/2, when wildcards are expanded, this is
4939 * done by ExpandOne() below.
4940 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004941#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 if (!has_wildcards)
4943#endif
4944 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
4946
4947 if (has_wildcards)
4948 {
4949 if (n == 1)
4950 {
4951 /*
4952 * First loop: May expand environment variables. This
4953 * can be done much faster with expand_env() than with
4954 * something else (e.g., calling a shell).
4955 * After expanding environment variables, check again
4956 * if there are still wildcards present.
4957 */
4958 if (vim_strchr(eap->arg, '$') != NULL
4959 || vim_strchr(eap->arg, '~') != NULL)
4960 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004961 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004962 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 has_wildcards = mch_has_wildcard(NameBuff);
4964 p = NameBuff;
4965 }
4966 else
4967 p = NULL;
4968 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004969 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 {
4971 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004972 int options = WILD_LIST_NOTFOUND
4973 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974
4975 ExpandInit(&xpc);
4976 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004977 if (p_wic)
4978 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004980 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 if (p == NULL)
4982 return FAIL;
4983 }
4984 if (p != NULL)
4985 {
4986 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4987 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004988 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 vim_free(p);
4990 }
4991 }
4992 }
4993 }
4994 return OK;
4995}
4996
4997/*
4998 * Replace part of the command line, keeping eap->cmd, eap->arg and
4999 * eap->nextcmd correct.
5000 * "src" points to the part that is to be replaced, of length "srclen".
5001 * "repl" is the replacement string.
5002 * Returns a pointer to the character after the replaced string.
5003 * Returns NULL for failure.
5004 */
5005 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005006repl_cmdline(
5007 exarg_T *eap,
5008 char_u *src,
5009 int srclen,
5010 char_u *repl,
5011 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012{
5013 int len;
5014 int i;
5015 char_u *new_cmdline;
5016
5017 /*
5018 * The new command line is build in new_cmdline[].
5019 * First allocate it.
5020 * Careful: a "+cmd" argument may have been NUL terminated.
5021 */
5022 len = (int)STRLEN(repl);
5023 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005024 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005025 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005026 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005027 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028
5029 /*
5030 * Copy the stuff before the expanded part.
5031 * Copy the expanded stuff.
5032 * Copy what came after the expanded part.
5033 * Copy the next commands, if there are any.
5034 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005035 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005037
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005039 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005041 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005043 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
5045 i = (int)STRLEN(new_cmdline) + 1;
5046 STRCPY(new_cmdline + i, eap->nextcmd);
5047 eap->nextcmd = new_cmdline + i;
5048 }
5049 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5050 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5051 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5052 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5053 vim_free(*cmdlinep);
5054 *cmdlinep = new_cmdline;
5055
5056 return src;
5057}
5058
5059/*
5060 * Check for '|' to separate commands and '"' to start comments.
5061 */
5062 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005063separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064{
5065 char_u *p;
5066
Bram Moolenaar86b68352004-12-27 21:59:20 +00005067#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005068 p = skip_grep_pat(eap);
5069#else
5070 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005071#endif
5072
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005073 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 {
5075 if (*p == Ctrl_V)
5076 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005077 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005078 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005080 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005081 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005082 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 break;
5084 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005085
5086#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005087 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005088 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005089 {
5090 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005091 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005092 if (*p == NUL) // stop at NUL after CTRL-V
5093 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005094 }
5095#endif
5096
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005097 // Check for '"': start of comment or '|': next command
5098 // :@" and :*" do not start a comment!
5099 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005100 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02005101#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005102 && !in_vim9script()
5103#endif
5104 && !(eap->argt & EX_NOTRLCOM)
5105 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5106 || p != eap->arg)
5107 && (eap->cmdidx != CMD_redir
5108 || p != eap->arg + 1 || p[-1] != '@'))
5109#ifdef FEAT_EVAL
5110 || (*p == '#'
5111 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005112 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005113 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02005114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 || *p == '|' || *p == '\n')
5116 {
5117 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005118 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 * AND 'b' is present in 'cpoptions'.
5120 */
5121 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005122 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005124 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 --p;
5126 }
5127 else
5128 {
5129 eap->nextcmd = check_nextcmd(p);
5130 *p = NUL;
5131 break;
5132 }
5133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005135
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005136 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 del_trailing_spaces(eap->arg);
5138}
5139
5140/*
5141 * get + command from ex argument
5142 */
5143 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005144getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145{
5146 char_u *arg = *argp;
5147 char_u *command = NULL;
5148
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005149 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 {
5151 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005152 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 command = dollar_command;
5154 else
5155 {
5156 command = arg;
5157 arg = skip_cmd_arg(command, TRUE);
5158 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005159 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 }
5161
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005162 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 *argp = arg;
5164 }
5165 return command;
5166}
5167
5168/*
5169 * Find end of "+command" argument. Skip over "\ " and "\\".
5170 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005171 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005172skip_cmd_arg(
5173 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005174 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175{
5176 while (*p && !vim_isspace(*p))
5177 {
5178 if (*p == '\\' && p[1] != NUL)
5179 {
5180 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005181 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 else
5183 ++p;
5184 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005185 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 }
5187 return p;
5188}
5189
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005190 int
5191get_bad_opt(char_u *p, exarg_T *eap)
5192{
5193 if (STRICMP(p, "keep") == 0)
5194 eap->bad_char = BAD_KEEP;
5195 else if (STRICMP(p, "drop") == 0)
5196 eap->bad_char = BAD_DROP;
5197 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5198 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005199 else
5200 return FAIL;
5201 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005202}
5203
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204/*
5205 * Get "++opt=arg" argument.
5206 * Return FAIL or OK.
5207 */
5208 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005209getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210{
5211 char_u *arg = eap->arg + 2;
5212 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005213 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005216 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5218 {
5219 if (*arg == 'n')
5220 {
5221 arg += 2;
5222 eap->force_bin = FORCE_NOBIN;
5223 }
5224 else
5225 eap->force_bin = FORCE_BIN;
5226 if (!checkforcmd(&arg, "binary", 3))
5227 return FAIL;
5228 eap->arg = skipwhite(arg);
5229 return OK;
5230 }
5231
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005232 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005233 if (STRNCMP(arg, "edit", 4) == 0)
5234 {
5235 eap->read_edit = TRUE;
5236 eap->arg = skipwhite(arg + 4);
5237 return OK;
5238 }
5239
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 if (STRNCMP(arg, "ff", 2) == 0)
5241 {
5242 arg += 2;
5243 pp = &eap->force_ff;
5244 }
5245 else if (STRNCMP(arg, "fileformat", 10) == 0)
5246 {
5247 arg += 10;
5248 pp = &eap->force_ff;
5249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 else if (STRNCMP(arg, "enc", 3) == 0)
5251 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005252 if (STRNCMP(arg, "encoding", 8) == 0)
5253 arg += 8;
5254 else
5255 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 pp = &eap->force_enc;
5257 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005258 else if (STRNCMP(arg, "bad", 3) == 0)
5259 {
5260 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005261 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263
5264 if (pp == NULL || *arg != '=')
5265 return FAIL;
5266
5267 ++arg;
5268 *pp = (int)(arg - eap->cmd);
5269 arg = skip_cmd_arg(arg, FALSE);
5270 eap->arg = skipwhite(arg);
5271 *arg = NUL;
5272
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 if (pp == &eap->force_ff)
5274 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5276 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005277 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005279 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005281 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5283 *p = TOLOWER_ASC(*p);
5284 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005285 else
5286 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005287 // Check ++bad= argument. Must be a single-byte character, "keep" or
5288 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005289 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005290 return FAIL;
5291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292
5293 return OK;
5294}
5295
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005297ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298{
5299 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005300 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 * directory for security reasons.
5302 */
5303 if (secure)
5304 {
5305 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005306 eap->errmsg =
5307 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 }
5309 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005310 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 else
5312 do_augroup(eap->arg, eap->forceit);
5313}
5314
5315/*
5316 * ":doautocmd": Apply the automatic commands to the current buffer.
5317 */
5318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005319ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005321 char_u *arg = eap->arg;
5322 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005323 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005324
Bram Moolenaar1610d052016-06-09 22:53:01 +02005325 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005326 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005327 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005328 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331/*
5332 * :[N]bunload[!] [N] [bufname] unload buffer
5333 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5334 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5335 */
5336 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005337ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005339 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005340 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 eap->errmsg = do_bufdel(
5342 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5343 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5344 : DOBUF_UNLOAD, eap->arg,
5345 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5346}
5347
5348/*
5349 * :[N]buffer [N] to buffer N
5350 * :[N]sbuffer [N] to buffer N
5351 */
5352 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005353ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005355 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005356 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005358 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 else
5360 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005361 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5363 else
5364 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005365 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005366 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 }
5368}
5369
5370/*
5371 * :[N]bmodified [N] to next mod. buffer
5372 * :[N]sbmodified [N] to next mod. buffer
5373 */
5374 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005375ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376{
5377 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005378 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005379 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380}
5381
5382/*
5383 * :[N]bnext [N] to next buffer
5384 * :[N]sbnext [N] split and to next buffer
5385 */
5386 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005387ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005389 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005390 return;
5391
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005393 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005394 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395}
5396
5397/*
5398 * :[N]bNext [N] to previous buffer
5399 * :[N]bprevious [N] to previous buffer
5400 * :[N]sbNext [N] split and to previous buffer
5401 * :[N]sbprevious [N] split and to previous buffer
5402 */
5403 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005404ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005406 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005407 return;
5408
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005410 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005411 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412}
5413
5414/*
5415 * :brewind to first buffer
5416 * :bfirst to first buffer
5417 * :sbrewind split and to first buffer
5418 * :sbfirst split and to first buffer
5419 */
5420 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005421ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005423 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005424 return;
5425
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005427 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005428 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429}
5430
5431/*
5432 * :blast to last buffer
5433 * :sblast split and to last buffer
5434 */
5435 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005436ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005438 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005439 return;
5440
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005442 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005443 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445
Bram Moolenaar7a092242020-04-16 22:10:49 +02005446/*
5447 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005448 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005449 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005451ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005453 int comment_char = '"';
5454
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005455#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005456 if (in_vim9script())
5457 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005458#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005459 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005460}
5461
5462/*
5463 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5464 * to "cmd_start" or has a white space character before it.
5465 */
5466 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005467ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005468{
5469 int c = *cmd;
5470
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005471 if (c == NUL || c == '|' || c == '\n')
5472 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005473#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005474 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005475 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5476 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005477 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005478#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005479 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480}
5481
5482#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5483 || defined(PROTO)
5484/*
5485 * Return the next command, after the first '|' or '\n'.
5486 * Return NULL if not found.
5487 */
5488 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005489find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490{
5491 while (*p != '|' && *p != '\n')
5492 {
5493 if (*p == NUL)
5494 return NULL;
5495 ++p;
5496 }
5497 return (p + 1);
5498}
5499#endif
5500
5501/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005502 * Check if *p is a separator between Ex commands, skipping over white space.
5503 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 */
5505 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005506check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005508 char_u *s = skipwhite(p);
5509
5510 if (*s == '|' || *s == '\n')
5511 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 else
5513 return NULL;
5514}
5515
5516/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005517 * If "eap->nextcmd" is not set, check for a next command at "p".
5518 */
5519 void
5520set_nextcmd(exarg_T *eap, char_u *arg)
5521{
5522 char_u *p = check_nextcmd(arg);
5523
5524 if (eap->nextcmd == NULL)
5525 eap->nextcmd = p;
5526 else if (p != NULL)
5527 // cannot use "| command" inside a {} block
5528 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5529}
5530
5531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 * - if there are more files to edit
5533 * - and this is the last window
5534 * - and forceit not used
5535 * - and not repeated twice on a row
5536 * return FAIL and give error message if 'message' TRUE
5537 * return OK otherwise
5538 */
5539 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005540check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005541 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005542 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543{
5544 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5545
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005546 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005547 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 {
5549 if (message)
5550 {
5551#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005552 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5553 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005555 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005557 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5558 NGETTEXT("%d more file to edit. Quit anyway?",
5559 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5561 return OK;
5562 return FAIL;
5563 }
5564#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005565 semsg(NGETTEXT("E173: %d more file to edit",
5566 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005567 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 }
5569 return FAIL;
5570 }
5571 return OK;
5572}
5573
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574/*
5575 * Function given to ExpandGeneric() to obtain the list of command names.
5576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005578get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579{
5580 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005581 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 return cmdnames[idx].cmd_name;
5583}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005586ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587{
Bram Moolenaare6850792010-05-14 15:28:44 +02005588 if (*eap->arg == NUL)
5589 {
5590#ifdef FEAT_EVAL
5591 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5592 char_u *p = NULL;
5593
5594 if (expr != NULL)
5595 {
5596 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005597 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005598 --emsg_off;
5599 vim_free(expr);
5600 }
5601 if (p != NULL)
5602 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005603 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005604 vim_free(p);
5605 }
5606 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005607 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005608#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005609 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005610#endif
5611 }
5612 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005613 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005614
5615#ifdef FEAT_VTP
5616 else if (has_vtp_working())
5617 {
5618 // background color change requires clear + redraw
5619 update_screen(CLEAR);
5620 redrawcmd();
5621 }
5622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623}
5624
5625 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005626ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627{
5628 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005629 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 do_highlight(eap->arg, eap->forceit, FALSE);
5631}
5632
5633
5634/*
5635 * Call this function if we thought we were going to exit, but we won't
5636 * (because of an error). May need to restore the terminal mode.
5637 */
5638 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005639not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640{
5641 exiting = FALSE;
5642 settmode(TMODE_RAW);
5643}
5644
Bram Moolenaar3552e742021-05-29 12:21:58 +02005645 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005646before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5647{
5648 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5649
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005650 // Bail out when autocommands closed the window.
5651 // Refuse to quit when the buffer in the last window is being closed (can
5652 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005653 if (!win_valid(wp)
5654 || curbuf_locked()
5655 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5656 return TRUE;
5657
5658 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5659 {
5660 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005661 // Refuse to quit when locked or when the window was closed or the
5662 // buffer in the last window is being closed (can only happen in
5663 // autocommands).
5664 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005665 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5666 return TRUE;
5667 }
5668
5669 return FALSE;
5670}
5671
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005673 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005674 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005675 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005677 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005678ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005680 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005681
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682#ifdef FEAT_CMDWIN
5683 if (cmdwin_type != 0)
5684 {
5685 cmdwin_result = Ctrl_C;
5686 return;
5687 }
5688#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005689 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005690 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005691 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005692 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005693 return;
5694 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005695 if (eap->addr_count > 0)
5696 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005697 int wnr = eap->line2;
5698
5699 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5700 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005701 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005702 }
5703 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005704 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005705
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005706 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005707 if (curbuf_locked())
5708 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005709
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005710 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005711 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005712 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713
5714#ifdef FEAT_NETBEANS_INTG
5715 netbeansForcedQuit = eap->forceit;
5716#endif
5717
5718 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02005719 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 */
5721 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5722 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005723 if ((!buf_hide(wp->w_buffer)
5724 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005725 | (eap->forceit ? CCGD_FORCEIT : 0)
5726 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005728 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 {
5730 not_exiting();
5731 }
5732 else
5733 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005734 // quit last window
5735 // Note: only_one_window() returns true, even so a help window is
5736 // still open. In that case only quit, if no address has been
5737 // specified. Example:
5738 // :h|wincmd w|1q - don't quit
5739 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005740 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005742 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005743#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005745#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005746 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005747 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 }
5749}
5750
5751/*
5752 * ":cquit".
5753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005755ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005757 // this does not always pass on the exit code to the Manx compiler. why?
5758 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759}
5760
5761/*
5762 * ":qall": try to quit all windows
5763 */
5764 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005765ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766{
5767# ifdef FEAT_CMDWIN
5768 if (cmdwin_type != 0)
5769 {
5770 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005771 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 else
5773 cmdwin_result = K_XF2;
5774 return;
5775 }
5776# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005777
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005778 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005779 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005780 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005781 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005782 return;
5783 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005784
5785 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005786 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005787
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005789 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 getout(0);
5791 not_exiting();
5792}
5793
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794/*
5795 * ":close": close current window, unless it is the last one
5796 */
5797 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005798ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005800 win_T *win;
5801 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005802#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005804 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005806#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005807 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005808 {
5809 if (eap->addr_count == 0)
5810 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005811 else
5812 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005813 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005814 {
5815 winnr++;
5816 if (winnr == eap->line2)
5817 break;
5818 }
5819 if (win == NULL)
5820 win = lastwin;
5821 ex_win_close(eap->forceit, win, NULL);
5822 }
5823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824}
5825
Bram Moolenaar4033c552017-09-16 20:54:51 +02005826#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005827/*
5828 * ":pclose": Close any preview window.
5829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005831ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005832{
5833 win_T *win;
5834
Bram Moolenaar79648732019-07-18 21:43:07 +02005835 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005836 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005837 if (win->w_p_pvw)
5838 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005839 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005840 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005841 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005842# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005843 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005844 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005845# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005846}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005847#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005848
Bram Moolenaarf740b292006-02-16 22:11:02 +00005849/*
5850 * Close window "win" and take care of handling closing the last window for a
5851 * modified buffer.
5852 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005853 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005854ex_win_close(
5855 int forceit,
5856 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005857 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858{
5859 int need_hide;
5860 buf_T *buf = win->w_buffer;
5861
Bram Moolenaarcf844172020-06-26 19:44:06 +02005862 // Never close the autocommand window.
5863 if (win == aucmd_win)
5864 {
5865 emsg(_(e_autocmd_close));
5866 return;
5867 }
5868
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005870 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005872#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005873 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005875 bufref_T bufref;
5876
5877 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005879 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 return;
5881 need_hide = FALSE;
5882 }
5883 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005886 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 return;
5888 }
5889 }
5890
Bram Moolenaar4033c552017-09-16 20:54:51 +02005891#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005893#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005894
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005895 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005896 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005897 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005898 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005899 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900}
5901
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005903 * Handle the argument for a tabpage related ex command.
5904 * Returns a tabpage number.
5905 * When an error is encountered then eap->errmsg is set.
5906 */
5907 static int
5908get_tabpage_arg(exarg_T *eap)
5909{
5910 int tab_number;
5911 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5912
5913 if (eap->arg && *eap->arg != NUL)
5914 {
5915 char_u *p = eap->arg;
5916 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005917 int relative = 0; // argument +N/-N means: go to N places to the
5918 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005919
5920 if (*p == '-')
5921 {
5922 relative = -1;
5923 p++;
5924 }
5925 else if (*p == '+')
5926 {
5927 relative = 1;
5928 p++;
5929 }
5930
5931 p_save = p;
5932 tab_number = getdigits(&p);
5933
5934 if (relative == 0)
5935 {
5936 if (STRCMP(p, "$") == 0)
5937 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005938 else if (STRCMP(p, "#") == 0)
5939 if (valid_tabpage(lastused_tabpage))
5940 tab_number = tabpage_index(lastused_tabpage);
5941 else
5942 {
5943 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5944 tab_number = 0;
5945 goto theend;
5946 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005947 else if (p == p_save || *p_save == '-' || *p != NUL
5948 || tab_number > LAST_TAB_NR)
5949 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005950 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005951 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005952 goto theend;
5953 }
5954 }
5955 else
5956 {
5957 if (*p_save == NUL)
5958 tab_number = 1;
5959 else if (p == p_save || *p_save == '-' || *p != NUL
5960 || tab_number == 0)
5961 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005962 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005963 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005964 goto theend;
5965 }
5966 tab_number = tab_number * relative + tabpage_index(curtab);
5967 if (!unaccept_arg0 && relative == -1)
5968 --tab_number;
5969 }
5970 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005971 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005972 }
5973 else if (eap->addr_count > 0)
5974 {
5975 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005976 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005977 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005978 tab_number = 0;
5979 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005980 else
5981 {
5982 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005983 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005984 {
5985 --tab_number;
5986 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005987 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01005988 }
5989 }
5990 }
5991 else
5992 {
5993 switch (eap->cmdidx)
5994 {
5995 case CMD_tabnext:
5996 tab_number = tabpage_index(curtab) + 1;
5997 if (tab_number > LAST_TAB_NR)
5998 tab_number = 1;
5999 break;
6000 case CMD_tabmove:
6001 tab_number = LAST_TAB_NR;
6002 break;
6003 default:
6004 tab_number = tabpage_index(curtab);
6005 }
6006 }
6007
6008theend:
6009 return tab_number;
6010}
6011
6012/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006013 * ":tabclose": close current tab page, unless it is the last one.
6014 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 */
6016 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006017ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006019 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006020 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006021
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006022# ifdef FEAT_CMDWIN
6023 if (cmdwin_type != 0)
6024 cmdwin_result = K_IGNORE;
6025 else
6026# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006027 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006028 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006029 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006031 tab_number = get_tabpage_arg(eap);
6032 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006033 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006034 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006035 if (tp == NULL)
6036 {
6037 beep_flush();
6038 return;
6039 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006040 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006041 {
6042 tabpage_close_other(tp, eap->forceit);
6043 return;
6044 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006045 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006046 tabpage_close(eap->forceit);
6047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006049}
6050
6051/*
6052 * ":tabonly": close all tab pages except the current one
6053 */
6054 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006055ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006056{
6057 tabpage_T *tp;
6058 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006059 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006060
6061# ifdef FEAT_CMDWIN
6062 if (cmdwin_type != 0)
6063 cmdwin_result = K_IGNORE;
6064 else
6065# endif
6066 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006067 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006068 else
6069 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006070 tab_number = get_tabpage_arg(eap);
6071 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006072 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006073 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006074 // Repeat this up to a 1000 times, because autocommands may
6075 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006076 for (done = 0; done < 1000; ++done)
6077 {
6078 FOR_ALL_TABPAGES(tp)
6079 if (tp->tp_topframe != topframe)
6080 {
6081 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006082 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006083 if (valid_tabpage(tp))
6084 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006085 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006086 break;
6087 }
6088 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006089 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006090 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006091 }
6092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094
6095/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006096 * Close the current tab page.
6097 */
6098 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006099tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006100{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006101 // First close all the windows but the current one. If that worked then
6102 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006103 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006104 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006105 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006106 ex_win_close(forceit, curwin, NULL);
6107# ifdef FEAT_GUI
6108 need_mouse_correct = TRUE;
6109# endif
6110}
6111
6112/*
6113 * Close tab page "tp", which is not the current tab page.
6114 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006115 * Also takes care of the tab pages line disappearing when closing the
6116 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006117 */
6118 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006119tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006120{
6121 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006122 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006123 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006124
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006125 // Limit to 1000 windows, autocommands may add a window while we close
6126 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006127 while (++done < 1000)
6128 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006129 wp = tp->tp_firstwin;
6130 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006131
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006132 // Autocommands may delete the tab page under our fingers and we may
6133 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006134 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006135 break;
6136 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006137
Bram Moolenaar29323592016-07-24 22:04:11 +02006138 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006139
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006140 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006141 if (h != tabline_height())
6142 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006143}
6144
6145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 * ":only".
6147 */
6148 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006149ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006151 win_T *wp;
6152 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153# ifdef FEAT_GUI
6154 need_mouse_correct = TRUE;
6155# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006156 if (eap->addr_count > 0)
6157 {
6158 wnr = eap->line2;
6159 for (wp = firstwin; --wnr > 0; )
6160 {
6161 if (wp->w_next == NULL)
6162 break;
6163 else
6164 wp = wp->w_next;
6165 }
6166 win_goto(wp);
6167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 close_others(TRUE, eap->forceit);
6169}
6170
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006172ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006174 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01006175 if (!eap->skip)
6176 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006177#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006178 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006179#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006180 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006181 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01006182 else
6183 {
6184 int winnr = 0;
6185 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006186
Bram Moolenaar2256c992016-11-15 21:17:07 +01006187 FOR_ALL_WINDOWS(win)
6188 {
6189 winnr++;
6190 if (winnr == eap->line2)
6191 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006192 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006193 if (win == NULL)
6194 win = lastwin;
6195 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 }
6198}
6199
6200/*
6201 * ":stop" and ":suspend": Suspend Vim.
6202 */
6203 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006204ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205{
6206 /*
6207 * Disallow suspending for "rvim".
6208 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006209 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 {
6211 if (!eap->forceit)
6212 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01006213 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 windgoto((int)Rows - 1, 0);
6215 out_char('\n');
6216 out_flush();
6217 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006218 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006219 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006220 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006222 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006224 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006226 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006227 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 }
6229}
6230
6231/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006232 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 */
6234 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006235ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006237#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006238 if (not_in_vim9(eap) == FAIL)
6239 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241#ifdef FEAT_CMDWIN
6242 if (cmdwin_type != 0)
6243 {
6244 cmdwin_result = Ctrl_C;
6245 return;
6246 }
6247#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006248 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006249 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006250 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006251 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006252 return;
6253 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006254
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006256 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 */
6258 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6259 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006260
6261 // Write the buffer for ":wq" or when it was changed.
6262 // Trigger QuitPre and ExitPre.
6263 // Check if we can exit now, after autocommands have changed things.
6264 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6265 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006267 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 {
6269 not_exiting();
6270 }
6271 else
6272 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006273 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006275 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276# ifdef FEAT_GUI
6277 need_mouse_correct = TRUE;
6278# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006279 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006280 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 }
6282}
6283
6284/*
6285 * ":print", ":list", ":number".
6286 */
6287 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006288ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006290 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006291 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006292 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006294 for ( ;!got_int; ui_breakcheck())
6295 {
6296 print_line(eap->line1,
6297 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6298 || (eap->flags & EXFLAG_NR)),
6299 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6300 if (++eap->line1 > eap->line2)
6301 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006302 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006303 }
6304 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006305 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006306 curwin->w_cursor.lnum = eap->line2;
6307 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 }
6309
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311}
6312
6313#ifdef FEAT_BYTEOFF
6314 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006315ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316{
6317 goto_byte(eap->line2);
6318}
6319#endif
6320
6321/*
6322 * ":shell".
6323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006325ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
6327 do_shell(NULL, 0);
6328}
6329
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006330#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006332static int drop_busy = FALSE;
6333static int drop_filec;
6334static char_u **drop_filev = NULL;
6335static int drop_split;
6336static void (*drop_callback)(void *);
6337static void *drop_cookie;
6338
6339 static void
6340handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341{
6342 exarg_T ea;
6343 int save_msg_scroll = msg_scroll;
6344
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006345 // Setting the argument list may cause screen updates and being called
6346 // recursively. Avoid that by setting drop_busy.
6347 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006349 // Check whether the current buffer is changed. If so, we will need
6350 // to split the current window or data could be lost.
6351 // We don't need to check if the 'hidden' option is set, as in this
6352 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006353 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 {
6355 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006356 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 --emsg_off;
6358 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006359 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 if (win_split(0, 0) == FAIL)
6362 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006363 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006365 // When splitting the window, create a new alist. Otherwise the
6366 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 alist_unlink(curwin->w_alist);
6368 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 }
6370
6371 /*
6372 * Set up the new argument list.
6373 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006374 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375
6376 /*
6377 * Move to the first file.
6378 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006379 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006380 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 ea.cmd = (char_u *)"next";
6382 do_argfile(&ea, 0);
6383
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006384 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6385 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 need_start_insertmode = FALSE;
6387
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006388 // Restore msg_scroll, otherwise a following command may cause scrolling
6389 // unexpectedly. The screen will be redrawn by the caller, thus
6390 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006392
6393 if (drop_callback != NULL)
6394 drop_callback(drop_cookie);
6395
6396 drop_filev = NULL;
6397 drop_busy = FALSE;
6398}
6399
6400/*
6401 * Handle a file drop. The code is here because a drop is *nearly* like an
6402 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006403 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006404 * code is very similar to :args and hence needs access to a lot of the static
6405 * functions in this file.
6406 *
6407 * The "filev" list must have been allocated using alloc(), as should each item
6408 * in the list. This function takes over responsibility for freeing the "filev"
6409 * list.
6410 */
6411 void
6412handle_drop(
6413 int filec, // the number of files dropped
6414 char_u **filev, // the list of files dropped
6415 int split, // force splitting the window
6416 void (*callback)(void *), // to be called after setting the argument
6417 // list
6418 void *cookie) // argument for "callback" (allocated)
6419{
6420 // Cannot handle recursive drops, finish the pending one.
6421 if (drop_busy)
6422 {
6423 FreeWild(filec, filev);
6424 vim_free(cookie);
6425 return;
6426 }
6427
6428 // When calling handle_drop() more than once in a row we only use the last
6429 // one.
6430 if (drop_filev != NULL)
6431 {
6432 FreeWild(drop_filec, drop_filev);
6433 vim_free(drop_cookie);
6434 }
6435
6436 drop_filec = filec;
6437 drop_filev = filev;
6438 drop_split = split;
6439 drop_callback = callback;
6440 drop_cookie = cookie;
6441
6442 // Postpone this when:
6443 // - editing the command line
6444 // - not possible to change the current buffer
6445 // - updating the screen
6446 // As it may change buffers and window structures that are in use and cause
6447 // freed memory to be used.
6448 if (text_locked() || curbuf_locked() || updating_screen)
6449 return;
6450
6451 handle_drop_internal();
6452}
6453
6454/*
6455 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6456 * reset: Handle a postponed drop.
6457 */
6458 void
6459handle_any_postponed_drop(void)
6460{
6461 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006462 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006463 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464}
6465#endif
6466
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 * ":preserve".
6469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006471ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006473 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 ml_preserve(curbuf, TRUE);
6475}
6476
6477/*
6478 * ":recover".
6479 */
6480 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006481ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006483 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006485 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6486 | CCGD_MULTWIN
6487 | (eap->forceit ? CCGD_FORCEIT : 0)
6488 | CCGD_EXCMD)
6489
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 && (*eap->arg == NUL
6491 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006492 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 recoverymode = FALSE;
6494}
6495
6496/*
6497 * Command modifier used in a wrong way.
6498 */
6499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006500ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006502 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503}
6504
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505/*
6506 * :sview [+command] file split window with new file, read-only
6507 * :split [[+command] file] split window with current or new file
6508 * :vsplit [[+command] file] split window vertically with current or new file
6509 * :new [[+command] file] split window with no or new file
6510 * :vnew [[+command] file] split vertically window with no or new file
6511 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006512 *
6513 * :tabedit open new Tab page with empty window
6514 * :tabedit [+command] file open new Tab page and edit "file"
6515 * :tabnew [[+command] file] just like :tabedit
6516 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 */
6518 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006519ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006521 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006522#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006524#endif
6525#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006526 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006527 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006528#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006529 int use_tab = eap->cmdidx == CMD_tabedit
6530 || eap->cmdidx == CMD_tabfind
6531 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006533 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006534 return;
6535
Bram Moolenaar4033c552017-09-16 20:54:51 +02006536#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539
Bram Moolenaar4033c552017-09-16 20:54:51 +02006540#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006541 // A ":split" in the quickfix window works like ":new". Don't want two
6542 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006543 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 {
6545 if (eap->cmdidx == CMD_split)
6546 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 if (eap->cmdidx == CMD_vsplit)
6548 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551
Bram Moolenaar4033c552017-09-16 20:54:51 +02006552#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006553 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 {
6555 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6556 FNAME_MESS, TRUE, curbuf->b_ffname);
6557 if (fname == NULL)
6558 goto theend;
6559 eap->arg = fname;
6560 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006561# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006562 else
6563# endif
6564#endif
6565#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006566 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 && eap->cmdidx != CMD_new)
6569 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006570 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006571# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006572 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006573# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006574 au_has_group((char_u *)"FileExplorer"))
6575 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006576 // No browsing supported but we do have the file explorer:
6577 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006578 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006579 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006580 }
6581 else
6582 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006583 fname = do_browse(0, (char_u *)(use_tab
6584 ? _("Edit File in new tab page")
6585 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006587 if (fname == NULL)
6588 goto theend;
6589 eap->arg = fname;
6590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006592 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006595 /*
6596 * Either open new tab page or split the window.
6597 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006598 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006599 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006600 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006601 : eap->addr_count == 0 ? 0
6602 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006603 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006604 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006605
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006606 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006607 if (curwin != old_curwin
6608 && win_valid(old_curwin)
6609 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006610 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006611 old_curwin->w_alt_fnum = curbuf->b_fnum;
6612 }
6613 }
6614 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6616 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006617 // Reset 'scrollbind' when editing another file, but keep it when
6618 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006619 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006620 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 else
6622 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 do_exedit(eap, old_curwin);
6624 }
6625
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006626# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006627 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006628# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006630# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631theend:
6632 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006633# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006635
6636/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006637 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006638 */
6639 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006640tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006641{
6642 exarg_T ea;
6643
Bram Moolenaara80faa82020-04-12 19:37:17 +02006644 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006645 ea.cmdidx = CMD_tabnew;
6646 ea.cmd = (char_u *)"tabn";
6647 ea.arg = (char_u *)"";
6648 ex_splitview(&ea);
6649}
6650
6651/*
6652 * :tabnext command
6653 */
6654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006655ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006656{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006657 int tab_number;
6658
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006659 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006660 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006661 switch (eap->cmdidx)
6662 {
6663 case CMD_tabfirst:
6664 case CMD_tabrewind:
6665 goto_tabpage(1);
6666 break;
6667 case CMD_tablast:
6668 goto_tabpage(9999);
6669 break;
6670 case CMD_tabprevious:
6671 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006672 if (eap->arg && *eap->arg != NUL)
6673 {
6674 char_u *p = eap->arg;
6675 char_u *p_save = p;
6676
6677 tab_number = getdigits(&p);
6678 if (p == p_save || *p_save == '-' || *p != NUL
6679 || tab_number == 0)
6680 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006681 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006682 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006683 return;
6684 }
6685 }
6686 else
6687 {
6688 if (eap->addr_count == 0)
6689 tab_number = 1;
6690 else
6691 {
6692 tab_number = eap->line2;
6693 if (tab_number < 1)
6694 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006695 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006696 return;
6697 }
6698 }
6699 }
6700 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006701 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006702 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006703 tab_number = get_tabpage_arg(eap);
6704 if (eap->errmsg == NULL)
6705 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006706 break;
6707 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006708}
6709
6710/*
6711 * :tabmove command
6712 */
6713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006714ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006715{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006716 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006717
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006718 tab_number = get_tabpage_arg(eap);
6719 if (eap->errmsg == NULL)
6720 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006721}
6722
6723/*
6724 * :tabs command: List tabs and their contents.
6725 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006726 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006727ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006728{
6729 tabpage_T *tp;
6730 win_T *wp;
6731 int tabcount = 1;
6732
6733 msg_start();
6734 msg_scroll = TRUE;
6735 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6736 {
6737 msg_putchar('\n');
6738 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006739 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006740 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006741 ui_breakcheck();
6742
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006743 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006744 wp = firstwin;
6745 else
6746 wp = tp->tp_firstwin;
6747 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6748 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006749 msg_putchar('\n');
6750 msg_putchar(wp == curwin ? '>' : ' ');
6751 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006752 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6753 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006754 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006755 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006756 else
6757 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6758 IObuff, IOSIZE, TRUE);
6759 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006760 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006761 ui_breakcheck();
6762 }
6763 }
6764}
6765
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766/*
6767 * ":mode": Set screen mode.
6768 * If no argument given, just get the screen size and redraw.
6769 */
6770 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006771ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772{
6773 if (*eap->arg == NUL)
6774 shell_resized();
6775 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006776 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777}
6778
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779/*
6780 * ":resize".
6781 * set, increment or decrement current window height
6782 */
6783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006784ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785{
6786 int n;
6787 win_T *wp = curwin;
6788
6789 if (eap->addr_count > 0)
6790 {
6791 n = eap->line2;
6792 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6793 ;
6794 }
6795
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006796# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006798# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006800 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 {
6802 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006803 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006804 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 n = 9999;
6806 win_setwidth_win((int)n, wp);
6807 }
6808 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 {
6810 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006811 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006812 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 n = 9999;
6814 win_setheight_win((int)n, wp);
6815 }
6816}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817
6818/*
6819 * ":find [+command] <file>" command.
6820 */
6821 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006822ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823{
6824#ifdef FEAT_SEARCHPATH
6825 char_u *fname;
6826 int count;
6827
6828 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6829 TRUE, curbuf->b_ffname);
6830 if (eap->addr_count > 0)
6831 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006832 // Repeat finding the file "count" times. This matters when it
6833 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 count = eap->line2;
6835 while (fname != NULL && --count > 0)
6836 {
6837 vim_free(fname);
6838 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6839 FALSE, curbuf->b_ffname);
6840 }
6841 }
6842
6843 if (fname != NULL)
6844 {
6845 eap->arg = fname;
6846#endif
6847 do_exedit(eap, NULL);
6848#ifdef FEAT_SEARCHPATH
6849 vim_free(fname);
6850 }
6851#endif
6852}
6853
6854/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006855 * ":open" simulation: for now just work like ":visual".
6856 */
6857 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006858ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006859{
6860 regmatch_T regmatch;
6861 char_u *p;
6862
Bram Moolenaar65088802021-03-13 21:07:21 +01006863#ifdef FEAT_EVAL
6864 if (not_in_vim9(eap) == FAIL)
6865 return;
6866#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006867 curwin->w_cursor.lnum = eap->line2;
6868 beginline(BL_SOL | BL_FIX);
6869 if (*eap->arg == '/')
6870 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006871 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006872 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006873 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00006874 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006875 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006876 if (regmatch.regprog != NULL)
6877 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00006878 // make a copy of the line, when searching for a mark it might be
6879 // flushed
6880 char_u *line = vim_strsave(ml_get_curline());
6881
Bram Moolenaardf177f62005-02-22 08:39:57 +00006882 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00006883 if (vim_regexec(&regmatch, line, (colnr_T)0))
6884 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006885 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006886 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006887 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00006888 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006889 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006890 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006891 eap->arg += STRLEN(eap->arg);
6892 }
6893 check_cursor();
6894
6895 eap->cmdidx = CMD_visual;
6896 do_exedit(eap, NULL);
6897}
6898
6899/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006900 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 */
6902 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006903ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904{
6905 do_exedit(eap, NULL);
6906}
6907
6908/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006909 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006912do_exedit(
6913 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006914 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915{
6916 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006918 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006920 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6921 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006922 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 /*
6924 * ":vi" command ends Ex mode.
6925 */
6926 if (exmode_active && (eap->cmdidx == CMD_visual
6927 || eap->cmdidx == CMD_view))
6928 {
6929 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006930 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006932 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006933 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006934 if (global_busy)
6935 {
6936 int rd = RedrawingDisabled;
6937 int nwr = no_wait_return;
6938 int ms = msg_scroll;
6939#ifdef FEAT_GUI
6940 int he = hold_gui_events;
6941#endif
6942
6943 if (eap->nextcmd != NULL)
6944 {
6945 stuffReadbuff(eap->nextcmd);
6946 eap->nextcmd = NULL;
6947 }
6948
6949 if (exmode_was != EXMODE_VIM)
6950 settmode(TMODE_RAW);
6951 RedrawingDisabled = 0;
6952 no_wait_return = 0;
6953 need_wait_return = FALSE;
6954 msg_scroll = 0;
6955#ifdef FEAT_GUI
6956 hold_gui_events = 0;
6957#endif
6958 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006959 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006960
6961 main_loop(FALSE, TRUE);
6962
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006963 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006964 RedrawingDisabled = rd;
6965 no_wait_return = nwr;
6966 msg_scroll = ms;
6967#ifdef FEAT_GUI
6968 hold_gui_events = he;
6969#endif
6970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 }
6974
6975 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006976 || eap->cmdidx == CMD_tabnew
6977 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006978 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00006980 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 setpcmark();
6982 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006983 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6984 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006986 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 || *eap->arg != NUL
6988#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006989 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990#endif
6991 )
6992 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006993 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6994 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006995 if (*eap->arg != NUL && curbuf_locked())
6996 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006997
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 n = readonlymode;
6999 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7000 readonlymode = TRUE;
7001 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007002 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7003 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007004 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7005 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7007 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007008 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7010 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7011 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007012 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007014 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007015 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007016 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7017 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007018 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007020 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 if (old_curwin != NULL)
7022 {
7023 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007024 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007026#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007027 cleanup_T cs;
7028
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007029 // Reset the error/interrupt/exception state here so that
7030 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007031 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007032#endif
7033#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007035#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007036 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007037
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007038#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007039 // Restore the error/interrupt/exception state if not
7040 // discarded by a new aborting error, interrupt, or
7041 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007042 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 }
7045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 }
7047 else if (readonlymode && curbuf->b_nwindows == 1)
7048 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007049 // When editing an already visited buffer, 'readonly' won't be set
7050 // but the previous value is kept. With ":view" and ":sview" we
7051 // want the file to be readonly, except when another window is
7052 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 curbuf->b_p_ro = TRUE;
7054 }
7055 readonlymode = n;
7056 }
7057 else
7058 {
7059 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007060 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 if (n != curwin->w_arg_idx_invalid)
7064 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 }
7066
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 /*
7068 * if ":split file" worked, set alternate file name in old window to new
7069 * file
7070 */
7071 if (old_curwin != NULL
7072 && *eap->arg != NUL
7073 && curwin != old_curwin
7074 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007075 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007076 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078
7079 ex_no_reprint = TRUE;
7080}
7081
7082#ifndef FEAT_GUI
7083/*
7084 * ":gui" and ":gvim" when there is no GUI.
7085 */
7086 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007087ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007089 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090}
7091#endif
7092
Bram Moolenaar4f974752019-02-17 17:44:42 +01007093#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007095ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096{
7097 gui_make_tearoff(eap->arg);
7098}
7099#endif
7100
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007101#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7102 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007104ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007106# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7107 if (gui.in_use)
7108 gui_make_popup(eap->arg, eap->forceit);
7109# ifdef FEAT_TERM_POPUP_MENU
7110 else
7111# endif
7112# endif
7113# ifdef FEAT_TERM_POPUP_MENU
7114 pum_make_popup(eap->arg, eap->forceit);
7115# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116}
7117#endif
7118
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007120ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121{
7122 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007123 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007125 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126}
7127
7128/*
7129 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7130 * offset.
7131 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7132 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007134ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007137 win_T *save_curwin = curwin;
7138 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 long topline;
7140 long y;
7141 linenr_T old_linenr = curwin->w_cursor.lnum;
7142
7143 setpcmark();
7144
7145 /*
7146 * determine max topline
7147 */
7148 if (curwin->w_p_scb)
7149 {
7150 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007151 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 {
7153 if (wp->w_p_scb && wp->w_buffer)
7154 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007155 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 if (topline > y)
7157 topline = y;
7158 }
7159 }
7160 if (topline < 1)
7161 topline = 1;
7162 }
7163 else
7164 {
7165 topline = 1;
7166 }
7167
7168
7169 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007170 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007172 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 {
7174 if (curwin->w_p_scb)
7175 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007176 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 y = topline - curwin->w_topline;
7178 if (y > 0)
7179 scrollup(y, TRUE);
7180 else
7181 scrolldown(-y, TRUE);
7182 curwin->w_scbind_pos = topline;
7183 redraw_later(VALID);
7184 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 }
7187 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007188 curwin = save_curwin;
7189 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 if (curwin->w_p_scb)
7191 {
7192 did_syncbind = TRUE;
7193 checkpcmark();
7194 if (old_linenr != curwin->w_cursor.lnum)
7195 {
7196 char_u ctrl_o[2];
7197
7198 ctrl_o[0] = Ctrl_O;
7199 ctrl_o[1] = 0;
7200 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7201 }
7202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203}
7204
7205
7206 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007207ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007209 int i;
7210 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7211 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007213 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 do_bang(1, eap, FALSE, FALSE, TRUE);
7215 else
7216 {
7217 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7218 return;
7219
7220#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007221 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 {
7223 char_u *browseFile;
7224
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007225 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 NULL, NULL, NULL, curbuf);
7227 if (browseFile != NULL)
7228 {
7229 i = readfile(browseFile, NULL,
7230 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7231 vim_free(browseFile);
7232 }
7233 else
7234 i = OK;
7235 }
7236 else
7237#endif
7238 if (*eap->arg == NUL)
7239 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007240 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 return;
7242 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7243 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7244 }
7245 else
7246 {
7247 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7248 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7249 i = readfile(eap->arg, NULL,
7250 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7251
7252 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007253 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007255#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 if (!aborting())
7257#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007258 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 }
7260 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007261 {
7262 if (empty && exmode_active)
7263 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007264 // Delete the empty line that remains. Historically ex does
7265 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007266 if (eap->line2 == 0)
7267 lnum = curbuf->b_ml.ml_line_count;
7268 else
7269 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007270 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007271 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007272 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007273 if (curwin->w_cursor.lnum > 1
7274 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007275 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007276 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007277 }
7278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 }
7282}
7283
Bram Moolenaarea408852005-06-25 22:49:46 +00007284static char_u *prev_dir = NULL;
7285
7286#if defined(EXITFREE) || defined(PROTO)
7287 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007288free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007289{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007290 VIM_CLEAR(prev_dir);
7291 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007292}
7293#endif
7294
Bram Moolenaarf4258302013-06-02 18:20:17 +02007295/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007296 * Get the previous directory for the given chdir scope.
7297 */
7298 static char_u *
7299get_prevdir(cdscope_T scope)
7300{
7301 if (scope == CDSCOPE_WINDOW)
7302 return curwin->w_prevdir;
7303 else if (scope == CDSCOPE_TABPAGE)
7304 return curtab->tp_prevdir;
7305 return prev_dir;
7306}
7307
7308/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007309 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007310 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7311 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007312 */
7313 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007314post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007315{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007316 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007317 // Clear tab local directory for both :cd and :tcd
7318 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007319 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007320 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007321 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007322 char_u *pdir = get_prevdir(scope);
7323
7324 // If still in the global directory, need to remember current
7325 // directory as the global directory.
7326 if (globaldir == NULL && pdir != NULL)
7327 globaldir = vim_strsave(pdir);
7328
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007329 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007330 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007331 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007332 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007333 curtab->tp_localdir = vim_strsave(NameBuff);
7334 else
7335 curwin->w_localdir = vim_strsave(NameBuff);
7336 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007337 }
7338 else
7339 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007340 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007341 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007342 }
7343
zeertzjq64be6aa2021-11-19 11:59:08 +00007344 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007345 shorten_fnames(TRUE);
7346}
7347
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007348/*
7349 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007350 * chdir() function.
7351 * scope == CDSCOPE_WINDOW: changes the window-local directory
7352 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7353 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007354 * Returns TRUE if the directory is successfully changed.
7355 */
7356 int
7357changedir_func(
7358 char_u *new_dir,
7359 int forceit,
7360 cdscope_T scope)
7361{
7362 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02007363 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007364 int dir_differs;
7365 int retval = FALSE;
7366
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007367 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007368 return FALSE;
7369
7370 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7371 {
7372 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7373 return FALSE;
7374 }
7375
7376 // ":cd -": Change to previous directory
7377 if (STRCMP(new_dir, "-") == 0)
7378 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007379 pdir = get_prevdir(scope);
7380 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007381 {
7382 emsg(_("E186: No previous directory"));
7383 return FALSE;
7384 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007385 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007386 }
7387
Bram Moolenaar002bc792020-06-05 22:33:42 +02007388 // Free the previous directory
7389 tofree = get_prevdir(scope);
7390
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007391 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007392 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007393 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007394 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007395 pdir = NULL;
7396 if (scope == CDSCOPE_WINDOW)
7397 curwin->w_prevdir = pdir;
7398 else if (scope == CDSCOPE_TABPAGE)
7399 curtab->tp_prevdir = pdir;
7400 else
7401 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007402
Bakudankun29f3a452021-12-11 12:28:08 +00007403 // For UNIX ":cd" means: go to home directory.
7404 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007405#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007406 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007407#else
7408 if (*new_dir == NUL && p_cdh)
7409#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007410 {
7411 // use NameBuff for home directory name
7412# ifdef VMS
7413 char_u *p;
7414
7415 p = mch_getenv((char_u *)"SYS$LOGIN");
7416 if (p == NULL || *p == NUL) // empty is the same as not set
7417 NameBuff[0] = NUL;
7418 else
7419 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7420# else
7421 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7422# endif
7423 new_dir = NameBuff;
7424 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007425 dir_differs = new_dir == NULL || pdir == NULL
7426 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007427 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7428 emsg(_(e_failed));
7429 else
7430 {
7431 char_u *acmd_fname;
7432
7433 post_chdir(scope);
7434
7435 if (dir_differs)
7436 {
7437 if (scope == CDSCOPE_WINDOW)
7438 acmd_fname = (char_u *)"window";
7439 else if (scope == CDSCOPE_TABPAGE)
7440 acmd_fname = (char_u *)"tabpage";
7441 else
7442 acmd_fname = (char_u *)"global";
7443 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7444 curbuf);
7445 }
7446 retval = TRUE;
7447 }
7448 vim_free(tofree);
7449
7450 return retval;
7451}
Bram Moolenaarea408852005-06-25 22:49:46 +00007452
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007454 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007456 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007457ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007459 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460
7461 new_dir = eap->arg;
7462#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007463 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7464 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 ex_pwd(NULL);
7466 else
7467#endif
7468 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007469 cdscope_T scope = CDSCOPE_GLOBAL;
7470
7471 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7472 scope = CDSCOPE_WINDOW;
7473 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7474 scope = CDSCOPE_TABPAGE;
7475
7476 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007477 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007478 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007479 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 ex_pwd(eap);
7481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 }
7483}
7484
7485/*
7486 * ":pwd".
7487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007489ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490{
7491 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7492 {
7493#ifdef BACKSLASH_IN_FILENAME
7494 slash_adjust(NameBuff);
7495#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007496 if (p_verbose > 0)
7497 {
7498 char *context = "global";
7499
Bram Moolenaar05268152021-11-18 18:53:45 +00007500 if (last_chdir_reason != NULL)
7501 context = last_chdir_reason;
7502 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007503 context = "window";
7504 else if (curtab->tp_localdir != NULL)
7505 context = "tabpage";
7506 smsg("[%s] %s", context, (char *)NameBuff);
7507 }
7508 else
7509 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 }
7511 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007512 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513}
7514
7515/*
7516 * ":=".
7517 */
7518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007519ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007521 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007522 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523}
7524
7525 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007526ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007528 int n;
7529 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530
7531 if (cursor_valid())
7532 {
7533 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7534 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007535 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007537
7538 len = eap->line2;
7539 switch (*eap->arg)
7540 {
7541 case 'm': break;
7542 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007543 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007544 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007545
7546 // Hide the cursor if invoked with !
7547 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548}
7549
7550/*
7551 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007552 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 */
7554 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007555do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556{
Bram Moolenaar52953192019-08-16 10:27:13 +02007557 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007558 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007559# ifdef ELAPSED_FUNC
7560 elapsed_T start_tv;
7561
7562 // Remember at what time we started, so that we know how much longer we
7563 // should wait after waiting for a bit.
7564 ELAPSED_INIT(start_tv);
7565# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007567 if (hide_cursor)
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007568 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007569 else
7570 cursor_on();
7571
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007572 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007573 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007575 wait_now = msec - done > 1000L ? 1000L : msec - done;
7576#ifdef FEAT_TIMERS
7577 {
7578 long due_time = check_due_timer();
7579
7580 if (due_time > 0 && due_time < wait_now)
7581 wait_now = due_time;
7582 }
7583#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007584#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007585 if (has_any_channel() && wait_now > 20L)
7586 wait_now = 20L;
7587#endif
7588#ifdef FEAT_SOUND
7589 if (has_any_sound_callback() && wait_now > 20L)
7590 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007591#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007592 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007593
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007594#ifdef FEAT_JOB_CHANNEL
7595 if (has_any_channel())
7596 ui_breakcheck_force(TRUE);
7597 else
7598#endif
7599 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007600#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007601 // Process the netbeans and clientserver messages that may have been
7602 // received in the call to ui_breakcheck() when the GUI is in use. This
7603 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007604 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007605#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007606
7607# ifdef ELAPSED_FUNC
7608 // actual time passed
7609 done = ELAPSED_FUNC(start_tv);
7610# else
7611 // guestimate time passed (will actually be more)
7612 done += wait_now;
7613# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007615
7616 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7617 // input buffer, otherwise a following call to input() fails.
7618 if (got_int)
7619 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007620
7621 if (hide_cursor)
7622 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623}
7624
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625/*
7626 * ":winsize" command (obsolete).
7627 */
7628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007629ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630{
7631 int w, h;
7632 char_u *arg = eap->arg;
7633 char_u *p;
7634
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007635 if (!isdigit(*arg))
7636 {
7637 semsg(_(e_invarg2), arg);
7638 return;
7639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 w = getdigits(&arg);
7641 arg = skipwhite(arg);
7642 p = arg;
7643 h = getdigits(&arg);
7644 if (*p != NUL && *arg == NUL)
7645 set_shellsize(w, h, TRUE);
7646 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007647 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648}
7649
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007651ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652{
7653 int xchar = NUL;
7654 char_u *p;
7655
7656 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7657 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007658 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 if (eap->arg[1] == NUL)
7660 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007661 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 return;
7663 }
7664 xchar = eap->arg[1];
7665 p = eap->arg + 2;
7666 }
7667 else
7668 p = eap->arg + 1;
7669
Bram Moolenaar63b91732021-08-05 20:40:03 +02007670 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007672 if (*p != NUL && *p != (
7673#ifdef FEAT_EVAL
7674 in_vim9script() ? '#' :
7675#endif
7676 '"')
7677 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007678 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007679 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007681 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007682 postponed_split_flags = cmdmod.cmod_split;
7683 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7685 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007686 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 }
7688}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689
Bram Moolenaar843ee412004-06-30 16:16:41 +00007690#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691/*
7692 * ":winpos".
7693 */
7694 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007695ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696{
7697 int x, y;
7698 char_u *arg = eap->arg;
7699 char_u *p;
7700
7701 if (*arg == NUL)
7702 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007703# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007704# ifdef VIMDLL
7705 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7706 mch_get_winpos(&x, &y) != FAIL)
7707# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007709# else
7710 if (mch_get_winpos(&x, &y) != FAIL)
7711# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 {
7713 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007714 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 }
7716 else
7717# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007718 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 }
7720 else
7721 {
7722 x = getdigits(&arg);
7723 arg = skipwhite(arg);
7724 p = arg;
7725 y = getdigits(&arg);
7726 if (*p == NUL || *arg != NUL)
7727 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007728 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 return;
7730 }
7731# ifdef FEAT_GUI
7732 if (gui.in_use)
7733 gui_mch_set_winpos(x, y);
7734 else if (gui.starting)
7735 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007736 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 gui_win_x = x;
7738 gui_win_y = y;
7739 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007740# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 else
7742# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007743# endif
7744# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007745 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746# endif
7747# ifdef HAVE_TGETENT
7748 if (*T_CWP)
7749 term_set_winpos(x, y);
7750# endif
7751 }
7752}
7753#endif
7754
7755/*
7756 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7757 */
7758 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007759ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760{
7761 oparg_T oa;
7762
7763 clear_oparg(&oa);
7764 oa.regname = eap->regname;
7765 oa.start.lnum = eap->line1;
7766 oa.end.lnum = eap->line2;
7767 oa.line_count = eap->line2 - eap->line1 + 1;
7768 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007770 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 {
7772 setpcmark();
7773 curwin->w_cursor.lnum = eap->line1;
7774 beginline(BL_SOL | BL_FIX);
7775 }
7776
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007777 if (VIsual_active)
7778 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007779
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 switch (eap->cmdidx)
7781 {
7782 case CMD_delete:
7783 oa.op_type = OP_DELETE;
7784 op_delete(&oa);
7785 break;
7786
7787 case CMD_yank:
7788 oa.op_type = OP_YANK;
7789 (void)op_yank(&oa, FALSE, TRUE);
7790 break;
7791
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007792 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007793 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007795 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7796#else
7797 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007799 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 oa.op_type = OP_RSHIFT;
7801 else
7802 oa.op_type = OP_LSHIFT;
7803 op_shift(&oa, FALSE, eap->amount);
7804 break;
7805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007807 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808}
7809
7810/*
7811 * ":put".
7812 */
7813 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007814ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007816 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 if (eap->line2 == 0)
7818 {
7819 eap->line2 = 1;
7820 eap->forceit = TRUE;
7821 }
7822 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00007823 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007824 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007825 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826}
7827
7828/*
7829 * Handle ":copy" and ":move".
7830 */
7831 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007832ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833{
7834 long n;
7835
Bram Moolenaar491799b2020-08-01 19:23:43 +02007836#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007837 if (not_in_vim9(eap) == FAIL)
7838 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007839#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007840 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007841 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {
7843 eap->nextcmd = NULL;
7844 return;
7845 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007846 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847
7848 /*
7849 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7850 */
7851 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7852 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007853 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 return;
7855 }
7856
7857 if (eap->cmdidx == CMD_move)
7858 {
7859 if (do_move(eap->line1, eap->line2, n) == FAIL)
7860 return;
7861 }
7862 else
7863 ex_copy(eap->line1, eap->line2, n);
7864 u_clearline();
7865 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007866 ex_may_print(eap);
7867}
7868
7869/*
7870 * Print the current line if flags were given to the Ex command.
7871 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007872 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007873ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007874{
7875 if (eap->flags != 0)
7876 {
7877 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7878 (eap->flags & EXFLAG_LIST));
7879 ex_no_reprint = TRUE;
7880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881}
7882
7883/*
7884 * ":smagic" and ":snomagic".
7885 */
7886 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007887ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007889 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007891 magic_overruled = eap->cmdidx == CMD_smagic
7892 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02007893 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007894 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895}
7896
7897/*
7898 * ":join".
7899 */
7900 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007901ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902{
7903 curwin->w_cursor.lnum = eap->line1;
7904 if (eap->line1 == eap->line2)
7905 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007906 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 return;
7908 if (eap->line2 == curbuf->b_ml.ml_line_count)
7909 {
7910 beep_flush();
7911 return;
7912 }
7913 ++eap->line2;
7914 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007915 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007917 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918}
7919
7920/*
7921 * ":[addr]@r" or ":[addr]*r": execute register
7922 */
7923 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007924ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925{
7926 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007927 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928
7929 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007930 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931
7932#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007933 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934#endif
7935
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007936 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 c = *eap->arg;
7938 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7939 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007940 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007941 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7942 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007943 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 else
7947 {
7948 int save_efr = exec_from_reg;
7949
7950 exec_from_reg = TRUE;
7951
7952 /*
7953 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007954 * Continue until the stuff buffer is empty and all added characters
7955 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007957 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7959
7960 exec_from_reg = save_efr;
7961 }
7962}
7963
7964/*
7965 * ":!".
7966 */
7967 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007968ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969{
7970 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7971}
7972
7973/*
7974 * ":undo".
7975 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007977ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007979 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007980 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007981 else
7982 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983}
7984
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007985#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007986 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007987ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007988{
7989 char_u hash[UNDO_HASH_SIZE];
7990
7991 u_compute_hash(hash);
7992 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7993}
7994
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007995 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007996ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007997{
7998 char_u hash[UNDO_HASH_SIZE];
7999
8000 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008001 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008002}
8003#endif
8004
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005/*
8006 * ":redo".
8007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008009ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010{
8011 u_redo(1);
8012}
8013
8014/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008015 * ":earlier" and ":later".
8016 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008017 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008018ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008019{
8020 long count = 0;
8021 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008022 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008023 char_u *p = eap->arg;
8024
8025 if (*p == NUL)
8026 count = 1;
8027 else if (isdigit(*p))
8028 {
8029 count = getdigits(&p);
8030 switch (*p)
8031 {
8032 case 's': ++p; sec = TRUE; break;
8033 case 'm': ++p; sec = TRUE; count *= 60; break;
8034 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008035 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8036 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008037 }
8038 }
8039
8040 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008041 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008042 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008043 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8044 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008045}
8046
8047/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 * ":redir": start/stop redirection.
8049 */
8050 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008051ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052{
8053 char *mode;
8054 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008055 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056
Bram Moolenaarba768492016-07-08 15:32:54 +02008057#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008058 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008059 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008060 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008061 return;
8062 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008063#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008064
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 if (STRICMP(eap->arg, "END") == 0)
8066 close_redir();
8067 else
8068 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008069 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008071 ++arg;
8072 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008074 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 mode = "a";
8076 }
8077 else
8078 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008079 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080
8081 close_redir();
8082
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008083 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008084 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 if (fname == NULL)
8086 return;
8087#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008088 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 {
8090 char_u *browseFile;
8091
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008092 browseFile = do_browse(BROWSE_SAVE,
8093 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008094 fname, NULL, NULL,
8095 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008097 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 vim_free(fname);
8099 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008100 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 }
8102#endif
8103
8104 redir_fd = open_exfile(fname, eap->forceit, mode);
8105 vim_free(fname);
8106 }
8107#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008108 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008110 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008112 ++arg;
8113 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008115 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008116 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008118 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008120 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008121 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008122 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008123 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008125 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008126 if (*arg == '>')
8127 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008128 // Make register empty when not using @A-@Z and the
8129 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00008130 if (*arg == NUL && !isupper(redir_reg))
8131 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008133 }
8134 if (*arg != NUL)
8135 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008136 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008137 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008138 }
8139 }
8140 else if (*arg == '=' && arg[1] == '>')
8141 {
8142 int append;
8143
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008144 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008145 close_redir();
8146 arg += 2;
8147
8148 if (*arg == '>')
8149 {
8150 ++arg;
8151 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 }
8153 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008154 append = FALSE;
8155
8156 if (var_redir_start(skipwhite(arg), append) == OK)
8157 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 }
8159#endif
8160
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008161 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008164 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008166
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008167 // Make sure redirection is not off. Can happen for cmdline completion
8168 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008169 if (redir_fd != NULL
8170#ifdef FEAT_EVAL
8171 || redir_reg || redir_vname
8172#endif
8173 )
8174 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175}
8176
8177/*
8178 * ":redraw": force redraw
8179 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008180 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008181ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182{
8183 int r = RedrawingDisabled;
8184 int p = p_lz;
8185
8186 RedrawingDisabled = 0;
8187 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008188 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008190 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 if (need_maketitle)
8192 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008193#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8194# ifdef VIMDLL
8195 if (!gui.in_use)
8196# endif
8197 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 RedrawingDisabled = r;
8200 p_lz = p;
8201
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008202 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 msg_didout = FALSE;
8204 msg_col = 0;
8205
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008206 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008207 need_wait_return = FALSE;
8208
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 out_flush();
8210}
8211
8212/*
8213 * ":redrawstatus": force redraw of status line(s)
8214 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008216ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 int r = RedrawingDisabled;
8219 int p = p_lz;
8220
8221 RedrawingDisabled = 0;
8222 p_lz = FALSE;
8223 if (eap->forceit)
8224 status_redraw_all();
8225 else
8226 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008227 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 RedrawingDisabled = r;
8229 p_lz = p;
8230 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231}
8232
Bram Moolenaare12bab32019-01-08 22:02:56 +01008233/*
8234 * ":redrawtabline": force redraw of the tabline
8235 */
8236 static void
8237ex_redrawtabline(exarg_T *eap UNUSED)
8238{
8239 int r = RedrawingDisabled;
8240 int p = p_lz;
8241
8242 RedrawingDisabled = 0;
8243 p_lz = FALSE;
8244
8245 draw_tabline();
8246
8247 RedrawingDisabled = r;
8248 p_lz = p;
8249 out_flush();
8250}
8251
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008253close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254{
8255 if (redir_fd != NULL)
8256 {
8257 fclose(redir_fd);
8258 redir_fd = NULL;
8259 }
8260#ifdef FEAT_EVAL
8261 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008262 if (redir_vname)
8263 {
8264 var_redir_stop();
8265 redir_vname = 0;
8266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267#endif
8268}
8269
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008270#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008271 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008272vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008273{
8274 if (vim_mkdir(name, prot) != 0)
8275 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008276 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008277 return FAIL;
8278 }
8279 return OK;
8280}
8281#endif
8282
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283/*
8284 * Open a file for writing for an Ex command, with some checks.
8285 * Return file descriptor, or NULL on failure.
8286 */
8287 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008288open_exfile(
8289 char_u *fname,
8290 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008291 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292{
8293 FILE *fd;
8294
8295#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008296 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 if (mch_isdir(fname))
8298 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008299 semsg(_(e_src_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 return NULL;
8301 }
8302#endif
8303 if (!forceit && *mode != 'a' && vim_fexists(fname))
8304 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008305 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 return NULL;
8307 }
8308
8309 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008310 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311
8312 return fd;
8313}
8314
8315/*
8316 * ":mark" and ":k".
8317 */
8318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008319ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320{
8321 pos_T pos;
8322
Bram Moolenaar10b94212021-02-19 21:42:57 +01008323#ifdef FEAT_EVAL
8324 if (not_in_vim9(eap) == FAIL)
8325 return;
8326#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008327 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008328 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008329 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02008330 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 else
8332 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008333 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 curwin->w_cursor.lnum = eap->line2;
8335 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008336 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008337 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008338 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 }
8340}
8341
8342/*
8343 * Update w_topline, w_leftcol and the cursor position.
8344 */
8345 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008346update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008348 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 update_topline();
8350 if (!curwin->w_p_wrap)
8351 validate_cursor();
8352 update_curswant();
8353}
8354
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008356 * Save the current State and go to Normal mode.
8357 * Return TRUE if the typeahead could be saved.
8358 */
8359 int
8360save_current_state(save_state_T *sst)
8361{
8362 sst->save_msg_scroll = msg_scroll;
8363 sst->save_restart_edit = restart_edit;
8364 sst->save_msg_didout = msg_didout;
8365 sst->save_State = State;
8366 sst->save_insertmode = p_im;
8367 sst->save_finish_op = finish_op;
8368 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008369 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008370
Bram Moolenaar4324d872020-12-01 20:12:24 +01008371 msg_scroll = FALSE; // no msg scrolling in Normal mode
8372 restart_edit = 0; // don't go to Insert mode
8373 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008374
Bram Moolenaara452b802020-12-01 21:47:59 +01008375 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008376 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008377
8378 /*
8379 * Save the current typeahead. This is required to allow using ":normal"
8380 * from an event handler and makes sure we don't hang when the argument
8381 * ends with half a command.
8382 */
8383 save_typeahead(&sst->tabuf);
8384 return sst->tabuf.typebuf_valid;
8385}
8386
8387 void
8388restore_current_state(save_state_T *sst)
8389{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008390 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008391 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008392
8393 msg_scroll = sst->save_msg_scroll;
8394 restart_edit = sst->save_restart_edit;
8395 p_im = sst->save_insertmode;
8396 finish_op = sst->save_finish_op;
8397 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008398 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008399 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008400 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008401
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008402 // Restore the state (needed when called from a function executed for
8403 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008404 State = sst->save_State;
8405#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008406 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008407#endif
8408}
8409
8410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 * ":normal[!] {commands}": Execute normal mode commands.
8412 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008413 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008414ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008416 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 char_u *arg = NULL;
8418 int l;
8419 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008421 if (ex_normal_lock > 0)
8422 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008423 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008424 return;
8425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 if (ex_normal_busy >= p_mmd)
8427 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008428 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 return;
8430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 /*
8433 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8434 * this for the K_SPECIAL leading byte, otherwise special keys will not
8435 * work.
8436 */
8437 if (has_mbyte)
8438 {
8439 int len = 0;
8440
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008441 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 for (p = eap->arg; *p != NUL; ++p)
8443 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008444#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008445 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008447#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008448 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008449 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008450#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 )
8454 len += 2;
8455 }
8456 if (len > 0)
8457 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008458 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 if (arg != NULL)
8460 {
8461 len = 0;
8462 for (p = eap->arg; *p != NUL; ++p)
8463 {
8464 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008465#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 if (*p == CSI)
8467 {
8468 arg[len++] = KS_EXTRA;
8469 arg[len++] = (int)KE_CSI;
8470 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008471#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008472 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 {
8474 arg[len++] = *++p;
8475 if (*p == K_SPECIAL)
8476 {
8477 arg[len++] = KS_SPECIAL;
8478 arg[len++] = KE_FILLER;
8479 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008480#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 else if (*p == CSI)
8482 {
8483 arg[len++] = KS_EXTRA;
8484 arg[len++] = (int)KE_CSI;
8485 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 }
8488 arg[len] = NUL;
8489 }
8490 }
8491 }
8492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008494 ++ex_normal_busy;
8495 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 {
8497 /*
8498 * Repeat the :normal command for each line in the range. When no
8499 * range given, execute it just once, without positioning the cursor
8500 * first.
8501 */
8502 do
8503 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 if (eap->addr_count != 0)
8505 {
8506 curwin->w_cursor.lnum = eap->line1++;
8507 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008508 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 }
8510
Bram Moolenaar13505972019-01-24 15:04:48 +01008511 exec_normal_cmd(arg != NULL
8512 ? arg
8513 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 }
8515 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8516 }
8517
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008518 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 update_topline_cursor();
8520
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008521 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008523 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008524#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008525 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008526#endif
8527
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529}
8530
8531/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008532 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 */
8534 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008535ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008537 if (eap->forceit)
8538 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008539 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008540 if (!curwin->w_cursor.lnum)
8541 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008542 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008543 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008544#ifdef FEAT_TERMINAL
8545 // Ignore this when running in an active terminal.
8546 if (term_job_running(curbuf->b_term))
8547 return;
8548#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008549
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008550 // Ignore the command when already in Insert mode. Inserting an
8551 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008552 if (State & INSERT)
8553 return;
8554
Bram Moolenaar64969662005-12-14 21:59:55 +00008555 if (eap->cmdidx == CMD_startinsert)
8556 restart_edit = 'a';
8557 else if (eap->cmdidx == CMD_startreplace)
8558 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008560 restart_edit = 'V';
8561
8562 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008564 if (eap->cmdidx == CMD_startinsert)
8565 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008566 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008568
8569 if (VIsual_active)
8570 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571}
8572
8573/*
8574 * ":stopinsert"
8575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008577ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
8579 restart_edit = 0;
8580 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008581 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008584/*
8585 * Execute normal mode command "cmd".
8586 * "remap" can be REMAP_NONE or REMAP_YES.
8587 */
8588 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008589exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008590{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008591 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008592 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008593 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008594}
Bram Moolenaar25281632016-01-21 23:32:32 +01008595
Bram Moolenaar25281632016-01-21 23:32:32 +01008596/*
8597 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008598 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008599 */
8600 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008601exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008602{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008603 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008604 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008605
Bram Moolenaar905dd902019-04-07 14:21:47 +02008606 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8607 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008608 clear_oparg(&oa);
8609 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008610 while ((!stuff_empty()
8611 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008612 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008613 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008614 {
8615 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008616#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008617 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008618 && oa.op_type == OP_NOP && oa.regname == NUL
8619 && !VIsual_active)
8620 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008621 // If terminal_loop() returns OK we got a key that is handled
8622 // in Normal model. With FAIL we first need to position the
8623 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008624 if (terminal_loop(TRUE) == OK)
8625 normal_cmd(&oa, TRUE);
8626 }
8627 else
8628#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008629 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008630 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008631 }
8632}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008633
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634#ifdef FEAT_FIND_ID
8635 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008636ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637{
8638 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8639 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8640 (linenr_T)1, (linenr_T)MAXLNUM);
8641}
8642
Bram Moolenaar4033c552017-09-16 20:54:51 +02008643#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644/*
8645 * ":psearch"
8646 */
8647 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008648ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649{
8650 g_do_tagpreview = p_pvh;
8651 ex_findpat(eap);
8652 g_do_tagpreview = 0;
8653}
8654#endif
8655
8656 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008657ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658{
8659 int whole = TRUE;
8660 long n;
8661 char_u *p;
8662 int action;
8663
8664 switch (cmdnames[eap->cmdidx].cmd_name[2])
8665 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008666 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8668 action = ACTION_GOTO;
8669 else
8670 action = ACTION_SHOW;
8671 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008672 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 action = ACTION_SHOW_ALL;
8674 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008675 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 action = ACTION_GOTO;
8677 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008678 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 action = ACTION_SPLIT;
8680 break;
8681 }
8682
8683 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008684 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 {
8686 n = getdigits(&eap->arg);
8687 eap->arg = skipwhite(eap->arg);
8688 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008689 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 {
8691 whole = FALSE;
8692 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008693 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 if (*p)
8695 {
8696 *p++ = NUL;
8697 p = skipwhite(p);
8698
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008699 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008700 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008701 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02008703 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 }
8705 }
8706 if (!eap->skip)
8707 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8708 whole, !eap->forceit,
8709 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8710 n, action, eap->line1, eap->line2);
8711}
8712#endif
8713
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714
Bram Moolenaar4033c552017-09-16 20:54:51 +02008715#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716/*
8717 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8718 */
8719 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008720ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008722 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8724}
8725
8726/*
8727 * ":pedit"
8728 */
8729 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008730ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731{
8732 win_T *curwin_save = curwin;
8733
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008734 if (ERROR_IF_ANY_POPUP_WINDOW)
8735 return;
8736
Bram Moolenaar026587b2019-08-17 15:08:00 +02008737 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008739 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008740
Bram Moolenaar026587b2019-08-17 15:08:00 +02008741 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008743
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 if (curwin != curwin_save && win_valid(curwin_save))
8745 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008746 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 validate_cursor();
8748 redraw_later(VALID);
8749 win_enter(curwin_save, TRUE);
8750 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008751# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008752 else if (WIN_IS_POPUP(curwin))
8753 {
8754 // can't keep focus in popup window
8755 win_enter(firstwin, TRUE);
8756 }
8757# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 g_do_tagpreview = 0;
8759}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761
8762/*
8763 * ":stag", ":stselect" and ":stjump".
8764 */
8765 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008766ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767{
8768 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008769 postponed_split_flags = cmdmod.cmod_split;
8770 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8772 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008773 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775
8776/*
8777 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8778 */
8779 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008780ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781{
8782 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8783}
8784
8785 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008786ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787{
8788 int cmd;
8789
8790 switch (name[1])
8791 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008792 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008794 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008796 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008798 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008800 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008802 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008804 case 'f': // ":tfirst"
8805 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008807 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008809 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008811 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008813 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 return;
8815 }
8816#endif
8817 cmd = DT_TAG;
8818 break;
8819 }
8820
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008821 if (name[0] == 'l')
8822 {
8823#ifndef FEAT_QUICKFIX
8824 ex_ni(eap);
8825 return;
8826#else
8827 cmd = DT_LTAG;
8828#endif
8829 }
8830
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8832 eap->forceit, TRUE);
8833}
8834
8835/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008836 * Check "str" for starting with a special cmdline variable.
8837 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8838 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8839 */
8840 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008841find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008842{
8843 int len;
8844 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008845 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008846 "%",
8847#define SPEC_PERC 0
8848 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008849#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008850 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008851#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008852 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008853#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008854 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008855#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008856 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008857#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008858 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008859#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008860 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008861#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008862 "<stack>", // call stack
8863#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008864 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008865#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008866 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008867#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008868 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008869#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008870 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008871#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008872 "<SID>", // script ID: <SNR>123_
8873#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008874#ifdef FEAT_CLIENTSERVER
8875 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008876# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008877#endif
8878 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008879
K.Takataeeec2542021-06-02 13:28:16 +02008880 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008881 {
8882 len = (int)STRLEN(spec_str[i]);
8883 if (STRNCMP(src, spec_str[i], len) == 0)
8884 {
8885 *usedlen = len;
8886 return i;
8887 }
8888 }
8889 return -1;
8890}
8891
8892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 * Evaluate cmdline variables.
8894 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008895 * change "%" to curbuf->b_ffname
8896 * "#" to curwin->w_alt_fnum
8897 * "%%" to curwin->w_alt_fnum in Vim9 script
8898 * "<cword>" to word under the cursor
8899 * "<cWORD>" to WORD under the cursor
8900 * "<cexpr>" to C-expression under the cursor
8901 * "<cfile>" to path name under the cursor
8902 * "<sfile>" to sourced file name
8903 * "<stack>" to call stack
8904 * "<slnum>" to sourced file line number
8905 * "<afile>" to file name for autocommand
8906 * "<abuf>" to buffer number for autocommand
8907 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 *
8909 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8910 * "" for error without a message) and NULL is returned.
8911 * Returns an allocated string if a valid match was found.
8912 * Returns NULL if no match was found. "usedlen" then still contains the
8913 * number of characters to skip.
8914 */
8915 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008916eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008917 char_u *src, // pointer into commandline
8918 char_u *srcstart, // beginning of valid memory for src
8919 int *usedlen, // characters after src that are used
8920 linenr_T *lnump, // line number for :e command, or NULL
8921 char **errormsg, // pointer to error message
8922 int *escaped) // return value has escaped white space (can
8923 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924{
8925 int i;
8926 char_u *s;
8927 char_u *result;
8928 char_u *resultbuf = NULL;
8929 int resultlen;
8930 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008931 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008933 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936
8937 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008938 if (escaped != NULL)
8939 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940
8941 /*
8942 * Check if there is something to do.
8943 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008944 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008945 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 {
8947 *usedlen = 1;
8948 return NULL;
8949 }
8950
8951 /*
8952 * Skip when preceded with a backslash "\%" and "\#".
8953 * Note: In "\\%" the % is also not recognized!
8954 */
8955 if (src > srcstart && src[-1] == '\\')
8956 {
8957 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008958 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 return NULL;
8960 }
8961
8962 /*
8963 * word or WORD under cursor
8964 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008965 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8966 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008968 resultlen = find_ident_under_cursor(&result,
8969 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8970 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8971 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 if (resultlen == 0)
8973 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008974 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 return NULL;
8976 }
8977 }
8978
8979 /*
8980 * '#': Alternate file name
8981 * '%': Current file name
8982 * File name under the cursor
8983 * File name for autocommand
8984 * and following modifiers
8985 */
8986 else
8987 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008988 int off = 0;
8989
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 switch (spec_idx)
8991 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008992 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008993#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008994 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008997 // '%': current file
8998 if (curbuf->b_fname == NULL)
8999 {
9000 result = (char_u *)"";
9001 valid = 0; // Must have ":p:h" to be valid
9002 }
9003 else
9004 {
9005 result = curbuf->b_fname;
9006 tilde_file = STRCMP(result, "~") == 0;
9007 }
9008 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009010#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009011 // "%%" alternate file
9012 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009013#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009014 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009015 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009016 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009018 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 result = arg_all();
9020 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009021 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009022 if (escaped != NULL)
9023 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 break;
9026 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009027 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009028 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009029 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009031 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009032 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009033 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009034 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009036 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009038 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009039 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009040 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009041 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009042 return NULL;
9043 }
9044#ifdef FEAT_EVAL
9045 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9046 (long)i);
9047 if (result == NULL)
9048 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009049 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009050 return NULL;
9051 }
9052#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01009053 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 }
9057 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009058 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009059 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9060 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009061 buf = buflist_findnr(i);
9062 if (buf == NULL)
9063 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009064 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00009065 return NULL;
9066 }
9067 if (lnump != NULL)
9068 *lnump = ECMD_LAST;
9069 if (buf->b_fname == NULL)
9070 {
9071 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009072 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009073 }
9074 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009075 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009076 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009077 tilde_file = STRCMP(result, "~") == 0;
9078 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 break;
9081
9082#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009083 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009084 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 if (result == NULL)
9086 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009087 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 return NULL;
9089 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009090 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 break;
9092#endif
9093
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009094 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009096 if (result != NULL && !autocmd_fname_full)
9097 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009098 // Still need to turn the fname into a full path. It is
9099 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009100 autocmd_fname_full = TRUE;
9101 result = FullName_save(autocmd_fname, FALSE);
9102 vim_free(autocmd_fname);
9103 autocmd_fname = result;
9104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 if (result == NULL)
9106 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009107 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 return NULL;
9109 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009110 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 break;
9112
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009113 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 if (autocmd_bufnr <= 0)
9115 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009116 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 return NULL;
9118 }
9119 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9120 result = strbuf;
9121 break;
9122
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009123 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 result = autocmd_match;
9125 if (result == NULL)
9126 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009127 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 return NULL;
9129 }
9130 break;
9131
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009132 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02009133 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02009134 result = estack_sfile(spec_idx == SPEC_SFILE
9135 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 if (result == NULL)
9137 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02009138 *errormsg = spec_idx == SPEC_SFILE
9139 ? _("E498: no :source file name to substitute for \"<sfile>\"")
9140 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 return NULL;
9142 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009143 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009145
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009146 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009147 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009148 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009149 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009150 return NULL;
9151 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009152 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009153 result = strbuf;
9154 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009155
9156#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009157 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009158 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009159 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009160 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009161 return NULL;
9162 }
9163 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009164 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009165 result = strbuf;
9166 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009167
Bram Moolenaar90944302020-08-01 20:45:11 +02009168 case SPEC_SID:
9169 if (current_sctx.sc_sid <= 0)
9170 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009171 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009172 return NULL;
9173 }
9174 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9175 result = strbuf;
9176 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009177#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009178
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009179#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009180 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009181 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9182 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 result = strbuf;
9184 break;
9185#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009186
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009187 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009188 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009189 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 }
9191
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009192 resultlen = (int)STRLEN(result); // length of new string
9193 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 {
9195 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 resultlen = (int)(s - result);
9198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 else if (!skip_mod)
9200 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009201 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 &resultlen);
9203 if (result == NULL)
9204 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009205 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 return NULL;
9207 }
9208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 }
9210
9211 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9212 {
9213 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009214 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009215 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009217 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 result = NULL;
9219 }
9220 else
9221 result = vim_strnsave(result, resultlen);
9222 vim_free(resultbuf);
9223 return result;
9224}
9225
9226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 * Expand the <sfile> string in "arg".
9228 *
9229 * Returns an allocated string, or NULL for any error.
9230 */
9231 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009232expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009234 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 int len;
9236 char_u *result;
9237 char_u *newres;
9238 char_u *repl;
9239 int srclen;
9240 char_u *p;
9241
9242 result = vim_strsave(arg);
9243 if (result == NULL)
9244 return NULL;
9245
9246 for (p = result; *p; )
9247 {
9248 if (STRNCMP(p, "<sfile>", 7) != 0)
9249 ++p;
9250 else
9251 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009252 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00009253 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 if (errormsg != NULL)
9255 {
9256 if (*errormsg)
9257 emsg(errormsg);
9258 vim_free(result);
9259 return NULL;
9260 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009261 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 {
9263 p += srclen;
9264 continue;
9265 }
9266 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9267 newres = alloc(len);
9268 if (newres == NULL)
9269 {
9270 vim_free(repl);
9271 vim_free(result);
9272 return NULL;
9273 }
9274 mch_memmove(newres, result, (size_t)(p - result));
9275 STRCPY(newres + (p - result), repl);
9276 len = (int)STRLEN(newres);
9277 STRCAT(newres, p + srclen);
9278 vim_free(repl);
9279 vim_free(result);
9280 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009281 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 }
9283 }
9284
9285 return result;
9286}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009289/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009290 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009291 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009294dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 if (fname == NULL)
9297 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009298 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299}
9300#endif
9301
9302/*
9303 * ":behave {mswin,xterm}"
9304 */
9305 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009306ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307{
9308 if (STRCMP(eap->arg, "mswin") == 0)
9309 {
9310 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9311 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9312 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9313 set_option_value((char_u *)"keymodel", 0L,
9314 (char_u *)"startsel,stopsel", 0);
9315 }
9316 else if (STRCMP(eap->arg, "xterm") == 0)
9317 {
9318 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9319 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9320 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9321 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9322 }
9323 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009324 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325}
9326
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327static int filetype_detect = FALSE;
9328static int filetype_plugin = FALSE;
9329static int filetype_indent = FALSE;
9330
9331/*
9332 * ":filetype [plugin] [indent] {on,off,detect}"
9333 * on: Load the filetype.vim file to install autocommands for file types.
9334 * off: Load the ftoff.vim file to remove all autocommands for file types.
9335 * plugin on: load filetype.vim and ftplugin.vim
9336 * plugin off: load ftplugof.vim
9337 * indent on: load filetype.vim and indent.vim
9338 * indent off: load indoff.vim
9339 */
9340 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009341ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342{
9343 char_u *arg = eap->arg;
9344 int plugin = FALSE;
9345 int indent = FALSE;
9346
9347 if (*eap->arg == NUL)
9348 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009349 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009350 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 filetype_detect ? "ON" : "OFF",
9352 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9353 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9354 return;
9355 }
9356
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009357 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 for (;;)
9359 {
9360 if (STRNCMP(arg, "plugin", 6) == 0)
9361 {
9362 plugin = TRUE;
9363 arg = skipwhite(arg + 6);
9364 continue;
9365 }
9366 if (STRNCMP(arg, "indent", 6) == 0)
9367 {
9368 indent = TRUE;
9369 arg = skipwhite(arg + 6);
9370 continue;
9371 }
9372 break;
9373 }
9374 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9375 {
9376 if (*arg == 'o' || !filetype_detect)
9377 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009378 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 filetype_detect = TRUE;
9380 if (plugin)
9381 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009382 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 filetype_plugin = TRUE;
9384 }
9385 if (indent)
9386 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009387 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 filetype_indent = TRUE;
9389 }
9390 }
9391 if (*arg == 'd')
9392 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009393 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009394 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 }
9396 }
9397 else if (STRCMP(arg, "off") == 0)
9398 {
9399 if (plugin || indent)
9400 {
9401 if (plugin)
9402 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009403 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 filetype_plugin = FALSE;
9405 }
9406 if (indent)
9407 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009408 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 filetype_indent = FALSE;
9410 }
9411 }
9412 else
9413 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009414 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 filetype_detect = FALSE;
9416 }
9417 }
9418 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009419 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420}
9421
9422/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009423 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 */
9425 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009426ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427{
9428 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009429 {
9430 char_u *arg = eap->arg;
9431
9432 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9433 arg += 9;
9434
9435 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9436 if (arg != eap->arg)
9437 did_filetype = FALSE;
9438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009442ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443{
9444#ifdef FEAT_DIGRAPHS
9445 if (*eap->arg != NUL)
9446 putdigraph(eap->arg);
9447 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009448 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449#else
mityu61065042021-07-19 20:07:21 +02009450 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451#endif
9452}
9453
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009454#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9455 void
9456set_no_hlsearch(int flag)
9457{
9458 no_hlsearch = flag;
9459# ifdef FEAT_EVAL
9460 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9461# endif
9462}
9463
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464/*
9465 * ":nohlsearch"
9466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009468ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009470 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009471 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473#endif
9474
9475#ifdef FEAT_CRYPT
9476/*
9477 * ":X": Get crypt key
9478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009480ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009482 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009483 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484}
9485#endif
9486
9487#ifdef FEAT_FOLDING
9488 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009489ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490{
9491 if (foldManualAllowed(TRUE))
9492 foldCreate(eap->line1, eap->line2);
9493}
9494
9495 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009496ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497{
9498 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9499 eap->forceit, FALSE);
9500}
9501
9502 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009503ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504{
9505 linenr_T lnum;
9506
Bram Moolenaar4facea32019-10-12 20:17:40 +02009507# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009508 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009509# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009510
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009511 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9513 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9514 ml_setmarked(lnum);
9515
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009516 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009518 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009519# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009520 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009521# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522}
9523#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009525#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009526/*
9527 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9528 * instead of a quickfix command.
9529 */
9530 int
9531is_loclist_cmd(int cmdidx)
9532{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009533 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009534 return FALSE;
9535 return cmdnames[cmdidx].cmd_name[0] == 'l';
9536}
9537#endif
9538
Bram Moolenaar4facea32019-10-12 20:17:40 +02009539#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009540 int
9541get_pressedreturn(void)
9542{
9543 return ex_pressedreturn;
9544}
9545
9546 void
9547set_pressedreturn(int val)
9548{
9549 ex_pressedreturn = val;
9550}
9551#endif