blob: 6d2923ccb2518b16d487256b767e1d17dc00ed86 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +020023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaar20b23c62020-08-20 15:25:00 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
39static void ex_bmodified(exarg_T *eap);
40static void ex_bnext(exarg_T *eap);
41static void ex_bprevious(exarg_T *eap);
42static void ex_brewind(exarg_T *eap);
43static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifndef FEAT_QUICKFIX
47# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000048# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# define ex_cc ex_ni
50# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020051# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cfile ex_ni
53# define qf_list ex_ni
54# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020055# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000057# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020059#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define ex_cclose ex_ni
61# define ex_copen ex_ni
62# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020063# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000065#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
66# define ex_cexpr ex_ni
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020069static linenr_T default_address(exarg_T *eap);
Bram Moolenaarb7316892019-05-01 18:08:42 +020070static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020071static void address_default_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010072static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020073#if !defined(FEAT_PERL) \
74 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
75 || !defined(FEAT_TCL) \
76 || !defined(FEAT_RUBY) \
77 || !defined(FEAT_LUA) \
78 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000079# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010080static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010082static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000086#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010087static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
88static void ex_highlight(exarg_T *eap);
89static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_cquit(exarg_T *eap);
91static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010092static void ex_close(exarg_T *eap);
93static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
94static void ex_only(exarg_T *eap);
95static void ex_resize(exarg_T *eap);
96static void ex_stag(exarg_T *eap);
97static void ex_tabclose(exarg_T *eap);
98static void ex_tabonly(exarg_T *eap);
99static void ex_tabnext(exarg_T *eap);
100static void ex_tabmove(exarg_T *eap);
101static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200102#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100103static void ex_pclose(exarg_T *eap);
104static void ex_ptag(exarg_T *eap);
105static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106#else
107# define ex_pclose ex_ni
108# define ex_ptag ex_ni
109# define ex_pedit ex_ni
110#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100111static void ex_hide(exarg_T *eap);
112static void ex_stop(exarg_T *eap);
113static void ex_exit(exarg_T *eap);
114static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100116static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#else
118# define ex_goto ex_ni
119#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100120static void ex_shell(exarg_T *eap);
121static void ex_preserve(exarg_T *eap);
122static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100123static void ex_mode(exarg_T *eap);
124static void ex_wrongmodifier(exarg_T *eap);
125static void ex_find(exarg_T *eap);
126static void ex_open(exarg_T *eap);
127static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#ifndef FEAT_GUI
129# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100132#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100133static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#else
135# define ex_tearoff ex_ni
136#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100137#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
138 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#else
141# define ex_popup ex_ni
142#endif
143#ifndef FEAT_GUI_MSWIN
144# define ex_simalt ex_ni
145#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000146#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147# define gui_mch_find_dialog ex_ni
148# define gui_mch_replace_dialog ex_ni
149#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000150#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151# define ex_helpfind ex_ni
152#endif
153#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100154# define ex_cscope ex_ni
155# define ex_scscope ex_ni
156# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#endif
158#ifndef FEAT_SYN_HL
159# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200160# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000161#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200162#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200163# define ex_syntime ex_ni
164#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000165#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000166# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000167# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000168# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000169# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000170# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000171#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200172#ifndef FEAT_PERSISTENT_UNDO
173# define ex_rundo ex_ni
174# define ex_wundo ex_ni
175#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200176#ifndef FEAT_LUA
177# define ex_lua ex_script_ni
178# define ex_luado ex_ni
179# define ex_luafile ex_ni
180#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000181#ifndef FEAT_MZSCHEME
182# define ex_mzscheme ex_script_ni
183# define ex_mzfile ex_ni
184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#ifndef FEAT_PERL
186# define ex_perl ex_script_ni
187# define ex_perldo ex_ni
188#endif
189#ifndef FEAT_PYTHON
190# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200191# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192# define ex_pyfile ex_ni
193#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200194#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200195# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200196# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200197# define ex_py3file ex_ni
198#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100199#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
200# define ex_pyx ex_script_ni
201# define ex_pyxdo ex_ni
202# define ex_pyxfile ex_ni
203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#ifndef FEAT_TCL
205# define ex_tcl ex_script_ni
206# define ex_tcldo ex_ni
207# define ex_tclfile ex_ni
208#endif
209#ifndef FEAT_RUBY
210# define ex_ruby ex_script_ni
211# define ex_rubydo ex_ni
212# define ex_rubyfile ex_ni
213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#ifndef FEAT_KEYMAP
215# define ex_loadkeymap ex_ni
216#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100217static void ex_swapname(exarg_T *eap);
218static void ex_syncbind(exarg_T *eap);
219static void ex_read(exarg_T *eap);
220static void ex_pwd(exarg_T *eap);
221static void ex_equal(exarg_T *eap);
222static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000225#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#else
228# define ex_winpos ex_ni
229#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100230static void ex_operators(exarg_T *eap);
231static void ex_put(exarg_T *eap);
232static void ex_copymove(exarg_T *eap);
233static void ex_submagic(exarg_T *eap);
234static void ex_join(exarg_T *eap);
235static void ex_at(exarg_T *eap);
236static void ex_bang(exarg_T *eap);
237static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200238#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100239static void ex_wundo(exarg_T *eap);
240static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200241#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ex_redo(exarg_T *eap);
243static void ex_later(exarg_T *eap);
244static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100246static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100249static void ex_startinsert(exarg_T *eap);
250static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100252static void ex_checkpath(exarg_T *eap);
253static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254#else
255# define ex_findpat ex_ni
256# define ex_checkpath ex_ni
257#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200258#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100259static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260#else
261# define ex_psearch ex_ni
262#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static void ex_tag(exarg_T *eap);
264static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#ifndef FEAT_EVAL
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200266# define ex_block ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200267# define ex_break ex_ni
268# define ex_breakadd ex_ni
269# define ex_breakdel ex_ni
270# define ex_breaklist ex_ni
271# define ex_call ex_ni
272# define ex_catch ex_ni
273# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200274# define ex_continue ex_ni
275# define ex_debug ex_ni
276# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200277# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200278# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_echo ex_ni
281# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282# define ex_else ex_ni
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200283# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200284# define ex_endfunction ex_ni
285# define ex_endif ex_ni
286# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200288# define ex_eval ex_ni
289# define ex_execute ex_ni
Bram Moolenaar340c59e2021-04-24 20:37:03 +0200290# define ex_incdec ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200291# define ex_finally ex_ni
292# define ex_finish ex_ni
293# define ex_function ex_ni
294# define ex_if ex_ni
295# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200296# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200297# define ex_lockvar ex_ni
298# define ex_oldfiles ex_ni
299# define ex_options ex_ni
300# define ex_packadd ex_ni
301# define ex_packloadall ex_ni
302# define ex_return ex_ni
303# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304# define ex_throw ex_ni
305# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000307# define ex_unlockvar ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200308# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100309# define ex_import ex_ni
310# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200312#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313# define ex_loadview ex_ni
314#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200315#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316# define ex_viminfo ex_ni
317#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100319static void ex_filetype(exarg_T *eap);
320static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000322# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323# define ex_diffpatch ex_ni
324# define ex_diffgetput ex_ni
325# define ex_diffsplit ex_ni
326# define ex_diffthis ex_ni
327# define ex_diffupdate ex_ni
328#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100329static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100331static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332#else
333# define ex_nohlsearch ex_ni
334# define ex_match ex_ni
335#endif
336#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100337static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338#else
339# define ex_X ex_ni
340#endif
341#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100342static void ex_fold(exarg_T *eap);
343static void ex_foldopen(exarg_T *eap);
344static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345#else
346# define ex_fold ex_ni
347# define ex_foldopen ex_ni
348# define ex_folddo ex_ni
349#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100350#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351# define ex_language ex_ni
352#endif
353#ifndef FEAT_SIGNS
354# define ex_sign ex_ni
355#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200357# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000358# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200359# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362#ifndef FEAT_JUMPLIST
363# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200364# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365# define ex_changes ex_ni
366#endif
367
Bram Moolenaar05159a02005-02-26 23:04:13 +0000368#ifndef FEAT_PROFILE
369# define ex_profile ex_ni
370#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200371#ifndef FEAT_TERMINAL
372# define ex_terminal ex_ni
373#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200374#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
375# define ex_xrestore ex_ni
376#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100377#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200378# define ex_popupclear ex_ni
379#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000380
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381/*
382 * Declare cmdnames[].
383 */
384#define DO_DECLARE_EXCMD
385#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200386#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100387
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388static char_u dollar_command[2] = {'$', 0};
389
390
391#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100392// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393typedef struct
394{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100395 char_u *line; // command line
396 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397} wcmd_T;
398
399/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000402 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000404struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100406 garray_T *lines_gap; // growarray with line info
407 int current_line; // last read line from growarray
408 int repeating; // TRUE when looping a second time
409 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200410 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 void *cookie;
412};
413
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200414static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100415static int store_loop_line(garray_T *gap, char_u *line);
416static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000417
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100418// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000419struct dbg_stuff
420{
421 int trylevel;
422 int force_abort;
423 except_T *caught_stack;
424 char_u *vv_exception;
425 char_u *vv_throwpoint;
426 int did_emsg;
427 int got_int;
428 int did_throw;
429 int need_rethrow;
430 int check_cstack;
431 except_T *current_exception;
432};
433
Bram Moolenaared203462004-06-16 11:19:22 +0000434 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100435save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000436{
437 dsp->trylevel = trylevel; trylevel = 0;
438 dsp->force_abort = force_abort; force_abort = FALSE;
439 dsp->caught_stack = caught_stack; caught_stack = NULL;
440 dsp->vv_exception = v_exception(NULL);
441 dsp->vv_throwpoint = v_throwpoint(NULL);
442
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100443 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000444 dsp->did_emsg = did_emsg; did_emsg = FALSE;
445 dsp->got_int = got_int; got_int = FALSE;
446 dsp->did_throw = did_throw; did_throw = FALSE;
447 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
448 dsp->check_cstack = check_cstack; check_cstack = FALSE;
449 dsp->current_exception = current_exception; current_exception = NULL;
450}
451
452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100453restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000454{
455 suppress_errthrow = FALSE;
456 trylevel = dsp->trylevel;
457 force_abort = dsp->force_abort;
458 caught_stack = dsp->caught_stack;
459 (void)v_exception(dsp->vv_exception);
460 (void)v_throwpoint(dsp->vv_throwpoint);
461 did_emsg = dsp->did_emsg;
462 got_int = dsp->got_int;
463 did_throw = dsp->did_throw;
464 need_rethrow = dsp->need_rethrow;
465 check_cstack = dsp->check_cstack;
466 current_exception = dsp->current_exception;
467}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468#endif
469
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470/*
471 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
472 * command is given.
473 */
474 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100475do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100476 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477{
478 int save_msg_scroll;
479 int prev_msg_row;
480 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100481 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000482
483 if (improved)
484 exmode_active = EXMODE_VIM;
485 else
486 exmode_active = EXMODE_NORMAL;
487 State = NORMAL;
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +0200488 trigger_modechanged();
Bram Moolenaardf177f62005-02-22 08:39:57 +0000489
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100490 // When using ":global /pat/ visual" and then "Q" we return to continue
491 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000492 if (global_busy)
493 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494
495 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100496 ++RedrawingDisabled; // don't redisplay the window
497 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100499 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 ++hold_gui_events;
501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502
Bram Moolenaar32526b32019-01-19 17:43:09 +0100503 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 while (exmode_active)
505 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100506 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000507 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
508 {
509 exmode_active = FALSE;
510 break;
511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 msg_scroll = TRUE;
513 need_wait_return = FALSE;
514 ex_pressedreturn = FALSE;
515 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100516 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 prev_msg_row = msg_row;
518 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 if (improved)
520 {
521 cmdline_row = msg_row;
522 do_cmdline(NULL, getexline, NULL, 0);
523 }
524 else
525 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
526 lines_left = Rows - 1;
527
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100529 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000531 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100532 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000533 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000535 if (ex_pressedreturn)
536 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100537 // go up one line, to overwrite the ":<CR>" line, so the
538 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000539 msg_row = prev_msg_row;
540 if (prev_msg_row == Rows - 1)
541 msg_row--;
542 }
543 msg_col = 0;
544 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
545 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100548 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 {
550 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100551 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000552 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100553 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 }
556
557#ifdef FEAT_GUI
558 --hold_gui_events;
559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 --RedrawingDisabled;
561 --no_wait_return;
562 update_screen(CLEAR);
563 need_wait_return = FALSE;
564 msg_scroll = save_msg_scroll;
565}
566
567/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200568 * Print the executed command for when 'verbose' is set.
569 * When "lnum" is 0 only print the command.
570 */
571 static void
572msg_verbose_cmd(linenr_T lnum, char_u *cmd)
573{
574 ++no_wait_return;
575 verbose_enter_scroll();
576
577 if (lnum == 0)
578 smsg(_("Executing: %s"), cmd);
579 else
580 smsg(_("line %ld: %s"), (long)lnum, cmd);
581 if (msg_silent == 0)
582 msg_puts("\n"); // don't overwrite this
583
584 verbose_leave_scroll();
585 --no_wait_return;
586}
587
588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 * Execute a simple command line. Used for translated commands like "*".
590 */
591 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100592do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593{
594 return do_cmdline(cmd, NULL, NULL,
595 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
596}
597
598/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100599 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
600 * This allows for using a range without ":" in Vim9 script.
601 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200602 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100603do_cmd_argument(char_u *cmd)
604{
605 return do_cmdline(cmd, NULL, NULL,
606 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
607}
608
609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 * do_cmdline(): execute one Ex command line
611 *
612 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100613 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 * 2. Split up in parts separated with '|'.
615 *
616 * This function can be called recursively!
617 *
618 * flags:
619 * DOCMD_VERBOSE - The command will be included in the error message.
620 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100621 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 * DOCMD_KEYTYPED - Don't reset KeyTyped.
623 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
624 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
625 *
626 * return FAIL if cmdline could not be executed, OK otherwise
627 */
628 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100629do_cmdline(
630 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200631 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100632 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100633 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100635 char_u *next_cmdline; // next cmd to execute
636 char_u *cmdline_copy = NULL; // copy of cmd line
637 int used_getline = FALSE; // used "fgetline" to obtain command
638 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100640 int count = 0; // line number count
641 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 int retval = OK;
643#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100644 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100645 garray_T lines_ga; // keep lines for ":while"/":for"
646 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200647 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100648 char_u *fname = NULL; // function or script name
649 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
650 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
651 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200653 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200654 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100656 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200657 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000659 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000661 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100663# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664# define cmd_cookie cookie
665#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100666 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200667#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100668 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
669 // location for storing error messages to be converted to an exception.
670 // This ensures that the do_errthrow() call in do_one_cmd() does not
671 // combine the messages stored by an earlier invocation of do_one_cmd()
672 // with the command name of the later one. This would happen when
673 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 saved_msg_list = msg_list;
675 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676#endif
677
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100678 // It's possible to create an endless loop with ":execute", catch that
679 // here. The value of 200 allows nested function calls, ":source", etc.
680 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100681 if (call_depth >= 200
682#ifdef FEAT_EVAL
683 && call_depth >= p_mfd
684#endif
685 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100687 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100689 // When converting to an exception, we do not include the command name
690 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100691 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 msg_list = saved_msg_list;
693#endif
694 return FAIL;
695 }
696 ++call_depth;
697
698#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100699 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 cstack.cs_idx = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
702
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100703 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100705 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100706 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000707 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 ++ex_nesting_level;
709
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100710 // Get the function or script name and the address where the next breakpoint
711 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000712 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {
714 fname = func_name(real_cookie);
715 breakpoint = func_breakpoint(real_cookie);
716 dbg_tick = func_dbg_tick(real_cookie);
717 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100718 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100720 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 breakpoint = source_breakpoint(real_cookie);
722 dbg_tick = source_dbg_tick(real_cookie);
723 }
724
725 /*
726 * Initialize "force_abort" and "suppress_errthrow" at the top level.
727 */
728 if (!recursive)
729 {
730 force_abort = FALSE;
731 suppress_errthrow = FALSE;
732 }
733
734 /*
735 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000736 * exception handling (used when debugging). Otherwise clear it to avoid
737 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000739 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000740 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000741 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200742 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
744 initial_trylevel = trylevel;
745
746 /*
747 * "did_throw" will be set to TRUE when an exception is being thrown.
748 */
749 did_throw = FALSE;
750#endif
751 /*
752 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000753 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 * If force_abort is set, we cancel everything.
755 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100756#ifdef FEAT_EVAL
757 did_emsg_cumul += did_emsg;
758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 did_emsg = FALSE;
760
761 /*
762 * KeyTyped is only set when calling vgetc(). Reset it here when not
763 * calling vgetc() (sourced command lines).
764 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100765 if (!(flags & DOCMD_KEYTYPED)
766 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 KeyTyped = FALSE;
768
769 /*
770 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000771 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 * - for multiple commands on one line, separated with '|'
773 * - when repeating until there are no more lines (for ":source")
774 */
775 next_cmdline = cmdline;
776 do
777 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000778#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100779 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000780#endif
781
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100782 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 if (next_cmdline == NULL
784#ifdef FEAT_EVAL
785 && !force_abort
786 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000787 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788#endif
789 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100790 {
791#ifdef FEAT_EVAL
792 did_emsg_cumul += did_emsg;
793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
797 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000798 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100799 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 * 3. If a line is given: Make a copy, so we can mess with it.
801 */
802
803#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100804 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000805 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100807 // Each '|' separated command is stored separately in lines_ga, to
808 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100809 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100811 // Check if a function has returned or, unless it has an unclosed
812 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000813 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000815# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000816 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000817 func_line_end(real_cookie);
818# endif
819 if (func_has_ended(real_cookie))
820 {
821 retval = FAIL;
822 break;
823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000825#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000826 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000828 script_line_end();
829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100831 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100832 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 {
834 retval = FAIL;
835 break;
836 }
837
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100838 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (breakpoint != NULL && dbg_tick != NULL
840 && *dbg_tick != debug_tick)
841 {
842 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100843 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100844 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 *dbg_tick = debug_tick;
846 }
847
848 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100849 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100851 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100853 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100855 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100856 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100858 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100859 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 *dbg_tick = debug_tick;
861 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000862# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000863 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000864 {
865 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100866 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100867 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000868 script_line_start();
869 }
870# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872#endif
873
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100874 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (next_cmdline == NULL)
876 {
877 /*
878 * Need to set msg_didout for the first line after an ":if",
879 * otherwise the ":if" will be overwritten.
880 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100881 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100883 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#ifdef FEAT_EVAL
885 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
886#else
887 0
888#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100889 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
890 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100892 // Don't call wait_return for aborted command line. The NULL
893 // returned for the end of a sourced file or executed function
894 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 if (KeyTyped && !(flags & DOCMD_REPEAT))
896 need_wait_return = FALSE;
897 retval = FAIL;
898 break;
899 }
900 used_getline = TRUE;
901
902 /*
903 * Keep the first typed line. Clear it when more lines are typed.
904 */
905 if (flags & DOCMD_KEEPLINE)
906 {
907 vim_free(repeat_cmdline);
908 if (count == 0)
909 repeat_cmdline = vim_strsave(next_cmdline);
910 else
911 repeat_cmdline = NULL;
912 }
913 }
914
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100915 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 else if (cmdline_copy == NULL)
917 {
918 next_cmdline = vim_strsave(next_cmdline);
919 if (next_cmdline == NULL)
920 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200921 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 retval = FAIL;
923 break;
924 }
925 }
926 cmdline_copy = next_cmdline;
927
928#ifdef FEAT_EVAL
929 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200930 * Inside a while/for loop, and when the command looks like a ":while"
931 * or ":for", the line is stored, because we may need it later when
932 * looping.
933 *
934 * When there is a '|' and another command, it is stored separately,
935 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000936 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200937 *
938 * Pass a different "fgetline" function to do_one_cmd() below,
939 * that it stores lines in or reads them from "lines_ga". Makes it
940 * possible to define a function inside a while/for loop and handles
941 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200943 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200945 cmd_getline = get_loop_line;
946 cmd_cookie = (void *)&cmd_loop_cookie;
947 cmd_loop_cookie.lines_gap = &lines_ga;
948 cmd_loop_cookie.current_line = current_line;
949 cmd_loop_cookie.getline = fgetline;
950 cmd_loop_cookie.cookie = cookie;
951 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
952
953 // Save the current line when encountering it the first time.
954 if (current_line == lines_ga.ga_len
955 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 {
957 retval = FAIL;
958 break;
959 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200960 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200962 else
963 {
964 cmd_getline = fgetline;
965 cmd_cookie = cookie;
966 }
967
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 did_endif = FALSE;
969#endif
970
971 if (count++ == 0)
972 {
973 /*
974 * All output from the commands is put below each other, without
975 * waiting for a return. Don't do this when executing commands
976 * from a script or when being called recursive (e.g. for ":e
977 * +command file").
978 */
979 if (!(flags & DOCMD_NOWAIT) && !recursive)
980 {
981 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100982 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100984 msg_scroll = TRUE; // put messages below each other
985 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 ++RedrawingDisabled;
987 did_inc = TRUE;
988 }
989 }
990
Bram Moolenaar823654b2020-05-29 23:03:09 +0200991 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100992 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
994 /*
995 * 2. Execute one '|' separated command.
996 * do_one_cmd() will return NULL if there is no trailing '|'.
997 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
998 */
999 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001000 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001#ifdef FEAT_EVAL
1002 &cstack,
1003#endif
1004 cmd_getline, cmd_cookie);
1005 --recursive;
1006
1007#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001008 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001009 // Use "current_line" from "cmd_loop_cookie", it may have been
1010 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001011 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012#endif
1013
1014 if (next_cmdline == NULL)
1015 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001016 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001017
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 /*
1019 * If the command was typed, remember it for the ':' register.
1020 * Do this AFTER executing the command to make :@: work.
1021 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001022 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 && new_last_cmdline != NULL)
1024 {
1025 vim_free(last_cmdline);
1026 last_cmdline = new_last_cmdline;
1027 new_last_cmdline = NULL;
1028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 }
1030 else
1031 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001032 // need to copy the command after the '|' to cmdline_copy, for the
1033 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001034 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 next_cmdline = cmdline_copy;
1036 }
1037
1038
1039#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001040 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001042 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001044 {
1045 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001049 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 {
1051 ++current_line;
1052
1053 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001054 * An ":endwhile", ":endfor" and ":continue" is handled here.
1055 * If we were executing commands, jump back to the ":while" or
1056 * ":for".
1057 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001059 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001061 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001063 // Jump back to the matching ":while" or ":for". Be careful
1064 // not to use a cs_line[] from an entry that isn't a ":while"
1065 // or ":for": It would make "current_line" invalid and can
1066 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 if (!did_emsg && !got_int && !did_throw
1068 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001069 && (cstack.cs_flags[cstack.cs_idx]
1070 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 && cstack.cs_line[cstack.cs_idx] >= 0
1072 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1073 {
1074 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001075 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001076 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001077 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001079 // Check for the next breakpoint at or after the ":while"
1080 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 if (breakpoint != NULL)
1082 {
1083 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001084 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 fname,
1086 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1087 *dbg_tick = debug_tick;
1088 }
1089 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001090 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001092 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001094 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1095 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 }
1097 }
1098
1099 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001100 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001102 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001104 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001105 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 }
1107 }
1108
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001109 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001110 if (breakpoint != NULL && has_watchexpr())
1111 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001112 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001113 *dbg_tick = debug_tick;
1114 }
1115
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 /*
1117 * When not inside any ":while" loop, clear remembered lines.
1118 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001119 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 {
1121 if (lines_ga.ga_len > 0)
1122 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001123 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1125 free_cmdlines(&lines_ga);
1126 }
1127 current_line = 0;
1128 }
1129
1130 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1132 * being restored at the ":endtry". Reset them here and set the
1133 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1134 * This includes the case where a missing ":endif", ":endwhile" or
1135 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001137 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001139 cstack.cs_lflags &= ~CSL_HAD_FINA;
1140 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1141 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 did_throw ? (void *)current_exception : NULL);
1143 did_emsg = got_int = did_throw = FALSE;
1144 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1145 }
1146
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001147 // Update global "trylevel" for recursive calls to do_cmdline() from
1148 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 trylevel = initial_trylevel + cstack.cs_trylevel;
1150
1151 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001152 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 * files) is aborted because of an error, an interrupt, or an uncaught
1154 * exception, cancel everything. If it is left normally, reset
1155 * force_abort to get the non-EH compatible abortion behavior for
1156 * the rest of the script.
1157 */
1158 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1159 force_abort = FALSE;
1160
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001161 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001163#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164
1165 }
1166 /*
1167 * Continue executing command lines when:
1168 * - no CTRL-C typed, no aborting error, no exception thrown or try
1169 * conditionals need to be checked for executing finally clauses or
1170 * catching an interrupt exception
1171 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001172 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 * looping for ":source" command or function call.
1174 */
1175 while (!((got_int
1176#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001177 || (did_emsg && (force_abort || in_vim9script()))
1178 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179#endif
1180 )
1181#ifdef FEAT_EVAL
1182 && cstack.cs_trylevel == 0
1183#endif
1184 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001185 && !(did_emsg
1186#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001187 // Keep going when inside try/catch, so that the error can be
1188 // deal with, except when it is a syntax error, it may cause
1189 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001190 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1191#endif
1192 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001193 && (getline_equal(fgetline, cookie, getexmodeline)
1194 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 && (next_cmdline != NULL
1196#ifdef FEAT_EVAL
1197 || cstack.cs_idx >= 0
1198#endif
1199 || (flags & DOCMD_REPEAT)));
1200
1201 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001202 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203#ifdef FEAT_EVAL
1204 free_cmdlines(&lines_ga);
1205 ga_clear(&lines_ga);
1206
1207 if (cstack.cs_idx >= 0)
1208 {
1209 /*
1210 * If a sourced file or executed function ran to its end, report the
1211 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001212 * In Vim9 script do not give a second error, executing aborts after
1213 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 */
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001215 if (!got_int && !did_throw && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001216 && ((getline_equal(fgetline, cookie, getsourceline)
1217 && !source_finished(fgetline, cookie))
1218 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 && !func_has_ended(real_cookie))))
1220 {
1221 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001222 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001224 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001225 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001226 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001228 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 }
1230
1231 /*
1232 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1233 * ":endtry" in a sourced file or executed function. If the try
1234 * conditional is in its finally clause, ignore anything pending.
1235 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001236 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 */
1238 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001239 {
1240 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1241
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001242 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001243 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001244 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1245 &cstack.cs_looplevel);
1246 }
1247 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 trylevel = initial_trylevel;
1249 }
1250
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001251 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1252 // lack was reported above and the error message is to be converted to an
1253 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001254 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 ? (char_u *)"endfunction" : (char_u *)NULL);
1256
1257 if (trylevel == 0)
1258 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001259 // Just in case did_throw got set but current_exception wasn't.
1260 if (current_exception == NULL)
1261 did_throw = FALSE;
1262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 /*
1264 * When an exception is being thrown out of the outermost try
1265 * conditional, discard the uncaught exception, disable the conversion
1266 * of interrupts or errors to exceptions, and ensure that no more
1267 * commands are executed.
1268 */
1269 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001270 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271
1272 /*
1273 * On an interrupt or an aborting error not converted to an exception,
1274 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001275 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 * when force_abort is set and did_emsg unset in case of an interrupt
1277 * from a finally clause after an error.
1278 */
1279 else if (got_int || (did_emsg && force_abort))
1280 suppress_errthrow = TRUE;
1281 }
1282
1283 /*
1284 * The current cstack will be freed when do_cmdline() returns. An uncaught
1285 * exception will have to be rethrown in the previous cstack. If a function
1286 * has just returned or a script file was just finished and the previous
1287 * cstack belongs to the same function or, respectively, script file, it
1288 * will have to be checked for finally clauses to be executed due to the
1289 * ":return" or ":finish". This is done in do_one_cmd().
1290 */
1291 if (did_throw)
1292 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001293 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001295 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 && ex_nesting_level > func_level(real_cookie) + 1))
1297 {
1298 if (!did_throw)
1299 check_cstack = TRUE;
1300 }
1301 else
1302 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001303 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001304 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 --ex_nesting_level;
1306 /*
1307 * Go to debug mode when returning from a function in which we are
1308 * single-stepping.
1309 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001310 if ((getline_equal(fgetline, cookie, getsourceline)
1311 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001313 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 ? (char_u *)_("End of sourced file")
1315 : (char_u *)_("End of function"));
1316 }
1317
1318 /*
1319 * Restore the exception environment (done after returning from the
1320 * debugger).
1321 */
1322 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001323 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324
1325 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001326
1327 // Cleanup if "cs_emsg_silent_list" remains.
1328 if (cstack.cs_emsg_silent_list != NULL)
1329 {
1330 eslist_T *elem, *temp;
1331
1332 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1333 {
1334 temp = elem->next;
1335 vim_free(elem);
1336 }
1337 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001338#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339
1340 /*
1341 * If there was too much output to fit on the command line, ask the user to
1342 * hit return before redrawing the screen. With the ":global" command we do
1343 * this only once after the command is finished.
1344 */
1345 if (did_inc)
1346 {
1347 --RedrawingDisabled;
1348 --no_wait_return;
1349 msg_scroll = FALSE;
1350
1351 /*
1352 * When just finished an ":if"-":else" which was typed, no need to
1353 * wait for hit-return. Also for an error situation.
1354 */
1355 if (retval == FAIL
1356#ifdef FEAT_EVAL
1357 || (did_endif && KeyTyped && !did_emsg)
1358#endif
1359 )
1360 {
1361 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001362 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 }
1364 else if (need_wait_return)
1365 {
1366 /*
1367 * The msg_start() above clears msg_didout. The wait_return we do
1368 * here should not overwrite the command that may be shown before
1369 * doing that.
1370 */
1371 msg_didout |= msg_didout_before_start;
1372 wait_return(FALSE);
1373 }
1374 }
1375
Bram Moolenaar2a942252012-11-28 23:03:07 +01001376#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001377 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001378#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 /*
1380 * Reset if_level, in case a sourced script file contains more ":if" than
1381 * ":endif" (could be ":if x | foo | endif").
1382 */
1383 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001384#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001385
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 --call_depth;
1387 return retval;
1388}
1389
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001390#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001391/*
1392 * Handle when "did_throw" is set after executing commands.
1393 */
1394 void
1395handle_did_throw()
1396{
1397 char *p = NULL;
1398 msglist_T *messages = NULL;
Bram Moolenaar9e0ee592021-07-31 22:17:28 +02001399 ESTACK_CHECK_DECLARATION
Bram Moolenaar620c9592021-07-31 21:32:31 +02001400
1401 /*
1402 * If the uncaught exception is a user exception, report it as an
1403 * error. If it is an error exception, display the saved error
1404 * message now. For an interrupt exception, do nothing; the
1405 * interrupt message is given elsewhere.
1406 */
1407 switch (current_exception->type)
1408 {
1409 case ET_USER:
1410 vim_snprintf((char *)IObuff, IOSIZE,
1411 _("E605: Exception not caught: %s"),
1412 current_exception->value);
1413 p = (char *)vim_strsave(IObuff);
1414 break;
1415 case ET_ERROR:
1416 messages = current_exception->messages;
1417 current_exception->messages = NULL;
1418 break;
1419 case ET_INTERRUPT:
1420 break;
1421 }
1422
1423 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1424 current_exception->throw_lnum);
1425 ESTACK_CHECK_SETUP
1426 current_exception->throw_name = NULL;
1427
1428 discard_current_exception(); // uses IObuff if 'verbose'
1429 suppress_errthrow = TRUE;
1430 force_abort = TRUE;
1431
1432 if (messages != NULL)
1433 {
1434 do
1435 {
1436 msglist_T *next = messages->next;
1437 int save_compiling = estack_compiling;
1438
1439 estack_compiling = messages->msg_compiling;
1440 emsg(messages->msg);
1441 vim_free(messages->msg);
1442 vim_free(messages->sfile);
1443 vim_free(messages);
1444 messages = next;
1445 estack_compiling = save_compiling;
1446 }
1447 while (messages != NULL);
1448 }
1449 else if (p != NULL)
1450 {
1451 emsg(p);
1452 vim_free(p);
1453 }
1454 vim_free(SOURCING_NAME);
1455 ESTACK_CHECK_NOW
1456 estack_pop();
1457}
1458
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001460 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 */
1462 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001463get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001465 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 wcmd_T *wp;
1467 char_u *line;
1468
1469 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1470 {
1471 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001472 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001474 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001476 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001478 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001479 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 ++cp->current_line;
1481
1482 return line;
1483 }
1484
1485 KeyTyped = FALSE;
1486 ++cp->current_line;
1487 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001488 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 return vim_strsave(wp->line);
1490}
1491
1492/*
1493 * Store a line in "gap" so that a ":while" loop can execute it again.
1494 */
1495 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001496store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497{
1498 if (ga_grow(gap, 1) == FAIL)
1499 return FAIL;
1500 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001501 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 return OK;
1504}
1505
1506/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001507 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 */
1509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001510free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511{
1512 while (gap->ga_len > 0)
1513 {
1514 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1515 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 }
1517}
1518#endif
1519
1520/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001521 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1522 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001525getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001526 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001527 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001528 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529{
1530#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001531 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001532 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001534 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1535 // function that's originally used to obtain the lines. This may be
1536 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001537 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001538 cp = (struct loop_cookie *)cookie;
1539 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 {
1541 gp = cp->getline;
1542 cp = cp->cookie;
1543 }
1544 return gp == func;
1545#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001546 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547#endif
1548}
1549
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001551 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 * getline function. Otherwise return "cookie".
1553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001555getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001556 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001557 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558{
Bram Moolenaar13505972019-01-24 15:04:48 +01001559#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001560 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001561 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001563 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1564 // cookie that's originally used to obtain the lines. This may be nested
1565 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001566 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001567 cp = (struct loop_cookie *)cookie;
1568 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 {
1570 gp = cp->getline;
1571 cp = cp->cookie;
1572 }
1573 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001574#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001577}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
Bram Moolenaard5053d02020-06-28 15:51:16 +02001579#if defined(FEAT_EVAL) || defined(PROT)
1580/*
1581 * Get the next line source line without advancing.
1582 */
1583 char_u *
1584getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001585 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001586 void *cookie) // argument for fgetline()
1587{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001588 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001589 struct loop_cookie *cp;
1590 wcmd_T *wp;
1591
1592 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1593 // cookie that's originally used to obtain the lines. This may be nested
1594 // several levels.
1595 gp = fgetline;
1596 cp = (struct loop_cookie *)cookie;
1597 while (gp == get_loop_line)
1598 {
1599 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1600 {
1601 // executing lines a second time, use the stored copy
1602 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1603 return wp->line;
1604 }
1605 gp = cp->getline;
1606 cp = cp->cookie;
1607 }
1608 if (gp == getsourceline)
1609 return source_nextline(cp);
1610 return NULL;
1611}
1612#endif
1613
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001614
1615/*
1616 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1617 * ":bwipeout", etc.
1618 * Returns the buffer number.
1619 */
1620 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001621compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001622{
1623 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001624 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001625 int count = offset;
1626
1627 buf = firstbuf;
1628 while (buf->b_next != NULL && buf->b_fnum < lnum)
1629 buf = buf->b_next;
1630 while (count != 0)
1631 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001632 count += (offset < 0) ? 1 : -1;
1633 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1634 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001635 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001636 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001637 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001638 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001639 while (buf->b_ml.ml_mfp == NULL)
1640 {
1641 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1642 if (nextbuf == NULL)
1643 break;
1644 buf = nextbuf;
1645 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001646 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001647 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001648 if (addr_type == ADDR_LOADED_BUFFERS)
1649 while (buf->b_ml.ml_mfp == NULL)
1650 {
1651 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1652 if (nextbuf == NULL)
1653 break;
1654 buf = nextbuf;
1655 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001656 return buf->b_fnum;
1657}
1658
Bram Moolenaarb7316892019-05-01 18:08:42 +02001659/*
1660 * Return the window number of "win".
1661 * When "win" is NULL return the number of windows.
1662 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001663 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001664current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001665{
1666 win_T *wp;
1667 int nr = 0;
1668
Bram Moolenaar29323592016-07-24 22:04:11 +02001669 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001670 {
1671 ++nr;
1672 if (wp == win)
1673 break;
1674 }
1675 return nr;
1676}
1677
1678 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001679current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001680{
1681 tabpage_T *tp;
1682 int nr = 0;
1683
Bram Moolenaar29323592016-07-24 22:04:11 +02001684 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001685 {
1686 ++nr;
1687 if (tp == tab)
1688 break;
1689 }
1690 return nr;
1691}
1692
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001693 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001694comment_start(char_u *p, int starts_with_colon UNUSED)
1695{
1696#ifdef FEAT_EVAL
1697 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001698 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001699#endif
1700 return *p == '"';
1701}
1702
Bram Moolenaarf240e182014-11-27 18:33:02 +01001703# define CURRENT_WIN_NR current_win_nr(curwin)
1704# define LAST_WIN_NR current_win_nr(NULL)
1705# define CURRENT_TAB_NR current_tab_nr(curtab)
1706# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001707
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708/*
1709 * Execute one Ex command.
1710 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001711 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1712 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 *
1714 * 1. skip comment lines and leading space
1715 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001716 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001717 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001718 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001719 * 6. parse arguments
1720 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001722 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 *
1724 * This function may be called recursively!
1725 */
1726#if (_MSC_VER == 1200)
1727/*
Bram Moolenaared203462004-06-16 11:19:22 +00001728 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001730 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731#endif
1732 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001733do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001734 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001735 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001737 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001739 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001740 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001742 char_u *p;
1743 linenr_T lnum;
1744 long n;
1745 char *errormsg = NULL; // error message
1746 char_u *after_modifier = NULL;
1747 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001748 cmdmod_T save_cmdmod;
1749 int save_reg_executing = reg_executing;
1750 int ni; // set when Not Implemented
1751 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001752 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001753#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001754 int may_have_range;
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001755 int vim9script;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001756 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001757#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001758 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
Bram Moolenaara80faa82020-04-12 19:37:17 +02001760 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 ea.line1 = 1;
1762 ea.line2 = 1;
1763#ifdef FEAT_EVAL
1764 ++ex_nesting_level;
1765#endif
1766
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001767 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 if (quitmore
1769#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001770 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001771 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001772#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001773 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001774 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 --quitmore;
1776
1777 /*
1778 * Reset browse, confirm, etc.. They are restored when returning, for
1779 * recursive calls.
1780 */
1781 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001783 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001784 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1785 goto doend;
1786
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001787/*
1788 * 1. Skip comment lines and leading white space and colons.
1789 * 2. Handle command modifiers.
1790 */
1791 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001793 ea.cmdlinep = cmdlinep;
1794 ea.getline = fgetline;
1795 ea.cookie = cookie;
1796#ifdef FEAT_EVAL
1797 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001798 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001800 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001801 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001802 apply_cmdmod(&cmdmod);
Bram Moolenaar2379f872021-02-14 14:07:34 +01001803#ifdef FEAT_EVAL
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001804 vim9script = in_vim9script();
Bram Moolenaar2379f872021-02-14 14:07:34 +01001805#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001806 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807
1808#ifdef FEAT_EVAL
1809 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1810 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1811#else
1812 ea.skip = (if_level > 0);
1813#endif
1814
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001816 * 3. Skip over the range to find the command. Let "p" point to after it.
1817 *
1818 * We need the command to know what kind of range it uses.
1819 */
1820 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001821#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001822 // In Vim9 script a colon is required before the range. This may also be
1823 // after command modifiers.
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001824 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001825 {
1826 may_have_range = FALSE;
1827 for (p = ea.cmd; p >= *cmdlinep; --p)
1828 {
1829 if (*p == ':')
1830 may_have_range = TRUE;
1831 if (p < ea.cmd && !VIM_ISWHITE(*p))
1832 break;
1833 }
1834 }
1835 else
1836 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001837 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001838#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001839 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840
1841#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001842 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001843 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001844 if (ea.cmd == cmd + 1 && *cmd == '$')
1845 // should be "$VAR = val"
1846 --ea.cmd;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001847 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001848 if (ea.cmdidx == CMD_SIZE)
1849 {
1850 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1851
1852 // If a ':' before the range is missing, give a clearer error
1853 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001854 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001855 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001856 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001857 goto doend;
1858 }
1859 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001860 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001861 else
1862#endif
1863 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001864
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001865#ifdef FEAT_EVAL
1866# ifdef FEAT_PROFILE
1867 // Count this line for profiling if skip is TRUE.
1868 if (do_profiling == PROF_YES
1869 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1870 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1871 {
1872 int skip = did_emsg || got_int || did_throw;
1873
1874 if (ea.cmdidx == CMD_catch)
1875 skip = !skip && !(cstack->cs_idx >= 0
1876 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1877 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1878 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1879 skip = skip || !(cstack->cs_idx >= 0
1880 && !(cstack->cs_flags[cstack->cs_idx]
1881 & (CSF_ACTIVE | CSF_TRUE)));
1882 else if (ea.cmdidx == CMD_finally)
1883 skip = FALSE;
1884 else if (ea.cmdidx != CMD_endif
1885 && ea.cmdidx != CMD_endfor
1886 && ea.cmdidx != CMD_endtry
1887 && ea.cmdidx != CMD_endwhile)
1888 skip = ea.skip;
1889
1890 if (!skip)
1891 {
1892 if (getline_equal(fgetline, cookie, get_func_line))
1893 func_line_exec(getline_cookie(fgetline, cookie));
1894 else if (getline_equal(fgetline, cookie, getsourceline))
1895 script_line_exec();
1896 }
1897 }
1898# endif
1899
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001900 // May go to debug mode. If this happens and the ">quit" debug command is
1901 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001902 dbg_check_breakpoint(&ea);
1903 if (!ea.skip && got_int)
1904 {
1905 ea.skip = TRUE;
1906 (void)do_intthrow(cstack);
1907 }
1908#endif
1909
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001910/*
1911 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 *
1913 * where 'addr' is:
1914 *
1915 * % (entire file)
1916 * $ [+-NUM]
1917 * 'x [+-NUM] (where x denotes a currently defined mark)
1918 * . [+-NUM]
1919 * [+-NUM]..
1920 * NUM
1921 *
1922 * The ea.cmd pointer is updated to point to the first character following the
1923 * range spec. If an initial address is found, but no second, the upper bound
1924 * is equal to the lower.
1925 */
1926
Bram Moolenaar25190db2019-05-04 15:05:28 +02001927 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001928 if (!IS_USER_CMDIDX(ea.cmdidx))
1929 {
1930 if (ea.cmdidx != CMD_SIZE)
1931 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1932 else
1933 ea.addr_type = ADDR_LINES;
1934
Bram Moolenaar25190db2019-05-04 15:05:28 +02001935 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001936 if (ea.cmdidx == CMD_wincmd && p != NULL)
1937 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001938#ifdef FEAT_QUICKFIX
1939 // :.cc in quickfix window uses line number
1940 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1941 ea.addr_type = ADDR_OTHER;
1942#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001943 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001944
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001945 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001946#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001947 if (!may_have_range)
1948 ea.line1 = ea.line2 = default_address(&ea);
1949 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001950#endif
1951 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1952 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001955 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 */
1957
1958 /*
1959 * Skip ':' and any white space
1960 */
1961 ea.cmd = skipwhite(ea.cmd);
1962 while (*ea.cmd == ':')
1963 ea.cmd = skipwhite(ea.cmd + 1);
1964
1965 /*
1966 * If we got a line, but no command, then go to the line.
1967 * If we find a '|' or '\n' we set ea.nextcmd.
1968 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001969 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1970 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
1972 /*
1973 * strange vi behaviour:
1974 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001975 * ":3|..." prints line 3 (not in Vim9 script)
1976 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001978 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 goto doend;
Bram Moolenaarb8554302021-02-15 21:30:30 +01001980 if ((*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
1981#ifdef FEAT_EVAL
1982 && !vim9script
1983#endif
1984 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 {
1986 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001987 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 if ((errormsg = invalid_range(&ea)) == NULL)
1989 {
1990 correct_range(&ea);
1991 ex_print(&ea);
1992 }
1993 }
1994 else if (ea.addr_count != 0)
1995 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001996 if (ea.line2 > curbuf->b_ml.ml_line_count)
1997 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001998 // With '-' in 'cpoptions' a line number past the file is an
1999 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002000 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2001 ea.line2 = -1;
2002 else
2003 ea.line2 = curbuf->b_ml.ml_line_count;
2004 }
2005
2006 if (ea.line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002007 errormsg = _(e_invalid_range);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 else
2009 {
2010 if (ea.line2 == 0)
2011 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 else
2013 curwin->w_cursor.lnum = ea.line2;
2014 beginline(BL_SOL | BL_FIX);
2015 }
2016 }
2017 goto doend;
2018 }
2019
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002020 // If this looks like an undefined user command and there are CmdUndefined
2021 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002022 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2023 && ASCII_ISUPPER(*ea.cmd)
2024 && has_cmdundefined())
2025 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002026 int ret;
2027
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002028 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002029 while (ASCII_ISALNUM(*p))
2030 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002031 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002032 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2033 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002034 // If the autocommands did something and didn't cause an error, try
2035 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002036 p = (ret
2037#ifdef FEAT_EVAL
2038 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002039#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002041 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002042
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 if (p == NULL)
2044 {
2045 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002046 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 goto doend;
2048 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002049 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002050 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2051 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 {
2053 errormsg = uc_fun_cmd();
2054 goto doend;
2055 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002056
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 if (ea.cmdidx == CMD_SIZE)
2058 {
2059 if (!ea.skip)
2060 {
2061 STRCPY(IObuff, _("E492: Not an editor command"));
2062 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002063 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002064 // If the modifier was parsed OK the error must be in the
2065 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002066 if (after_modifier != NULL)
2067 append_command(after_modifier);
2068 else
2069 append_command(*cmdlinep);
2070 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002071 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002072 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 }
2074 goto doend;
2075 }
2076
Bram Moolenaar958636c2014-10-21 20:01:58 +02002077 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2078 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002079#ifdef HAVE_EX_SCRIPT_NI
2080 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2081#endif
2082 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083
2084#ifndef FEAT_EVAL
2085 /*
2086 * When the expression evaluation is disabled, recognize the ":if" and
2087 * ":endif" commands and ignore everything in between it.
2088 */
2089 if (ea.cmdidx == CMD_if)
2090 ++if_level;
2091 if (if_level)
2092 {
2093 if (ea.cmdidx == CMD_endif)
2094 --if_level;
2095 goto doend;
2096 }
2097
2098#endif
2099
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002100 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002101 if (*p == '!' && ea.cmdidx != CMD_substitute
2102 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 {
2104 ++p;
2105 ea.forceit = TRUE;
2106 }
2107 else
2108 ea.forceit = FALSE;
2109
2110/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002111 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002113 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002114 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115
2116 if (!ea.skip)
2117 {
2118#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002119 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002121 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002122 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 goto doend;
2124 }
2125#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002126 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002127 {
2128 errormsg = _("E981: Command not allowed in rvim");
2129 goto doend;
2130 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002131 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002133 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002134 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 goto doend;
2136 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002137
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002138 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002140#ifdef FEAT_CMDWIN
2141 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2142 {
2143 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002144 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002145 goto doend;
2146 }
2147#endif
2148 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2149 {
2150 // Command not allowed when text is locked
2151 errormsg = _(get_text_locked_msg());
2152 goto doend;
2153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002155
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002156 // Disallow editing another buffer when "curbuf_lock" is set.
2157 // Do allow ":checktime" (it is postponed).
2158 // Do allow ":edit" (check for an argument later).
2159 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002160 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002161 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002162 && ea.cmdidx != CMD_edit
2163 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002164 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002165 && curbuf_locked())
2166 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002168 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002170 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002171 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 goto doend;
2173 }
2174 }
2175
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002176 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002178 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 goto doend;
2180 }
2181
2182 /*
2183 * Don't complain about the range if it is not used
2184 * (could happen if line_count is accidentally set to 0).
2185 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002186 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 {
2188 /*
2189 * If the range is backwards, ask for confirmation and, if given, swap
2190 * ea.line1 & ea.line2 so it's forwards again.
2191 * When global command is busy, don't ask, will fail below.
2192 */
2193 if (!global_busy && ea.line1 > ea.line2)
2194 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002195 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002197 if (sourcing || exmode_active)
2198 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002199 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002200 goto doend;
2201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 if (ask_yesno((char_u *)
2203 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002204 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 }
2206 lnum = ea.line1;
2207 ea.line1 = ea.line2;
2208 ea.line2 = lnum;
2209 }
2210 if ((errormsg = invalid_range(&ea)) != NULL)
2211 goto doend;
2212 }
2213
Bram Moolenaarb7316892019-05-01 18:08:42 +02002214 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2215 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 ea.line2 = 1;
2217
2218 correct_range(&ea);
2219
2220#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002221 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002222 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002224 // Put the first line at the start of a closed fold, put the last line
2225 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 (void)hasFolding(ea.line1, &ea.line1, NULL);
2227 (void)hasFolding(ea.line2, NULL, &ea.line2);
2228 }
2229#endif
2230
2231#ifdef FEAT_QUICKFIX
2232 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002233 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 * option here, so things like % get expanded.
2235 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002236 p = replace_makeprg(&ea, p, cmdlinep);
2237 if (p == NULL)
2238 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239#endif
2240
2241 /*
2242 * Skip to start of argument.
2243 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2244 */
2245 if (ea.cmdidx == CMD_bang)
2246 ea.arg = p;
2247 else
2248 ea.arg = skipwhite(p);
2249
Bram Moolenaar379fb762018-08-30 15:58:28 +02002250 // ":file" cannot be run with an argument when "curbuf_lock" is set
2251 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2252 goto doend;
2253
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 /*
2255 * Check for "++opt=val" argument.
2256 * Must be first, allow ":w ++enc=utf8 !cmd"
2257 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002258 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2260 if (getargopt(&ea) == FAIL && !ni)
2261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002262 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 goto doend;
2264 }
2265
2266 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2267 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002268 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002270 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002272 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 goto doend;
2274 }
2275 ea.arg = skipwhite(ea.arg + 1);
2276 ea.append = TRUE;
2277 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002278 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 {
2280 ++ea.arg;
2281 ea.usefilter = TRUE;
2282 }
2283 }
2284
2285 if (ea.cmdidx == CMD_read)
2286 {
2287 if (ea.forceit)
2288 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002289 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 ea.forceit = FALSE;
2291 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002292 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 {
2294 ++ea.arg;
2295 ea.usefilter = TRUE;
2296 }
2297 }
2298
2299 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2300 {
2301 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002302 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
2304 ++ea.arg;
2305 ++ea.amount;
2306 }
2307 ea.arg = skipwhite(ea.arg);
2308 }
2309
2310 /*
2311 * Check for "+command" argument, before checking for next command.
2312 * Don't do this for ":read !cmd" and ":write !cmd".
2313 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002314 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2316
2317 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002318 * For commands that do not use '|' inside their argument: Check for '|' to
2319 * separate commands and '"' or '#' to start comments.
2320 *
2321 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002322 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002323 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002324 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002326 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2327 {
2328 separate_nextcmd(&ea);
2329 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002330 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002331 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002332 || ea.cmdidx == CMD_global
2333 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002334 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002335#ifdef FEAT_EVAL
2336 || inside_block(&ea)
2337#endif
2338 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 {
2340 for (p = ea.arg; *p; ++p)
2341 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002342 // Remove one backslash before a newline, so that it's possible to
2343 // pass a newline to the shell and also a newline that is preceded
2344 // with a backslash. This makes it impossible to end a shell
2345 // command in a backslash, but that doesn't appear useful.
2346 // Halving the number of backslashes is incompatible with previous
2347 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002349 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 else if (*p == '\n')
2351 {
2352 ea.nextcmd = p + 1;
2353 *p = NUL;
2354 break;
2355 }
2356 }
2357 }
2358
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002359 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002360 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002362 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002363 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002365 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002366 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002367 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002369#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002370 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002371 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002373 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002374 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 }
2376#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002377 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2378 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002379 {
2380 ea.regname = *ea.arg++;
2381#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002382 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002383 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2384 {
kuuote08d7b1c2021-10-04 22:17:36 +01002385 if (!ea.skip)
2386 {
2387 set_expr_line(vim_strsave(ea.arg), &ea);
2388 did_set_expr_line = TRUE;
2389 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002390 ea.arg += STRLEN(ea.arg);
2391 }
2392#endif
2393 ea.arg = skipwhite(ea.arg);
2394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 }
2396
2397 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002398 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 * count, it's a buffer name.
2400 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002401 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002402 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002403 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 {
2405 n = getdigits(&ea.arg);
2406 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002407 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002409 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 goto doend;
2411 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002412 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 {
2414 ea.line2 = n;
2415 if (ea.addr_count == 0)
2416 ea.addr_count = 1;
2417 }
2418 else
2419 {
2420 ea.line1 = ea.line2;
2421 ea.line2 += n - 1;
2422 ++ea.addr_count;
2423 /*
2424 * Be vi compatible: no error message for out of range.
2425 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002426 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 ea.line2 = curbuf->b_ml.ml_line_count;
2428 }
2429 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002430
2431 /*
2432 * Check for flags: 'l', 'p' and '#'.
2433 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002434 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002435 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002436 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2437 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002439 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002440 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 goto doend;
2442 }
2443
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002444 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002446 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 goto doend;
2448 }
2449
2450#ifdef FEAT_EVAL
2451 /*
2452 * Skip the command when it's not going to be executed.
2453 * The commands like :if, :endif, etc. always need to be executed.
2454 * Also make an exception for commands that handle a trailing command
2455 * themselves.
2456 */
2457 if (ea.skip)
2458 {
2459 switch (ea.cmdidx)
2460 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002461 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 case CMD_while:
2463 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002464 case CMD_for:
2465 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 case CMD_if:
2467 case CMD_elseif:
2468 case CMD_else:
2469 case CMD_endif:
2470 case CMD_try:
2471 case CMD_catch:
2472 case CMD_finally:
2473 case CMD_endtry:
2474 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002475 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 break;
2477
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002478 // Commands that handle '|' themselves. Check: A command should
2479 // either have the EX_TRLBAR flag, appear in this list or appear in
2480 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 case CMD_aboveleft:
2482 case CMD_and:
2483 case CMD_belowright:
2484 case CMD_botright:
2485 case CMD_browse:
2486 case CMD_call:
2487 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002488 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 case CMD_delfunction:
2490 case CMD_djump:
2491 case CMD_dlist:
2492 case CMD_dsearch:
2493 case CMD_dsplit:
2494 case CMD_echo:
2495 case CMD_echoerr:
2496 case CMD_echomsg:
2497 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002498 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002500 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002501 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 case CMD_help:
2503 case CMD_hide:
2504 case CMD_ijump:
2505 case CMD_ilist:
2506 case CMD_isearch:
2507 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002508 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 case CMD_keepjumps:
2510 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002511 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 case CMD_leftabove:
2513 case CMD_let:
2514 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002515 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002516 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002518 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002519 case CMD_noautocmd:
2520 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 case CMD_perl:
2522 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002523 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002524 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002525 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 case CMD_return:
2527 case CMD_rightbelow:
2528 case CMD_ruby:
2529 case CMD_silent:
2530 case CMD_smagic:
2531 case CMD_snomagic:
2532 case CMD_substitute:
2533 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002534 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 case CMD_tcl:
2536 case CMD_throw:
2537 case CMD_tilde:
2538 case CMD_topleft:
2539 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002540 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002541 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 case CMD_verbose:
2543 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002544 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 break;
2546
2547 default: goto doend;
2548 }
2549 }
2550#endif
2551
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002552 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 {
2554 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2555 goto doend;
2556 }
2557
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 /*
2559 * Accept buffer name. Cannot be used at the same time with a buffer
2560 * number. Don't do this for a user command.
2561 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002562 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002563 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 {
2565 /*
2566 * :bdelete, :bwipeout and :bunload take several arguments, separated
2567 * by spaces: find next space (skipping over escaped characters).
2568 * The others take one argument: ignore trailing spaces.
2569 */
2570 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2571 || ea.cmdidx == CMD_bunload)
2572 p = skiptowhite_esc(ea.arg);
2573 else
2574 {
2575 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002576 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 --p;
2578 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002579 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002580 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002581 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 goto doend;
2583 ea.addr_count = 1;
2584 ea.arg = skipwhite(p);
2585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002587 // The :try command saves the emsg_silent flag, reset it here when
2588 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002589 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002590 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002591 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002592 if (emsg_silent < 0)
2593 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002594 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002595 }
2596
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002598 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600
Bram Moolenaar958636c2014-10-21 20:01:58 +02002601 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 {
2603 /*
2604 * Execute a user-defined command.
2605 */
2606 do_ucmd(&ea);
2607 }
2608 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 {
2610 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002611 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 */
2613 ea.errmsg = NULL;
2614 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2615 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002616 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 }
2618
2619#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002620 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002621 // Not if this was a command that wasn't executed or :endif.
Bram Moolenaar16531552020-02-08 16:00:46 +01002622 if (getline_equal(ea.getline, ea.cookie, getsourceline)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002623 && current_sctx.sc_sid > 0
2624 && ea.cmdidx != CMD_endif
2625 && (cstack->cs_idx < 0
2626 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002627 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 /*
2630 * If the command just executed called do_cmdline(), any throw or ":return"
2631 * or ":finish" encountered there must also check the cstack of the still
2632 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2633 * exception, or reanimate a returned function or finished script file and
2634 * return or finish it again.
2635 */
2636 if (need_rethrow)
2637 do_throw(cstack);
2638 else if (check_cstack)
2639 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002640 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002642 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 && current_func_returned())
2644 do_return(&ea, TRUE, FALSE, NULL);
2645 }
2646 need_rethrow = check_cstack = FALSE;
2647#endif
2648
2649doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002650 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002651 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002653 curwin->w_cursor.col = 0;
2654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655
2656 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2657 {
2658 if (sourcing)
2659 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002660 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 {
2662 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002663 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002665 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
2667 emsg(errormsg);
2668 }
2669#ifdef FEAT_EVAL
2670 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002671 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2672 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002673
2674 if (did_set_expr_line)
2675 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676#endif
2677
Bram Moolenaare1004402020-10-24 20:49:43 +02002678 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002680 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002682 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 ea.nextcmd = NULL;
2684
2685#ifdef FEAT_EVAL
2686 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002687 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688#endif
2689
2690 return ea.nextcmd;
2691}
2692#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002693 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694#endif
2695
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002696static char ex_error_buf[MSG_BUF_LEN];
2697
2698/*
2699 * Return an error message with argument included.
2700 * Uses a static buffer, only the last error will be kept.
2701 * "msg" will be translated, caller should use N_().
2702 */
2703 char *
2704ex_errmsg(char *msg, char_u *arg)
2705{
2706 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2707 return ex_error_buf;
2708}
2709
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002711 * Check for an Ex command with optional tail.
2712 * If there is a match advance "pp" to the argument and return TRUE.
2713 * If "noparen" is TRUE do not recognize the command followed by "(".
2714 */
2715 static int
2716checkforcmd_opt(
2717 char_u **pp, // start of command
2718 char *cmd, // name of command
2719 int len, // required length
2720 int noparen)
2721{
2722 int i;
2723
2724 for (i = 0; cmd[i] != NUL; ++i)
2725 if (((char_u *)cmd)[i] != (*pp)[i])
2726 break;
2727 if (i >= len && !isalpha((*pp)[i])
2728 && (*pp)[i] != '_' && (!noparen || (*pp)[i] != '('))
2729 {
2730 *pp = skipwhite(*pp + i);
2731 return TRUE;
2732 }
2733 return FALSE;
2734}
2735
2736/*
2737 * Check for an Ex command with optional tail.
2738 * If there is a match advance "pp" to the argument and return TRUE.
2739 */
2740 int
2741checkforcmd(
2742 char_u **pp, // start of command
2743 char *cmd, // name of command
2744 int len) // required length
2745{
2746 return checkforcmd_opt(pp, cmd, len, FALSE);
2747}
2748
2749/*
2750 * Check for an Ex command with optional tail, not followed by "(".
2751 * If there is a match advance "pp" to the argument and return TRUE.
2752 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002753 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002754checkforcmd_noparen(
2755 char_u **pp, // start of command
2756 char *cmd, // name of command
2757 int len) // required length
2758{
2759 return checkforcmd_opt(pp, cmd, len, TRUE);
2760}
2761
2762/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002763 * Parse and skip over command modifiers:
2764 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002765 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002766 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002767 * When "skip_only" is TRUE the global variables are not changed, except for
2768 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002769 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2770 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002771 * Call apply_cmdmod() to get the side effects of the modifiers:
2772 * - Increment "sandbox" for ":sandbox"
2773 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002774 * - set msg_silent for ":silent"
2775 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002776 * Return FAIL when the command is not to be executed.
2777 * May set "errormsg" to an error message.
2778 */
2779 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002780parse_command_modifiers(
2781 exarg_T *eap,
2782 char **errormsg,
2783 cmdmod_T *cmod,
2784 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002785{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002786 char_u *p;
2787 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002788
Bram Moolenaare1004402020-10-24 20:49:43 +02002789 CLEAR_POINTER(cmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002790
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002791 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002792 for (;;)
2793 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002794 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002795 {
2796 if (*eap->cmd == ':')
2797 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002798 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002799 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002800
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002801 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002802 if (*eap->cmd == NUL && exmode_active
2803 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2804 || getline_equal(eap->getline, eap->cookie, getexline))
2805 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2806 {
2807 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002808 if (!skip_only)
2809 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002810 }
2811
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002812 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002813 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002814 {
2815 // a comment ends at a NL
2816 if (eap->nextcmd == NULL)
2817 {
2818 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2819 if (eap->nextcmd != NULL)
2820 ++eap->nextcmd;
2821 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002822 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002823 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002824 if (*eap->cmd == NUL)
2825 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002826 if (!skip_only)
2827 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002828 return FAIL;
2829 }
2830
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002831 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002832
2833 // In Vim9 script a variable can shadow a command modifier:
2834 // verbose = 123
2835 // verbose += 123
2836 // silent! verbose = func()
2837 // verbose.member = 2
2838 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002839 // But not:
2840 // verbose [a, b] = list
Bram Moolenaar17126b12021-01-07 22:03:02 +01002841 if (in_vim9script())
2842 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002843 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002844
2845 for (s = p; ASCII_ISALPHA(*s); ++s)
2846 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002847 n = skipwhite(s);
2848 if (vim_strchr((char_u *)".=", *n) != NULL
2849 || *s == '['
2850 || (*n != NUL && n[1] == '='))
Bram Moolenaar17126b12021-01-07 22:03:02 +01002851 break;
2852 }
2853
Bram Moolenaareffed932018-08-14 13:38:17 +02002854 switch (*p)
2855 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002856 // When adding an entry, also modify cmd_exists().
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002857 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002858 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002859 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002860 continue;
2861
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002862 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002863 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002864 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002865 continue;
2866 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002867 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002868 {
2869#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002870 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002871#endif
2872 continue;
2873 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002874 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002875 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002876 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002877 continue;
2878
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002879 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002880 break;
2881#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002882 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002883#endif
2884 continue;
2885
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002886 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002887 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002888 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002889 continue;
2890 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002891 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002892 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002893 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002894 continue;
2895 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002896 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002897 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002898 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002899 continue;
2900 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002901 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002902 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002903 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002904 continue;
2905
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002906 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002907 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02002908 char_u *reg_pat;
2909 char_u *nulp = NULL;
2910 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002911
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002912 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002913 || *p == NUL
2914 || (ends_excmd(*p)
2915#ifdef FEAT_EVAL
2916 // in ":filter #pat# cmd" # does not
2917 // start a comment
2918 && (!in_vim9script() || VIM_ISWHITE(p[1]))
2919#endif
2920 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02002921 break;
2922 if (*p == '!')
2923 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002924 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002925 p = skipwhite(p + 1);
2926 if (*p == NUL || ends_excmd(*p))
2927 break;
2928 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002929#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002930 // Avoid that "filter(arg)" is recognized.
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +01002931 if (in_vim9script() && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002932 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002933#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002934 if (skip_only)
2935 p = skip_vimgrep_pat(p, NULL, NULL);
2936 else
2937 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02002938 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
2939 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02002940 if (p == NULL || *p == NUL)
2941 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002942 if (!skip_only)
2943 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002944 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002945 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002946 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002947 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02002948 // restore the character overwritten by NUL
2949 if (nulp != NULL)
2950 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002951 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002952 eap->cmd = p;
2953 continue;
2954 }
2955
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002956 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002957 case 'h': if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02002958 || *p == NUL || ends_excmd(*p))
2959 break;
2960 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002961 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002962 continue;
2963
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002964 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002965 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002966 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002967 continue;
2968 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002969 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
2970 {
2971 if (ends_excmd2(p, eap->cmd))
2972 {
2973 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02002974 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002975 return FAIL;
2976 }
2977 cmod->cmod_flags |= CMOD_LEGACY;
2978 continue;
2979 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002980
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002981 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002982 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002983 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002984 continue;
2985
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002986 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002987 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002988 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002989 continue;
2990 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002991 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002992 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002993 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002994 continue;
2995
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002996 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02002997 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002998 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002999 continue;
3000
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003001 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003002 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003003 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003004 continue;
3005 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003006 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003007 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003008 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003009 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3010 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003011 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003012 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003013 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003014 }
3015 continue;
3016
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003017 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003018 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003019 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003020 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003021 long tabnr = get_address(eap, &eap->cmd,
3022 ADDR_TABS, eap->skip,
3023 skip_only, FALSE, 1);
3024 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003025 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003026 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003027 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003028 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3029 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003030 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003031 return FAIL;
3032 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003033 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003034 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003035 }
3036 eap->cmd = p;
3037 continue;
3038 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003039 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003040 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003041 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003042 continue;
3043
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003044 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003045 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003046 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003047 continue;
3048
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003049 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003050 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003051 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003052 continue;
3053 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003054 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003055 {
3056 if (ends_excmd2(p, eap->cmd))
3057 {
3058 *errormsg =
3059 _(e_vim9cmd_must_be_followed_by_command);
3060 return FAIL;
3061 }
3062 cmod->cmod_flags |= CMOD_VIM9CMD;
3063 continue;
3064 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003065 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003066 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003067 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02003068 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003069 else
Bram Moolenaare1004402020-10-24 20:49:43 +02003070 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003071 eap->cmd = p;
3072 continue;
3073 }
3074 break;
3075 }
3076
3077 return OK;
3078}
3079
3080/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003081 * Return TRUE if "cmod" has anything set.
3082 */
3083 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003084has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003085{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003086 return (cmod->cmod_flags != 0 && (!ignore_silent
3087 || (cmod->cmod_flags
3088 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003089 || cmod->cmod_split != 0
3090 || cmod->cmod_verbose != 0
3091 || cmod->cmod_tab != 0
3092 || cmod->cmod_filter_regmatch.regprog != NULL;
3093}
3094
3095/*
3096 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3097 */
3098 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003099cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003100{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003101 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003102 {
3103 emsg(_(e_misplaced_command_modifier));
3104 return TRUE;
3105 }
3106 return FALSE;
3107}
3108
3109/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003110 * Apply the command modifiers. Saves current state in "cmdmod", call
3111 * undo_cmdmod() later.
3112 */
3113 void
3114apply_cmdmod(cmdmod_T *cmod)
3115{
3116#ifdef HAVE_SANDBOX
3117 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3118 {
3119 ++sandbox;
3120 cmod->cmod_did_sandbox = TRUE;
3121 }
3122#endif
3123 if (cmod->cmod_verbose > 0)
3124 {
3125 if (cmod->cmod_verbose_save == 0)
3126 cmod->cmod_verbose_save = p_verbose + 1;
3127 p_verbose = cmod->cmod_verbose;
3128 }
3129
3130 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3131 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003132 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003133 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003134 cmod->cmod_save_msg_scroll = msg_scroll;
3135 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003136 if (cmod->cmod_flags & CMOD_SILENT)
3137 ++msg_silent;
3138 if (cmod->cmod_flags & CMOD_UNSILENT)
3139 msg_silent = 0;
3140
3141 if (cmod->cmod_flags & CMOD_ERRSILENT)
3142 {
3143 ++emsg_silent;
3144 ++cmod->cmod_did_esilent;
3145 }
3146
Bram Moolenaare1004402020-10-24 20:49:43 +02003147 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003148 {
3149 // Set 'eventignore' to "all".
3150 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003151 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003152 set_string_option_direct((char_u *)"ei", -1,
3153 (char_u *)"all", OPT_FREE, SID_NONE);
3154 }
3155}
3156
3157/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003158 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003159 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003160 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003161undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003162{
Bram Moolenaare1004402020-10-24 20:49:43 +02003163 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003164 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003165 p_verbose = cmod->cmod_verbose_save - 1;
3166 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003167 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003168
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003169#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003170 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003171 {
3172 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003173 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003174 }
3175#endif
3176
Bram Moolenaare1004402020-10-24 20:49:43 +02003177 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003178 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003179 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003180 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3181 OPT_FREE, SID_NONE);
3182 free_string_option(cmod->cmod_save_ei);
3183 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003184 }
3185
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003186 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003187
Bram Moolenaare1004402020-10-24 20:49:43 +02003188 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003189 {
3190 // messages could be enabled for a serious error, need to check if the
3191 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003192 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3193 msg_silent = cmod->cmod_save_msg_silent - 1;
3194 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003195 if (emsg_silent < 0)
3196 emsg_silent = 0;
3197 // Restore msg_scroll, it's set by file I/O commands, even when no
3198 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003199 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003200
3201 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3202 // somewhere in the line. Put it back in the first column.
3203 if (redirecting())
3204 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003205
Bram Moolenaare1004402020-10-24 20:49:43 +02003206 cmod->cmod_save_msg_silent = 0;
3207 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003208 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003209}
3210
3211/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003212 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003213 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003214 * Return FAIL and set "errormsg" or return OK.
3215 */
3216 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003217parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003218{
3219 int address_count = 1;
3220 linenr_T lnum;
3221
3222 // Repeat for all ',' or ';' separated addresses.
3223 for (;;)
3224 {
3225 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003226 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003227 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003228 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003229 eap->addr_count == 0, address_count++);
3230 if (eap->cmd == NULL) // error detected
3231 return FAIL;
3232 if (lnum == MAXLNUM)
3233 {
3234 if (*eap->cmd == '%') // '%' - all lines
3235 {
3236 ++eap->cmd;
3237 switch (eap->addr_type)
3238 {
3239 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003240 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003241 eap->line1 = 1;
3242 eap->line2 = curbuf->b_ml.ml_line_count;
3243 break;
3244 case ADDR_LOADED_BUFFERS:
3245 {
3246 buf_T *buf = firstbuf;
3247
3248 while (buf->b_next != NULL
3249 && buf->b_ml.ml_mfp == NULL)
3250 buf = buf->b_next;
3251 eap->line1 = buf->b_fnum;
3252 buf = lastbuf;
3253 while (buf->b_prev != NULL
3254 && buf->b_ml.ml_mfp == NULL)
3255 buf = buf->b_prev;
3256 eap->line2 = buf->b_fnum;
3257 break;
3258 }
3259 case ADDR_BUFFERS:
3260 eap->line1 = firstbuf->b_fnum;
3261 eap->line2 = lastbuf->b_fnum;
3262 break;
3263 case ADDR_WINDOWS:
3264 case ADDR_TABS:
3265 if (IS_USER_CMDIDX(eap->cmdidx))
3266 {
3267 eap->line1 = 1;
3268 eap->line2 = eap->addr_type == ADDR_WINDOWS
3269 ? LAST_WIN_NR : LAST_TAB_NR;
3270 }
3271 else
3272 {
3273 // there is no Vim command which uses '%' and
3274 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003275 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003276 return FAIL;
3277 }
3278 break;
3279 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003280 case ADDR_UNSIGNED:
3281 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003282 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003283 return FAIL;
3284 case ADDR_ARGUMENTS:
3285 if (ARGCOUNT == 0)
3286 eap->line1 = eap->line2 = 0;
3287 else
3288 {
3289 eap->line1 = 1;
3290 eap->line2 = ARGCOUNT;
3291 }
3292 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003293 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003294#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003295 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003296 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003297 if (eap->line2 == 0)
3298 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003299#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003300 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003301 case ADDR_NONE:
3302 // Will give an error later if a range is found.
3303 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003304 }
3305 ++eap->addr_count;
3306 }
3307 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3308 {
3309 pos_T *fp;
3310
3311 // '*' - visual area
3312 if (eap->addr_type != ADDR_LINES)
3313 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003314 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003315 return FAIL;
3316 }
3317
3318 ++eap->cmd;
3319 if (!eap->skip)
3320 {
3321 fp = getmark('<', FALSE);
3322 if (check_mark(fp) == FAIL)
3323 return FAIL;
3324 eap->line1 = fp->lnum;
3325 fp = getmark('>', FALSE);
3326 if (check_mark(fp) == FAIL)
3327 return FAIL;
3328 eap->line2 = fp->lnum;
3329 ++eap->addr_count;
3330 }
3331 }
3332 }
3333 else
3334 eap->line2 = lnum;
3335 eap->addr_count++;
3336
3337 if (*eap->cmd == ';')
3338 {
3339 if (!eap->skip)
3340 {
3341 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003342 // Don't leave the cursor on an illegal line or column, but do
3343 // accept zero as address, so 0;/PATTERN/ works correctly.
3344 if (eap->line2 > 0)
3345 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003346 }
3347 }
3348 else if (*eap->cmd != ',')
3349 break;
3350 ++eap->cmd;
3351 }
3352
3353 // One address given: set start and end lines.
3354 if (eap->addr_count == 1)
3355 {
3356 eap->line1 = eap->line2;
3357 // ... but only implicit: really no address given
3358 if (lnum == MAXLNUM)
3359 eap->addr_count = 0;
3360 }
3361 return OK;
3362}
3363
3364/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003365 * Append "cmd" to the error message in IObuff.
3366 * Takes care of limiting the length and handling 0xa0, which would be
3367 * invisible otherwise.
3368 */
3369 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003370append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003371{
3372 char_u *s = cmd;
3373 char_u *d;
3374
3375 STRCAT(IObuff, ": ");
3376 d = IObuff + STRLEN(IObuff);
3377 while (*s != NUL && d - IObuff < IOSIZE - 7)
3378 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003379 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003380 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003381 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003382 STRCPY(d, "<a0>");
3383 d += 4;
3384 }
3385 else
3386 MB_COPY_CHAR(s, d);
3387 }
3388 *d = NUL;
3389}
3390
3391/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003392 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3393 * the name. Otherwise just return "start".
3394 */
3395 char_u *
3396skip_option_env_lead(char_u *start)
3397{
3398 char_u *name = start;
3399
3400 if (*start == '&')
3401 {
3402 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3403 name += 3;
3404 else
3405 name += 1;
3406 }
3407 else if (*start == '$')
3408 name += 1;
3409 return name;
3410}
3411
3412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003414 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003415 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003416 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 *
3418 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3419 * assignment without "let". Sets eap->cmdidx to the command while returning
3420 * "eap->cmd".
3421 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 * Returns NULL for an ambiguous user command.
3423 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424 char_u *
3425find_ex_command(
3426 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003427 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003428 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003429 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430{
3431 int len;
3432 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003433 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003434#ifndef FEAT_EVAL
3435 int vim9 = FALSE;
3436#else
3437 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 /*
3440 * Recognize a Vim9 script function/method call and assignment:
3441 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3442 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003443 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003444 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003446 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003448 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003449 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003451 int oplen;
3452 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003453 char_u *swp;
3454
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003455 if (*eap->cmd == '&'
3456 || *eap->cmd == '$'
3457 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003458 && (valid_yank_reg(eap->cmd[1], FALSE)
3459 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003460 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003461 if (*eap->cmd == '&')
3462 {
3463 p = eap->cmd + 1;
3464 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3465 p += 2;
3466 p = to_name_end(p, FALSE);
3467 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003468 else if (*eap->cmd == '$')
3469 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003470 else
3471 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003472 if (ends_excmd(*skipwhite(p)))
3473 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003474 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3475 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003476 eap->cmdidx = CMD_eval;
3477 return eap->cmd;
3478 }
3479 // "&option" can be followed by "->" or "=", check below
3480 }
3481
3482 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003483
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003484 if (
3485 // "(..." is an expression.
3486 // "funcname(" is always a function call.
3487 *p == '('
3488 || (p == eap->cmd
3489 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003490 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003491 *eap->cmd == '{'
3492 // "'string'->func()" is an expression.
3493 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003494 // '"string"->func()' is an expression.
3495 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003496 // "g:varname" is an expression.
3497 || eap->cmd[1] == ':'
3498 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003499 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003500 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003501 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003502 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3503 {
3504 // "{" by itself is the start of a block.
3505 eap->cmdidx = CMD_block;
3506 return eap->cmd + 1;
3507 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003508 eap->cmdidx = CMD_eval;
3509 return eap->cmd;
3510 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003511
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003512 if (p != eap->cmd && (
3513 // "varname[]" is an expression.
3514 *p == '['
3515 // "varname.key" is an expression.
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02003516 || (*p == '.' && (ASCII_ISALPHA(p[1])
3517 || p[1] == '_'))))
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003518 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003519 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003520
3521 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003522 // Skip over the whole thing, it can be:
3523 // name.member = val
3524 // name[a : b] = val
3525 // name[idx] = val
3526 // name[idx].member = val
3527 // etc.
3528 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003529 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003530 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003531 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003532 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003533 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003534 || (after[0] == '.' && after[1] == '.'
3535 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003536 eap->cmdidx = CMD_var;
3537 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003538 --emsg_silent;
3539 return eap->cmd;
3540 }
3541
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003542 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3543 // an assignment.
3544 // If there is no line break inside the "[...]" then "p" is
3545 // advanced to after the "]" by to_name_const_end(): check if a "="
3546 // follows.
3547 // If "[...]" has a line break "p" still points at the "[" and it
3548 // can't be an assignment.
3549 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003550 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003551 char_u *eq;
3552
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003553 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003554 if (p == eap->cmd && *p == '[')
3555 {
3556 int count = 0;
3557 int semicolon = FALSE;
3558
3559 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3560 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003561 eq = p;
3562 if (eq != NULL)
3563 {
3564 eq = skipwhite(eq);
3565 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3566 ++eq;
3567 }
3568 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003569 {
3570 eap->cmdidx = CMD_eval;
3571 return eap->cmd;
3572 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003573 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003574 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003575 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003576 return eap->cmd;
3577 }
3578 }
3579
3580 // Recognize an assignment if we recognize the variable name:
3581 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003582 // "@r = expr"
3583 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003584 // "var = expr" where "var" is a variable name or we are skipping
3585 // (variable declaration might have been skipped).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003586 oplen = assignment_len(skipwhite(p), &heredoc);
3587 if (oplen > 0)
3588 {
3589 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3590 || *eap->cmd == '&'
3591 || *eap->cmd == '$'
3592 || *eap->cmd == '@'
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003593 || eap->skip
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003594 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003595 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003596 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003597 return eap->cmd;
3598 }
3599 }
3600
3601 // Recognize using a type for a w:, b:, t: or g: variable:
3602 // "w:varname: number = 123".
3603 if (eap->cmd[1] == ':' && *p == ':')
3604 {
3605 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003606 return eap->cmd;
3607 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003608 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003609
Bram Moolenaar7b829262021-10-13 15:04:34 +01003610 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3611 // an expression. A global/substitute/list command needs to use a
3612 // longer name.
3613 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3614 {
3615 eap->cmdidx = CMD_eval;
3616 return eap->cmd;
3617 }
3618
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003619 // If it is an ID it might be a variable with an operator on the next
3620 // line, if the variable exists it can't be an Ex command.
3621 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003622 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003623 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3624 {
3625 eap->cmdidx = CMD_eval;
3626 return eap->cmd;
3627 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003628
3629 // Check for "++nr" and "--nr".
3630 if (p == eap->cmd && p[0] == p[1] && (*p == '+' || *p == '-'))
3631 {
3632 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3633 return eap->cmd + 2;
3634 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 }
3636#endif
3637
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 /*
3639 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003640 * Exceptions:
Bram Moolenaar10b94212021-02-19 21:42:57 +01003641 * - The 'k' command can directly be followed by any character.
3642 * But it is not used in Vim9 script.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003644 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003646 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 */
3648 p = eap->cmd;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003649 if (!vim9 && *p == 'k')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 {
3651 eap->cmdidx = CMD_k;
3652 ++p;
3653 }
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003654 else if (!vim9
3655 && p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003656 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3657 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 || p[1] == 'g'
3659 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3660 || p[1] == 'I'
3661 || (p[1] == 'r' && p[2] != 'e')))
3662 {
3663 eap->cmdidx = CMD_substitute;
3664 ++p;
3665 }
3666 else
3667 {
3668 while (ASCII_ISALPHA(*p))
3669 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003670 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003671 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003673 while (ASCII_ISALNUM(*p))
3674 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 }
3676 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3677 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003678 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 ++p;
3680 while (ASCII_ISALPHA(*p))
3681 ++p;
3682 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003683
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003684 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003685 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 ++p;
3687 len = (int)(p - eap->cmd);
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003688 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003689 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003690 // Check for ":dl", ":dell", etc. to ":deletel": that's
3691 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003692 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003693 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003694 break;
3695 if (i == len - 1)
3696 {
3697 --len;
3698 if (p[-1] == 'l')
3699 eap->flags |= EXFLAG_LIST;
3700 else
3701 eap->flags |= EXFLAG_PRINT;
3702 }
3703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003705 if (ASCII_ISLOWER(eap->cmd[0]))
3706 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003707 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003708 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003709
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003710 if (command_count != (int)CMD_SIZE)
3711 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003712 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003713 getout(1);
3714 }
3715
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003716 // Use a precomputed index for fast look-up in cmdnames[]
3717 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003718 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3719 if (ASCII_ISLOWER(c2))
3720 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3721 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003722 else if (ASCII_ISUPPER(eap->cmd[0]))
3723 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003725 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726
3727 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3728 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3729 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3730 (size_t)len) == 0)
3731 {
3732#ifdef FEAT_EVAL
3733 if (full != NULL
3734 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3735 *full = TRUE;
3736#endif
3737 break;
3738 }
3739
Bram Moolenaar95388e32020-11-20 21:07:00 +01003740 // Not not recognize ":*" as the star command unless '*' is in
3741 // 'cpoptions'.
3742 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3743 p = eap->cmd;
3744
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003745 // Look for a user defined command as a last resort. Let ":Print" be
3746 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003747 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3748 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003750 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 while (ASCII_ISALNUM(*p))
3752 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003753 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003755 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 eap->cmdidx = CMD_SIZE;
3757 }
3758
Bram Moolenaar373863e2020-09-26 17:20:53 +02003759 // ":fina" means ":finally" for backwards compatibility.
3760 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3761 eap->cmdidx = CMD_finally;
3762
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003763#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003764 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003765 && vim9
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +02003766 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003767 && (eap->cmdidx < 0 ||
3768 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003769 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003770 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3771
3772 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003773 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003774 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003775 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003776#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003777
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 return p;
3779}
3780
3781#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003782static struct cmdmod
3783{
3784 char *name;
3785 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003786 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003787} cmdmods[] = {
3788 {"aboveleft", 3, FALSE},
3789 {"belowright", 3, FALSE},
3790 {"botright", 2, FALSE},
3791 {"browse", 3, FALSE},
3792 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003793 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003794 {"hide", 3, FALSE},
3795 {"keepalt", 5, FALSE},
3796 {"keepjumps", 5, FALSE},
3797 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003798 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003799 {"leftabove", 5, FALSE},
3800 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003801 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003802 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003803 {"rightbelow", 6, FALSE},
3804 {"sandbox", 3, FALSE},
3805 {"silent", 3, FALSE},
3806 {"tab", 3, TRUE},
3807 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003808 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003809 {"verbose", 4, TRUE},
3810 {"vertical", 4, FALSE},
3811};
3812
3813/*
3814 * Return length of a command modifier (including optional count).
3815 * Return zero when it's not a modifier.
3816 */
3817 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003818modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003819{
3820 int i, j;
3821 char_u *p = cmd;
3822
3823 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02003824 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02003825 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003826 {
3827 for (j = 0; p[j] != NUL; ++j)
3828 if (p[j] != cmdmods[i].name[j])
3829 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003830 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003831 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003832 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003833 }
3834 return 0;
3835}
3836
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837/*
3838 * Return > 0 if an Ex command "name" exists.
3839 * Return 2 if there is an exact match.
3840 * Return 3 if there is an ambiguous match.
3841 */
3842 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003843cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844{
3845 exarg_T ea;
3846 int full = FALSE;
3847 int i;
3848 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003849 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003851 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02003852 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 {
3854 for (j = 0; name[j] != NUL; ++j)
3855 if (name[j] != cmdmods[i].name[j])
3856 break;
3857 if (name[j] == NUL && j >= cmdmods[i].minlen)
3858 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3859 }
3860
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003861 // Check built-in commands and user defined commands.
3862 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003863 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003866 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003868 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3869 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003870 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003871 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3873}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003874
3875/*
3876 * "fullcommand" function
3877 */
3878 void
3879f_fullcommand(typval_T *argvars, typval_T *rettv)
3880{
3881 exarg_T ea;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003882 char_u *name;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003883 char_u *p;
3884
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003885 rettv->v_type = VAR_STRING;
3886 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003887
3888 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
3889 return;
3890
3891 name = argvars[0].vval.v_string;
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003892 if (name == NULL)
3893 return;
3894
3895 while (*name != NUL && *name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003896 name++;
3897 name = skip_range(name, TRUE, NULL);
3898
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003899 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
3900 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003901 ea.addr_count = 0;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003902 p = find_ex_command(&ea, NULL, NULL, NULL);
3903 if (p == NULL || ea.cmdidx == CMD_SIZE)
3904 return;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003905 if (in_vim9script())
3906 {
3907 int res;
3908
3909 ++emsg_silent;
3910 res = not_in_vim9(&ea);
3911 --emsg_silent;
3912
3913 if (res == FAIL)
3914 return;
3915 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003916
3917 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02003918 ? get_user_command_name(ea.useridx, ea.cmdidx)
3919 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003920}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921#endif
3922
Bram Moolenaard0190392019-08-23 21:17:35 +02003923 cmdidx_T
3924excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925{
Bram Moolenaard0190392019-08-23 21:17:35 +02003926 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
Bram Moolenaard0190392019-08-23 21:17:35 +02003928 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3929 idx = (cmdidx_T)((int)idx + 1))
3930 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 break;
3932
Bram Moolenaard0190392019-08-23 21:17:35 +02003933 return idx;
3934}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935
Bram Moolenaard0190392019-08-23 21:17:35 +02003936 long
3937excmd_get_argt(cmdidx_T idx)
3938{
3939 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940}
3941
3942/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003943 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 *
3945 * Backslashed delimiters after / or ? will be skipped, and commands will
3946 * not be expanded between /'s and ?'s or after "'".
3947 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003948 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 * Returns the "cmd" pointer advanced to beyond the range.
3950 */
3951 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003952skip_range(
3953 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003954 int skip_star, // skip "*" used for Visual range
3955 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003957 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003959 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003961 if (*cmd == '\\')
3962 {
3963 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3964 ++cmd;
3965 else
3966 break;
3967 }
3968 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 {
3970 if (*++cmd == NUL && ctx != NULL)
3971 *ctx = EXPAND_NOTHING;
3972 }
3973 else if (*cmd == '/' || *cmd == '?')
3974 {
3975 delim = *cmd++;
3976 while (*cmd != NUL && *cmd != delim)
3977 if (*cmd++ == '\\' && *cmd != NUL)
3978 ++cmd;
3979 if (*cmd == NUL && ctx != NULL)
3980 *ctx = EXPAND_NOTHING;
3981 }
3982 if (*cmd != NUL)
3983 ++cmd;
3984 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003985
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003986 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003987 while (*cmd == ':')
3988 cmd = skipwhite(cmd + 1);
3989
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003990 // Skip "*" used for Visual range.
3991 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3992 cmd = skipwhite(cmd + 1);
3993
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 return cmd;
3995}
3996
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003997 static void
3998addr_error(cmd_addr_T addr_type)
3999{
4000 if (addr_type == ADDR_NONE)
4001 emsg(_(e_norange));
4002 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004003 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004004}
4005
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004007 * Return the default address for an address type.
4008 */
4009 static linenr_T
4010default_address(exarg_T *eap)
4011{
4012 linenr_T lnum = 0;
4013
4014 switch (eap->addr_type)
4015 {
4016 case ADDR_LINES:
4017 case ADDR_OTHER:
4018 // Default is the cursor line number. Avoid using an invalid
4019 // line number though.
4020 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4021 lnum = curbuf->b_ml.ml_line_count;
4022 else
4023 lnum = curwin->w_cursor.lnum;
4024 break;
4025 case ADDR_WINDOWS:
4026 lnum = CURRENT_WIN_NR;
4027 break;
4028 case ADDR_ARGUMENTS:
4029 lnum = curwin->w_arg_idx + 1;
4030 if (lnum > ARGCOUNT)
4031 lnum = ARGCOUNT;
4032 break;
4033 case ADDR_LOADED_BUFFERS:
4034 case ADDR_BUFFERS:
4035 lnum = curbuf->b_fnum;
4036 break;
4037 case ADDR_TABS:
4038 lnum = CURRENT_TAB_NR;
4039 break;
4040 case ADDR_TABS_RELATIVE:
4041 case ADDR_UNSIGNED:
4042 lnum = 1;
4043 break;
4044 case ADDR_QUICKFIX:
4045#ifdef FEAT_QUICKFIX
4046 lnum = qf_get_cur_idx(eap);
4047#endif
4048 break;
4049 case ADDR_QUICKFIX_VALID:
4050#ifdef FEAT_QUICKFIX
4051 lnum = qf_get_cur_valid_idx(eap);
4052#endif
4053 break;
4054 case ADDR_NONE:
4055 // Will give an error later if a range is found.
4056 break;
4057 }
4058 return lnum;
4059}
4060
4061/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004062 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 *
4064 * Set ptr to the next character after the part that was interpreted.
4065 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004066 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 *
4068 * Return MAXLNUM when no Ex address was found.
4069 */
4070 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004071get_address(
4072 exarg_T *eap UNUSED,
4073 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004074 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004075 int skip, // only skip the address, don't use it
4076 int silent, // no errors or side effects
4077 int to_other_file, // flag: may jump to other file
4078 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079{
4080 int c;
4081 int i;
4082 long n;
4083 char_u *cmd;
4084 pos_T pos;
4085 pos_T *fp;
4086 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004087 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
4089 cmd = skipwhite(*ptr);
4090 lnum = MAXLNUM;
4091 do
4092 {
4093 switch (*cmd)
4094 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004095 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004096 ++cmd;
4097 switch (addr_type)
4098 {
4099 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004100 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 lnum = curwin->w_cursor.lnum;
4102 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004103 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004104 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004105 break;
4106 case ADDR_ARGUMENTS:
4107 lnum = curwin->w_arg_idx + 1;
4108 break;
4109 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004110 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004111 lnum = curbuf->b_fnum;
4112 break;
4113 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004114 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004115 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004116 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004117 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004118 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004119 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004120 cmd = NULL;
4121 goto error;
4122 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004123 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004124#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004125 lnum = qf_get_cur_idx(eap);
4126#endif
4127 break;
4128 case ADDR_QUICKFIX_VALID:
4129#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004130 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004131#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004132 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004133 }
4134 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004136 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004137 ++cmd;
4138 switch (addr_type)
4139 {
4140 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004141 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 lnum = curbuf->b_ml.ml_line_count;
4143 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004144 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004145 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004146 break;
4147 case ADDR_ARGUMENTS:
4148 lnum = ARGCOUNT;
4149 break;
4150 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004151 buf = lastbuf;
4152 while (buf->b_ml.ml_mfp == NULL)
4153 {
4154 if (buf->b_prev == NULL)
4155 break;
4156 buf = buf->b_prev;
4157 }
4158 lnum = buf->b_fnum;
4159 break;
4160 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004161 lnum = lastbuf->b_fnum;
4162 break;
4163 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004164 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004165 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004166 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004167 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004168 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004169 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004170 cmd = NULL;
4171 goto error;
4172 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004173 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004174#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004175 lnum = qf_get_size(eap);
4176 if (lnum == 0)
4177 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004178#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004179 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004180 case ADDR_QUICKFIX_VALID:
4181#ifdef FEAT_QUICKFIX
4182 lnum = qf_get_valid_size(eap);
4183 if (lnum == 0)
4184 lnum = 1;
4185#endif
4186 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004187 }
4188 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004190 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004191 if (*++cmd == NUL)
4192 {
4193 cmd = NULL;
4194 goto error;
4195 }
4196 if (addr_type != ADDR_LINES)
4197 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004198 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004199 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004200 goto error;
4201 }
4202 if (skip)
4203 ++cmd;
4204 else
4205 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004206 // Only accept a mark in another file when it is
4207 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004208 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4209 ++cmd;
4210 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004211 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004212 lnum = curwin->w_cursor.lnum;
4213 else
4214 {
4215 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 {
4217 cmd = NULL;
4218 goto error;
4219 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004220 lnum = fp->lnum;
4221 }
4222 }
4223 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224
4225 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004226 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004227 c = *cmd++;
4228 if (addr_type != ADDR_LINES)
4229 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004230 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004231 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004232 goto error;
4233 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004234 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004235 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004236 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004237 if (*cmd == c)
4238 ++cmd;
4239 }
4240 else
4241 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004242 int flags;
4243
4244 pos = curwin->w_cursor; // save curwin->w_cursor
4245
4246 // When '/' or '?' follows another address, start from
4247 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004248 if (lnum > 0 && lnum != MAXLNUM)
4249 curwin->w_cursor.lnum =
4250 lnum > curbuf->b_ml.ml_line_count
4251 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004252
4253 // Start a forward search at the end of the line (unless
4254 // before the first line).
4255 // Start a backward search at the start of the line.
4256 // This makes sure we never match in the current
4257 // line, and can match anywhere in the
4258 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004259 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004260 curwin->w_cursor.col = MAXCOL;
4261 else
4262 curwin->w_cursor.col = 0;
4263 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004264 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004265 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004266 {
4267 curwin->w_cursor = pos;
4268 cmd = NULL;
4269 goto error;
4270 }
4271 lnum = curwin->w_cursor.lnum;
4272 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004273 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004274 cmd += searchcmdlen;
4275 }
4276 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004278 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004279 ++cmd;
4280 if (addr_type != ADDR_LINES)
4281 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004282 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004283 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004284 goto error;
4285 }
4286 if (*cmd == '&')
4287 i = RE_SUBST;
4288 else if (*cmd == '?' || *cmd == '/')
4289 i = RE_SEARCH;
4290 else
4291 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004292 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004293 cmd = NULL;
4294 goto error;
4295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004297 if (!skip)
4298 {
4299 /*
4300 * When search follows another address, start from
4301 * there.
4302 */
4303 if (lnum != MAXLNUM)
4304 pos.lnum = lnum;
4305 else
4306 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004308 /*
4309 * Start the search just like for the above
4310 * do_search().
4311 */
4312 if (*cmd != '?')
4313 pos.col = MAXCOL;
4314 else
4315 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004316 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004317 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004318 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004319 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004320 lnum = pos.lnum;
4321 else
4322 {
4323 cmd = NULL;
4324 goto error;
4325 }
4326 }
4327 ++cmd;
4328 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329
4330 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004331 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004332 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 }
4334
4335 for (;;)
4336 {
4337 cmd = skipwhite(cmd);
4338 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4339 break;
4340
4341 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004342 {
4343 switch (addr_type)
4344 {
4345 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004346 case ADDR_OTHER:
4347 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004348 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004349 break;
4350 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004351 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004352 break;
4353 case ADDR_ARGUMENTS:
4354 lnum = curwin->w_arg_idx + 1;
4355 break;
4356 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004357 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004358 lnum = curbuf->b_fnum;
4359 break;
4360 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004361 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004362 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004363 case ADDR_TABS_RELATIVE:
4364 lnum = 1;
4365 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004366 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004367#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004368 lnum = qf_get_cur_idx(eap);
4369#endif
4370 break;
4371 case ADDR_QUICKFIX_VALID:
4372#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004373 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004374#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004375 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004376 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004377 case ADDR_UNSIGNED:
4378 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004379 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004380 }
4381 }
4382
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004384 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 else
4386 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004387 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 n = 1;
4389 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004390 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004392 if (n == MAXLNUM)
4393 {
4394 emsg(_(e_line_number_out_of_range));
4395 goto error;
4396 }
4397 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004398
4399 if (addr_type == ADDR_TABS_RELATIVE)
4400 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004401 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004402 cmd = NULL;
4403 goto error;
4404 }
4405 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004406 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004407 lnum = compute_buffer_local_count(
4408 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004410 {
4411#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004412 // Relative line addressing, need to adjust for folded lines
4413 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004414 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
Bram Moolenaar03725c52021-11-24 12:17:53 +00004415 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004416 (void)hasFolding(lnum, NULL, &lnum);
4417#endif
4418 if (i == '-')
4419 lnum -= n;
4420 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004421 {
4422 if (n >= LONG_MAX - lnum)
4423 {
4424 emsg(_(e_line_number_out_of_range));
4425 goto error;
4426 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004427 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004428 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 }
4431 } while (*cmd == '/' || *cmd == '?');
4432
4433error:
4434 *ptr = cmd;
4435 return lnum;
4436}
4437
4438/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004439 * Set eap->line1 and eap->line2 to the whole range.
4440 * Used for commands with the EX_DFLALL flag and no range given.
4441 */
4442 static void
4443address_default_all(exarg_T *eap)
4444{
4445 eap->line1 = 1;
4446 switch (eap->addr_type)
4447 {
4448 case ADDR_LINES:
4449 case ADDR_OTHER:
4450 eap->line2 = curbuf->b_ml.ml_line_count;
4451 break;
4452 case ADDR_LOADED_BUFFERS:
4453 {
4454 buf_T *buf = firstbuf;
4455
4456 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4457 buf = buf->b_next;
4458 eap->line1 = buf->b_fnum;
4459 buf = lastbuf;
4460 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4461 buf = buf->b_prev;
4462 eap->line2 = buf->b_fnum;
4463 }
4464 break;
4465 case ADDR_BUFFERS:
4466 eap->line1 = firstbuf->b_fnum;
4467 eap->line2 = lastbuf->b_fnum;
4468 break;
4469 case ADDR_WINDOWS:
4470 eap->line2 = LAST_WIN_NR;
4471 break;
4472 case ADDR_TABS:
4473 eap->line2 = LAST_TAB_NR;
4474 break;
4475 case ADDR_TABS_RELATIVE:
4476 eap->line2 = 1;
4477 break;
4478 case ADDR_ARGUMENTS:
4479 if (ARGCOUNT == 0)
4480 eap->line1 = eap->line2 = 0;
4481 else
4482 eap->line2 = ARGCOUNT;
4483 break;
4484 case ADDR_QUICKFIX_VALID:
4485#ifdef FEAT_QUICKFIX
4486 eap->line2 = qf_get_valid_size(eap);
4487 if (eap->line2 == 0)
4488 eap->line2 = 1;
4489#endif
4490 break;
4491 case ADDR_NONE:
4492 case ADDR_UNSIGNED:
4493 case ADDR_QUICKFIX:
4494 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4495 break;
4496 }
4497}
4498
4499
4500/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004501 * Get flags from an Ex command argument.
4502 */
4503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004504get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004505{
4506 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4507 {
4508 if (*eap->arg == 'l')
4509 eap->flags |= EXFLAG_LIST;
4510 else if (*eap->arg == 'p')
4511 eap->flags |= EXFLAG_PRINT;
4512 else
4513 eap->flags |= EXFLAG_NR;
4514 eap->arg = skipwhite(eap->arg + 1);
4515 }
4516}
4517
4518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 * Function called for command which is Not Implemented. NI!
4520 */
4521 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004522ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523{
4524 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004525 eap->errmsg =
4526 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527}
4528
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004529#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530/*
4531 * Function called for script command which is Not Implemented. NI!
4532 * Skips over ":perl <<EOF" constructs.
4533 */
4534 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004535ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536{
4537 if (!eap->skip)
4538 ex_ni(eap);
4539 else
4540 vim_free(script_get(eap, eap->arg));
4541}
4542#endif
4543
4544/*
4545 * Check range in Ex command for validity.
4546 * Return NULL when valid, error message when invalid.
4547 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004548 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004549invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004551 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004552
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 if ( eap->line1 < 0
4554 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004555 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004556 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004557
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004558 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004559 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004560 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004561 {
4562 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004563 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004564#ifdef FEAT_DIFF
4565 + (eap->cmdidx == CMD_diffget)
4566#endif
4567 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004568 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004569 break;
4570 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004571 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004572 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004573 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004574 break;
4575 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004576 // Only a boundary check, not whether the buffers actually
4577 // exist.
4578 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004579 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004580 break;
4581 case ADDR_LOADED_BUFFERS:
4582 buf = firstbuf;
4583 while (buf->b_ml.ml_mfp == NULL)
4584 {
4585 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004586 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004587 buf = buf->b_next;
4588 }
4589 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004590 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004591 buf = lastbuf;
4592 while (buf->b_ml.ml_mfp == NULL)
4593 {
4594 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004595 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004596 buf = buf->b_prev;
4597 }
4598 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004599 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004600 break;
4601 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004602 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004603 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004604 break;
4605 case ADDR_TABS:
4606 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004607 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004608 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004609 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004610 case ADDR_OTHER:
4611 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004612 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004613 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004614#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004615 // No error for value that is too big, will use the last entry.
4616 if (eap->line2 <= 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004617 return _(e_invalid_range);
Bram Moolenaare906c502015-09-09 21:10:39 +02004618#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004619 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004620 case ADDR_QUICKFIX_VALID:
4621#ifdef FEAT_QUICKFIX
4622 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4623 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004624 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004625#endif
4626 break;
4627 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004628 case ADDR_NONE:
4629 // Will give an error elsewhere.
4630 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004631 }
4632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 return NULL;
4634}
4635
4636/*
4637 * Correct the range for zero line number, if required.
4638 */
4639 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004640correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004642 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 {
4644 if (eap->line1 == 0)
4645 eap->line1 = 1;
4646 if (eap->line2 == 0)
4647 eap->line2 = 1;
4648 }
4649}
4650
Bram Moolenaar748bf032005-02-02 23:04:36 +00004651#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004652/*
4653 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4654 * pattern. Otherwise return eap->arg.
4655 */
4656 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004657skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004658{
4659 char_u *p = eap->arg;
4660
Bram Moolenaara37420f2006-02-04 22:37:47 +00004661 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4662 || eap->cmdidx == CMD_vimgrepadd
4663 || eap->cmdidx == CMD_lvimgrepadd
4664 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004665 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004666 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004667 if (p == NULL)
4668 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004669 }
4670 return p;
4671}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004672
4673/*
4674 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4675 * in the command line, so that things like % get expanded.
4676 */
4677 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004678replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004679{
4680 char_u *new_cmdline;
4681 char_u *program;
4682 char_u *pos;
4683 char_u *ptr;
4684 int len;
4685 int i;
4686
4687 /*
4688 * Don't do it when ":vimgrep" is used for ":grep".
4689 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004690 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4691 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4692 || eap->cmdidx == CMD_grepadd
4693 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004694 && !grep_internal(eap->cmdidx))
4695 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004696 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4697 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004698 {
4699 if (*curbuf->b_p_gp == NUL)
4700 program = p_gp;
4701 else
4702 program = curbuf->b_p_gp;
4703 }
4704 else
4705 {
4706 if (*curbuf->b_p_mp == NUL)
4707 program = p_mp;
4708 else
4709 program = curbuf->b_p_mp;
4710 }
4711
4712 p = skipwhite(p);
4713
4714 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4715 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004716 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004717 i = 1;
4718 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4719 ++i;
4720 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004721 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004722 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004723 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004724 ptr = new_cmdline;
4725 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4726 {
4727 i = (int)(pos - program);
4728 STRNCPY(ptr, program, i);
4729 STRCPY(ptr += i, p);
4730 ptr += len;
4731 program = pos + 2;
4732 }
4733 STRCPY(ptr, program);
4734 }
4735 else
4736 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004737 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004738 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004739 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004740 STRCPY(new_cmdline, program);
4741 STRCAT(new_cmdline, " ");
4742 STRCAT(new_cmdline, p);
4743 }
4744 msg_make(p);
4745
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004746 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004747 vim_free(*cmdlinep);
4748 *cmdlinep = new_cmdline;
4749 p = new_cmdline;
4750 }
4751 return p;
4752}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004753#endif
4754
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755/*
4756 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004757 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 * Return FAIL for failure, OK otherwise.
4759 */
4760 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004761expand_filename(
4762 exarg_T *eap,
4763 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004764 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004766 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 char_u *repl;
4768 int srclen;
4769 char_u *p;
4770 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004771 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772
Bram Moolenaar748bf032005-02-02 23:04:36 +00004773#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004774 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004775 p = skip_grep_pat(eap);
4776#else
4777 p = eap->arg;
4778#endif
4779
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 /*
4781 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4782 * the file name contains a wildcard it should not cause expanding.
4783 * (it will be expanded anyway if there is a wildcard before replacing).
4784 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004785 has_wildcards = mch_has_wildcard(p);
4786 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004788#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004789 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004790 if (p[0] == '`' && p[1] == '=')
4791 {
4792 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004793 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004794 if (*p == '`')
4795 ++p;
4796 continue;
4797 }
4798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 /*
4800 * Quick check if this cannot be the start of a special string.
4801 * Also removes backslash before '%', '#' and '<'.
4802 */
4803 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4804 {
4805 ++p;
4806 continue;
4807 }
4808
4809 /*
4810 * Try to find a match at this position.
4811 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004812 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4813 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004814 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004816 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 {
4818 p += srclen;
4819 continue;
4820 }
4821
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004822 // Wildcards won't be expanded below, the replacement is taken
4823 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004824 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4825 {
4826 char_u *l = repl;
4827
4828 repl = expand_env_save(repl);
4829 vim_free(l);
4830 }
4831
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004832 // Need to escape white space et al. with a backslash.
4833 // Don't do this for:
4834 // - replacement that already has been escaped: "##"
4835 // - shell commands (may have to use quotes instead).
4836 // - non-unix systems when there is a single argument (spaces don't
4837 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004839 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 && eap->cmdidx != CMD_grep
4842 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004843 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004844 && eap->cmdidx != CMD_lgrep
4845 && eap->cmdidx != CMD_lgrepadd
4846 && eap->cmdidx != CMD_lmake
4847 && eap->cmdidx != CMD_make
4848 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004850 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851#endif
4852 )
4853 {
4854 char_u *l;
4855#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004856 // Don't escape a backslash here, because rem_backslash() doesn't
4857 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 static char_u *nobslash = (char_u *)" \t\"|";
4859# define ESCAPE_CHARS nobslash
4860#else
4861# define ESCAPE_CHARS escape_chars
4862#endif
4863
4864 for (l = repl; *l; ++l)
4865 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4866 {
4867 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4868 if (l != NULL)
4869 {
4870 vim_free(repl);
4871 repl = l;
4872 }
4873 break;
4874 }
4875 }
4876
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004877 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004878 if ((eap->usefilter || eap->cmdidx == CMD_bang
4879 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004880 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 {
4882 char_u *l;
4883
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004884 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 if (l != NULL)
4886 {
4887 vim_free(repl);
4888 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 }
4890 }
4891
4892 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4893 vim_free(repl);
4894 if (p == NULL)
4895 return FAIL;
4896 }
4897
4898 /*
4899 * One file argument: Expand wildcards.
4900 * Don't do this with ":r !command" or ":w !command".
4901 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004902 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 {
4904 /*
4905 * May do this twice:
4906 * 1. Replace environment variables.
4907 * 2. Replace any other wildcards, remove backslashes.
4908 */
4909 for (n = 1; n <= 2; ++n)
4910 {
4911 if (n == 2)
4912 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 /*
4914 * Halve the number of backslashes (this is Vi compatible).
4915 * For Unix and OS/2, when wildcards are expanded, this is
4916 * done by ExpandOne() below.
4917 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004918#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 if (!has_wildcards)
4920#endif
4921 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
4923
4924 if (has_wildcards)
4925 {
4926 if (n == 1)
4927 {
4928 /*
4929 * First loop: May expand environment variables. This
4930 * can be done much faster with expand_env() than with
4931 * something else (e.g., calling a shell).
4932 * After expanding environment variables, check again
4933 * if there are still wildcards present.
4934 */
4935 if (vim_strchr(eap->arg, '$') != NULL
4936 || vim_strchr(eap->arg, '~') != NULL)
4937 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004938 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004939 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 has_wildcards = mch_has_wildcard(NameBuff);
4941 p = NameBuff;
4942 }
4943 else
4944 p = NULL;
4945 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004946 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 {
4948 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004949 int options = WILD_LIST_NOTFOUND
4950 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951
4952 ExpandInit(&xpc);
4953 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004954 if (p_wic)
4955 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004957 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 if (p == NULL)
4959 return FAIL;
4960 }
4961 if (p != NULL)
4962 {
4963 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4964 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004965 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 vim_free(p);
4967 }
4968 }
4969 }
4970 }
4971 return OK;
4972}
4973
4974/*
4975 * Replace part of the command line, keeping eap->cmd, eap->arg and
4976 * eap->nextcmd correct.
4977 * "src" points to the part that is to be replaced, of length "srclen".
4978 * "repl" is the replacement string.
4979 * Returns a pointer to the character after the replaced string.
4980 * Returns NULL for failure.
4981 */
4982 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004983repl_cmdline(
4984 exarg_T *eap,
4985 char_u *src,
4986 int srclen,
4987 char_u *repl,
4988 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989{
4990 int len;
4991 int i;
4992 char_u *new_cmdline;
4993
4994 /*
4995 * The new command line is build in new_cmdline[].
4996 * First allocate it.
4997 * Careful: a "+cmd" argument may have been NUL terminated.
4998 */
4999 len = (int)STRLEN(repl);
5000 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005001 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005002 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005003 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005004 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005
5006 /*
5007 * Copy the stuff before the expanded part.
5008 * Copy the expanded stuff.
5009 * Copy what came after the expanded part.
5010 * Copy the next commands, if there are any.
5011 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005012 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005014
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005016 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005018 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005020 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 {
5022 i = (int)STRLEN(new_cmdline) + 1;
5023 STRCPY(new_cmdline + i, eap->nextcmd);
5024 eap->nextcmd = new_cmdline + i;
5025 }
5026 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5027 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5028 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5029 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5030 vim_free(*cmdlinep);
5031 *cmdlinep = new_cmdline;
5032
5033 return src;
5034}
5035
5036/*
5037 * Check for '|' to separate commands and '"' to start comments.
5038 */
5039 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005040separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041{
5042 char_u *p;
5043
Bram Moolenaar86b68352004-12-27 21:59:20 +00005044#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005045 p = skip_grep_pat(eap);
5046#else
5047 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005048#endif
5049
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005050 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 {
5052 if (*p == Ctrl_V)
5053 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005054 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005055 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005057 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005058 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005059 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 break;
5061 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005062
5063#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005064 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005065 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005066 {
5067 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005068 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005069 if (*p == NUL) // stop at NUL after CTRL-V
5070 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005071 }
5072#endif
5073
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005074 // Check for '"': start of comment or '|': next command
5075 // :@" and :*" do not start a comment!
5076 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005077 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02005078#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005079 && !in_vim9script()
5080#endif
5081 && !(eap->argt & EX_NOTRLCOM)
5082 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5083 || p != eap->arg)
5084 && (eap->cmdidx != CMD_redir
5085 || p != eap->arg + 1 || p[-1] != '@'))
5086#ifdef FEAT_EVAL
5087 || (*p == '#'
5088 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005089 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005090 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02005091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 || *p == '|' || *p == '\n')
5093 {
5094 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005095 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 * AND 'b' is present in 'cpoptions'.
5097 */
5098 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005099 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005101 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 --p;
5103 }
5104 else
5105 {
5106 eap->nextcmd = check_nextcmd(p);
5107 *p = NUL;
5108 break;
5109 }
5110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005112
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005113 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 del_trailing_spaces(eap->arg);
5115}
5116
5117/*
5118 * get + command from ex argument
5119 */
5120 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005121getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122{
5123 char_u *arg = *argp;
5124 char_u *command = NULL;
5125
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005126 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 {
5128 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005129 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 command = dollar_command;
5131 else
5132 {
5133 command = arg;
5134 arg = skip_cmd_arg(command, TRUE);
5135 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005136 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 }
5138
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005139 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 *argp = arg;
5141 }
5142 return command;
5143}
5144
5145/*
5146 * Find end of "+command" argument. Skip over "\ " and "\\".
5147 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005148 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005149skip_cmd_arg(
5150 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005151 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152{
5153 while (*p && !vim_isspace(*p))
5154 {
5155 if (*p == '\\' && p[1] != NUL)
5156 {
5157 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005158 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 else
5160 ++p;
5161 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005162 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 }
5164 return p;
5165}
5166
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005167 int
5168get_bad_opt(char_u *p, exarg_T *eap)
5169{
5170 if (STRICMP(p, "keep") == 0)
5171 eap->bad_char = BAD_KEEP;
5172 else if (STRICMP(p, "drop") == 0)
5173 eap->bad_char = BAD_DROP;
5174 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5175 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005176 else
5177 return FAIL;
5178 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005179}
5180
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181/*
5182 * Get "++opt=arg" argument.
5183 * Return FAIL or OK.
5184 */
5185 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005186getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187{
5188 char_u *arg = eap->arg + 2;
5189 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005190 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005193 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5195 {
5196 if (*arg == 'n')
5197 {
5198 arg += 2;
5199 eap->force_bin = FORCE_NOBIN;
5200 }
5201 else
5202 eap->force_bin = FORCE_BIN;
5203 if (!checkforcmd(&arg, "binary", 3))
5204 return FAIL;
5205 eap->arg = skipwhite(arg);
5206 return OK;
5207 }
5208
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005209 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005210 if (STRNCMP(arg, "edit", 4) == 0)
5211 {
5212 eap->read_edit = TRUE;
5213 eap->arg = skipwhite(arg + 4);
5214 return OK;
5215 }
5216
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 if (STRNCMP(arg, "ff", 2) == 0)
5218 {
5219 arg += 2;
5220 pp = &eap->force_ff;
5221 }
5222 else if (STRNCMP(arg, "fileformat", 10) == 0)
5223 {
5224 arg += 10;
5225 pp = &eap->force_ff;
5226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 else if (STRNCMP(arg, "enc", 3) == 0)
5228 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005229 if (STRNCMP(arg, "encoding", 8) == 0)
5230 arg += 8;
5231 else
5232 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 pp = &eap->force_enc;
5234 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005235 else if (STRNCMP(arg, "bad", 3) == 0)
5236 {
5237 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005238 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240
5241 if (pp == NULL || *arg != '=')
5242 return FAIL;
5243
5244 ++arg;
5245 *pp = (int)(arg - eap->cmd);
5246 arg = skip_cmd_arg(arg, FALSE);
5247 eap->arg = skipwhite(arg);
5248 *arg = NUL;
5249
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 if (pp == &eap->force_ff)
5251 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5253 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005254 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005256 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005258 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5260 *p = TOLOWER_ASC(*p);
5261 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005262 else
5263 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005264 // Check ++bad= argument. Must be a single-byte character, "keep" or
5265 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005266 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005267 return FAIL;
5268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269
5270 return OK;
5271}
5272
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005274ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275{
5276 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005277 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 * directory for security reasons.
5279 */
5280 if (secure)
5281 {
5282 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005283 eap->errmsg =
5284 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 }
5286 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005287 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 else
5289 do_augroup(eap->arg, eap->forceit);
5290}
5291
5292/*
5293 * ":doautocmd": Apply the automatic commands to the current buffer.
5294 */
5295 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005296ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005298 char_u *arg = eap->arg;
5299 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005300 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005301
Bram Moolenaar1610d052016-06-09 22:53:01 +02005302 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005303 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005304 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005305 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308/*
5309 * :[N]bunload[!] [N] [bufname] unload buffer
5310 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5311 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5312 */
5313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005314ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005316 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005317 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 eap->errmsg = do_bufdel(
5319 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5320 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5321 : DOBUF_UNLOAD, eap->arg,
5322 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5323}
5324
5325/*
5326 * :[N]buffer [N] to buffer N
5327 * :[N]sbuffer [N] to buffer N
5328 */
5329 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005330ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005332 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005333 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005335 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 else
5337 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005338 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5340 else
5341 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005342 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005343 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 }
5345}
5346
5347/*
5348 * :[N]bmodified [N] to next mod. buffer
5349 * :[N]sbmodified [N] to next mod. buffer
5350 */
5351 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005352ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353{
5354 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005355 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005356 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357}
5358
5359/*
5360 * :[N]bnext [N] to next buffer
5361 * :[N]sbnext [N] split and to next buffer
5362 */
5363 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005364ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005366 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005367 return;
5368
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005370 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005371 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372}
5373
5374/*
5375 * :[N]bNext [N] to previous buffer
5376 * :[N]bprevious [N] to previous buffer
5377 * :[N]sbNext [N] split and to previous buffer
5378 * :[N]sbprevious [N] split and to previous buffer
5379 */
5380 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005381ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005383 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005384 return;
5385
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005387 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005388 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389}
5390
5391/*
5392 * :brewind to first buffer
5393 * :bfirst to first buffer
5394 * :sbrewind split and to first buffer
5395 * :sbfirst split and to first buffer
5396 */
5397 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005398ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005400 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005401 return;
5402
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005404 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005405 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406}
5407
5408/*
5409 * :blast to last buffer
5410 * :sblast split and to last buffer
5411 */
5412 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005413ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005415 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005416 return;
5417
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005419 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005420 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422
Bram Moolenaar7a092242020-04-16 22:10:49 +02005423/*
5424 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005425 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005428ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005430 int comment_char = '"';
5431
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005432#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005433 if (in_vim9script())
5434 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005435#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005436 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005437}
5438
5439/*
5440 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5441 * to "cmd_start" or has a white space character before it.
5442 */
5443 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005444ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005445{
5446 int c = *cmd;
5447
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005448 if (c == NUL || c == '|' || c == '\n')
5449 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005450#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005451 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005452 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5453 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005454 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005455#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005456 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457}
5458
5459#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5460 || defined(PROTO)
5461/*
5462 * Return the next command, after the first '|' or '\n'.
5463 * Return NULL if not found.
5464 */
5465 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005466find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467{
5468 while (*p != '|' && *p != '\n')
5469 {
5470 if (*p == NUL)
5471 return NULL;
5472 ++p;
5473 }
5474 return (p + 1);
5475}
5476#endif
5477
5478/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005479 * Check if *p is a separator between Ex commands, skipping over white space.
5480 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 */
5482 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005483check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005485 char_u *s = skipwhite(p);
5486
5487 if (*s == '|' || *s == '\n')
5488 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 else
5490 return NULL;
5491}
5492
5493/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005494 * If "eap->nextcmd" is not set, check for a next command at "p".
5495 */
5496 void
5497set_nextcmd(exarg_T *eap, char_u *arg)
5498{
5499 char_u *p = check_nextcmd(arg);
5500
5501 if (eap->nextcmd == NULL)
5502 eap->nextcmd = p;
5503 else if (p != NULL)
5504 // cannot use "| command" inside a {} block
5505 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5506}
5507
5508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 * - if there are more files to edit
5510 * - and this is the last window
5511 * - and forceit not used
5512 * - and not repeated twice on a row
5513 * return FAIL and give error message if 'message' TRUE
5514 * return OK otherwise
5515 */
5516 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005517check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005518 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005519 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520{
5521 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5522
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005523 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005524 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 {
5526 if (message)
5527 {
5528#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005529 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5530 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005532 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005534 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5535 NGETTEXT("%d more file to edit. Quit anyway?",
5536 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5538 return OK;
5539 return FAIL;
5540 }
5541#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005542 semsg(NGETTEXT("E173: %d more file to edit",
5543 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005544 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 }
5546 return FAIL;
5547 }
5548 return OK;
5549}
5550
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551/*
5552 * Function given to ExpandGeneric() to obtain the list of command names.
5553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005555get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556{
5557 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005558 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 return cmdnames[idx].cmd_name;
5560}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005563ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564{
Bram Moolenaare6850792010-05-14 15:28:44 +02005565 if (*eap->arg == NUL)
5566 {
5567#ifdef FEAT_EVAL
5568 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5569 char_u *p = NULL;
5570
5571 if (expr != NULL)
5572 {
5573 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005574 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005575 --emsg_off;
5576 vim_free(expr);
5577 }
5578 if (p != NULL)
5579 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005580 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005581 vim_free(p);
5582 }
5583 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005584 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005585#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005586 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005587#endif
5588 }
5589 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005590 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005591
5592#ifdef FEAT_VTP
5593 else if (has_vtp_working())
5594 {
5595 // background color change requires clear + redraw
5596 update_screen(CLEAR);
5597 redrawcmd();
5598 }
5599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600}
5601
5602 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005603ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604{
5605 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005606 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 do_highlight(eap->arg, eap->forceit, FALSE);
5608}
5609
5610
5611/*
5612 * Call this function if we thought we were going to exit, but we won't
5613 * (because of an error). May need to restore the terminal mode.
5614 */
5615 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005616not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617{
5618 exiting = FALSE;
5619 settmode(TMODE_RAW);
5620}
5621
Bram Moolenaar3552e742021-05-29 12:21:58 +02005622 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005623before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5624{
5625 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5626
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005627 // Bail out when autocommands closed the window.
5628 // Refuse to quit when the buffer in the last window is being closed (can
5629 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005630 if (!win_valid(wp)
5631 || curbuf_locked()
5632 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5633 return TRUE;
5634
5635 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5636 {
5637 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005638 // Refuse to quit when locked or when the window was closed or the
5639 // buffer in the last window is being closed (can only happen in
5640 // autocommands).
5641 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005642 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5643 return TRUE;
5644 }
5645
5646 return FALSE;
5647}
5648
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005650 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005651 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005652 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005654 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005655ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005657 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005658
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659#ifdef FEAT_CMDWIN
5660 if (cmdwin_type != 0)
5661 {
5662 cmdwin_result = Ctrl_C;
5663 return;
5664 }
5665#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005666 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005667 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005668 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005669 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005670 return;
5671 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005672 if (eap->addr_count > 0)
5673 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005674 int wnr = eap->line2;
5675
5676 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5677 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005678 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005679 }
5680 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005681 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005682
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005683 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005684 if (curbuf_locked())
5685 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005686
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005687 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005688 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005689 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690
5691#ifdef FEAT_NETBEANS_INTG
5692 netbeansForcedQuit = eap->forceit;
5693#endif
5694
5695 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02005696 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 */
5698 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5699 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005700 if ((!buf_hide(wp->w_buffer)
5701 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005702 | (eap->forceit ? CCGD_FORCEIT : 0)
5703 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005705 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 {
5707 not_exiting();
5708 }
5709 else
5710 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005711 // quit last window
5712 // Note: only_one_window() returns true, even so a help window is
5713 // still open. In that case only quit, if no address has been
5714 // specified. Example:
5715 // :h|wincmd w|1q - don't quit
5716 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005717 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005719 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005720#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005722#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005723 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005724 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 }
5726}
5727
5728/*
5729 * ":cquit".
5730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005732ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005734 // this does not always pass on the exit code to the Manx compiler. why?
5735 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736}
5737
5738/*
5739 * ":qall": try to quit all windows
5740 */
5741 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005742ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743{
5744# ifdef FEAT_CMDWIN
5745 if (cmdwin_type != 0)
5746 {
5747 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005748 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 else
5750 cmdwin_result = K_XF2;
5751 return;
5752 }
5753# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005754
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005755 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005756 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005757 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005758 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005759 return;
5760 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005761
5762 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005763 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005764
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005766 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 getout(0);
5768 not_exiting();
5769}
5770
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771/*
5772 * ":close": close current window, unless it is the last one
5773 */
5774 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005775ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005777 win_T *win;
5778 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005779#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005781 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005783#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005784 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005785 {
5786 if (eap->addr_count == 0)
5787 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005788 else
5789 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005790 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005791 {
5792 winnr++;
5793 if (winnr == eap->line2)
5794 break;
5795 }
5796 if (win == NULL)
5797 win = lastwin;
5798 ex_win_close(eap->forceit, win, NULL);
5799 }
5800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801}
5802
Bram Moolenaar4033c552017-09-16 20:54:51 +02005803#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005804/*
5805 * ":pclose": Close any preview window.
5806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005808ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005809{
5810 win_T *win;
5811
Bram Moolenaar79648732019-07-18 21:43:07 +02005812 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005813 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005814 if (win->w_p_pvw)
5815 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005816 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005817 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005818 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005819# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005820 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005821 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005822# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005823}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005824#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005825
Bram Moolenaarf740b292006-02-16 22:11:02 +00005826/*
5827 * Close window "win" and take care of handling closing the last window for a
5828 * modified buffer.
5829 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005830 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005831ex_win_close(
5832 int forceit,
5833 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005834 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835{
5836 int need_hide;
5837 buf_T *buf = win->w_buffer;
5838
Bram Moolenaarcf844172020-06-26 19:44:06 +02005839 // Never close the autocommand window.
5840 if (win == aucmd_win)
5841 {
5842 emsg(_(e_autocmd_close));
5843 return;
5844 }
5845
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005847 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005849#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005850 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005852 bufref_T bufref;
5853
5854 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005856 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 return;
5858 need_hide = FALSE;
5859 }
5860 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005863 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 return;
5865 }
5866 }
5867
Bram Moolenaar4033c552017-09-16 20:54:51 +02005868#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005870#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005871
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005872 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005873 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005874 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005875 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005876 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877}
5878
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005880 * Handle the argument for a tabpage related ex command.
5881 * Returns a tabpage number.
5882 * When an error is encountered then eap->errmsg is set.
5883 */
5884 static int
5885get_tabpage_arg(exarg_T *eap)
5886{
5887 int tab_number;
5888 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5889
5890 if (eap->arg && *eap->arg != NUL)
5891 {
5892 char_u *p = eap->arg;
5893 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005894 int relative = 0; // argument +N/-N means: go to N places to the
5895 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005896
5897 if (*p == '-')
5898 {
5899 relative = -1;
5900 p++;
5901 }
5902 else if (*p == '+')
5903 {
5904 relative = 1;
5905 p++;
5906 }
5907
5908 p_save = p;
5909 tab_number = getdigits(&p);
5910
5911 if (relative == 0)
5912 {
5913 if (STRCMP(p, "$") == 0)
5914 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005915 else if (STRCMP(p, "#") == 0)
5916 if (valid_tabpage(lastused_tabpage))
5917 tab_number = tabpage_index(lastused_tabpage);
5918 else
5919 {
5920 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5921 tab_number = 0;
5922 goto theend;
5923 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005924 else if (p == p_save || *p_save == '-' || *p != NUL
5925 || tab_number > LAST_TAB_NR)
5926 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005927 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005928 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005929 goto theend;
5930 }
5931 }
5932 else
5933 {
5934 if (*p_save == NUL)
5935 tab_number = 1;
5936 else if (p == p_save || *p_save == '-' || *p != NUL
5937 || tab_number == 0)
5938 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005939 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005940 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005941 goto theend;
5942 }
5943 tab_number = tab_number * relative + tabpage_index(curtab);
5944 if (!unaccept_arg0 && relative == -1)
5945 --tab_number;
5946 }
5947 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005948 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005949 }
5950 else if (eap->addr_count > 0)
5951 {
5952 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005953 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005954 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005955 tab_number = 0;
5956 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005957 else
5958 {
5959 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005960 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005961 {
5962 --tab_number;
5963 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005964 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01005965 }
5966 }
5967 }
5968 else
5969 {
5970 switch (eap->cmdidx)
5971 {
5972 case CMD_tabnext:
5973 tab_number = tabpage_index(curtab) + 1;
5974 if (tab_number > LAST_TAB_NR)
5975 tab_number = 1;
5976 break;
5977 case CMD_tabmove:
5978 tab_number = LAST_TAB_NR;
5979 break;
5980 default:
5981 tab_number = tabpage_index(curtab);
5982 }
5983 }
5984
5985theend:
5986 return tab_number;
5987}
5988
5989/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005990 * ":tabclose": close current tab page, unless it is the last one.
5991 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 */
5993 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005994ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005996 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005997 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005998
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005999# ifdef FEAT_CMDWIN
6000 if (cmdwin_type != 0)
6001 cmdwin_result = K_IGNORE;
6002 else
6003# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006004 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006005 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006006 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006008 tab_number = get_tabpage_arg(eap);
6009 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006010 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006011 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006012 if (tp == NULL)
6013 {
6014 beep_flush();
6015 return;
6016 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006017 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006018 {
6019 tabpage_close_other(tp, eap->forceit);
6020 return;
6021 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006022 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006023 tabpage_close(eap->forceit);
6024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006026}
6027
6028/*
6029 * ":tabonly": close all tab pages except the current one
6030 */
6031 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006032ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006033{
6034 tabpage_T *tp;
6035 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006036 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006037
6038# ifdef FEAT_CMDWIN
6039 if (cmdwin_type != 0)
6040 cmdwin_result = K_IGNORE;
6041 else
6042# endif
6043 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006044 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006045 else
6046 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006047 tab_number = get_tabpage_arg(eap);
6048 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006049 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006050 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006051 // Repeat this up to a 1000 times, because autocommands may
6052 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006053 for (done = 0; done < 1000; ++done)
6054 {
6055 FOR_ALL_TABPAGES(tp)
6056 if (tp->tp_topframe != topframe)
6057 {
6058 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006059 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006060 if (valid_tabpage(tp))
6061 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006062 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006063 break;
6064 }
6065 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006066 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006067 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006068 }
6069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071
6072/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006073 * Close the current tab page.
6074 */
6075 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006076tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006077{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006078 // First close all the windows but the current one. If that worked then
6079 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006080 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006081 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006082 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006083 ex_win_close(forceit, curwin, NULL);
6084# ifdef FEAT_GUI
6085 need_mouse_correct = TRUE;
6086# endif
6087}
6088
6089/*
6090 * Close tab page "tp", which is not the current tab page.
6091 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006092 * Also takes care of the tab pages line disappearing when closing the
6093 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006094 */
6095 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006096tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006097{
6098 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006099 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006100 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006101
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006102 // Limit to 1000 windows, autocommands may add a window while we close
6103 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006104 while (++done < 1000)
6105 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006106 wp = tp->tp_firstwin;
6107 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006108
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006109 // Autocommands may delete the tab page under our fingers and we may
6110 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006111 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006112 break;
6113 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006114
Bram Moolenaar29323592016-07-24 22:04:11 +02006115 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006116
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006117 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006118 if (h != tabline_height())
6119 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006120}
6121
6122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 * ":only".
6124 */
6125 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006126ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006128 win_T *wp;
6129 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130# ifdef FEAT_GUI
6131 need_mouse_correct = TRUE;
6132# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006133 if (eap->addr_count > 0)
6134 {
6135 wnr = eap->line2;
6136 for (wp = firstwin; --wnr > 0; )
6137 {
6138 if (wp->w_next == NULL)
6139 break;
6140 else
6141 wp = wp->w_next;
6142 }
6143 win_goto(wp);
6144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 close_others(TRUE, eap->forceit);
6146}
6147
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006149ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006151 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01006152 if (!eap->skip)
6153 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006154#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006155 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006156#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006157 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006158 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01006159 else
6160 {
6161 int winnr = 0;
6162 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006163
Bram Moolenaar2256c992016-11-15 21:17:07 +01006164 FOR_ALL_WINDOWS(win)
6165 {
6166 winnr++;
6167 if (winnr == eap->line2)
6168 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006169 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006170 if (win == NULL)
6171 win = lastwin;
6172 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 }
6175}
6176
6177/*
6178 * ":stop" and ":suspend": Suspend Vim.
6179 */
6180 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006181ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182{
6183 /*
6184 * Disallow suspending for "rvim".
6185 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006186 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 {
6188 if (!eap->forceit)
6189 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01006190 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 windgoto((int)Rows - 1, 0);
6192 out_char('\n');
6193 out_flush();
6194 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006195 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006197 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006199 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200#ifdef FEAT_TITLE
6201 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006202 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203#endif
6204 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006205 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006207 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006208 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 }
6210}
6211
6212/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006213 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 */
6215 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006216ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006218#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006219 if (not_in_vim9(eap) == FAIL)
6220 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222#ifdef FEAT_CMDWIN
6223 if (cmdwin_type != 0)
6224 {
6225 cmdwin_result = Ctrl_C;
6226 return;
6227 }
6228#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006229 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006230 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006231 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006232 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006233 return;
6234 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006235
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006237 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 */
6239 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6240 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006241
6242 // Write the buffer for ":wq" or when it was changed.
6243 // Trigger QuitPre and ExitPre.
6244 // Check if we can exit now, after autocommands have changed things.
6245 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6246 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006248 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 {
6250 not_exiting();
6251 }
6252 else
6253 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006254 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006256 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257# ifdef FEAT_GUI
6258 need_mouse_correct = TRUE;
6259# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006260 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006261 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 }
6263}
6264
6265/*
6266 * ":print", ":list", ":number".
6267 */
6268 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006269ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006271 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006272 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006273 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006275 for ( ;!got_int; ui_breakcheck())
6276 {
6277 print_line(eap->line1,
6278 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6279 || (eap->flags & EXFLAG_NR)),
6280 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6281 if (++eap->line1 > eap->line2)
6282 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006283 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006284 }
6285 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006286 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006287 curwin->w_cursor.lnum = eap->line2;
6288 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 }
6290
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292}
6293
6294#ifdef FEAT_BYTEOFF
6295 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006296ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297{
6298 goto_byte(eap->line2);
6299}
6300#endif
6301
6302/*
6303 * ":shell".
6304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006306ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307{
6308 do_shell(NULL, 0);
6309}
6310
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006311#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006313static int drop_busy = FALSE;
6314static int drop_filec;
6315static char_u **drop_filev = NULL;
6316static int drop_split;
6317static void (*drop_callback)(void *);
6318static void *drop_cookie;
6319
6320 static void
6321handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322{
6323 exarg_T ea;
6324 int save_msg_scroll = msg_scroll;
6325
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006326 // Setting the argument list may cause screen updates and being called
6327 // recursively. Avoid that by setting drop_busy.
6328 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006330 // Check whether the current buffer is changed. If so, we will need
6331 // to split the current window or data could be lost.
6332 // We don't need to check if the 'hidden' option is set, as in this
6333 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006334 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 {
6336 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006337 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 --emsg_off;
6339 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006340 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 if (win_split(0, 0) == FAIL)
6343 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006344 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006346 // When splitting the window, create a new alist. Otherwise the
6347 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 alist_unlink(curwin->w_alist);
6349 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 }
6351
6352 /*
6353 * Set up the new argument list.
6354 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006355 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356
6357 /*
6358 * Move to the first file.
6359 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006360 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006361 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 ea.cmd = (char_u *)"next";
6363 do_argfile(&ea, 0);
6364
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006365 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6366 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 need_start_insertmode = FALSE;
6368
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006369 // Restore msg_scroll, otherwise a following command may cause scrolling
6370 // unexpectedly. The screen will be redrawn by the caller, thus
6371 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006373
6374 if (drop_callback != NULL)
6375 drop_callback(drop_cookie);
6376
6377 drop_filev = NULL;
6378 drop_busy = FALSE;
6379}
6380
6381/*
6382 * Handle a file drop. The code is here because a drop is *nearly* like an
6383 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006384 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006385 * code is very similar to :args and hence needs access to a lot of the static
6386 * functions in this file.
6387 *
6388 * The "filev" list must have been allocated using alloc(), as should each item
6389 * in the list. This function takes over responsibility for freeing the "filev"
6390 * list.
6391 */
6392 void
6393handle_drop(
6394 int filec, // the number of files dropped
6395 char_u **filev, // the list of files dropped
6396 int split, // force splitting the window
6397 void (*callback)(void *), // to be called after setting the argument
6398 // list
6399 void *cookie) // argument for "callback" (allocated)
6400{
6401 // Cannot handle recursive drops, finish the pending one.
6402 if (drop_busy)
6403 {
6404 FreeWild(filec, filev);
6405 vim_free(cookie);
6406 return;
6407 }
6408
6409 // When calling handle_drop() more than once in a row we only use the last
6410 // one.
6411 if (drop_filev != NULL)
6412 {
6413 FreeWild(drop_filec, drop_filev);
6414 vim_free(drop_cookie);
6415 }
6416
6417 drop_filec = filec;
6418 drop_filev = filev;
6419 drop_split = split;
6420 drop_callback = callback;
6421 drop_cookie = cookie;
6422
6423 // Postpone this when:
6424 // - editing the command line
6425 // - not possible to change the current buffer
6426 // - updating the screen
6427 // As it may change buffers and window structures that are in use and cause
6428 // freed memory to be used.
6429 if (text_locked() || curbuf_locked() || updating_screen)
6430 return;
6431
6432 handle_drop_internal();
6433}
6434
6435/*
6436 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6437 * reset: Handle a postponed drop.
6438 */
6439 void
6440handle_any_postponed_drop(void)
6441{
6442 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006443 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006444 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445}
6446#endif
6447
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 * ":preserve".
6450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006452ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006454 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 ml_preserve(curbuf, TRUE);
6456}
6457
6458/*
6459 * ":recover".
6460 */
6461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006462ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006464 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006466 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6467 | CCGD_MULTWIN
6468 | (eap->forceit ? CCGD_FORCEIT : 0)
6469 | CCGD_EXCMD)
6470
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 && (*eap->arg == NUL
6472 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006473 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 recoverymode = FALSE;
6475}
6476
6477/*
6478 * Command modifier used in a wrong way.
6479 */
6480 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006481ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006483 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484}
6485
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486/*
6487 * :sview [+command] file split window with new file, read-only
6488 * :split [[+command] file] split window with current or new file
6489 * :vsplit [[+command] file] split window vertically with current or new file
6490 * :new [[+command] file] split window with no or new file
6491 * :vnew [[+command] file] split vertically window with no or new file
6492 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006493 *
6494 * :tabedit open new Tab page with empty window
6495 * :tabedit [+command] file open new Tab page and edit "file"
6496 * :tabnew [[+command] file] just like :tabedit
6497 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 */
6499 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006500ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006502 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006503#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006505#endif
6506#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006507 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006508 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006509#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006510 int use_tab = eap->cmdidx == CMD_tabedit
6511 || eap->cmdidx == CMD_tabfind
6512 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006514 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006515 return;
6516
Bram Moolenaar4033c552017-09-16 20:54:51 +02006517#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520
Bram Moolenaar4033c552017-09-16 20:54:51 +02006521#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006522 // A ":split" in the quickfix window works like ":new". Don't want two
6523 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006524 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 {
6526 if (eap->cmdidx == CMD_split)
6527 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 if (eap->cmdidx == CMD_vsplit)
6529 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532
Bram Moolenaar4033c552017-09-16 20:54:51 +02006533#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006534 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 {
6536 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6537 FNAME_MESS, TRUE, curbuf->b_ffname);
6538 if (fname == NULL)
6539 goto theend;
6540 eap->arg = fname;
6541 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006542# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006543 else
6544# endif
6545#endif
6546#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006547 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 && eap->cmdidx != CMD_new)
6550 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006551 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006552# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006553 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006554# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006555 au_has_group((char_u *)"FileExplorer"))
6556 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006557 // No browsing supported but we do have the file explorer:
6558 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006559 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006560 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006561 }
6562 else
6563 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006564 fname = do_browse(0, (char_u *)(use_tab
6565 ? _("Edit File in new tab page")
6566 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006568 if (fname == NULL)
6569 goto theend;
6570 eap->arg = fname;
6571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006573 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006576 /*
6577 * Either open new tab page or split the window.
6578 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006579 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006580 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006581 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006582 : eap->addr_count == 0 ? 0
6583 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006584 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006585 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006586
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006587 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006588 if (curwin != old_curwin
6589 && win_valid(old_curwin)
6590 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006591 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006592 old_curwin->w_alt_fnum = curbuf->b_fnum;
6593 }
6594 }
6595 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6597 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006598 // Reset 'scrollbind' when editing another file, but keep it when
6599 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006600 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006601 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 else
6603 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 do_exedit(eap, old_curwin);
6605 }
6606
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006607# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006608 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006609# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006611# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612theend:
6613 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006614# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006616
6617/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006618 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006619 */
6620 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006621tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006622{
6623 exarg_T ea;
6624
Bram Moolenaara80faa82020-04-12 19:37:17 +02006625 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006626 ea.cmdidx = CMD_tabnew;
6627 ea.cmd = (char_u *)"tabn";
6628 ea.arg = (char_u *)"";
6629 ex_splitview(&ea);
6630}
6631
6632/*
6633 * :tabnext command
6634 */
6635 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006636ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006637{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006638 int tab_number;
6639
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006640 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006641 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006642 switch (eap->cmdidx)
6643 {
6644 case CMD_tabfirst:
6645 case CMD_tabrewind:
6646 goto_tabpage(1);
6647 break;
6648 case CMD_tablast:
6649 goto_tabpage(9999);
6650 break;
6651 case CMD_tabprevious:
6652 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006653 if (eap->arg && *eap->arg != NUL)
6654 {
6655 char_u *p = eap->arg;
6656 char_u *p_save = p;
6657
6658 tab_number = getdigits(&p);
6659 if (p == p_save || *p_save == '-' || *p != NUL
6660 || tab_number == 0)
6661 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006662 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006663 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006664 return;
6665 }
6666 }
6667 else
6668 {
6669 if (eap->addr_count == 0)
6670 tab_number = 1;
6671 else
6672 {
6673 tab_number = eap->line2;
6674 if (tab_number < 1)
6675 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006676 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006677 return;
6678 }
6679 }
6680 }
6681 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006682 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006683 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006684 tab_number = get_tabpage_arg(eap);
6685 if (eap->errmsg == NULL)
6686 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006687 break;
6688 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006689}
6690
6691/*
6692 * :tabmove command
6693 */
6694 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006695ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006696{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006697 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006698
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006699 tab_number = get_tabpage_arg(eap);
6700 if (eap->errmsg == NULL)
6701 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006702}
6703
6704/*
6705 * :tabs command: List tabs and their contents.
6706 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006707 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006708ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006709{
6710 tabpage_T *tp;
6711 win_T *wp;
6712 int tabcount = 1;
6713
6714 msg_start();
6715 msg_scroll = TRUE;
6716 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6717 {
6718 msg_putchar('\n');
6719 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006720 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006721 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006722 ui_breakcheck();
6723
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006724 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006725 wp = firstwin;
6726 else
6727 wp = tp->tp_firstwin;
6728 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6729 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006730 msg_putchar('\n');
6731 msg_putchar(wp == curwin ? '>' : ' ');
6732 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006733 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6734 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006735 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006736 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006737 else
6738 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6739 IObuff, IOSIZE, TRUE);
6740 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006741 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006742 ui_breakcheck();
6743 }
6744 }
6745}
6746
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747/*
6748 * ":mode": Set screen mode.
6749 * If no argument given, just get the screen size and redraw.
6750 */
6751 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006752ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753{
6754 if (*eap->arg == NUL)
6755 shell_resized();
6756 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006757 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758}
6759
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760/*
6761 * ":resize".
6762 * set, increment or decrement current window height
6763 */
6764 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006765ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766{
6767 int n;
6768 win_T *wp = curwin;
6769
6770 if (eap->addr_count > 0)
6771 {
6772 n = eap->line2;
6773 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6774 ;
6775 }
6776
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006777# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006779# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006781 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 {
6783 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006784 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006785 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 n = 9999;
6787 win_setwidth_win((int)n, wp);
6788 }
6789 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 {
6791 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006792 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006793 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 n = 9999;
6795 win_setheight_win((int)n, wp);
6796 }
6797}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798
6799/*
6800 * ":find [+command] <file>" command.
6801 */
6802 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006803ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804{
6805#ifdef FEAT_SEARCHPATH
6806 char_u *fname;
6807 int count;
6808
6809 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6810 TRUE, curbuf->b_ffname);
6811 if (eap->addr_count > 0)
6812 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006813 // Repeat finding the file "count" times. This matters when it
6814 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 count = eap->line2;
6816 while (fname != NULL && --count > 0)
6817 {
6818 vim_free(fname);
6819 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6820 FALSE, curbuf->b_ffname);
6821 }
6822 }
6823
6824 if (fname != NULL)
6825 {
6826 eap->arg = fname;
6827#endif
6828 do_exedit(eap, NULL);
6829#ifdef FEAT_SEARCHPATH
6830 vim_free(fname);
6831 }
6832#endif
6833}
6834
6835/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006836 * ":open" simulation: for now just work like ":visual".
6837 */
6838 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006839ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006840{
6841 regmatch_T regmatch;
6842 char_u *p;
6843
Bram Moolenaar65088802021-03-13 21:07:21 +01006844#ifdef FEAT_EVAL
6845 if (not_in_vim9(eap) == FAIL)
6846 return;
6847#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006848 curwin->w_cursor.lnum = eap->line2;
6849 beginline(BL_SOL | BL_FIX);
6850 if (*eap->arg == '/')
6851 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006852 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006853 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006854 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00006855 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006856 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006857 if (regmatch.regprog != NULL)
6858 {
6859 regmatch.rm_ic = p_ic;
6860 p = ml_get_curline();
6861 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006862 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006863 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006864 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006865 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006866 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006867 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006868 eap->arg += STRLEN(eap->arg);
6869 }
6870 check_cursor();
6871
6872 eap->cmdidx = CMD_visual;
6873 do_exedit(eap, NULL);
6874}
6875
6876/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006877 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 */
6879 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006880ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881{
6882 do_exedit(eap, NULL);
6883}
6884
6885/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006886 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006889do_exedit(
6890 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006891 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892{
6893 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006895 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006897 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6898 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006899 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 /*
6901 * ":vi" command ends Ex mode.
6902 */
6903 if (exmode_active && (eap->cmdidx == CMD_visual
6904 || eap->cmdidx == CMD_view))
6905 {
6906 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006907 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006909 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006910 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006911 if (global_busy)
6912 {
6913 int rd = RedrawingDisabled;
6914 int nwr = no_wait_return;
6915 int ms = msg_scroll;
6916#ifdef FEAT_GUI
6917 int he = hold_gui_events;
6918#endif
6919
6920 if (eap->nextcmd != NULL)
6921 {
6922 stuffReadbuff(eap->nextcmd);
6923 eap->nextcmd = NULL;
6924 }
6925
6926 if (exmode_was != EXMODE_VIM)
6927 settmode(TMODE_RAW);
6928 RedrawingDisabled = 0;
6929 no_wait_return = 0;
6930 need_wait_return = FALSE;
6931 msg_scroll = 0;
6932#ifdef FEAT_GUI
6933 hold_gui_events = 0;
6934#endif
6935 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006936 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006937
6938 main_loop(FALSE, TRUE);
6939
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006940 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006941 RedrawingDisabled = rd;
6942 no_wait_return = nwr;
6943 msg_scroll = ms;
6944#ifdef FEAT_GUI
6945 hold_gui_events = he;
6946#endif
6947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 }
6951
6952 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006953 || eap->cmdidx == CMD_tabnew
6954 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006955 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006957 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 setpcmark();
6959 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006960 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6961 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006963 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 || *eap->arg != NUL
6965#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006966 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967#endif
6968 )
6969 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006970 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6971 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006972 if (*eap->arg != NUL && curbuf_locked())
6973 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006974
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 n = readonlymode;
6976 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6977 readonlymode = TRUE;
6978 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006979 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6980 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01006981 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
6982 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6984 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006985 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6987 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6988 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006989 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006991 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006992 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006993 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
6994 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006995 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006997 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 if (old_curwin != NULL)
6999 {
7000 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007001 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007003#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007004 cleanup_T cs;
7005
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007006 // Reset the error/interrupt/exception state here so that
7007 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007008 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007009#endif
7010#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007012#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007013 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007014
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007015#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007016 // Restore the error/interrupt/exception state if not
7017 // discarded by a new aborting error, interrupt, or
7018 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007019 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 }
7022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 }
7024 else if (readonlymode && curbuf->b_nwindows == 1)
7025 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007026 // When editing an already visited buffer, 'readonly' won't be set
7027 // but the previous value is kept. With ":view" and ":sview" we
7028 // want the file to be readonly, except when another window is
7029 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 curbuf->b_p_ro = TRUE;
7031 }
7032 readonlymode = n;
7033 }
7034 else
7035 {
7036 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007037 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038#ifdef FEAT_TITLE
7039 n = curwin->w_arg_idx_invalid;
7040#endif
7041 check_arg_idx(curwin);
7042#ifdef FEAT_TITLE
7043 if (n != curwin->w_arg_idx_invalid)
7044 maketitle();
7045#endif
7046 }
7047
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 /*
7049 * if ":split file" worked, set alternate file name in old window to new
7050 * file
7051 */
7052 if (old_curwin != NULL
7053 && *eap->arg != NUL
7054 && curwin != old_curwin
7055 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007056 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007057 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059
7060 ex_no_reprint = TRUE;
7061}
7062
7063#ifndef FEAT_GUI
7064/*
7065 * ":gui" and ":gvim" when there is no GUI.
7066 */
7067 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007068ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007070 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071}
7072#endif
7073
Bram Moolenaar4f974752019-02-17 17:44:42 +01007074#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007076ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077{
7078 gui_make_tearoff(eap->arg);
7079}
7080#endif
7081
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007082#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7083 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007085ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007087# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7088 if (gui.in_use)
7089 gui_make_popup(eap->arg, eap->forceit);
7090# ifdef FEAT_TERM_POPUP_MENU
7091 else
7092# endif
7093# endif
7094# ifdef FEAT_TERM_POPUP_MENU
7095 pum_make_popup(eap->arg, eap->forceit);
7096# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097}
7098#endif
7099
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007101ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102{
7103 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007104 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007106 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107}
7108
7109/*
7110 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7111 * offset.
7112 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7113 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007115ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007118 win_T *save_curwin = curwin;
7119 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 long topline;
7121 long y;
7122 linenr_T old_linenr = curwin->w_cursor.lnum;
7123
7124 setpcmark();
7125
7126 /*
7127 * determine max topline
7128 */
7129 if (curwin->w_p_scb)
7130 {
7131 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007132 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 {
7134 if (wp->w_p_scb && wp->w_buffer)
7135 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007136 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 if (topline > y)
7138 topline = y;
7139 }
7140 }
7141 if (topline < 1)
7142 topline = 1;
7143 }
7144 else
7145 {
7146 topline = 1;
7147 }
7148
7149
7150 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007151 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007153 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 {
7155 if (curwin->w_p_scb)
7156 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007157 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 y = topline - curwin->w_topline;
7159 if (y > 0)
7160 scrollup(y, TRUE);
7161 else
7162 scrolldown(-y, TRUE);
7163 curwin->w_scbind_pos = topline;
7164 redraw_later(VALID);
7165 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 }
7168 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007169 curwin = save_curwin;
7170 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 if (curwin->w_p_scb)
7172 {
7173 did_syncbind = TRUE;
7174 checkpcmark();
7175 if (old_linenr != curwin->w_cursor.lnum)
7176 {
7177 char_u ctrl_o[2];
7178
7179 ctrl_o[0] = Ctrl_O;
7180 ctrl_o[1] = 0;
7181 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7182 }
7183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184}
7185
7186
7187 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007188ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007190 int i;
7191 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7192 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007194 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 do_bang(1, eap, FALSE, FALSE, TRUE);
7196 else
7197 {
7198 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7199 return;
7200
7201#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007202 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 {
7204 char_u *browseFile;
7205
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007206 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 NULL, NULL, NULL, curbuf);
7208 if (browseFile != NULL)
7209 {
7210 i = readfile(browseFile, NULL,
7211 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7212 vim_free(browseFile);
7213 }
7214 else
7215 i = OK;
7216 }
7217 else
7218#endif
7219 if (*eap->arg == NUL)
7220 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007221 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 return;
7223 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7224 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7225 }
7226 else
7227 {
7228 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7229 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7230 i = readfile(eap->arg, NULL,
7231 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7232
7233 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007234 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007236#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 if (!aborting())
7238#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007239 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 }
7241 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007242 {
7243 if (empty && exmode_active)
7244 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007245 // Delete the empty line that remains. Historically ex does
7246 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007247 if (eap->line2 == 0)
7248 lnum = curbuf->b_ml.ml_line_count;
7249 else
7250 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007251 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007252 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007253 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007254 if (curwin->w_cursor.lnum > 1
7255 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007256 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007257 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007258 }
7259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 }
7263}
7264
Bram Moolenaarea408852005-06-25 22:49:46 +00007265static char_u *prev_dir = NULL;
7266
7267#if defined(EXITFREE) || defined(PROTO)
7268 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007269free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007270{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007271 VIM_CLEAR(prev_dir);
7272 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007273}
7274#endif
7275
Bram Moolenaarf4258302013-06-02 18:20:17 +02007276/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007277 * Get the previous directory for the given chdir scope.
7278 */
7279 static char_u *
7280get_prevdir(cdscope_T scope)
7281{
7282 if (scope == CDSCOPE_WINDOW)
7283 return curwin->w_prevdir;
7284 else if (scope == CDSCOPE_TABPAGE)
7285 return curtab->tp_prevdir;
7286 return prev_dir;
7287}
7288
7289/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007290 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007291 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7292 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007293 */
7294 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007295post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007296{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007297 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007298 // Clear tab local directory for both :cd and :tcd
7299 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007300 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007301 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007302 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007303 char_u *pdir = get_prevdir(scope);
7304
7305 // If still in the global directory, need to remember current
7306 // directory as the global directory.
7307 if (globaldir == NULL && pdir != NULL)
7308 globaldir = vim_strsave(pdir);
7309
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007310 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007311 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007312 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007313 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007314 curtab->tp_localdir = vim_strsave(NameBuff);
7315 else
7316 curwin->w_localdir = vim_strsave(NameBuff);
7317 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007318 }
7319 else
7320 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007321 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007322 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007323 }
7324
zeertzjq64be6aa2021-11-19 11:59:08 +00007325 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007326 shorten_fnames(TRUE);
7327}
7328
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007329/*
7330 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007331 * chdir() function.
7332 * scope == CDSCOPE_WINDOW: changes the window-local directory
7333 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7334 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007335 * Returns TRUE if the directory is successfully changed.
7336 */
7337 int
7338changedir_func(
7339 char_u *new_dir,
7340 int forceit,
7341 cdscope_T scope)
7342{
7343 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02007344 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007345 int dir_differs;
7346 int retval = FALSE;
7347
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007348 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007349 return FALSE;
7350
7351 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7352 {
7353 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7354 return FALSE;
7355 }
7356
7357 // ":cd -": Change to previous directory
7358 if (STRCMP(new_dir, "-") == 0)
7359 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007360 pdir = get_prevdir(scope);
7361 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007362 {
7363 emsg(_("E186: No previous directory"));
7364 return FALSE;
7365 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007366 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007367 }
7368
Bram Moolenaar002bc792020-06-05 22:33:42 +02007369 // Free the previous directory
7370 tofree = get_prevdir(scope);
7371
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007372 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007373 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007374 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007375 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007376 pdir = NULL;
7377 if (scope == CDSCOPE_WINDOW)
7378 curwin->w_prevdir = pdir;
7379 else if (scope == CDSCOPE_TABPAGE)
7380 curtab->tp_prevdir = pdir;
7381 else
7382 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007383
7384#if defined(UNIX) || defined(VMS)
7385 // for UNIX ":cd" means: go to home directory
7386 if (*new_dir == NUL)
7387 {
7388 // use NameBuff for home directory name
7389# ifdef VMS
7390 char_u *p;
7391
7392 p = mch_getenv((char_u *)"SYS$LOGIN");
7393 if (p == NULL || *p == NUL) // empty is the same as not set
7394 NameBuff[0] = NUL;
7395 else
7396 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7397# else
7398 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7399# endif
7400 new_dir = NameBuff;
7401 }
7402#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02007403 dir_differs = new_dir == NULL || pdir == NULL
7404 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007405 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7406 emsg(_(e_failed));
7407 else
7408 {
7409 char_u *acmd_fname;
7410
7411 post_chdir(scope);
7412
7413 if (dir_differs)
7414 {
7415 if (scope == CDSCOPE_WINDOW)
7416 acmd_fname = (char_u *)"window";
7417 else if (scope == CDSCOPE_TABPAGE)
7418 acmd_fname = (char_u *)"tabpage";
7419 else
7420 acmd_fname = (char_u *)"global";
7421 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7422 curbuf);
7423 }
7424 retval = TRUE;
7425 }
7426 vim_free(tofree);
7427
7428 return retval;
7429}
Bram Moolenaarea408852005-06-25 22:49:46 +00007430
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007432 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007434 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007435ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007437 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438
7439 new_dir = eap->arg;
7440#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007441 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 if (*new_dir == NUL)
7443 ex_pwd(NULL);
7444 else
7445#endif
7446 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007447 cdscope_T scope = CDSCOPE_GLOBAL;
7448
7449 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7450 scope = CDSCOPE_WINDOW;
7451 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7452 scope = CDSCOPE_TABPAGE;
7453
7454 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007455 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007456 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007457 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 ex_pwd(eap);
7459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 }
7461}
7462
7463/*
7464 * ":pwd".
7465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007467ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468{
7469 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7470 {
7471#ifdef BACKSLASH_IN_FILENAME
7472 slash_adjust(NameBuff);
7473#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007474 if (p_verbose > 0)
7475 {
7476 char *context = "global";
7477
Bram Moolenaar05268152021-11-18 18:53:45 +00007478 if (last_chdir_reason != NULL)
7479 context = last_chdir_reason;
7480 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007481 context = "window";
7482 else if (curtab->tp_localdir != NULL)
7483 context = "tabpage";
7484 smsg("[%s] %s", context, (char *)NameBuff);
7485 }
7486 else
7487 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 }
7489 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007490 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491}
7492
7493/*
7494 * ":=".
7495 */
7496 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007497ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007499 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007500 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501}
7502
7503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007504ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007506 int n;
7507 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508
7509 if (cursor_valid())
7510 {
7511 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7512 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007513 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007515
7516 len = eap->line2;
7517 switch (*eap->arg)
7518 {
7519 case 'm': break;
7520 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007521 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007522 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007523
7524 // Hide the cursor if invoked with !
7525 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526}
7527
7528/*
7529 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007530 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 */
7532 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007533do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534{
Bram Moolenaar52953192019-08-16 10:27:13 +02007535 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007536 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007537# ifdef ELAPSED_FUNC
7538 elapsed_T start_tv;
7539
7540 // Remember at what time we started, so that we know how much longer we
7541 // should wait after waiting for a bit.
7542 ELAPSED_INIT(start_tv);
7543# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007545 if (hide_cursor)
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007546 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007547 else
7548 cursor_on();
7549
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007550 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007551 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007553 wait_now = msec - done > 1000L ? 1000L : msec - done;
7554#ifdef FEAT_TIMERS
7555 {
7556 long due_time = check_due_timer();
7557
7558 if (due_time > 0 && due_time < wait_now)
7559 wait_now = due_time;
7560 }
7561#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007562#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007563 if (has_any_channel() && wait_now > 20L)
7564 wait_now = 20L;
7565#endif
7566#ifdef FEAT_SOUND
7567 if (has_any_sound_callback() && wait_now > 20L)
7568 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007569#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007570 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007571
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007572#ifdef FEAT_JOB_CHANNEL
7573 if (has_any_channel())
7574 ui_breakcheck_force(TRUE);
7575 else
7576#endif
7577 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007578#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007579 // Process the netbeans and clientserver messages that may have been
7580 // received in the call to ui_breakcheck() when the GUI is in use. This
7581 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007582 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007583#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007584
7585# ifdef ELAPSED_FUNC
7586 // actual time passed
7587 done = ELAPSED_FUNC(start_tv);
7588# else
7589 // guestimate time passed (will actually be more)
7590 done += wait_now;
7591# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007593
7594 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7595 // input buffer, otherwise a following call to input() fails.
7596 if (got_int)
7597 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007598
7599 if (hide_cursor)
7600 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601}
7602
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603/*
7604 * ":winsize" command (obsolete).
7605 */
7606 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007607ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608{
7609 int w, h;
7610 char_u *arg = eap->arg;
7611 char_u *p;
7612
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007613 if (!isdigit(*arg))
7614 {
7615 semsg(_(e_invarg2), arg);
7616 return;
7617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 w = getdigits(&arg);
7619 arg = skipwhite(arg);
7620 p = arg;
7621 h = getdigits(&arg);
7622 if (*p != NUL && *arg == NUL)
7623 set_shellsize(w, h, TRUE);
7624 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007625 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626}
7627
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007629ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630{
7631 int xchar = NUL;
7632 char_u *p;
7633
7634 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7635 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007636 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 if (eap->arg[1] == NUL)
7638 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007639 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 return;
7641 }
7642 xchar = eap->arg[1];
7643 p = eap->arg + 2;
7644 }
7645 else
7646 p = eap->arg + 1;
7647
Bram Moolenaar63b91732021-08-05 20:40:03 +02007648 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007650 if (*p != NUL && *p != (
7651#ifdef FEAT_EVAL
7652 in_vim9script() ? '#' :
7653#endif
7654 '"')
7655 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007656 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007657 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007659 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007660 postponed_split_flags = cmdmod.cmod_split;
7661 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7663 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007664 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 }
7666}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667
Bram Moolenaar843ee412004-06-30 16:16:41 +00007668#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669/*
7670 * ":winpos".
7671 */
7672 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007673ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674{
7675 int x, y;
7676 char_u *arg = eap->arg;
7677 char_u *p;
7678
7679 if (*arg == NUL)
7680 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007681# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007682# ifdef VIMDLL
7683 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7684 mch_get_winpos(&x, &y) != FAIL)
7685# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007687# else
7688 if (mch_get_winpos(&x, &y) != FAIL)
7689# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 {
7691 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007692 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 }
7694 else
7695# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007696 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 }
7698 else
7699 {
7700 x = getdigits(&arg);
7701 arg = skipwhite(arg);
7702 p = arg;
7703 y = getdigits(&arg);
7704 if (*p == NUL || *arg != NUL)
7705 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007706 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 return;
7708 }
7709# ifdef FEAT_GUI
7710 if (gui.in_use)
7711 gui_mch_set_winpos(x, y);
7712 else if (gui.starting)
7713 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007714 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 gui_win_x = x;
7716 gui_win_y = y;
7717 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007718# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 else
7720# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007721# endif
7722# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007723 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724# endif
7725# ifdef HAVE_TGETENT
7726 if (*T_CWP)
7727 term_set_winpos(x, y);
7728# endif
7729 }
7730}
7731#endif
7732
7733/*
7734 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7735 */
7736 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007737ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738{
7739 oparg_T oa;
7740
7741 clear_oparg(&oa);
7742 oa.regname = eap->regname;
7743 oa.start.lnum = eap->line1;
7744 oa.end.lnum = eap->line2;
7745 oa.line_count = eap->line2 - eap->line1 + 1;
7746 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007748 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 {
7750 setpcmark();
7751 curwin->w_cursor.lnum = eap->line1;
7752 beginline(BL_SOL | BL_FIX);
7753 }
7754
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007755 if (VIsual_active)
7756 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007757
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 switch (eap->cmdidx)
7759 {
7760 case CMD_delete:
7761 oa.op_type = OP_DELETE;
7762 op_delete(&oa);
7763 break;
7764
7765 case CMD_yank:
7766 oa.op_type = OP_YANK;
7767 (void)op_yank(&oa, FALSE, TRUE);
7768 break;
7769
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007770 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007771 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007773 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7774#else
7775 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007777 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 oa.op_type = OP_RSHIFT;
7779 else
7780 oa.op_type = OP_LSHIFT;
7781 op_shift(&oa, FALSE, eap->amount);
7782 break;
7783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007785 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786}
7787
7788/*
7789 * ":put".
7790 */
7791 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007792ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007794 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 if (eap->line2 == 0)
7796 {
7797 eap->line2 = 1;
7798 eap->forceit = TRUE;
7799 }
7800 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00007801 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007802 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007803 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804}
7805
7806/*
7807 * Handle ":copy" and ":move".
7808 */
7809 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007810ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811{
7812 long n;
7813
Bram Moolenaar491799b2020-08-01 19:23:43 +02007814#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007815 if (not_in_vim9(eap) == FAIL)
7816 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007817#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007818 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007819 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 {
7821 eap->nextcmd = NULL;
7822 return;
7823 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007824 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825
7826 /*
7827 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7828 */
7829 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7830 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007831 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 return;
7833 }
7834
7835 if (eap->cmdidx == CMD_move)
7836 {
7837 if (do_move(eap->line1, eap->line2, n) == FAIL)
7838 return;
7839 }
7840 else
7841 ex_copy(eap->line1, eap->line2, n);
7842 u_clearline();
7843 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007844 ex_may_print(eap);
7845}
7846
7847/*
7848 * Print the current line if flags were given to the Ex command.
7849 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007850 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007851ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007852{
7853 if (eap->flags != 0)
7854 {
7855 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7856 (eap->flags & EXFLAG_LIST));
7857 ex_no_reprint = TRUE;
7858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859}
7860
7861/*
7862 * ":smagic" and ":snomagic".
7863 */
7864 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007865ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007867 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007869 magic_overruled = eap->cmdidx == CMD_smagic
7870 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02007871 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007872 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873}
7874
7875/*
7876 * ":join".
7877 */
7878 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007879ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880{
7881 curwin->w_cursor.lnum = eap->line1;
7882 if (eap->line1 == eap->line2)
7883 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007884 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 return;
7886 if (eap->line2 == curbuf->b_ml.ml_line_count)
7887 {
7888 beep_flush();
7889 return;
7890 }
7891 ++eap->line2;
7892 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007893 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007895 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896}
7897
7898/*
7899 * ":[addr]@r" or ":[addr]*r": execute register
7900 */
7901 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007902ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903{
7904 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007905 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906
7907 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007908 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909
7910#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007911 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912#endif
7913
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007914 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 c = *eap->arg;
7916 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7917 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007918 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007919 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7920 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007921 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 else
7925 {
7926 int save_efr = exec_from_reg;
7927
7928 exec_from_reg = TRUE;
7929
7930 /*
7931 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007932 * Continue until the stuff buffer is empty and all added characters
7933 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007935 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7937
7938 exec_from_reg = save_efr;
7939 }
7940}
7941
7942/*
7943 * ":!".
7944 */
7945 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007946ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947{
7948 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7949}
7950
7951/*
7952 * ":undo".
7953 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007955ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007957 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007958 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007959 else
7960 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961}
7962
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007963#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007964 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007965ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007966{
7967 char_u hash[UNDO_HASH_SIZE];
7968
7969 u_compute_hash(hash);
7970 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7971}
7972
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007973 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007974ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007975{
7976 char_u hash[UNDO_HASH_SIZE];
7977
7978 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007979 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007980}
7981#endif
7982
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983/*
7984 * ":redo".
7985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007987ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988{
7989 u_redo(1);
7990}
7991
7992/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007993 * ":earlier" and ":later".
7994 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007995 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007996ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007997{
7998 long count = 0;
7999 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008000 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008001 char_u *p = eap->arg;
8002
8003 if (*p == NUL)
8004 count = 1;
8005 else if (isdigit(*p))
8006 {
8007 count = getdigits(&p);
8008 switch (*p)
8009 {
8010 case 's': ++p; sec = TRUE; break;
8011 case 'm': ++p; sec = TRUE; count *= 60; break;
8012 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008013 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8014 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008015 }
8016 }
8017
8018 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008019 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008020 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008021 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8022 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008023}
8024
8025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 * ":redir": start/stop redirection.
8027 */
8028 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008029ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030{
8031 char *mode;
8032 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008033 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034
Bram Moolenaarba768492016-07-08 15:32:54 +02008035#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008036 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008037 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008038 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008039 return;
8040 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008041#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008042
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 if (STRICMP(eap->arg, "END") == 0)
8044 close_redir();
8045 else
8046 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008047 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008049 ++arg;
8050 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008052 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 mode = "a";
8054 }
8055 else
8056 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008057 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058
8059 close_redir();
8060
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008061 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008062 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 if (fname == NULL)
8064 return;
8065#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008066 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 {
8068 char_u *browseFile;
8069
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008070 browseFile = do_browse(BROWSE_SAVE,
8071 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008072 fname, NULL, NULL,
8073 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008075 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 vim_free(fname);
8077 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008078 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 }
8080#endif
8081
8082 redir_fd = open_exfile(fname, eap->forceit, mode);
8083 vim_free(fname);
8084 }
8085#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008086 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008088 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008090 ++arg;
8091 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008093 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008094 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008096 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008098 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008099 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008100 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008101 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008103 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008104 if (*arg == '>')
8105 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008106 // Make register empty when not using @A-@Z and the
8107 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00008108 if (*arg == NUL && !isupper(redir_reg))
8109 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008111 }
8112 if (*arg != NUL)
8113 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008114 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008115 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008116 }
8117 }
8118 else if (*arg == '=' && arg[1] == '>')
8119 {
8120 int append;
8121
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008122 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008123 close_redir();
8124 arg += 2;
8125
8126 if (*arg == '>')
8127 {
8128 ++arg;
8129 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 }
8131 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008132 append = FALSE;
8133
8134 if (var_redir_start(skipwhite(arg), append) == OK)
8135 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 }
8137#endif
8138
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008139 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008142 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008144
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008145 // Make sure redirection is not off. Can happen for cmdline completion
8146 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008147 if (redir_fd != NULL
8148#ifdef FEAT_EVAL
8149 || redir_reg || redir_vname
8150#endif
8151 )
8152 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153}
8154
8155/*
8156 * ":redraw": force redraw
8157 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008158 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008159ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160{
8161 int r = RedrawingDisabled;
8162 int p = p_lz;
8163
8164 RedrawingDisabled = 0;
8165 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008166 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008168 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169#ifdef FEAT_TITLE
8170 if (need_maketitle)
8171 maketitle();
8172#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008173#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8174# ifdef VIMDLL
8175 if (!gui.in_use)
8176# endif
8177 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 RedrawingDisabled = r;
8180 p_lz = p;
8181
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008182 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 msg_didout = FALSE;
8184 msg_col = 0;
8185
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008186 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008187 need_wait_return = FALSE;
8188
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 out_flush();
8190}
8191
8192/*
8193 * ":redrawstatus": force redraw of status line(s)
8194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008196ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 int r = RedrawingDisabled;
8199 int p = p_lz;
8200
8201 RedrawingDisabled = 0;
8202 p_lz = FALSE;
8203 if (eap->forceit)
8204 status_redraw_all();
8205 else
8206 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008207 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 RedrawingDisabled = r;
8209 p_lz = p;
8210 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211}
8212
Bram Moolenaare12bab32019-01-08 22:02:56 +01008213/*
8214 * ":redrawtabline": force redraw of the tabline
8215 */
8216 static void
8217ex_redrawtabline(exarg_T *eap UNUSED)
8218{
8219 int r = RedrawingDisabled;
8220 int p = p_lz;
8221
8222 RedrawingDisabled = 0;
8223 p_lz = FALSE;
8224
8225 draw_tabline();
8226
8227 RedrawingDisabled = r;
8228 p_lz = p;
8229 out_flush();
8230}
8231
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008233close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234{
8235 if (redir_fd != NULL)
8236 {
8237 fclose(redir_fd);
8238 redir_fd = NULL;
8239 }
8240#ifdef FEAT_EVAL
8241 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008242 if (redir_vname)
8243 {
8244 var_redir_stop();
8245 redir_vname = 0;
8246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247#endif
8248}
8249
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008250#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008251 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008252vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008253{
8254 if (vim_mkdir(name, prot) != 0)
8255 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008256 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008257 return FAIL;
8258 }
8259 return OK;
8260}
8261#endif
8262
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263/*
8264 * Open a file for writing for an Ex command, with some checks.
8265 * Return file descriptor, or NULL on failure.
8266 */
8267 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008268open_exfile(
8269 char_u *fname,
8270 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008271 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272{
8273 FILE *fd;
8274
8275#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008276 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 if (mch_isdir(fname))
8278 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008279 semsg(_(e_src_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 return NULL;
8281 }
8282#endif
8283 if (!forceit && *mode != 'a' && vim_fexists(fname))
8284 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008285 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 return NULL;
8287 }
8288
8289 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008290 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291
8292 return fd;
8293}
8294
8295/*
8296 * ":mark" and ":k".
8297 */
8298 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008299ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300{
8301 pos_T pos;
8302
Bram Moolenaar10b94212021-02-19 21:42:57 +01008303#ifdef FEAT_EVAL
8304 if (not_in_vim9(eap) == FAIL)
8305 return;
8306#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008307 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008308 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008309 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02008310 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 else
8312 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008313 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 curwin->w_cursor.lnum = eap->line2;
8315 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008316 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008317 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008318 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 }
8320}
8321
8322/*
8323 * Update w_topline, w_leftcol and the cursor position.
8324 */
8325 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008326update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008328 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 update_topline();
8330 if (!curwin->w_p_wrap)
8331 validate_cursor();
8332 update_curswant();
8333}
8334
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008336 * Save the current State and go to Normal mode.
8337 * Return TRUE if the typeahead could be saved.
8338 */
8339 int
8340save_current_state(save_state_T *sst)
8341{
8342 sst->save_msg_scroll = msg_scroll;
8343 sst->save_restart_edit = restart_edit;
8344 sst->save_msg_didout = msg_didout;
8345 sst->save_State = State;
8346 sst->save_insertmode = p_im;
8347 sst->save_finish_op = finish_op;
8348 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008349 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008350
Bram Moolenaar4324d872020-12-01 20:12:24 +01008351 msg_scroll = FALSE; // no msg scrolling in Normal mode
8352 restart_edit = 0; // don't go to Insert mode
8353 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008354
Bram Moolenaara452b802020-12-01 21:47:59 +01008355 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008356 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008357
8358 /*
8359 * Save the current typeahead. This is required to allow using ":normal"
8360 * from an event handler and makes sure we don't hang when the argument
8361 * ends with half a command.
8362 */
8363 save_typeahead(&sst->tabuf);
8364 return sst->tabuf.typebuf_valid;
8365}
8366
8367 void
8368restore_current_state(save_state_T *sst)
8369{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008370 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008371 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008372
8373 msg_scroll = sst->save_msg_scroll;
8374 restart_edit = sst->save_restart_edit;
8375 p_im = sst->save_insertmode;
8376 finish_op = sst->save_finish_op;
8377 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008378 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008379 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008380 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008381
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008382 // Restore the state (needed when called from a function executed for
8383 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008384 State = sst->save_State;
8385#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008386 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008387#endif
8388}
8389
8390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 * ":normal[!] {commands}": Execute normal mode commands.
8392 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008393 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008394ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008396 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 char_u *arg = NULL;
8398 int l;
8399 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008401 if (ex_normal_lock > 0)
8402 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008403 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008404 return;
8405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 if (ex_normal_busy >= p_mmd)
8407 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008408 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 return;
8410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 /*
8413 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8414 * this for the K_SPECIAL leading byte, otherwise special keys will not
8415 * work.
8416 */
8417 if (has_mbyte)
8418 {
8419 int len = 0;
8420
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008421 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 for (p = eap->arg; *p != NUL; ++p)
8423 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008424#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008425 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008427#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008428 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008429 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008430#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 )
8434 len += 2;
8435 }
8436 if (len > 0)
8437 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008438 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 if (arg != NULL)
8440 {
8441 len = 0;
8442 for (p = eap->arg; *p != NUL; ++p)
8443 {
8444 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008445#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 if (*p == CSI)
8447 {
8448 arg[len++] = KS_EXTRA;
8449 arg[len++] = (int)KE_CSI;
8450 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008451#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008452 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 {
8454 arg[len++] = *++p;
8455 if (*p == K_SPECIAL)
8456 {
8457 arg[len++] = KS_SPECIAL;
8458 arg[len++] = KE_FILLER;
8459 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008460#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 else if (*p == CSI)
8462 {
8463 arg[len++] = KS_EXTRA;
8464 arg[len++] = (int)KE_CSI;
8465 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 }
8468 arg[len] = NUL;
8469 }
8470 }
8471 }
8472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008474 ++ex_normal_busy;
8475 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 {
8477 /*
8478 * Repeat the :normal command for each line in the range. When no
8479 * range given, execute it just once, without positioning the cursor
8480 * first.
8481 */
8482 do
8483 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 if (eap->addr_count != 0)
8485 {
8486 curwin->w_cursor.lnum = eap->line1++;
8487 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008488 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 }
8490
Bram Moolenaar13505972019-01-24 15:04:48 +01008491 exec_normal_cmd(arg != NULL
8492 ? arg
8493 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 }
8495 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8496 }
8497
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008498 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 update_topline_cursor();
8500
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008501 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008503 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008504#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008505 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008506#endif
8507
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509}
8510
8511/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008512 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 */
8514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008515ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008517 if (eap->forceit)
8518 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008519 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008520 if (!curwin->w_cursor.lnum)
8521 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008522 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008523 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008524#ifdef FEAT_TERMINAL
8525 // Ignore this when running in an active terminal.
8526 if (term_job_running(curbuf->b_term))
8527 return;
8528#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008529
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008530 // Ignore the command when already in Insert mode. Inserting an
8531 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008532 if (State & INSERT)
8533 return;
8534
Bram Moolenaar64969662005-12-14 21:59:55 +00008535 if (eap->cmdidx == CMD_startinsert)
8536 restart_edit = 'a';
8537 else if (eap->cmdidx == CMD_startreplace)
8538 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008540 restart_edit = 'V';
8541
8542 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008544 if (eap->cmdidx == CMD_startinsert)
8545 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008546 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008548
8549 if (VIsual_active)
8550 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551}
8552
8553/*
8554 * ":stopinsert"
8555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008557ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558{
8559 restart_edit = 0;
8560 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008561 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008564/*
8565 * Execute normal mode command "cmd".
8566 * "remap" can be REMAP_NONE or REMAP_YES.
8567 */
8568 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008569exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008570{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008571 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008572 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008573 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008574}
Bram Moolenaar25281632016-01-21 23:32:32 +01008575
Bram Moolenaar25281632016-01-21 23:32:32 +01008576/*
8577 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008578 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008579 */
8580 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008581exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008582{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008583 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008584 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008585
Bram Moolenaar905dd902019-04-07 14:21:47 +02008586 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8587 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008588 clear_oparg(&oa);
8589 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008590 while ((!stuff_empty()
8591 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008592 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008593 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008594 {
8595 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008596#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008597 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008598 && oa.op_type == OP_NOP && oa.regname == NUL
8599 && !VIsual_active)
8600 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008601 // If terminal_loop() returns OK we got a key that is handled
8602 // in Normal model. With FAIL we first need to position the
8603 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008604 if (terminal_loop(TRUE) == OK)
8605 normal_cmd(&oa, TRUE);
8606 }
8607 else
8608#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008609 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008610 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008611 }
8612}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008613
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614#ifdef FEAT_FIND_ID
8615 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008616ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617{
8618 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8619 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8620 (linenr_T)1, (linenr_T)MAXLNUM);
8621}
8622
Bram Moolenaar4033c552017-09-16 20:54:51 +02008623#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624/*
8625 * ":psearch"
8626 */
8627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008628ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629{
8630 g_do_tagpreview = p_pvh;
8631 ex_findpat(eap);
8632 g_do_tagpreview = 0;
8633}
8634#endif
8635
8636 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008637ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638{
8639 int whole = TRUE;
8640 long n;
8641 char_u *p;
8642 int action;
8643
8644 switch (cmdnames[eap->cmdidx].cmd_name[2])
8645 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008646 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8648 action = ACTION_GOTO;
8649 else
8650 action = ACTION_SHOW;
8651 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008652 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 action = ACTION_SHOW_ALL;
8654 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008655 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 action = ACTION_GOTO;
8657 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008658 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 action = ACTION_SPLIT;
8660 break;
8661 }
8662
8663 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008664 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 {
8666 n = getdigits(&eap->arg);
8667 eap->arg = skipwhite(eap->arg);
8668 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008669 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 {
8671 whole = FALSE;
8672 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008673 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 if (*p)
8675 {
8676 *p++ = NUL;
8677 p = skipwhite(p);
8678
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008679 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008680 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008681 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02008683 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 }
8685 }
8686 if (!eap->skip)
8687 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8688 whole, !eap->forceit,
8689 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8690 n, action, eap->line1, eap->line2);
8691}
8692#endif
8693
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694
Bram Moolenaar4033c552017-09-16 20:54:51 +02008695#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696/*
8697 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8698 */
8699 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008700ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008702 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8704}
8705
8706/*
8707 * ":pedit"
8708 */
8709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008710ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711{
8712 win_T *curwin_save = curwin;
8713
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008714 if (ERROR_IF_ANY_POPUP_WINDOW)
8715 return;
8716
Bram Moolenaar026587b2019-08-17 15:08:00 +02008717 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008719 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008720
Bram Moolenaar026587b2019-08-17 15:08:00 +02008721 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008723
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 if (curwin != curwin_save && win_valid(curwin_save))
8725 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008726 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 validate_cursor();
8728 redraw_later(VALID);
8729 win_enter(curwin_save, TRUE);
8730 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008731# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008732 else if (WIN_IS_POPUP(curwin))
8733 {
8734 // can't keep focus in popup window
8735 win_enter(firstwin, TRUE);
8736 }
8737# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 g_do_tagpreview = 0;
8739}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741
8742/*
8743 * ":stag", ":stselect" and ":stjump".
8744 */
8745 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008746ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747{
8748 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008749 postponed_split_flags = cmdmod.cmod_split;
8750 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8752 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008753 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755
8756/*
8757 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8758 */
8759 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008760ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761{
8762 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8763}
8764
8765 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008766ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767{
8768 int cmd;
8769
8770 switch (name[1])
8771 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008772 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008774 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008776 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008778 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008780 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008782 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008784 case 'f': // ":tfirst"
8785 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008787 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008789 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008791 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008793 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 return;
8795 }
8796#endif
8797 cmd = DT_TAG;
8798 break;
8799 }
8800
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008801 if (name[0] == 'l')
8802 {
8803#ifndef FEAT_QUICKFIX
8804 ex_ni(eap);
8805 return;
8806#else
8807 cmd = DT_LTAG;
8808#endif
8809 }
8810
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8812 eap->forceit, TRUE);
8813}
8814
8815/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008816 * Check "str" for starting with a special cmdline variable.
8817 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8818 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8819 */
8820 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008821find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008822{
8823 int len;
8824 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008825 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008826 "%",
8827#define SPEC_PERC 0
8828 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008829#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008830 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008831#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008832 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008833#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008834 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008835#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008836 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008837#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008838 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008839#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008840 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008841#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008842 "<stack>", // call stack
8843#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008844 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008845#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008846 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008847#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008848 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008849#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008850 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008851#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008852 "<SID>", // script ID: <SNR>123_
8853#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008854#ifdef FEAT_CLIENTSERVER
8855 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008856# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008857#endif
8858 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008859
K.Takataeeec2542021-06-02 13:28:16 +02008860 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008861 {
8862 len = (int)STRLEN(spec_str[i]);
8863 if (STRNCMP(src, spec_str[i], len) == 0)
8864 {
8865 *usedlen = len;
8866 return i;
8867 }
8868 }
8869 return -1;
8870}
8871
8872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 * Evaluate cmdline variables.
8874 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008875 * change "%" to curbuf->b_ffname
8876 * "#" to curwin->w_alt_fnum
8877 * "%%" to curwin->w_alt_fnum in Vim9 script
8878 * "<cword>" to word under the cursor
8879 * "<cWORD>" to WORD under the cursor
8880 * "<cexpr>" to C-expression under the cursor
8881 * "<cfile>" to path name under the cursor
8882 * "<sfile>" to sourced file name
8883 * "<stack>" to call stack
8884 * "<slnum>" to sourced file line number
8885 * "<afile>" to file name for autocommand
8886 * "<abuf>" to buffer number for autocommand
8887 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 *
8889 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8890 * "" for error without a message) and NULL is returned.
8891 * Returns an allocated string if a valid match was found.
8892 * Returns NULL if no match was found. "usedlen" then still contains the
8893 * number of characters to skip.
8894 */
8895 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008896eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008897 char_u *src, // pointer into commandline
8898 char_u *srcstart, // beginning of valid memory for src
8899 int *usedlen, // characters after src that are used
8900 linenr_T *lnump, // line number for :e command, or NULL
8901 char **errormsg, // pointer to error message
8902 int *escaped) // return value has escaped white space (can
8903 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904{
8905 int i;
8906 char_u *s;
8907 char_u *result;
8908 char_u *resultbuf = NULL;
8909 int resultlen;
8910 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008911 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008913 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916
8917 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008918 if (escaped != NULL)
8919 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920
8921 /*
8922 * Check if there is something to do.
8923 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008924 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008925 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 {
8927 *usedlen = 1;
8928 return NULL;
8929 }
8930
8931 /*
8932 * Skip when preceded with a backslash "\%" and "\#".
8933 * Note: In "\\%" the % is also not recognized!
8934 */
8935 if (src > srcstart && src[-1] == '\\')
8936 {
8937 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008938 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 return NULL;
8940 }
8941
8942 /*
8943 * word or WORD under cursor
8944 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008945 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8946 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008948 resultlen = find_ident_under_cursor(&result,
8949 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8950 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8951 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 if (resultlen == 0)
8953 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008954 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 return NULL;
8956 }
8957 }
8958
8959 /*
8960 * '#': Alternate file name
8961 * '%': Current file name
8962 * File name under the cursor
8963 * File name for autocommand
8964 * and following modifiers
8965 */
8966 else
8967 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008968 int off = 0;
8969
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 switch (spec_idx)
8971 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008972 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008973#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008974 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008977 // '%': current file
8978 if (curbuf->b_fname == NULL)
8979 {
8980 result = (char_u *)"";
8981 valid = 0; // Must have ":p:h" to be valid
8982 }
8983 else
8984 {
8985 result = curbuf->b_fname;
8986 tilde_file = STRCMP(result, "~") == 0;
8987 }
8988 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008990#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008991 // "%%" alternate file
8992 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008993#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01008994 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008995 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008996 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008998 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 result = arg_all();
9000 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009001 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009002 if (escaped != NULL)
9003 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 break;
9006 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009007 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009008 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009009 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009011 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009012 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009013 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009014 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009016 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009018 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009019 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009020 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009021 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009022 return NULL;
9023 }
9024#ifdef FEAT_EVAL
9025 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9026 (long)i);
9027 if (result == NULL)
9028 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009029 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009030 return NULL;
9031 }
9032#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01009033 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 }
9037 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009038 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009039 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9040 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009041 buf = buflist_findnr(i);
9042 if (buf == NULL)
9043 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009044 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00009045 return NULL;
9046 }
9047 if (lnump != NULL)
9048 *lnump = ECMD_LAST;
9049 if (buf->b_fname == NULL)
9050 {
9051 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009052 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009053 }
9054 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009055 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009056 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009057 tilde_file = STRCMP(result, "~") == 0;
9058 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 break;
9061
9062#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009063 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009064 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 if (result == NULL)
9066 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009067 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 return NULL;
9069 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009070 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 break;
9072#endif
9073
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009074 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009076 if (result != NULL && !autocmd_fname_full)
9077 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009078 // Still need to turn the fname into a full path. It is
9079 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009080 autocmd_fname_full = TRUE;
9081 result = FullName_save(autocmd_fname, FALSE);
9082 vim_free(autocmd_fname);
9083 autocmd_fname = result;
9084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 if (result == NULL)
9086 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009087 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 return NULL;
9089 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009090 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 break;
9092
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009093 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 if (autocmd_bufnr <= 0)
9095 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009096 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 return NULL;
9098 }
9099 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9100 result = strbuf;
9101 break;
9102
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009103 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 result = autocmd_match;
9105 if (result == NULL)
9106 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009107 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 return NULL;
9109 }
9110 break;
9111
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009112 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02009113 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02009114 result = estack_sfile(spec_idx == SPEC_SFILE
9115 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 if (result == NULL)
9117 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02009118 *errormsg = spec_idx == SPEC_SFILE
9119 ? _("E498: no :source file name to substitute for \"<sfile>\"")
9120 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 return NULL;
9122 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009123 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009125
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009126 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009127 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009128 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009129 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009130 return NULL;
9131 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009132 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009133 result = strbuf;
9134 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009135
9136#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009137 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009138 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009139 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009140 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009141 return NULL;
9142 }
9143 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009144 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009145 result = strbuf;
9146 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009147
Bram Moolenaar90944302020-08-01 20:45:11 +02009148 case SPEC_SID:
9149 if (current_sctx.sc_sid <= 0)
9150 {
9151 *errormsg = _(e_usingsid);
9152 return NULL;
9153 }
9154 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9155 result = strbuf;
9156 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009157#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009158
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009159#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009160 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009161 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9162 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 result = strbuf;
9164 break;
9165#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009166
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009167 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009168 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009169 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 }
9171
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009172 resultlen = (int)STRLEN(result); // length of new string
9173 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 {
9175 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 resultlen = (int)(s - result);
9178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 else if (!skip_mod)
9180 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009181 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 &resultlen);
9183 if (result == NULL)
9184 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009185 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 return NULL;
9187 }
9188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 }
9190
9191 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9192 {
9193 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009194 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009195 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009197 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 result = NULL;
9199 }
9200 else
9201 result = vim_strnsave(result, resultlen);
9202 vim_free(resultbuf);
9203 return result;
9204}
9205
9206/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 * Expand the <sfile> string in "arg".
9208 *
9209 * Returns an allocated string, or NULL for any error.
9210 */
9211 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009212expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009214 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 int len;
9216 char_u *result;
9217 char_u *newres;
9218 char_u *repl;
9219 int srclen;
9220 char_u *p;
9221
9222 result = vim_strsave(arg);
9223 if (result == NULL)
9224 return NULL;
9225
9226 for (p = result; *p; )
9227 {
9228 if (STRNCMP(p, "<sfile>", 7) != 0)
9229 ++p;
9230 else
9231 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009232 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00009233 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 if (errormsg != NULL)
9235 {
9236 if (*errormsg)
9237 emsg(errormsg);
9238 vim_free(result);
9239 return NULL;
9240 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009241 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 {
9243 p += srclen;
9244 continue;
9245 }
9246 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9247 newres = alloc(len);
9248 if (newres == NULL)
9249 {
9250 vim_free(repl);
9251 vim_free(result);
9252 return NULL;
9253 }
9254 mch_memmove(newres, result, (size_t)(p - result));
9255 STRCPY(newres + (p - result), repl);
9256 len = (int)STRLEN(newres);
9257 STRCAT(newres, p + srclen);
9258 vim_free(repl);
9259 vim_free(result);
9260 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009261 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 }
9263 }
9264
9265 return result;
9266}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009269/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009270 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009271 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009272 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009274dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 if (fname == NULL)
9277 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009278 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279}
9280#endif
9281
9282/*
9283 * ":behave {mswin,xterm}"
9284 */
9285 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009286ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287{
9288 if (STRCMP(eap->arg, "mswin") == 0)
9289 {
9290 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9291 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9292 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9293 set_option_value((char_u *)"keymodel", 0L,
9294 (char_u *)"startsel,stopsel", 0);
9295 }
9296 else if (STRCMP(eap->arg, "xterm") == 0)
9297 {
9298 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9299 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9300 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9301 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9302 }
9303 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009304 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305}
9306
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307static int filetype_detect = FALSE;
9308static int filetype_plugin = FALSE;
9309static int filetype_indent = FALSE;
9310
9311/*
9312 * ":filetype [plugin] [indent] {on,off,detect}"
9313 * on: Load the filetype.vim file to install autocommands for file types.
9314 * off: Load the ftoff.vim file to remove all autocommands for file types.
9315 * plugin on: load filetype.vim and ftplugin.vim
9316 * plugin off: load ftplugof.vim
9317 * indent on: load filetype.vim and indent.vim
9318 * indent off: load indoff.vim
9319 */
9320 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009321ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322{
9323 char_u *arg = eap->arg;
9324 int plugin = FALSE;
9325 int indent = FALSE;
9326
9327 if (*eap->arg == NUL)
9328 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009329 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009330 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 filetype_detect ? "ON" : "OFF",
9332 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9333 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9334 return;
9335 }
9336
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009337 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 for (;;)
9339 {
9340 if (STRNCMP(arg, "plugin", 6) == 0)
9341 {
9342 plugin = TRUE;
9343 arg = skipwhite(arg + 6);
9344 continue;
9345 }
9346 if (STRNCMP(arg, "indent", 6) == 0)
9347 {
9348 indent = TRUE;
9349 arg = skipwhite(arg + 6);
9350 continue;
9351 }
9352 break;
9353 }
9354 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9355 {
9356 if (*arg == 'o' || !filetype_detect)
9357 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009358 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 filetype_detect = TRUE;
9360 if (plugin)
9361 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009362 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 filetype_plugin = TRUE;
9364 }
9365 if (indent)
9366 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009367 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 filetype_indent = TRUE;
9369 }
9370 }
9371 if (*arg == 'd')
9372 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009373 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009374 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 }
9376 }
9377 else if (STRCMP(arg, "off") == 0)
9378 {
9379 if (plugin || indent)
9380 {
9381 if (plugin)
9382 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009383 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 filetype_plugin = FALSE;
9385 }
9386 if (indent)
9387 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009388 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 filetype_indent = FALSE;
9390 }
9391 }
9392 else
9393 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009394 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 filetype_detect = FALSE;
9396 }
9397 }
9398 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009399 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400}
9401
9402/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009403 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 */
9405 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009406ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407{
9408 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009409 {
9410 char_u *arg = eap->arg;
9411
9412 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9413 arg += 9;
9414
9415 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9416 if (arg != eap->arg)
9417 did_filetype = FALSE;
9418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009422ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423{
9424#ifdef FEAT_DIGRAPHS
9425 if (*eap->arg != NUL)
9426 putdigraph(eap->arg);
9427 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009428 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429#else
mityu61065042021-07-19 20:07:21 +02009430 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431#endif
9432}
9433
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009434#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9435 void
9436set_no_hlsearch(int flag)
9437{
9438 no_hlsearch = flag;
9439# ifdef FEAT_EVAL
9440 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9441# endif
9442}
9443
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444/*
9445 * ":nohlsearch"
9446 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009448ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009450 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009451 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453#endif
9454
9455#ifdef FEAT_CRYPT
9456/*
9457 * ":X": Get crypt key
9458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009460ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009462 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009463 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464}
9465#endif
9466
9467#ifdef FEAT_FOLDING
9468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009469ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470{
9471 if (foldManualAllowed(TRUE))
9472 foldCreate(eap->line1, eap->line2);
9473}
9474
9475 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009476ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477{
9478 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9479 eap->forceit, FALSE);
9480}
9481
9482 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009483ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484{
9485 linenr_T lnum;
9486
Bram Moolenaar4facea32019-10-12 20:17:40 +02009487# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009488 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009489# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009490
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009491 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9493 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9494 ml_setmarked(lnum);
9495
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009496 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009498 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009499# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009500 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009501# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502}
9503#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009504
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009505#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009506/*
9507 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9508 * instead of a quickfix command.
9509 */
9510 int
9511is_loclist_cmd(int cmdidx)
9512{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009513 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009514 return FALSE;
9515 return cmdnames[cmdidx].cmd_name[0] == 'l';
9516}
9517#endif
9518
Bram Moolenaar4facea32019-10-12 20:17:40 +02009519#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009520 int
9521get_pressedreturn(void)
9522{
9523 return ex_pressedreturn;
9524}
9525
9526 void
9527set_pressedreturn(int val)
9528{
9529 ex_pressedreturn = val;
9530}
9531#endif