blob: fbfba52dd4063401c08aeb36a42d47f667c8607e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +020023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaar20b23c62020-08-20 15:25:00 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
39static void ex_bmodified(exarg_T *eap);
40static void ex_bnext(exarg_T *eap);
41static void ex_bprevious(exarg_T *eap);
42static void ex_brewind(exarg_T *eap);
43static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifndef FEAT_QUICKFIX
47# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000048# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# define ex_cc ex_ni
50# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020051# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cfile ex_ni
53# define qf_list ex_ni
54# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020055# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000057# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020059#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define ex_cclose ex_ni
61# define ex_copen ex_ni
62# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020063# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000065#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
66# define ex_cexpr ex_ni
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020069static linenr_T default_address(exarg_T *eap);
Bram Moolenaarb7316892019-05-01 18:08:42 +020070static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020071static void address_default_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010072static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020073#if !defined(FEAT_PERL) \
74 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
75 || !defined(FEAT_TCL) \
76 || !defined(FEAT_RUBY) \
77 || !defined(FEAT_LUA) \
78 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000079# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010080static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010082static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000086#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010087static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
88static void ex_highlight(exarg_T *eap);
89static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_cquit(exarg_T *eap);
91static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010092static void ex_close(exarg_T *eap);
93static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
94static void ex_only(exarg_T *eap);
95static void ex_resize(exarg_T *eap);
96static void ex_stag(exarg_T *eap);
97static void ex_tabclose(exarg_T *eap);
98static void ex_tabonly(exarg_T *eap);
99static void ex_tabnext(exarg_T *eap);
100static void ex_tabmove(exarg_T *eap);
101static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200102#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100103static void ex_pclose(exarg_T *eap);
104static void ex_ptag(exarg_T *eap);
105static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106#else
107# define ex_pclose ex_ni
108# define ex_ptag ex_ni
109# define ex_pedit ex_ni
110#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100111static void ex_hide(exarg_T *eap);
112static void ex_stop(exarg_T *eap);
113static void ex_exit(exarg_T *eap);
114static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100116static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#else
118# define ex_goto ex_ni
119#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100120static void ex_shell(exarg_T *eap);
121static void ex_preserve(exarg_T *eap);
122static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100123static void ex_mode(exarg_T *eap);
124static void ex_wrongmodifier(exarg_T *eap);
125static void ex_find(exarg_T *eap);
126static void ex_open(exarg_T *eap);
127static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#ifndef FEAT_GUI
129# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100132#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100133static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#else
135# define ex_tearoff ex_ni
136#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100137#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
138 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#else
141# define ex_popup ex_ni
142#endif
143#ifndef FEAT_GUI_MSWIN
144# define ex_simalt ex_ni
145#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000146#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147# define gui_mch_find_dialog ex_ni
148# define gui_mch_replace_dialog ex_ni
149#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000150#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151# define ex_helpfind ex_ni
152#endif
153#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100154# define ex_cscope ex_ni
155# define ex_scscope ex_ni
156# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#endif
158#ifndef FEAT_SYN_HL
159# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200160# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000161#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200162#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200163# define ex_syntime ex_ni
164#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000165#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000166# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000167# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000168# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000169# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000170# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000171#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200172#ifndef FEAT_PERSISTENT_UNDO
173# define ex_rundo ex_ni
174# define ex_wundo ex_ni
175#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200176#ifndef FEAT_LUA
177# define ex_lua ex_script_ni
178# define ex_luado ex_ni
179# define ex_luafile ex_ni
180#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000181#ifndef FEAT_MZSCHEME
182# define ex_mzscheme ex_script_ni
183# define ex_mzfile ex_ni
184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#ifndef FEAT_PERL
186# define ex_perl ex_script_ni
187# define ex_perldo ex_ni
188#endif
189#ifndef FEAT_PYTHON
190# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200191# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192# define ex_pyfile ex_ni
193#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200194#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200195# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200196# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200197# define ex_py3file ex_ni
198#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100199#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
200# define ex_pyx ex_script_ni
201# define ex_pyxdo ex_ni
202# define ex_pyxfile ex_ni
203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#ifndef FEAT_TCL
205# define ex_tcl ex_script_ni
206# define ex_tcldo ex_ni
207# define ex_tclfile ex_ni
208#endif
209#ifndef FEAT_RUBY
210# define ex_ruby ex_script_ni
211# define ex_rubydo ex_ni
212# define ex_rubyfile ex_ni
213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#ifndef FEAT_KEYMAP
215# define ex_loadkeymap ex_ni
216#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100217static void ex_swapname(exarg_T *eap);
218static void ex_syncbind(exarg_T *eap);
219static void ex_read(exarg_T *eap);
220static void ex_pwd(exarg_T *eap);
221static void ex_equal(exarg_T *eap);
222static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000225#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#else
228# define ex_winpos ex_ni
229#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100230static void ex_operators(exarg_T *eap);
231static void ex_put(exarg_T *eap);
232static void ex_copymove(exarg_T *eap);
233static void ex_submagic(exarg_T *eap);
234static void ex_join(exarg_T *eap);
235static void ex_at(exarg_T *eap);
236static void ex_bang(exarg_T *eap);
237static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200238#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100239static void ex_wundo(exarg_T *eap);
240static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200241#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ex_redo(exarg_T *eap);
243static void ex_later(exarg_T *eap);
244static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100246static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100249static void ex_startinsert(exarg_T *eap);
250static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100252static void ex_checkpath(exarg_T *eap);
253static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254#else
255# define ex_findpat ex_ni
256# define ex_checkpath ex_ni
257#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200258#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100259static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260#else
261# define ex_psearch ex_ni
262#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static void ex_tag(exarg_T *eap);
264static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#ifndef FEAT_EVAL
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200266# define ex_block ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200267# define ex_break ex_ni
268# define ex_breakadd ex_ni
269# define ex_breakdel ex_ni
270# define ex_breaklist ex_ni
271# define ex_call ex_ni
272# define ex_catch ex_ni
273# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200274# define ex_continue ex_ni
275# define ex_debug ex_ni
276# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200277# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200278# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_echo ex_ni
281# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282# define ex_else ex_ni
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200283# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200284# define ex_endfunction ex_ni
285# define ex_endif ex_ni
286# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200288# define ex_eval ex_ni
289# define ex_execute ex_ni
Bram Moolenaar340c59e2021-04-24 20:37:03 +0200290# define ex_incdec ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200291# define ex_finally ex_ni
292# define ex_finish ex_ni
293# define ex_function ex_ni
294# define ex_if ex_ni
295# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200296# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200297# define ex_lockvar ex_ni
298# define ex_oldfiles ex_ni
299# define ex_options ex_ni
300# define ex_packadd ex_ni
301# define ex_packloadall ex_ni
302# define ex_return ex_ni
303# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304# define ex_throw ex_ni
305# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000307# define ex_unlockvar ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200308# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100309# define ex_import ex_ni
310# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200312#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313# define ex_loadview ex_ni
314#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200315#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316# define ex_viminfo ex_ni
317#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100319static void ex_filetype(exarg_T *eap);
320static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000322# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323# define ex_diffpatch ex_ni
324# define ex_diffgetput ex_ni
325# define ex_diffsplit ex_ni
326# define ex_diffthis ex_ni
327# define ex_diffupdate ex_ni
328#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100329static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100331static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332#else
333# define ex_nohlsearch ex_ni
334# define ex_match ex_ni
335#endif
336#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100337static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338#else
339# define ex_X ex_ni
340#endif
341#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100342static void ex_fold(exarg_T *eap);
343static void ex_foldopen(exarg_T *eap);
344static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345#else
346# define ex_fold ex_ni
347# define ex_foldopen ex_ni
348# define ex_folddo ex_ni
349#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100350#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351# define ex_language ex_ni
352#endif
353#ifndef FEAT_SIGNS
354# define ex_sign ex_ni
355#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200357# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000358# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200359# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362#ifndef FEAT_JUMPLIST
363# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200364# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365# define ex_changes ex_ni
366#endif
367
Bram Moolenaar05159a02005-02-26 23:04:13 +0000368#ifndef FEAT_PROFILE
369# define ex_profile ex_ni
370#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200371#ifndef FEAT_TERMINAL
372# define ex_terminal ex_ni
373#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200374#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
375# define ex_xrestore ex_ni
376#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100377#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200378# define ex_popupclear ex_ni
379#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000380
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381/*
382 * Declare cmdnames[].
383 */
384#define DO_DECLARE_EXCMD
385#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200386#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100387
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388static char_u dollar_command[2] = {'$', 0};
389
390
391#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100392// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393typedef struct
394{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100395 char_u *line; // command line
396 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397} wcmd_T;
398
399/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000402 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000404struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100406 garray_T *lines_gap; // growarray with line info
407 int current_line; // last read line from growarray
408 int repeating; // TRUE when looping a second time
409 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200410 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 void *cookie;
412};
413
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200414static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100415static int store_loop_line(garray_T *gap, char_u *line);
416static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000417
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100418// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000419struct dbg_stuff
420{
421 int trylevel;
422 int force_abort;
423 except_T *caught_stack;
424 char_u *vv_exception;
425 char_u *vv_throwpoint;
426 int did_emsg;
427 int got_int;
428 int did_throw;
429 int need_rethrow;
430 int check_cstack;
431 except_T *current_exception;
432};
433
Bram Moolenaared203462004-06-16 11:19:22 +0000434 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100435save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000436{
437 dsp->trylevel = trylevel; trylevel = 0;
438 dsp->force_abort = force_abort; force_abort = FALSE;
439 dsp->caught_stack = caught_stack; caught_stack = NULL;
440 dsp->vv_exception = v_exception(NULL);
441 dsp->vv_throwpoint = v_throwpoint(NULL);
442
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100443 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000444 dsp->did_emsg = did_emsg; did_emsg = FALSE;
445 dsp->got_int = got_int; got_int = FALSE;
446 dsp->did_throw = did_throw; did_throw = FALSE;
447 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
448 dsp->check_cstack = check_cstack; check_cstack = FALSE;
449 dsp->current_exception = current_exception; current_exception = NULL;
450}
451
452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100453restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000454{
455 suppress_errthrow = FALSE;
456 trylevel = dsp->trylevel;
457 force_abort = dsp->force_abort;
458 caught_stack = dsp->caught_stack;
459 (void)v_exception(dsp->vv_exception);
460 (void)v_throwpoint(dsp->vv_throwpoint);
461 did_emsg = dsp->did_emsg;
462 got_int = dsp->got_int;
463 did_throw = dsp->did_throw;
464 need_rethrow = dsp->need_rethrow;
465 check_cstack = dsp->check_cstack;
466 current_exception = dsp->current_exception;
467}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468#endif
469
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470/*
471 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
472 * command is given.
473 */
474 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100475do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100476 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477{
478 int save_msg_scroll;
479 int prev_msg_row;
480 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100481 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000482
483 if (improved)
484 exmode_active = EXMODE_VIM;
485 else
486 exmode_active = EXMODE_NORMAL;
487 State = NORMAL;
488
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100489 // When using ":global /pat/ visual" and then "Q" we return to continue
490 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000491 if (global_busy)
492 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493
494 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100495 ++RedrawingDisabled; // don't redisplay the window
496 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100498 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 ++hold_gui_events;
500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501
Bram Moolenaar32526b32019-01-19 17:43:09 +0100502 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 while (exmode_active)
504 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100505 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000506 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
507 {
508 exmode_active = FALSE;
509 break;
510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 msg_scroll = TRUE;
512 need_wait_return = FALSE;
513 ex_pressedreturn = FALSE;
514 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100515 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 prev_msg_row = msg_row;
517 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 if (improved)
519 {
520 cmdline_row = msg_row;
521 do_cmdline(NULL, getexline, NULL, 0);
522 }
523 else
524 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
525 lines_left = Rows - 1;
526
Bram Moolenaardf177f62005-02-22 08:39:57 +0000527 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100528 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000530 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100531 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000532 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000534 if (ex_pressedreturn)
535 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100536 // go up one line, to overwrite the ":<CR>" line, so the
537 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000538 msg_row = prev_msg_row;
539 if (prev_msg_row == Rows - 1)
540 msg_row--;
541 }
542 msg_col = 0;
543 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
544 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100547 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000548 {
549 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100550 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000551 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100552 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 }
555
556#ifdef FEAT_GUI
557 --hold_gui_events;
558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 --RedrawingDisabled;
560 --no_wait_return;
561 update_screen(CLEAR);
562 need_wait_return = FALSE;
563 msg_scroll = save_msg_scroll;
564}
565
566/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200567 * Print the executed command for when 'verbose' is set.
568 * When "lnum" is 0 only print the command.
569 */
570 static void
571msg_verbose_cmd(linenr_T lnum, char_u *cmd)
572{
573 ++no_wait_return;
574 verbose_enter_scroll();
575
576 if (lnum == 0)
577 smsg(_("Executing: %s"), cmd);
578 else
579 smsg(_("line %ld: %s"), (long)lnum, cmd);
580 if (msg_silent == 0)
581 msg_puts("\n"); // don't overwrite this
582
583 verbose_leave_scroll();
584 --no_wait_return;
585}
586
587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 * Execute a simple command line. Used for translated commands like "*".
589 */
590 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100591do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592{
593 return do_cmdline(cmd, NULL, NULL,
594 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
595}
596
597/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100598 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
599 * This allows for using a range without ":" in Vim9 script.
600 */
601 int
602do_cmd_argument(char_u *cmd)
603{
604 return do_cmdline(cmd, NULL, NULL,
605 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
606}
607
608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 * do_cmdline(): execute one Ex command line
610 *
611 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100612 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 * 2. Split up in parts separated with '|'.
614 *
615 * This function can be called recursively!
616 *
617 * flags:
618 * DOCMD_VERBOSE - The command will be included in the error message.
619 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100620 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 * DOCMD_KEYTYPED - Don't reset KeyTyped.
622 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
623 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
624 *
625 * return FAIL if cmdline could not be executed, OK otherwise
626 */
627 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100628do_cmdline(
629 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200630 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100631 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100632 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100634 char_u *next_cmdline; // next cmd to execute
635 char_u *cmdline_copy = NULL; // copy of cmd line
636 int used_getline = FALSE; // used "fgetline" to obtain command
637 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100639 int count = 0; // line number count
640 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 int retval = OK;
642#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100643 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100644 garray_T lines_ga; // keep lines for ":while"/":for"
645 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200646 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100647 char_u *fname = NULL; // function or script name
648 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
649 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
650 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200652 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200653 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100655 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200656 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000658 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000660 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100662# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663# define cmd_cookie cookie
664#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100665 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200666#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100667 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100669 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
670 // location for storing error messages to be converted to an exception.
671 // This ensures that the do_errthrow() call in do_one_cmd() does not
672 // combine the messages stored by an earlier invocation of do_one_cmd()
673 // with the command name of the later one. This would happen when
674 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 saved_msg_list = msg_list;
676 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677#endif
678
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100679 // It's possible to create an endless loop with ":execute", catch that
680 // here. The value of 200 allows nested function calls, ":source", etc.
681 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100682 if (call_depth >= 200
683#ifdef FEAT_EVAL
684 && call_depth >= p_mfd
685#endif
686 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100688 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100690 // When converting to an exception, we do not include the command name
691 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100692 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 msg_list = saved_msg_list;
694#endif
695 return FAIL;
696 }
697 ++call_depth;
698
699#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100700 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 cstack.cs_idx = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
703
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100704 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100706 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100707 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000708 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 ++ex_nesting_level;
710
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100711 // Get the function or script name and the address where the next breakpoint
712 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000713 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 {
715 fname = func_name(real_cookie);
716 breakpoint = func_breakpoint(real_cookie);
717 dbg_tick = func_dbg_tick(real_cookie);
718 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100719 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100721 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 breakpoint = source_breakpoint(real_cookie);
723 dbg_tick = source_dbg_tick(real_cookie);
724 }
725
726 /*
727 * Initialize "force_abort" and "suppress_errthrow" at the top level.
728 */
729 if (!recursive)
730 {
731 force_abort = FALSE;
732 suppress_errthrow = FALSE;
733 }
734
735 /*
736 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000737 * exception handling (used when debugging). Otherwise clear it to avoid
738 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000740 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000741 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000742 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200743 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744
745 initial_trylevel = trylevel;
746
747 /*
748 * "did_throw" will be set to TRUE when an exception is being thrown.
749 */
750 did_throw = FALSE;
751#endif
752 /*
753 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000754 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 * If force_abort is set, we cancel everything.
756 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100757#ifdef FEAT_EVAL
758 did_emsg_cumul += did_emsg;
759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 did_emsg = FALSE;
761
762 /*
763 * KeyTyped is only set when calling vgetc(). Reset it here when not
764 * calling vgetc() (sourced command lines).
765 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100766 if (!(flags & DOCMD_KEYTYPED)
767 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 KeyTyped = FALSE;
769
770 /*
771 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000772 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 * - for multiple commands on one line, separated with '|'
774 * - when repeating until there are no more lines (for ":source")
775 */
776 next_cmdline = cmdline;
777 do
778 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000779#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100780 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000781#endif
782
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100783 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 if (next_cmdline == NULL
785#ifdef FEAT_EVAL
786 && !force_abort
787 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000788 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789#endif
790 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100791 {
792#ifdef FEAT_EVAL
793 did_emsg_cumul += did_emsg;
794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797
798 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000799 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100800 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 * 3. If a line is given: Make a copy, so we can mess with it.
802 */
803
804#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100805 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000806 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100808 // Each '|' separated command is stored separately in lines_ga, to
809 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100810 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100812 // Check if a function has returned or, unless it has an unclosed
813 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000814 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000816# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000817 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818 func_line_end(real_cookie);
819# endif
820 if (func_has_ended(real_cookie))
821 {
822 retval = FAIL;
823 break;
824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000827 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100828 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000829 script_line_end();
830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100832 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100833 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 {
835 retval = FAIL;
836 break;
837 }
838
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100839 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 if (breakpoint != NULL && dbg_tick != NULL
841 && *dbg_tick != debug_tick)
842 {
843 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100844 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100845 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 *dbg_tick = debug_tick;
847 }
848
849 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100850 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100852 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100854 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100856 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100857 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100859 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100860 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 *dbg_tick = debug_tick;
862 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000863# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000864 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000865 {
866 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100867 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100868 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000869 script_line_start();
870 }
871# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873#endif
874
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100875 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (next_cmdline == NULL)
877 {
878 /*
879 * Need to set msg_didout for the first line after an ":if",
880 * otherwise the ":if" will be overwritten.
881 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100882 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100884 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885#ifdef FEAT_EVAL
886 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
887#else
888 0
889#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100890 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
891 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100893 // Don't call wait_return for aborted command line. The NULL
894 // returned for the end of a sourced file or executed function
895 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 if (KeyTyped && !(flags & DOCMD_REPEAT))
897 need_wait_return = FALSE;
898 retval = FAIL;
899 break;
900 }
901 used_getline = TRUE;
902
903 /*
904 * Keep the first typed line. Clear it when more lines are typed.
905 */
906 if (flags & DOCMD_KEEPLINE)
907 {
908 vim_free(repeat_cmdline);
909 if (count == 0)
910 repeat_cmdline = vim_strsave(next_cmdline);
911 else
912 repeat_cmdline = NULL;
913 }
914 }
915
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100916 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 else if (cmdline_copy == NULL)
918 {
919 next_cmdline = vim_strsave(next_cmdline);
920 if (next_cmdline == NULL)
921 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200922 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 retval = FAIL;
924 break;
925 }
926 }
927 cmdline_copy = next_cmdline;
928
929#ifdef FEAT_EVAL
930 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200931 * Inside a while/for loop, and when the command looks like a ":while"
932 * or ":for", the line is stored, because we may need it later when
933 * looping.
934 *
935 * When there is a '|' and another command, it is stored separately,
936 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000937 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200938 *
939 * Pass a different "fgetline" function to do_one_cmd() below,
940 * that it stores lines in or reads them from "lines_ga". Makes it
941 * possible to define a function inside a while/for loop and handles
942 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200944 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200946 cmd_getline = get_loop_line;
947 cmd_cookie = (void *)&cmd_loop_cookie;
948 cmd_loop_cookie.lines_gap = &lines_ga;
949 cmd_loop_cookie.current_line = current_line;
950 cmd_loop_cookie.getline = fgetline;
951 cmd_loop_cookie.cookie = cookie;
952 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
953
954 // Save the current line when encountering it the first time.
955 if (current_line == lines_ga.ga_len
956 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 {
958 retval = FAIL;
959 break;
960 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200961 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200963 else
964 {
965 cmd_getline = fgetline;
966 cmd_cookie = cookie;
967 }
968
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 did_endif = FALSE;
970#endif
971
972 if (count++ == 0)
973 {
974 /*
975 * All output from the commands is put below each other, without
976 * waiting for a return. Don't do this when executing commands
977 * from a script or when being called recursive (e.g. for ":e
978 * +command file").
979 */
980 if (!(flags & DOCMD_NOWAIT) && !recursive)
981 {
982 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100983 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100985 msg_scroll = TRUE; // put messages below each other
986 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 ++RedrawingDisabled;
988 did_inc = TRUE;
989 }
990 }
991
Bram Moolenaar823654b2020-05-29 23:03:09 +0200992 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100993 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994
995 /*
996 * 2. Execute one '|' separated command.
997 * do_one_cmd() will return NULL if there is no trailing '|'.
998 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
999 */
1000 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001001 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002#ifdef FEAT_EVAL
1003 &cstack,
1004#endif
1005 cmd_getline, cmd_cookie);
1006 --recursive;
1007
1008#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001009 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001010 // Use "current_line" from "cmd_loop_cookie", it may have been
1011 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001012 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013#endif
1014
1015 if (next_cmdline == NULL)
1016 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001017 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001018
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 /*
1020 * If the command was typed, remember it for the ':' register.
1021 * Do this AFTER executing the command to make :@: work.
1022 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001023 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 && new_last_cmdline != NULL)
1025 {
1026 vim_free(last_cmdline);
1027 last_cmdline = new_last_cmdline;
1028 new_last_cmdline = NULL;
1029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 }
1031 else
1032 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001033 // need to copy the command after the '|' to cmdline_copy, for the
1034 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001035 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 next_cmdline = cmdline_copy;
1037 }
1038
1039
1040#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001041 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001043 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001045 {
1046 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001050 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 {
1052 ++current_line;
1053
1054 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001055 * An ":endwhile", ":endfor" and ":continue" is handled here.
1056 * If we were executing commands, jump back to the ":while" or
1057 * ":for".
1058 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001060 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001062 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001064 // Jump back to the matching ":while" or ":for". Be careful
1065 // not to use a cs_line[] from an entry that isn't a ":while"
1066 // or ":for": It would make "current_line" invalid and can
1067 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 if (!did_emsg && !got_int && !did_throw
1069 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001070 && (cstack.cs_flags[cstack.cs_idx]
1071 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 && cstack.cs_line[cstack.cs_idx] >= 0
1073 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1074 {
1075 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001076 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001077 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001078 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001080 // Check for the next breakpoint at or after the ":while"
1081 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 if (breakpoint != NULL)
1083 {
1084 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001085 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 fname,
1087 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1088 *dbg_tick = debug_tick;
1089 }
1090 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001091 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001093 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001095 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1096 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 }
1098 }
1099
1100 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001101 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001103 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001105 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001106 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 }
1108 }
1109
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001110 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001111 if (breakpoint != NULL && has_watchexpr())
1112 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001113 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001114 *dbg_tick = debug_tick;
1115 }
1116
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 /*
1118 * When not inside any ":while" loop, clear remembered lines.
1119 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001120 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 {
1122 if (lines_ga.ga_len > 0)
1123 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001124 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1126 free_cmdlines(&lines_ga);
1127 }
1128 current_line = 0;
1129 }
1130
1131 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001132 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1133 * being restored at the ":endtry". Reset them here and set the
1134 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1135 * This includes the case where a missing ":endif", ":endwhile" or
1136 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001138 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001140 cstack.cs_lflags &= ~CSL_HAD_FINA;
1141 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1142 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 did_throw ? (void *)current_exception : NULL);
1144 did_emsg = got_int = did_throw = FALSE;
1145 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1146 }
1147
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001148 // Update global "trylevel" for recursive calls to do_cmdline() from
1149 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 trylevel = initial_trylevel + cstack.cs_trylevel;
1151
1152 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001153 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 * files) is aborted because of an error, an interrupt, or an uncaught
1155 * exception, cancel everything. If it is left normally, reset
1156 * force_abort to get the non-EH compatible abortion behavior for
1157 * the rest of the script.
1158 */
1159 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1160 force_abort = FALSE;
1161
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001162 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001164#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165
1166 }
1167 /*
1168 * Continue executing command lines when:
1169 * - no CTRL-C typed, no aborting error, no exception thrown or try
1170 * conditionals need to be checked for executing finally clauses or
1171 * catching an interrupt exception
1172 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001173 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 * looping for ":source" command or function call.
1175 */
1176 while (!((got_int
1177#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001178 || (did_emsg && (force_abort || in_vim9script()))
1179 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180#endif
1181 )
1182#ifdef FEAT_EVAL
1183 && cstack.cs_trylevel == 0
1184#endif
1185 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001186 && !(did_emsg
1187#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001188 // Keep going when inside try/catch, so that the error can be
1189 // deal with, except when it is a syntax error, it may cause
1190 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001191 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1192#endif
1193 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001194 && (getline_equal(fgetline, cookie, getexmodeline)
1195 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 && (next_cmdline != NULL
1197#ifdef FEAT_EVAL
1198 || cstack.cs_idx >= 0
1199#endif
1200 || (flags & DOCMD_REPEAT)));
1201
1202 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001203 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204#ifdef FEAT_EVAL
1205 free_cmdlines(&lines_ga);
1206 ga_clear(&lines_ga);
1207
1208 if (cstack.cs_idx >= 0)
1209 {
1210 /*
1211 * If a sourced file or executed function ran to its end, report the
1212 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001213 * In Vim9 script do not give a second error, executing aborts after
1214 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 */
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001216 if (!got_int && !did_throw && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001217 && ((getline_equal(fgetline, cookie, getsourceline)
1218 && !source_finished(fgetline, cookie))
1219 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 && !func_has_ended(real_cookie))))
1221 {
1222 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001223 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001225 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001226 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001227 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001229 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 }
1231
1232 /*
1233 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1234 * ":endtry" in a sourced file or executed function. If the try
1235 * conditional is in its finally clause, ignore anything pending.
1236 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001237 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 */
1239 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001240 {
1241 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1242
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001243 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001244 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001245 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1246 &cstack.cs_looplevel);
1247 }
1248 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 trylevel = initial_trylevel;
1250 }
1251
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001252 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1253 // lack was reported above and the error message is to be converted to an
1254 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001255 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 ? (char_u *)"endfunction" : (char_u *)NULL);
1257
1258 if (trylevel == 0)
1259 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001260 // Just in case did_throw got set but current_exception wasn't.
1261 if (current_exception == NULL)
1262 did_throw = FALSE;
1263
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 /*
1265 * When an exception is being thrown out of the outermost try
1266 * conditional, discard the uncaught exception, disable the conversion
1267 * of interrupts or errors to exceptions, and ensure that no more
1268 * commands are executed.
1269 */
1270 if (did_throw)
1271 {
Bram Moolenaar033135e2021-01-14 21:40:22 +01001272 char *p = NULL;
Bram Moolenaare8c46602021-04-05 22:27:37 +02001273 msglist_T *messages = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274
1275 /*
1276 * If the uncaught exception is a user exception, report it as an
1277 * error. If it is an error exception, display the saved error
1278 * message now. For an interrupt exception, do nothing; the
1279 * interrupt message is given elsewhere.
1280 */
1281 switch (current_exception->type)
1282 {
1283 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001284 vim_snprintf((char *)IObuff, IOSIZE,
1285 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 current_exception->value);
Bram Moolenaar033135e2021-01-14 21:40:22 +01001287 p = (char *)vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 break;
1289 case ET_ERROR:
1290 messages = current_exception->messages;
1291 current_exception->messages = NULL;
1292 break;
1293 case ET_INTERRUPT:
1294 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 }
1296
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001297 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1298 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001299 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 current_exception->throw_name = NULL;
1301
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001302 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 suppress_errthrow = TRUE;
1304 force_abort = TRUE;
1305
1306 if (messages != NULL)
1307 {
1308 do
1309 {
Bram Moolenaare8c46602021-04-05 22:27:37 +02001310 msglist_T *next = messages->next;
1311 int save_compiling = estack_compiling;
1312
1313 estack_compiling = messages->msg_compiling;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 emsg(messages->msg);
1315 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001316 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 vim_free(messages);
1318 messages = next;
Bram Moolenaare8c46602021-04-05 22:27:37 +02001319 estack_compiling = save_compiling;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 }
1321 while (messages != NULL);
1322 }
1323 else if (p != NULL)
1324 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001325 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 vim_free(p);
1327 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001328 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001329 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001330 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 }
1332
1333 /*
1334 * On an interrupt or an aborting error not converted to an exception,
1335 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001336 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 * when force_abort is set and did_emsg unset in case of an interrupt
1338 * from a finally clause after an error.
1339 */
1340 else if (got_int || (did_emsg && force_abort))
1341 suppress_errthrow = TRUE;
1342 }
1343
1344 /*
1345 * The current cstack will be freed when do_cmdline() returns. An uncaught
1346 * exception will have to be rethrown in the previous cstack. If a function
1347 * has just returned or a script file was just finished and the previous
1348 * cstack belongs to the same function or, respectively, script file, it
1349 * will have to be checked for finally clauses to be executed due to the
1350 * ":return" or ":finish". This is done in do_one_cmd().
1351 */
1352 if (did_throw)
1353 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001354 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001356 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 && ex_nesting_level > func_level(real_cookie) + 1))
1358 {
1359 if (!did_throw)
1360 check_cstack = TRUE;
1361 }
1362 else
1363 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001364 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001365 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 --ex_nesting_level;
1367 /*
1368 * Go to debug mode when returning from a function in which we are
1369 * single-stepping.
1370 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001371 if ((getline_equal(fgetline, cookie, getsourceline)
1372 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001374 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 ? (char_u *)_("End of sourced file")
1376 : (char_u *)_("End of function"));
1377 }
1378
1379 /*
1380 * Restore the exception environment (done after returning from the
1381 * debugger).
1382 */
1383 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001384 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385
1386 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001387
1388 // Cleanup if "cs_emsg_silent_list" remains.
1389 if (cstack.cs_emsg_silent_list != NULL)
1390 {
1391 eslist_T *elem, *temp;
1392
1393 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1394 {
1395 temp = elem->next;
1396 vim_free(elem);
1397 }
1398 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001399#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400
1401 /*
1402 * If there was too much output to fit on the command line, ask the user to
1403 * hit return before redrawing the screen. With the ":global" command we do
1404 * this only once after the command is finished.
1405 */
1406 if (did_inc)
1407 {
1408 --RedrawingDisabled;
1409 --no_wait_return;
1410 msg_scroll = FALSE;
1411
1412 /*
1413 * When just finished an ":if"-":else" which was typed, no need to
1414 * wait for hit-return. Also for an error situation.
1415 */
1416 if (retval == FAIL
1417#ifdef FEAT_EVAL
1418 || (did_endif && KeyTyped && !did_emsg)
1419#endif
1420 )
1421 {
1422 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001423 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 }
1425 else if (need_wait_return)
1426 {
1427 /*
1428 * The msg_start() above clears msg_didout. The wait_return we do
1429 * here should not overwrite the command that may be shown before
1430 * doing that.
1431 */
1432 msg_didout |= msg_didout_before_start;
1433 wait_return(FALSE);
1434 }
1435 }
1436
Bram Moolenaar2a942252012-11-28 23:03:07 +01001437#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001438 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001439#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 /*
1441 * Reset if_level, in case a sourced script file contains more ":if" than
1442 * ":endif" (could be ":if x | foo | endif").
1443 */
1444 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001445#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001446
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 --call_depth;
1448 return retval;
1449}
1450
1451#ifdef FEAT_EVAL
1452/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001453 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 */
1455 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001456get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001458 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 wcmd_T *wp;
1460 char_u *line;
1461
1462 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1463 {
1464 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001465 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001467 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001469 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001471 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001472 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 ++cp->current_line;
1474
1475 return line;
1476 }
1477
1478 KeyTyped = FALSE;
1479 ++cp->current_line;
1480 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001481 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 return vim_strsave(wp->line);
1483}
1484
1485/*
1486 * Store a line in "gap" so that a ":while" loop can execute it again.
1487 */
1488 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001489store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
1491 if (ga_grow(gap, 1) == FAIL)
1492 return FAIL;
1493 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001494 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 return OK;
1497}
1498
1499/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001500 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 */
1502 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001503free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505 while (gap->ga_len > 0)
1506 {
1507 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1508 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 }
1510}
1511#endif
1512
1513/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001514 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1515 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001518getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001519 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001520 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001521 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
1523#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001524 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001525 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001527 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1528 // function that's originally used to obtain the lines. This may be
1529 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001530 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001531 cp = (struct loop_cookie *)cookie;
1532 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 {
1534 gp = cp->getline;
1535 cp = cp->cookie;
1536 }
1537 return gp == func;
1538#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001539 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540#endif
1541}
1542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001544 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 * getline function. Otherwise return "cookie".
1546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001548getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001549 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001550 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551{
Bram Moolenaar13505972019-01-24 15:04:48 +01001552#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001553 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001554 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001556 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1557 // cookie that's originally used to obtain the lines. This may be nested
1558 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001559 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001560 cp = (struct loop_cookie *)cookie;
1561 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 {
1563 gp = cp->getline;
1564 cp = cp->cookie;
1565 }
1566 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001567#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001570}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
Bram Moolenaard5053d02020-06-28 15:51:16 +02001572#if defined(FEAT_EVAL) || defined(PROT)
1573/*
1574 * Get the next line source line without advancing.
1575 */
1576 char_u *
1577getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001578 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001579 void *cookie) // argument for fgetline()
1580{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001581 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001582 struct loop_cookie *cp;
1583 wcmd_T *wp;
1584
1585 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1586 // cookie that's originally used to obtain the lines. This may be nested
1587 // several levels.
1588 gp = fgetline;
1589 cp = (struct loop_cookie *)cookie;
1590 while (gp == get_loop_line)
1591 {
1592 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1593 {
1594 // executing lines a second time, use the stored copy
1595 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1596 return wp->line;
1597 }
1598 gp = cp->getline;
1599 cp = cp->cookie;
1600 }
1601 if (gp == getsourceline)
1602 return source_nextline(cp);
1603 return NULL;
1604}
1605#endif
1606
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001607
1608/*
1609 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1610 * ":bwipeout", etc.
1611 * Returns the buffer number.
1612 */
1613 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001614compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001615{
1616 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001617 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001618 int count = offset;
1619
1620 buf = firstbuf;
1621 while (buf->b_next != NULL && buf->b_fnum < lnum)
1622 buf = buf->b_next;
1623 while (count != 0)
1624 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001625 count += (offset < 0) ? 1 : -1;
1626 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1627 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001628 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001629 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001630 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001631 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001632 while (buf->b_ml.ml_mfp == NULL)
1633 {
1634 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1635 if (nextbuf == NULL)
1636 break;
1637 buf = nextbuf;
1638 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001639 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001640 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001641 if (addr_type == ADDR_LOADED_BUFFERS)
1642 while (buf->b_ml.ml_mfp == NULL)
1643 {
1644 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1645 if (nextbuf == NULL)
1646 break;
1647 buf = nextbuf;
1648 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001649 return buf->b_fnum;
1650}
1651
Bram Moolenaarb7316892019-05-01 18:08:42 +02001652/*
1653 * Return the window number of "win".
1654 * When "win" is NULL return the number of windows.
1655 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001656 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001657current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001658{
1659 win_T *wp;
1660 int nr = 0;
1661
Bram Moolenaar29323592016-07-24 22:04:11 +02001662 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001663 {
1664 ++nr;
1665 if (wp == win)
1666 break;
1667 }
1668 return nr;
1669}
1670
1671 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001672current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001673{
1674 tabpage_T *tp;
1675 int nr = 0;
1676
Bram Moolenaar29323592016-07-24 22:04:11 +02001677 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001678 {
1679 ++nr;
1680 if (tp == tab)
1681 break;
1682 }
1683 return nr;
1684}
1685
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001686 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001687comment_start(char_u *p, int starts_with_colon UNUSED)
1688{
1689#ifdef FEAT_EVAL
1690 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001691 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001692#endif
1693 return *p == '"';
1694}
1695
Bram Moolenaarf240e182014-11-27 18:33:02 +01001696# define CURRENT_WIN_NR current_win_nr(curwin)
1697# define LAST_WIN_NR current_win_nr(NULL)
1698# define CURRENT_TAB_NR current_tab_nr(curtab)
1699# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001700
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701/*
1702 * Execute one Ex command.
1703 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001704 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1705 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 *
1707 * 1. skip comment lines and leading space
1708 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001709 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001710 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001711 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001712 * 6. parse arguments
1713 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001715 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 *
1717 * This function may be called recursively!
1718 */
1719#if (_MSC_VER == 1200)
1720/*
Bram Moolenaared203462004-06-16 11:19:22 +00001721 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001723 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724#endif
1725 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001726do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001727 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001728 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001730 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001732 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001733 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001735 char_u *p;
1736 linenr_T lnum;
1737 long n;
1738 char *errormsg = NULL; // error message
1739 char_u *after_modifier = NULL;
1740 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001741 cmdmod_T save_cmdmod;
1742 int save_reg_executing = reg_executing;
1743 int ni; // set when Not Implemented
1744 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001745 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001746#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001747 int may_have_range;
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001748 int vim9script;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001749 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001750#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001751 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752
Bram Moolenaara80faa82020-04-12 19:37:17 +02001753 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 ea.line1 = 1;
1755 ea.line2 = 1;
1756#ifdef FEAT_EVAL
1757 ++ex_nesting_level;
1758#endif
1759
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001760 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 if (quitmore
1762#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001763 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001764 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001765#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001766 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001767 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 --quitmore;
1769
1770 /*
1771 * Reset browse, confirm, etc.. They are restored when returning, for
1772 * recursive calls.
1773 */
1774 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001776 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001777 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1778 goto doend;
1779
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001780/*
1781 * 1. Skip comment lines and leading white space and colons.
1782 * 2. Handle command modifiers.
1783 */
1784 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001786 ea.cmdlinep = cmdlinep;
1787 ea.getline = fgetline;
1788 ea.cookie = cookie;
1789#ifdef FEAT_EVAL
1790 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001791 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001793 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001794 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001795 apply_cmdmod(&cmdmod);
Bram Moolenaar2379f872021-02-14 14:07:34 +01001796#ifdef FEAT_EVAL
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001797 vim9script = in_vim9script();
Bram Moolenaar2379f872021-02-14 14:07:34 +01001798#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001799 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800
1801#ifdef FEAT_EVAL
1802 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1803 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1804#else
1805 ea.skip = (if_level > 0);
1806#endif
1807
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001809 * 3. Skip over the range to find the command. Let "p" point to after it.
1810 *
1811 * We need the command to know what kind of range it uses.
1812 */
1813 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001814#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001815 // In Vim9 script a colon is required before the range. This may also be
1816 // after command modifiers.
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001817 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001818 {
1819 may_have_range = FALSE;
1820 for (p = ea.cmd; p >= *cmdlinep; --p)
1821 {
1822 if (*p == ':')
1823 may_have_range = TRUE;
1824 if (p < ea.cmd && !VIM_ISWHITE(*p))
1825 break;
1826 }
1827 }
1828 else
1829 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001830 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001831#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001832 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833
1834#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001835 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001836 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001837 if (ea.cmd == cmd + 1 && *cmd == '$')
1838 // should be "$VAR = val"
1839 --ea.cmd;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001840 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001841 if (ea.cmdidx == CMD_SIZE)
1842 {
1843 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1844
1845 // If a ':' before the range is missing, give a clearer error
1846 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001847 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001848 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001849 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001850 goto doend;
1851 }
1852 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001853 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854 else
1855#endif
1856 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001857
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001858#ifdef FEAT_EVAL
1859# ifdef FEAT_PROFILE
1860 // Count this line for profiling if skip is TRUE.
1861 if (do_profiling == PROF_YES
1862 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1863 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1864 {
1865 int skip = did_emsg || got_int || did_throw;
1866
1867 if (ea.cmdidx == CMD_catch)
1868 skip = !skip && !(cstack->cs_idx >= 0
1869 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1870 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1871 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1872 skip = skip || !(cstack->cs_idx >= 0
1873 && !(cstack->cs_flags[cstack->cs_idx]
1874 & (CSF_ACTIVE | CSF_TRUE)));
1875 else if (ea.cmdidx == CMD_finally)
1876 skip = FALSE;
1877 else if (ea.cmdidx != CMD_endif
1878 && ea.cmdidx != CMD_endfor
1879 && ea.cmdidx != CMD_endtry
1880 && ea.cmdidx != CMD_endwhile)
1881 skip = ea.skip;
1882
1883 if (!skip)
1884 {
1885 if (getline_equal(fgetline, cookie, get_func_line))
1886 func_line_exec(getline_cookie(fgetline, cookie));
1887 else if (getline_equal(fgetline, cookie, getsourceline))
1888 script_line_exec();
1889 }
1890 }
1891# endif
1892
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001893 // May go to debug mode. If this happens and the ">quit" debug command is
1894 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001895 dbg_check_breakpoint(&ea);
1896 if (!ea.skip && got_int)
1897 {
1898 ea.skip = TRUE;
1899 (void)do_intthrow(cstack);
1900 }
1901#endif
1902
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001903/*
1904 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 *
1906 * where 'addr' is:
1907 *
1908 * % (entire file)
1909 * $ [+-NUM]
1910 * 'x [+-NUM] (where x denotes a currently defined mark)
1911 * . [+-NUM]
1912 * [+-NUM]..
1913 * NUM
1914 *
1915 * The ea.cmd pointer is updated to point to the first character following the
1916 * range spec. If an initial address is found, but no second, the upper bound
1917 * is equal to the lower.
1918 */
1919
Bram Moolenaar25190db2019-05-04 15:05:28 +02001920 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001921 if (!IS_USER_CMDIDX(ea.cmdidx))
1922 {
1923 if (ea.cmdidx != CMD_SIZE)
1924 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1925 else
1926 ea.addr_type = ADDR_LINES;
1927
Bram Moolenaar25190db2019-05-04 15:05:28 +02001928 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001929 if (ea.cmdidx == CMD_wincmd && p != NULL)
1930 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001931#ifdef FEAT_QUICKFIX
1932 // :.cc in quickfix window uses line number
1933 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1934 ea.addr_type = ADDR_OTHER;
1935#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001936 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001937
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001938 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001939#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001940 if (!may_have_range)
1941 ea.line1 = ea.line2 = default_address(&ea);
1942 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001943#endif
1944 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1945 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001948 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 */
1950
1951 /*
1952 * Skip ':' and any white space
1953 */
1954 ea.cmd = skipwhite(ea.cmd);
1955 while (*ea.cmd == ':')
1956 ea.cmd = skipwhite(ea.cmd + 1);
1957
1958 /*
1959 * If we got a line, but no command, then go to the line.
1960 * If we find a '|' or '\n' we set ea.nextcmd.
1961 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001962 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1963 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
1965 /*
1966 * strange vi behaviour:
1967 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001968 * ":3|..." prints line 3 (not in Vim9 script)
1969 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001971 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 goto doend;
Bram Moolenaarb8554302021-02-15 21:30:30 +01001973 if ((*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
1974#ifdef FEAT_EVAL
1975 && !vim9script
1976#endif
1977 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 {
1979 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001980 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 if ((errormsg = invalid_range(&ea)) == NULL)
1982 {
1983 correct_range(&ea);
1984 ex_print(&ea);
1985 }
1986 }
1987 else if (ea.addr_count != 0)
1988 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001989 if (ea.line2 > curbuf->b_ml.ml_line_count)
1990 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001991 // With '-' in 'cpoptions' a line number past the file is an
1992 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001993 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1994 ea.line2 = -1;
1995 else
1996 ea.line2 = curbuf->b_ml.ml_line_count;
1997 }
1998
1999 if (ea.line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002000 errormsg = _(e_invalid_range);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 else
2002 {
2003 if (ea.line2 == 0)
2004 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 else
2006 curwin->w_cursor.lnum = ea.line2;
2007 beginline(BL_SOL | BL_FIX);
2008 }
2009 }
2010 goto doend;
2011 }
2012
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002013 // If this looks like an undefined user command and there are CmdUndefined
2014 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002015 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2016 && ASCII_ISUPPER(*ea.cmd)
2017 && has_cmdundefined())
2018 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002019 int ret;
2020
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002021 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002022 while (ASCII_ISALNUM(*p))
2023 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002024 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002025 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2026 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002027 // If the autocommands did something and didn't cause an error, try
2028 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002029 p = (ret
2030#ifdef FEAT_EVAL
2031 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002032#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002033 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002034 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002035
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 if (p == NULL)
2037 {
2038 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002039 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 goto doend;
2041 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002042 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002043 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2044 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 {
2046 errormsg = uc_fun_cmd();
2047 goto doend;
2048 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002049
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 if (ea.cmdidx == CMD_SIZE)
2051 {
2052 if (!ea.skip)
2053 {
2054 STRCPY(IObuff, _("E492: Not an editor command"));
2055 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002056 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002057 // If the modifier was parsed OK the error must be in the
2058 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002059 if (after_modifier != NULL)
2060 append_command(after_modifier);
2061 else
2062 append_command(*cmdlinep);
2063 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002064 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002065 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 }
2067 goto doend;
2068 }
2069
Bram Moolenaar958636c2014-10-21 20:01:58 +02002070 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2071 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002072#ifdef HAVE_EX_SCRIPT_NI
2073 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2074#endif
2075 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076
2077#ifndef FEAT_EVAL
2078 /*
2079 * When the expression evaluation is disabled, recognize the ":if" and
2080 * ":endif" commands and ignore everything in between it.
2081 */
2082 if (ea.cmdidx == CMD_if)
2083 ++if_level;
2084 if (if_level)
2085 {
2086 if (ea.cmdidx == CMD_endif)
2087 --if_level;
2088 goto doend;
2089 }
2090
2091#endif
2092
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002093 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002094 if (*p == '!' && ea.cmdidx != CMD_substitute
2095 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 {
2097 ++p;
2098 ea.forceit = TRUE;
2099 }
2100 else
2101 ea.forceit = FALSE;
2102
2103/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002104 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002106 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002107 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108
2109 if (!ea.skip)
2110 {
2111#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002112 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002114 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002115 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 goto doend;
2117 }
2118#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002119 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002120 {
2121 errormsg = _("E981: Command not allowed in rvim");
2122 goto doend;
2123 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002124 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002126 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002127 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 goto doend;
2129 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002130
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002131 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002133#ifdef FEAT_CMDWIN
2134 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2135 {
2136 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002137 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002138 goto doend;
2139 }
2140#endif
2141 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2142 {
2143 // Command not allowed when text is locked
2144 errormsg = _(get_text_locked_msg());
2145 goto doend;
2146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002148
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002149 // Disallow editing another buffer when "curbuf_lock" is set.
2150 // Do allow ":checktime" (it is postponed).
2151 // Do allow ":edit" (check for an argument later).
2152 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002153 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002154 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002155 && ea.cmdidx != CMD_edit
2156 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002157 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002158 && curbuf_locked())
2159 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002161 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002163 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002164 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 goto doend;
2166 }
2167 }
2168
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002169 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002171 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 goto doend;
2173 }
2174
2175 /*
2176 * Don't complain about the range if it is not used
2177 * (could happen if line_count is accidentally set to 0).
2178 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002179 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 {
2181 /*
2182 * If the range is backwards, ask for confirmation and, if given, swap
2183 * ea.line1 & ea.line2 so it's forwards again.
2184 * When global command is busy, don't ask, will fail below.
2185 */
2186 if (!global_busy && ea.line1 > ea.line2)
2187 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002188 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002190 if (sourcing || exmode_active)
2191 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002192 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002193 goto doend;
2194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 if (ask_yesno((char_u *)
2196 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002197 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 }
2199 lnum = ea.line1;
2200 ea.line1 = ea.line2;
2201 ea.line2 = lnum;
2202 }
2203 if ((errormsg = invalid_range(&ea)) != NULL)
2204 goto doend;
2205 }
2206
Bram Moolenaarb7316892019-05-01 18:08:42 +02002207 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2208 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 ea.line2 = 1;
2210
2211 correct_range(&ea);
2212
2213#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002214 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002215 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002217 // Put the first line at the start of a closed fold, put the last line
2218 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 (void)hasFolding(ea.line1, &ea.line1, NULL);
2220 (void)hasFolding(ea.line2, NULL, &ea.line2);
2221 }
2222#endif
2223
2224#ifdef FEAT_QUICKFIX
2225 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002226 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 * option here, so things like % get expanded.
2228 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002229 p = replace_makeprg(&ea, p, cmdlinep);
2230 if (p == NULL)
2231 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232#endif
2233
2234 /*
2235 * Skip to start of argument.
2236 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2237 */
2238 if (ea.cmdidx == CMD_bang)
2239 ea.arg = p;
2240 else
2241 ea.arg = skipwhite(p);
2242
Bram Moolenaar379fb762018-08-30 15:58:28 +02002243 // ":file" cannot be run with an argument when "curbuf_lock" is set
2244 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2245 goto doend;
2246
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 /*
2248 * Check for "++opt=val" argument.
2249 * Must be first, allow ":w ++enc=utf8 !cmd"
2250 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002251 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2253 if (getargopt(&ea) == FAIL && !ni)
2254 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002255 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 goto doend;
2257 }
2258
2259 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2260 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002261 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002263 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002265 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 goto doend;
2267 }
2268 ea.arg = skipwhite(ea.arg + 1);
2269 ea.append = TRUE;
2270 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002271 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
2273 ++ea.arg;
2274 ea.usefilter = TRUE;
2275 }
2276 }
2277
2278 if (ea.cmdidx == CMD_read)
2279 {
2280 if (ea.forceit)
2281 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002282 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 ea.forceit = FALSE;
2284 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002285 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 {
2287 ++ea.arg;
2288 ea.usefilter = TRUE;
2289 }
2290 }
2291
2292 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2293 {
2294 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002295 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
2297 ++ea.arg;
2298 ++ea.amount;
2299 }
2300 ea.arg = skipwhite(ea.arg);
2301 }
2302
2303 /*
2304 * Check for "+command" argument, before checking for next command.
2305 * Don't do this for ":read !cmd" and ":write !cmd".
2306 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002307 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2309
2310 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002311 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 * Don't do this for ":read !cmd" and ":write !cmd".
2313 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002314 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 separate_nextcmd(&ea);
2316
2317 /*
2318 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002319 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2320 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002322 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002323 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002324 || ea.cmdidx == CMD_global
2325 || ea.cmdidx == CMD_vglobal
2326 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
2328 for (p = ea.arg; *p; ++p)
2329 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002330 // Remove one backslash before a newline, so that it's possible to
2331 // pass a newline to the shell and also a newline that is preceded
2332 // with a backslash. This makes it impossible to end a shell
2333 // command in a backslash, but that doesn't appear useful.
2334 // Halving the number of backslashes is incompatible with previous
2335 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002337 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 else if (*p == '\n')
2339 {
2340 ea.nextcmd = p + 1;
2341 *p = NUL;
2342 break;
2343 }
2344 }
2345 }
2346
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002347 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002348 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002350 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002351 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002353 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002354 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002355 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002357#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002358 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002359 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002361 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002362 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 }
2364#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002365 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2366 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002367 {
2368 ea.regname = *ea.arg++;
2369#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002370 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002371 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2372 {
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002373 set_expr_line(vim_strsave(ea.arg), &ea);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002374 ea.arg += STRLEN(ea.arg);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002375 did_set_expr_line = TRUE;
Bram Moolenaar85de2062011-05-05 14:26:41 +02002376 }
2377#endif
2378 ea.arg = skipwhite(ea.arg);
2379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 }
2381
2382 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002383 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 * count, it's a buffer name.
2385 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002386 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002387 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002388 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {
2390 n = getdigits(&ea.arg);
2391 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002392 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002394 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 goto doend;
2396 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002397 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 {
2399 ea.line2 = n;
2400 if (ea.addr_count == 0)
2401 ea.addr_count = 1;
2402 }
2403 else
2404 {
2405 ea.line1 = ea.line2;
2406 ea.line2 += n - 1;
2407 ++ea.addr_count;
2408 /*
2409 * Be vi compatible: no error message for out of range.
2410 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002411 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 ea.line2 = curbuf->b_ml.ml_line_count;
2413 }
2414 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002415
2416 /*
2417 * Check for flags: 'l', 'p' and '#'.
2418 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002419 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002420 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002421 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2422 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002424 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002425 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 goto doend;
2427 }
2428
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002429 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002431 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 goto doend;
2433 }
2434
2435#ifdef FEAT_EVAL
2436 /*
2437 * Skip the command when it's not going to be executed.
2438 * The commands like :if, :endif, etc. always need to be executed.
2439 * Also make an exception for commands that handle a trailing command
2440 * themselves.
2441 */
2442 if (ea.skip)
2443 {
2444 switch (ea.cmdidx)
2445 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002446 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 case CMD_while:
2448 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002449 case CMD_for:
2450 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 case CMD_if:
2452 case CMD_elseif:
2453 case CMD_else:
2454 case CMD_endif:
2455 case CMD_try:
2456 case CMD_catch:
2457 case CMD_finally:
2458 case CMD_endtry:
2459 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002460 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 break;
2462
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002463 // Commands that handle '|' themselves. Check: A command should
2464 // either have the EX_TRLBAR flag, appear in this list or appear in
2465 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 case CMD_aboveleft:
2467 case CMD_and:
2468 case CMD_belowright:
2469 case CMD_botright:
2470 case CMD_browse:
2471 case CMD_call:
2472 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002473 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 case CMD_delfunction:
2475 case CMD_djump:
2476 case CMD_dlist:
2477 case CMD_dsearch:
2478 case CMD_dsplit:
2479 case CMD_echo:
2480 case CMD_echoerr:
2481 case CMD_echomsg:
2482 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002483 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002485 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002486 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 case CMD_help:
2488 case CMD_hide:
2489 case CMD_ijump:
2490 case CMD_ilist:
2491 case CMD_isearch:
2492 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002493 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 case CMD_keepjumps:
2495 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002496 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 case CMD_leftabove:
2498 case CMD_let:
2499 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002500 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002501 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002503 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002504 case CMD_noautocmd:
2505 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 case CMD_perl:
2507 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002508 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002509 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002510 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 case CMD_return:
2512 case CMD_rightbelow:
2513 case CMD_ruby:
2514 case CMD_silent:
2515 case CMD_smagic:
2516 case CMD_snomagic:
2517 case CMD_substitute:
2518 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002519 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 case CMD_tcl:
2521 case CMD_throw:
2522 case CMD_tilde:
2523 case CMD_topleft:
2524 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002525 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002526 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 case CMD_verbose:
2528 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002529 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 break;
2531
2532 default: goto doend;
2533 }
2534 }
2535#endif
2536
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002537 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 {
2539 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2540 goto doend;
2541 }
2542
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 /*
2544 * Accept buffer name. Cannot be used at the same time with a buffer
2545 * number. Don't do this for a user command.
2546 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002547 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002548 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 {
2550 /*
2551 * :bdelete, :bwipeout and :bunload take several arguments, separated
2552 * by spaces: find next space (skipping over escaped characters).
2553 * The others take one argument: ignore trailing spaces.
2554 */
2555 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2556 || ea.cmdidx == CMD_bunload)
2557 p = skiptowhite_esc(ea.arg);
2558 else
2559 {
2560 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002561 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 --p;
2563 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002564 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002565 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002566 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 goto doend;
2568 ea.addr_count = 1;
2569 ea.arg = skipwhite(p);
2570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002572 // The :try command saves the emsg_silent flag, reset it here when
2573 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002574 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002575 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002576 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002577 if (emsg_silent < 0)
2578 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002579 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002580 }
2581
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002583 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585
Bram Moolenaar958636c2014-10-21 20:01:58 +02002586 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 {
2588 /*
2589 * Execute a user-defined command.
2590 */
2591 do_ucmd(&ea);
2592 }
2593 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 {
2595 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002596 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 */
2598 ea.errmsg = NULL;
2599 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2600 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002601 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 }
2603
2604#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002606 // Not if this was a command that wasn't executed or :endif.
Bram Moolenaar16531552020-02-08 16:00:46 +01002607 if (getline_equal(ea.getline, ea.cookie, getsourceline)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002608 && current_sctx.sc_sid > 0
2609 && ea.cmdidx != CMD_endif
2610 && (cstack->cs_idx < 0
2611 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002612 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002613
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 /*
2615 * If the command just executed called do_cmdline(), any throw or ":return"
2616 * or ":finish" encountered there must also check the cstack of the still
2617 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2618 * exception, or reanimate a returned function or finished script file and
2619 * return or finish it again.
2620 */
2621 if (need_rethrow)
2622 do_throw(cstack);
2623 else if (check_cstack)
2624 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002625 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002627 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 && current_func_returned())
2629 do_return(&ea, TRUE, FALSE, NULL);
2630 }
2631 need_rethrow = check_cstack = FALSE;
2632#endif
2633
2634doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002635 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002636 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002638 curwin->w_cursor.col = 0;
2639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
2641 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2642 {
2643 if (sourcing)
2644 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002645 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 {
2647 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002648 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002650 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 }
2652 emsg(errormsg);
2653 }
2654#ifdef FEAT_EVAL
2655 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002656 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2657 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002658
2659 if (did_set_expr_line)
2660 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661#endif
2662
Bram Moolenaare1004402020-10-24 20:49:43 +02002663 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002665 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002667 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 ea.nextcmd = NULL;
2669
2670#ifdef FEAT_EVAL
2671 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002672 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673#endif
2674
2675 return ea.nextcmd;
2676}
2677#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002678 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679#endif
2680
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002681static char ex_error_buf[MSG_BUF_LEN];
2682
2683/*
2684 * Return an error message with argument included.
2685 * Uses a static buffer, only the last error will be kept.
2686 * "msg" will be translated, caller should use N_().
2687 */
2688 char *
2689ex_errmsg(char *msg, char_u *arg)
2690{
2691 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2692 return ex_error_buf;
2693}
2694
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002696 * Check for an Ex command with optional tail.
2697 * If there is a match advance "pp" to the argument and return TRUE.
2698 * If "noparen" is TRUE do not recognize the command followed by "(".
2699 */
2700 static int
2701checkforcmd_opt(
2702 char_u **pp, // start of command
2703 char *cmd, // name of command
2704 int len, // required length
2705 int noparen)
2706{
2707 int i;
2708
2709 for (i = 0; cmd[i] != NUL; ++i)
2710 if (((char_u *)cmd)[i] != (*pp)[i])
2711 break;
2712 if (i >= len && !isalpha((*pp)[i])
2713 && (*pp)[i] != '_' && (!noparen || (*pp)[i] != '('))
2714 {
2715 *pp = skipwhite(*pp + i);
2716 return TRUE;
2717 }
2718 return FALSE;
2719}
2720
2721/*
2722 * Check for an Ex command with optional tail.
2723 * If there is a match advance "pp" to the argument and return TRUE.
2724 */
2725 int
2726checkforcmd(
2727 char_u **pp, // start of command
2728 char *cmd, // name of command
2729 int len) // required length
2730{
2731 return checkforcmd_opt(pp, cmd, len, FALSE);
2732}
2733
2734/*
2735 * Check for an Ex command with optional tail, not followed by "(".
2736 * If there is a match advance "pp" to the argument and return TRUE.
2737 */
2738 static int
2739checkforcmd_noparen(
2740 char_u **pp, // start of command
2741 char *cmd, // name of command
2742 int len) // required length
2743{
2744 return checkforcmd_opt(pp, cmd, len, TRUE);
2745}
2746
2747/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002748 * Parse and skip over command modifiers:
2749 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002750 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002751 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002752 * When "skip_only" is TRUE the global variables are not changed, except for
2753 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002754 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2755 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002756 * Call apply_cmdmod() to get the side effects of the modifiers:
2757 * - Increment "sandbox" for ":sandbox"
2758 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002759 * - set msg_silent for ":silent"
2760 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002761 * Return FAIL when the command is not to be executed.
2762 * May set "errormsg" to an error message.
2763 */
2764 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002765parse_command_modifiers(
2766 exarg_T *eap,
2767 char **errormsg,
2768 cmdmod_T *cmod,
2769 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002770{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002771 char_u *p;
2772 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002773
Bram Moolenaare1004402020-10-24 20:49:43 +02002774 CLEAR_POINTER(cmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002775
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002776 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002777 for (;;)
2778 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002779 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002780 {
2781 if (*eap->cmd == ':')
2782 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002783 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002784 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002785
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002786 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002787 if (*eap->cmd == NUL && exmode_active
2788 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2789 || getline_equal(eap->getline, eap->cookie, getexline))
2790 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2791 {
2792 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002793 if (!skip_only)
2794 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002795 }
2796
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002797 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002798 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002799 return FAIL;
2800 if (*eap->cmd == NUL)
2801 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002802 if (!skip_only)
2803 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002804 return FAIL;
2805 }
2806
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002807 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002808
2809 // In Vim9 script a variable can shadow a command modifier:
2810 // verbose = 123
2811 // verbose += 123
2812 // silent! verbose = func()
2813 // verbose.member = 2
2814 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002815 // But not:
2816 // verbose [a, b] = list
Bram Moolenaar17126b12021-01-07 22:03:02 +01002817 if (in_vim9script())
2818 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002819 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002820
2821 for (s = p; ASCII_ISALPHA(*s); ++s)
2822 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002823 n = skipwhite(s);
2824 if (vim_strchr((char_u *)".=", *n) != NULL
2825 || *s == '['
2826 || (*n != NUL && n[1] == '='))
Bram Moolenaar17126b12021-01-07 22:03:02 +01002827 break;
2828 }
2829
Bram Moolenaareffed932018-08-14 13:38:17 +02002830 switch (*p)
2831 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002832 // When adding an entry, also modify cmd_exists().
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002833 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002834 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002835 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002836 continue;
2837
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002838 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002839 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002840 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002841 continue;
2842 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002843 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002844 {
2845#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002846 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002847#endif
2848 continue;
2849 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002850 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002851 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002852 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002853 continue;
2854
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002855 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002856 break;
2857#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002858 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002859#endif
2860 continue;
2861
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002862 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002863 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002864 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002865 continue;
2866 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002867 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002868 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002869 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002870 continue;
2871 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002872 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002873 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002874 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002875 continue;
2876 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002877 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002878 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002879 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002880 continue;
2881
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002882 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002883 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02002884 char_u *reg_pat;
2885 char_u *nulp = NULL;
2886 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002887
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002888 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaareffed932018-08-14 13:38:17 +02002889 || *p == NUL || ends_excmd(*p))
2890 break;
2891 if (*p == '!')
2892 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002893 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002894 p = skipwhite(p + 1);
2895 if (*p == NUL || ends_excmd(*p))
2896 break;
2897 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002898#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002899 // Avoid that "filter(arg)" is recognized.
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +01002900 if (in_vim9script() && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002901 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002902#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002903 if (skip_only)
2904 p = skip_vimgrep_pat(p, NULL, NULL);
2905 else
2906 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02002907 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
2908 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02002909 if (p == NULL || *p == NUL)
2910 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002911 if (!skip_only)
2912 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002913 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002914 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002915 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002916 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02002917 // restore the character overwritten by NUL
2918 if (nulp != NULL)
2919 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002920 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002921 eap->cmd = p;
2922 continue;
2923 }
2924
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002925 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002926 case 'h': if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02002927 || *p == NUL || ends_excmd(*p))
2928 break;
2929 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002930 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002931 continue;
2932
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002933 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002934 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002935 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002936 continue;
2937 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002938 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
2939 {
2940 if (ends_excmd2(p, eap->cmd))
2941 {
2942 *errormsg =
2943 _(e_vim9cmd_must_be_followed_by_command);
2944 return FAIL;
2945 }
2946 cmod->cmod_flags |= CMOD_LEGACY;
2947 continue;
2948 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002949
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002950 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002951 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002952 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002953 continue;
2954
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002955 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002956 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002957 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002958 continue;
2959 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002960 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002961 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002962 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002963 continue;
2964
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002965 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02002966 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002967 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002968 continue;
2969
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002970 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002971 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002972 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02002973 continue;
2974 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002975 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002976 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002977 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002978 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2979 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002980 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002981 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02002982 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002983 }
2984 continue;
2985
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002986 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002987 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002988 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002989 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002990 long tabnr = get_address(eap, &eap->cmd,
2991 ADDR_TABS, eap->skip,
2992 skip_only, FALSE, 1);
2993 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02002994 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01002995 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002996 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002997 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2998 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002999 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003000 return FAIL;
3001 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003002 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003003 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003004 }
3005 eap->cmd = p;
3006 continue;
3007 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003008 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003009 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003010 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003011 continue;
3012
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003013 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003014 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003015 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003016 continue;
3017
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003018 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003019 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003020 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003021 continue;
3022 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003023 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003024 {
3025 if (ends_excmd2(p, eap->cmd))
3026 {
3027 *errormsg =
3028 _(e_vim9cmd_must_be_followed_by_command);
3029 return FAIL;
3030 }
3031 cmod->cmod_flags |= CMOD_VIM9CMD;
3032 continue;
3033 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003034 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003035 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003036 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02003037 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003038 else
Bram Moolenaare1004402020-10-24 20:49:43 +02003039 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003040 eap->cmd = p;
3041 continue;
3042 }
3043 break;
3044 }
3045
3046 return OK;
3047}
3048
3049/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003050 * Return TRUE if "cmod" has anything set.
3051 */
3052 int
3053has_cmdmod(cmdmod_T *cmod)
3054{
3055 return cmod->cmod_flags != 0
3056 || cmod->cmod_split != 0
3057 || cmod->cmod_verbose != 0
3058 || cmod->cmod_tab != 0
3059 || cmod->cmod_filter_regmatch.regprog != NULL;
3060}
3061
3062/*
3063 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3064 */
3065 int
3066cmdmod_error(void)
3067{
3068 if (in_vim9script() && has_cmdmod(&cmdmod))
3069 {
3070 emsg(_(e_misplaced_command_modifier));
3071 return TRUE;
3072 }
3073 return FALSE;
3074}
3075
3076/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003077 * Apply the command modifiers. Saves current state in "cmdmod", call
3078 * undo_cmdmod() later.
3079 */
3080 void
3081apply_cmdmod(cmdmod_T *cmod)
3082{
3083#ifdef HAVE_SANDBOX
3084 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3085 {
3086 ++sandbox;
3087 cmod->cmod_did_sandbox = TRUE;
3088 }
3089#endif
3090 if (cmod->cmod_verbose > 0)
3091 {
3092 if (cmod->cmod_verbose_save == 0)
3093 cmod->cmod_verbose_save = p_verbose + 1;
3094 p_verbose = cmod->cmod_verbose;
3095 }
3096
3097 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3098 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003099 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003100 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003101 cmod->cmod_save_msg_scroll = msg_scroll;
3102 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003103 if (cmod->cmod_flags & CMOD_SILENT)
3104 ++msg_silent;
3105 if (cmod->cmod_flags & CMOD_UNSILENT)
3106 msg_silent = 0;
3107
3108 if (cmod->cmod_flags & CMOD_ERRSILENT)
3109 {
3110 ++emsg_silent;
3111 ++cmod->cmod_did_esilent;
3112 }
3113
Bram Moolenaare1004402020-10-24 20:49:43 +02003114 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003115 {
3116 // Set 'eventignore' to "all".
3117 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003118 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003119 set_string_option_direct((char_u *)"ei", -1,
3120 (char_u *)"all", OPT_FREE, SID_NONE);
3121 }
3122}
3123
3124/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003125 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003126 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003127 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003128undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003129{
Bram Moolenaare1004402020-10-24 20:49:43 +02003130 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003131 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003132 p_verbose = cmod->cmod_verbose_save - 1;
3133 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003134 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003135
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003136#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003137 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003138 {
3139 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003140 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003141 }
3142#endif
3143
Bram Moolenaare1004402020-10-24 20:49:43 +02003144 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003145 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003146 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003147 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3148 OPT_FREE, SID_NONE);
3149 free_string_option(cmod->cmod_save_ei);
3150 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003151 }
3152
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003153 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003154
Bram Moolenaare1004402020-10-24 20:49:43 +02003155 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003156 {
3157 // messages could be enabled for a serious error, need to check if the
3158 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003159 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3160 msg_silent = cmod->cmod_save_msg_silent - 1;
3161 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003162 if (emsg_silent < 0)
3163 emsg_silent = 0;
3164 // Restore msg_scroll, it's set by file I/O commands, even when no
3165 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003166 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003167
3168 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3169 // somewhere in the line. Put it back in the first column.
3170 if (redirecting())
3171 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003172
Bram Moolenaare1004402020-10-24 20:49:43 +02003173 cmod->cmod_save_msg_silent = 0;
3174 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003175 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003176}
3177
3178/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003179 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003180 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003181 * Return FAIL and set "errormsg" or return OK.
3182 */
3183 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003184parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003185{
3186 int address_count = 1;
3187 linenr_T lnum;
3188
3189 // Repeat for all ',' or ';' separated addresses.
3190 for (;;)
3191 {
3192 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003193 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003194 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003195 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003196 eap->addr_count == 0, address_count++);
3197 if (eap->cmd == NULL) // error detected
3198 return FAIL;
3199 if (lnum == MAXLNUM)
3200 {
3201 if (*eap->cmd == '%') // '%' - all lines
3202 {
3203 ++eap->cmd;
3204 switch (eap->addr_type)
3205 {
3206 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003207 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003208 eap->line1 = 1;
3209 eap->line2 = curbuf->b_ml.ml_line_count;
3210 break;
3211 case ADDR_LOADED_BUFFERS:
3212 {
3213 buf_T *buf = firstbuf;
3214
3215 while (buf->b_next != NULL
3216 && buf->b_ml.ml_mfp == NULL)
3217 buf = buf->b_next;
3218 eap->line1 = buf->b_fnum;
3219 buf = lastbuf;
3220 while (buf->b_prev != NULL
3221 && buf->b_ml.ml_mfp == NULL)
3222 buf = buf->b_prev;
3223 eap->line2 = buf->b_fnum;
3224 break;
3225 }
3226 case ADDR_BUFFERS:
3227 eap->line1 = firstbuf->b_fnum;
3228 eap->line2 = lastbuf->b_fnum;
3229 break;
3230 case ADDR_WINDOWS:
3231 case ADDR_TABS:
3232 if (IS_USER_CMDIDX(eap->cmdidx))
3233 {
3234 eap->line1 = 1;
3235 eap->line2 = eap->addr_type == ADDR_WINDOWS
3236 ? LAST_WIN_NR : LAST_TAB_NR;
3237 }
3238 else
3239 {
3240 // there is no Vim command which uses '%' and
3241 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003242 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003243 return FAIL;
3244 }
3245 break;
3246 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003247 case ADDR_UNSIGNED:
3248 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003249 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003250 return FAIL;
3251 case ADDR_ARGUMENTS:
3252 if (ARGCOUNT == 0)
3253 eap->line1 = eap->line2 = 0;
3254 else
3255 {
3256 eap->line1 = 1;
3257 eap->line2 = ARGCOUNT;
3258 }
3259 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003260 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003261#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003262 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003263 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003264 if (eap->line2 == 0)
3265 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003266#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003267 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003268 case ADDR_NONE:
3269 // Will give an error later if a range is found.
3270 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003271 }
3272 ++eap->addr_count;
3273 }
3274 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3275 {
3276 pos_T *fp;
3277
3278 // '*' - visual area
3279 if (eap->addr_type != ADDR_LINES)
3280 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003281 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003282 return FAIL;
3283 }
3284
3285 ++eap->cmd;
3286 if (!eap->skip)
3287 {
3288 fp = getmark('<', FALSE);
3289 if (check_mark(fp) == FAIL)
3290 return FAIL;
3291 eap->line1 = fp->lnum;
3292 fp = getmark('>', FALSE);
3293 if (check_mark(fp) == FAIL)
3294 return FAIL;
3295 eap->line2 = fp->lnum;
3296 ++eap->addr_count;
3297 }
3298 }
3299 }
3300 else
3301 eap->line2 = lnum;
3302 eap->addr_count++;
3303
3304 if (*eap->cmd == ';')
3305 {
3306 if (!eap->skip)
3307 {
3308 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003309 // Don't leave the cursor on an illegal line or column, but do
3310 // accept zero as address, so 0;/PATTERN/ works correctly.
3311 if (eap->line2 > 0)
3312 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003313 }
3314 }
3315 else if (*eap->cmd != ',')
3316 break;
3317 ++eap->cmd;
3318 }
3319
3320 // One address given: set start and end lines.
3321 if (eap->addr_count == 1)
3322 {
3323 eap->line1 = eap->line2;
3324 // ... but only implicit: really no address given
3325 if (lnum == MAXLNUM)
3326 eap->addr_count = 0;
3327 }
3328 return OK;
3329}
3330
3331/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003332 * Append "cmd" to the error message in IObuff.
3333 * Takes care of limiting the length and handling 0xa0, which would be
3334 * invisible otherwise.
3335 */
3336 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003337append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003338{
3339 char_u *s = cmd;
3340 char_u *d;
3341
3342 STRCAT(IObuff, ": ");
3343 d = IObuff + STRLEN(IObuff);
3344 while (*s != NUL && d - IObuff < IOSIZE - 7)
3345 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003346 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003347 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003348 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003349 STRCPY(d, "<a0>");
3350 d += 4;
3351 }
3352 else
3353 MB_COPY_CHAR(s, d);
3354 }
3355 *d = NUL;
3356}
3357
3358/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003359 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3360 * the name. Otherwise just return "start".
3361 */
3362 char_u *
3363skip_option_env_lead(char_u *start)
3364{
3365 char_u *name = start;
3366
3367 if (*start == '&')
3368 {
3369 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3370 name += 3;
3371 else
3372 name += 1;
3373 }
3374 else if (*start == '$')
3375 name += 1;
3376 return name;
3377}
3378
3379/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003381 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003382 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003383 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 *
3385 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3386 * assignment without "let". Sets eap->cmdidx to the command while returning
3387 * "eap->cmd".
3388 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 * Returns NULL for an ambiguous user command.
3390 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 char_u *
3392find_ex_command(
3393 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003394 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003395 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003396 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397{
3398 int len;
3399 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003400 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003401#ifndef FEAT_EVAL
3402 int vim9 = FALSE;
3403#else
3404 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 /*
3407 * Recognize a Vim9 script function/method call and assignment:
3408 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3409 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003410 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003411 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003413 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414
Bram Moolenaar83144542020-08-02 20:40:43 +02003415 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003416 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003418 int oplen;
3419 int heredoc;
3420 char_u *swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003422 if (
3423 // "(..." is an expression.
3424 // "funcname(" is always a function call.
3425 *p == '('
3426 || (p == eap->cmd
3427 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003428 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003429 *eap->cmd == '{'
3430 // "'string'->func()" is an expression.
3431 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003432 // '"string"->func()' is an expression.
3433 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003434 // "g:varname" is an expression.
3435 || eap->cmd[1] == ':'
3436 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003437 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003438 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003439 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003440 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3441 {
3442 // "{" by itself is the start of a block.
3443 eap->cmdidx = CMD_block;
3444 return eap->cmd + 1;
3445 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003446 eap->cmdidx = CMD_eval;
3447 return eap->cmd;
3448 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003449
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003450 if (p != eap->cmd && (
3451 // "varname[]" is an expression.
3452 *p == '['
3453 // "varname.key" is an expression.
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02003454 || (*p == '.' && (ASCII_ISALPHA(p[1])
3455 || p[1] == '_'))))
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003456 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003457 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003458
3459 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003460 // Skip over the whole thing, it can be:
3461 // name.member = val
3462 // name[a : b] = val
3463 // name[idx] = val
3464 // name[idx].member = val
3465 // etc.
3466 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003467 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003468 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003469 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003470 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003471 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003472 || (after[0] == '.' && after[1] == '.'
3473 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003474 eap->cmdidx = CMD_var;
3475 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003476 --emsg_silent;
3477 return eap->cmd;
3478 }
3479
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003480 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3481 // an assignment.
3482 // If there is no line break inside the "[...]" then "p" is
3483 // advanced to after the "]" by to_name_const_end(): check if a "="
3484 // follows.
3485 // If "[...]" has a line break "p" still points at the "[" and it
3486 // can't be an assignment.
3487 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003488 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003489 char_u *eq;
3490
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003491 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003492 if (p == eap->cmd && *p == '[')
3493 {
3494 int count = 0;
3495 int semicolon = FALSE;
3496
3497 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3498 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003499 eq = p;
3500 if (eq != NULL)
3501 {
3502 eq = skipwhite(eq);
3503 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3504 ++eq;
3505 }
3506 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003507 {
3508 eap->cmdidx = CMD_eval;
3509 return eap->cmd;
3510 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003511 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003512 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003513 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003514 return eap->cmd;
3515 }
3516 }
3517
3518 // Recognize an assignment if we recognize the variable name:
3519 // "g:var = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003520 // "var = expr" where "var" is a variable name or we are skipping
3521 // (variable declaration might have been skipped).
Bram Moolenaar83144542020-08-02 20:40:43 +02003522 if (*eap->cmd == '@')
3523 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003524 oplen = assignment_len(skipwhite(p), &heredoc);
3525 if (oplen > 0)
3526 {
3527 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3528 || *eap->cmd == '&'
3529 || *eap->cmd == '$'
3530 || *eap->cmd == '@'
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003531 || eap->skip
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003532 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003533 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003534 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003535 return eap->cmd;
3536 }
3537 }
3538
3539 // Recognize using a type for a w:, b:, t: or g: variable:
3540 // "w:varname: number = 123".
3541 if (eap->cmd[1] == ':' && *p == ':')
3542 {
3543 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003544 return eap->cmd;
3545 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003546 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003547
3548 // If it is an ID it might be a variable with an operator on the next
3549 // line, if the variable exists it can't be an Ex command.
3550 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003551 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003552 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3553 {
3554 eap->cmdidx = CMD_eval;
3555 return eap->cmd;
3556 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003557
3558 // Check for "++nr" and "--nr".
3559 if (p == eap->cmd && p[0] == p[1] && (*p == '+' || *p == '-'))
3560 {
3561 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3562 return eap->cmd + 2;
3563 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 }
3565#endif
3566
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 /*
3568 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003569 * Exceptions:
Bram Moolenaar10b94212021-02-19 21:42:57 +01003570 * - The 'k' command can directly be followed by any character.
3571 * But it is not used in Vim9 script.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003573 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003575 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 */
3577 p = eap->cmd;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003578 if (!vim9 && *p == 'k')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
3580 eap->cmdidx = CMD_k;
3581 ++p;
3582 }
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003583 else if (!vim9
3584 && p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003585 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3586 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 || p[1] == 'g'
3588 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3589 || p[1] == 'I'
3590 || (p[1] == 'r' && p[2] != 'e')))
3591 {
3592 eap->cmdidx = CMD_substitute;
3593 ++p;
3594 }
3595 else
3596 {
3597 while (ASCII_ISALPHA(*p))
3598 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003599 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003600 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003602 while (ASCII_ISALNUM(*p))
3603 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604 }
3605 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3606 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003607 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 ++p;
3609 while (ASCII_ISALPHA(*p))
3610 ++p;
3611 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003612
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003613 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003614 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 ++p;
3616 len = (int)(p - eap->cmd);
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003617 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003618 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003619 // Check for ":dl", ":dell", etc. to ":deletel": that's
3620 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003621 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003622 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003623 break;
3624 if (i == len - 1)
3625 {
3626 --len;
3627 if (p[-1] == 'l')
3628 eap->flags |= EXFLAG_LIST;
3629 else
3630 eap->flags |= EXFLAG_PRINT;
3631 }
3632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003634 if (ASCII_ISLOWER(eap->cmd[0]))
3635 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003636 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003637 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003638
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003639 if (command_count != (int)CMD_SIZE)
3640 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003641 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003642 getout(1);
3643 }
3644
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003645 // Use a precomputed index for fast look-up in cmdnames[]
3646 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003647 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3648 if (ASCII_ISLOWER(c2))
3649 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3650 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003651 else if (ASCII_ISUPPER(eap->cmd[0]))
3652 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003654 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655
3656 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3657 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3658 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3659 (size_t)len) == 0)
3660 {
3661#ifdef FEAT_EVAL
3662 if (full != NULL
3663 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3664 *full = TRUE;
3665#endif
3666 break;
3667 }
3668
Bram Moolenaar95388e32020-11-20 21:07:00 +01003669 // Not not recognize ":*" as the star command unless '*' is in
3670 // 'cpoptions'.
3671 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3672 p = eap->cmd;
3673
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003674 // Look for a user defined command as a last resort. Let ":Print" be
3675 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003676 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3677 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003679 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 while (ASCII_ISALNUM(*p))
3681 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003682 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003684 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 eap->cmdidx = CMD_SIZE;
3686 }
3687
Bram Moolenaar373863e2020-09-26 17:20:53 +02003688 // ":fina" means ":finally" for backwards compatibility.
3689 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3690 eap->cmdidx = CMD_finally;
3691
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003692#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003693 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003694 && vim9
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +02003695 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003696 && (eap->cmdidx < 0 ||
3697 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003698 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003699 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3700
3701 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003702 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003703 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003704 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003705#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003706
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 return p;
3708}
3709
3710#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003711static struct cmdmod
3712{
3713 char *name;
3714 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003715 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003716} cmdmods[] = {
3717 {"aboveleft", 3, FALSE},
3718 {"belowright", 3, FALSE},
3719 {"botright", 2, FALSE},
3720 {"browse", 3, FALSE},
3721 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003722 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003723 {"hide", 3, FALSE},
3724 {"keepalt", 5, FALSE},
3725 {"keepjumps", 5, FALSE},
3726 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003727 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003728 {"leftabove", 5, FALSE},
3729 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003730 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003731 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003732 {"rightbelow", 6, FALSE},
3733 {"sandbox", 3, FALSE},
3734 {"silent", 3, FALSE},
3735 {"tab", 3, TRUE},
3736 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003737 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003738 {"verbose", 4, TRUE},
3739 {"vertical", 4, FALSE},
3740};
3741
3742/*
3743 * Return length of a command modifier (including optional count).
3744 * Return zero when it's not a modifier.
3745 */
3746 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003747modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003748{
3749 int i, j;
3750 char_u *p = cmd;
3751
3752 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02003753 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02003754 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003755 {
3756 for (j = 0; p[j] != NUL; ++j)
3757 if (p[j] != cmdmods[i].name[j])
3758 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003759 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003760 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003761 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003762 }
3763 return 0;
3764}
3765
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766/*
3767 * Return > 0 if an Ex command "name" exists.
3768 * Return 2 if there is an exact match.
3769 * Return 3 if there is an ambiguous match.
3770 */
3771 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003772cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773{
3774 exarg_T ea;
3775 int full = FALSE;
3776 int i;
3777 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003778 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003780 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02003781 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 {
3783 for (j = 0; name[j] != NUL; ++j)
3784 if (name[j] != cmdmods[i].name[j])
3785 break;
3786 if (name[j] == NUL && j >= cmdmods[i].minlen)
3787 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3788 }
3789
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003790 // Check built-in commands and user defined commands.
3791 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003792 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003795 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003797 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3798 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003799 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003800 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3802}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003803
3804/*
3805 * "fullcommand" function
3806 */
3807 void
3808f_fullcommand(typval_T *argvars, typval_T *rettv)
3809{
3810 exarg_T ea;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003811 char_u *name;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003812 char_u *p;
3813
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003814 rettv->v_type = VAR_STRING;
3815 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003816
3817 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
3818 return;
3819
3820 name = argvars[0].vval.v_string;
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003821 if (name == NULL)
3822 return;
3823
3824 while (*name != NUL && *name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003825 name++;
3826 name = skip_range(name, TRUE, NULL);
3827
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003828 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
3829 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003830 ea.addr_count = 0;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003831 p = find_ex_command(&ea, NULL, NULL, NULL);
3832 if (p == NULL || ea.cmdidx == CMD_SIZE)
3833 return;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003834 if (in_vim9script())
3835 {
3836 int res;
3837
3838 ++emsg_silent;
3839 res = not_in_vim9(&ea);
3840 --emsg_silent;
3841
3842 if (res == FAIL)
3843 return;
3844 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003845
3846 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
3847 ? get_user_commands(NULL, ea.useridx)
3848 : cmdnames[ea.cmdidx].cmd_name);
3849}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850#endif
3851
Bram Moolenaard0190392019-08-23 21:17:35 +02003852 cmdidx_T
3853excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854{
Bram Moolenaard0190392019-08-23 21:17:35 +02003855 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856
Bram Moolenaard0190392019-08-23 21:17:35 +02003857 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3858 idx = (cmdidx_T)((int)idx + 1))
3859 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 break;
3861
Bram Moolenaard0190392019-08-23 21:17:35 +02003862 return idx;
3863}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864
Bram Moolenaard0190392019-08-23 21:17:35 +02003865 long
3866excmd_get_argt(cmdidx_T idx)
3867{
3868 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869}
3870
3871/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003872 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 *
3874 * Backslashed delimiters after / or ? will be skipped, and commands will
3875 * not be expanded between /'s and ?'s or after "'".
3876 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003877 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 * Returns the "cmd" pointer advanced to beyond the range.
3879 */
3880 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003881skip_range(
3882 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003883 int skip_star, // skip "*" used for Visual range
3884 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003886 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003888 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003890 if (*cmd == '\\')
3891 {
3892 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3893 ++cmd;
3894 else
3895 break;
3896 }
3897 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 {
3899 if (*++cmd == NUL && ctx != NULL)
3900 *ctx = EXPAND_NOTHING;
3901 }
3902 else if (*cmd == '/' || *cmd == '?')
3903 {
3904 delim = *cmd++;
3905 while (*cmd != NUL && *cmd != delim)
3906 if (*cmd++ == '\\' && *cmd != NUL)
3907 ++cmd;
3908 if (*cmd == NUL && ctx != NULL)
3909 *ctx = EXPAND_NOTHING;
3910 }
3911 if (*cmd != NUL)
3912 ++cmd;
3913 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003914
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003915 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003916 while (*cmd == ':')
3917 cmd = skipwhite(cmd + 1);
3918
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003919 // Skip "*" used for Visual range.
3920 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3921 cmd = skipwhite(cmd + 1);
3922
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 return cmd;
3924}
3925
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003926 static void
3927addr_error(cmd_addr_T addr_type)
3928{
3929 if (addr_type == ADDR_NONE)
3930 emsg(_(e_norange));
3931 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003932 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003933}
3934
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003936 * Return the default address for an address type.
3937 */
3938 static linenr_T
3939default_address(exarg_T *eap)
3940{
3941 linenr_T lnum = 0;
3942
3943 switch (eap->addr_type)
3944 {
3945 case ADDR_LINES:
3946 case ADDR_OTHER:
3947 // Default is the cursor line number. Avoid using an invalid
3948 // line number though.
3949 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3950 lnum = curbuf->b_ml.ml_line_count;
3951 else
3952 lnum = curwin->w_cursor.lnum;
3953 break;
3954 case ADDR_WINDOWS:
3955 lnum = CURRENT_WIN_NR;
3956 break;
3957 case ADDR_ARGUMENTS:
3958 lnum = curwin->w_arg_idx + 1;
3959 if (lnum > ARGCOUNT)
3960 lnum = ARGCOUNT;
3961 break;
3962 case ADDR_LOADED_BUFFERS:
3963 case ADDR_BUFFERS:
3964 lnum = curbuf->b_fnum;
3965 break;
3966 case ADDR_TABS:
3967 lnum = CURRENT_TAB_NR;
3968 break;
3969 case ADDR_TABS_RELATIVE:
3970 case ADDR_UNSIGNED:
3971 lnum = 1;
3972 break;
3973 case ADDR_QUICKFIX:
3974#ifdef FEAT_QUICKFIX
3975 lnum = qf_get_cur_idx(eap);
3976#endif
3977 break;
3978 case ADDR_QUICKFIX_VALID:
3979#ifdef FEAT_QUICKFIX
3980 lnum = qf_get_cur_valid_idx(eap);
3981#endif
3982 break;
3983 case ADDR_NONE:
3984 // Will give an error later if a range is found.
3985 break;
3986 }
3987 return lnum;
3988}
3989
3990/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003991 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 *
3993 * Set ptr to the next character after the part that was interpreted.
3994 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003995 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 *
3997 * Return MAXLNUM when no Ex address was found.
3998 */
3999 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004000get_address(
4001 exarg_T *eap UNUSED,
4002 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004003 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004004 int skip, // only skip the address, don't use it
4005 int silent, // no errors or side effects
4006 int to_other_file, // flag: may jump to other file
4007 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008{
4009 int c;
4010 int i;
4011 long n;
4012 char_u *cmd;
4013 pos_T pos;
4014 pos_T *fp;
4015 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004016 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017
4018 cmd = skipwhite(*ptr);
4019 lnum = MAXLNUM;
4020 do
4021 {
4022 switch (*cmd)
4023 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004024 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004025 ++cmd;
4026 switch (addr_type)
4027 {
4028 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004029 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 lnum = curwin->w_cursor.lnum;
4031 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004032 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004033 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004034 break;
4035 case ADDR_ARGUMENTS:
4036 lnum = curwin->w_arg_idx + 1;
4037 break;
4038 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004039 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004040 lnum = curbuf->b_fnum;
4041 break;
4042 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004043 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004044 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004045 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004046 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004047 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004048 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004049 cmd = NULL;
4050 goto error;
4051 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004052 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004053#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004054 lnum = qf_get_cur_idx(eap);
4055#endif
4056 break;
4057 case ADDR_QUICKFIX_VALID:
4058#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004059 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004060#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004061 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004062 }
4063 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004065 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004066 ++cmd;
4067 switch (addr_type)
4068 {
4069 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004070 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 lnum = curbuf->b_ml.ml_line_count;
4072 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004073 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004074 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004075 break;
4076 case ADDR_ARGUMENTS:
4077 lnum = ARGCOUNT;
4078 break;
4079 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004080 buf = lastbuf;
4081 while (buf->b_ml.ml_mfp == NULL)
4082 {
4083 if (buf->b_prev == NULL)
4084 break;
4085 buf = buf->b_prev;
4086 }
4087 lnum = buf->b_fnum;
4088 break;
4089 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004090 lnum = lastbuf->b_fnum;
4091 break;
4092 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004093 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004094 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004095 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004096 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004097 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004098 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004099 cmd = NULL;
4100 goto error;
4101 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004102 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004103#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004104 lnum = qf_get_size(eap);
4105 if (lnum == 0)
4106 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004107#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004108 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004109 case ADDR_QUICKFIX_VALID:
4110#ifdef FEAT_QUICKFIX
4111 lnum = qf_get_valid_size(eap);
4112 if (lnum == 0)
4113 lnum = 1;
4114#endif
4115 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004116 }
4117 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004119 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004120 if (*++cmd == NUL)
4121 {
4122 cmd = NULL;
4123 goto error;
4124 }
4125 if (addr_type != ADDR_LINES)
4126 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004127 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004128 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004129 goto error;
4130 }
4131 if (skip)
4132 ++cmd;
4133 else
4134 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004135 // Only accept a mark in another file when it is
4136 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004137 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4138 ++cmd;
4139 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004140 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004141 lnum = curwin->w_cursor.lnum;
4142 else
4143 {
4144 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 {
4146 cmd = NULL;
4147 goto error;
4148 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004149 lnum = fp->lnum;
4150 }
4151 }
4152 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153
4154 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004155 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004156 c = *cmd++;
4157 if (addr_type != ADDR_LINES)
4158 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004159 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004160 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004161 goto error;
4162 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004163 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004164 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004165 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004166 if (*cmd == c)
4167 ++cmd;
4168 }
4169 else
4170 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004171 int flags;
4172
4173 pos = curwin->w_cursor; // save curwin->w_cursor
4174
4175 // When '/' or '?' follows another address, start from
4176 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004177 if (lnum != MAXLNUM)
4178 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004179
4180 // Start a forward search at the end of the line (unless
4181 // before the first line).
4182 // Start a backward search at the start of the line.
4183 // This makes sure we never match in the current
4184 // line, and can match anywhere in the
4185 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004186 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004187 curwin->w_cursor.col = MAXCOL;
4188 else
4189 curwin->w_cursor.col = 0;
4190 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004191 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004192 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004193 {
4194 curwin->w_cursor = pos;
4195 cmd = NULL;
4196 goto error;
4197 }
4198 lnum = curwin->w_cursor.lnum;
4199 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004200 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004201 cmd += searchcmdlen;
4202 }
4203 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004205 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004206 ++cmd;
4207 if (addr_type != ADDR_LINES)
4208 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004209 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004210 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004211 goto error;
4212 }
4213 if (*cmd == '&')
4214 i = RE_SUBST;
4215 else if (*cmd == '?' || *cmd == '/')
4216 i = RE_SEARCH;
4217 else
4218 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004219 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004220 cmd = NULL;
4221 goto error;
4222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004224 if (!skip)
4225 {
4226 /*
4227 * When search follows another address, start from
4228 * there.
4229 */
4230 if (lnum != MAXLNUM)
4231 pos.lnum = lnum;
4232 else
4233 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004235 /*
4236 * Start the search just like for the above
4237 * do_search().
4238 */
4239 if (*cmd != '?')
4240 pos.col = MAXCOL;
4241 else
4242 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004243 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004244 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004245 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004246 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004247 lnum = pos.lnum;
4248 else
4249 {
4250 cmd = NULL;
4251 goto error;
4252 }
4253 }
4254 ++cmd;
4255 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004258 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004259 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 }
4261
4262 for (;;)
4263 {
4264 cmd = skipwhite(cmd);
4265 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4266 break;
4267
4268 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004269 {
4270 switch (addr_type)
4271 {
4272 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004273 case ADDR_OTHER:
4274 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004275 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004276 break;
4277 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004278 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004279 break;
4280 case ADDR_ARGUMENTS:
4281 lnum = curwin->w_arg_idx + 1;
4282 break;
4283 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004284 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004285 lnum = curbuf->b_fnum;
4286 break;
4287 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004288 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004289 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004290 case ADDR_TABS_RELATIVE:
4291 lnum = 1;
4292 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004293 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004294#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004295 lnum = qf_get_cur_idx(eap);
4296#endif
4297 break;
4298 case ADDR_QUICKFIX_VALID:
4299#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004300 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004301#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004302 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004303 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004304 case ADDR_UNSIGNED:
4305 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004306 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004307 }
4308 }
4309
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004311 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 else
4313 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004314 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 n = 1;
4316 else
4317 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004318
4319 if (addr_type == ADDR_TABS_RELATIVE)
4320 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004321 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004322 cmd = NULL;
4323 goto error;
4324 }
4325 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004326 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004327 lnum = compute_buffer_local_count(
4328 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004330 {
4331#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004332 // Relative line addressing, need to adjust for folded lines
4333 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004334 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4335 && address_count >= 2)
4336 (void)hasFolding(lnum, NULL, &lnum);
4337#endif
4338 if (i == '-')
4339 lnum -= n;
4340 else
4341 lnum += n;
4342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 }
4344 } while (*cmd == '/' || *cmd == '?');
4345
4346error:
4347 *ptr = cmd;
4348 return lnum;
4349}
4350
4351/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004352 * Set eap->line1 and eap->line2 to the whole range.
4353 * Used for commands with the EX_DFLALL flag and no range given.
4354 */
4355 static void
4356address_default_all(exarg_T *eap)
4357{
4358 eap->line1 = 1;
4359 switch (eap->addr_type)
4360 {
4361 case ADDR_LINES:
4362 case ADDR_OTHER:
4363 eap->line2 = curbuf->b_ml.ml_line_count;
4364 break;
4365 case ADDR_LOADED_BUFFERS:
4366 {
4367 buf_T *buf = firstbuf;
4368
4369 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4370 buf = buf->b_next;
4371 eap->line1 = buf->b_fnum;
4372 buf = lastbuf;
4373 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4374 buf = buf->b_prev;
4375 eap->line2 = buf->b_fnum;
4376 }
4377 break;
4378 case ADDR_BUFFERS:
4379 eap->line1 = firstbuf->b_fnum;
4380 eap->line2 = lastbuf->b_fnum;
4381 break;
4382 case ADDR_WINDOWS:
4383 eap->line2 = LAST_WIN_NR;
4384 break;
4385 case ADDR_TABS:
4386 eap->line2 = LAST_TAB_NR;
4387 break;
4388 case ADDR_TABS_RELATIVE:
4389 eap->line2 = 1;
4390 break;
4391 case ADDR_ARGUMENTS:
4392 if (ARGCOUNT == 0)
4393 eap->line1 = eap->line2 = 0;
4394 else
4395 eap->line2 = ARGCOUNT;
4396 break;
4397 case ADDR_QUICKFIX_VALID:
4398#ifdef FEAT_QUICKFIX
4399 eap->line2 = qf_get_valid_size(eap);
4400 if (eap->line2 == 0)
4401 eap->line2 = 1;
4402#endif
4403 break;
4404 case ADDR_NONE:
4405 case ADDR_UNSIGNED:
4406 case ADDR_QUICKFIX:
4407 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4408 break;
4409 }
4410}
4411
4412
4413/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004414 * Get flags from an Ex command argument.
4415 */
4416 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004417get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004418{
4419 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4420 {
4421 if (*eap->arg == 'l')
4422 eap->flags |= EXFLAG_LIST;
4423 else if (*eap->arg == 'p')
4424 eap->flags |= EXFLAG_PRINT;
4425 else
4426 eap->flags |= EXFLAG_NR;
4427 eap->arg = skipwhite(eap->arg + 1);
4428 }
4429}
4430
4431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 * Function called for command which is Not Implemented. NI!
4433 */
4434 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004435ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436{
4437 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004438 eap->errmsg =
4439 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440}
4441
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004442#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443/*
4444 * Function called for script command which is Not Implemented. NI!
4445 * Skips over ":perl <<EOF" constructs.
4446 */
4447 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004448ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449{
4450 if (!eap->skip)
4451 ex_ni(eap);
4452 else
4453 vim_free(script_get(eap, eap->arg));
4454}
4455#endif
4456
4457/*
4458 * Check range in Ex command for validity.
4459 * Return NULL when valid, error message when invalid.
4460 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004461 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004462invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004464 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004465
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 if ( eap->line1 < 0
4467 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004468 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004469 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004470
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004471 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004472 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004473 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004474 {
4475 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004476 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004477#ifdef FEAT_DIFF
4478 + (eap->cmdidx == CMD_diffget)
4479#endif
4480 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004481 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004482 break;
4483 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004484 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004485 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004486 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004487 break;
4488 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004489 // Only a boundary check, not whether the buffers actually
4490 // exist.
4491 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004492 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004493 break;
4494 case ADDR_LOADED_BUFFERS:
4495 buf = firstbuf;
4496 while (buf->b_ml.ml_mfp == NULL)
4497 {
4498 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004499 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004500 buf = buf->b_next;
4501 }
4502 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004503 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004504 buf = lastbuf;
4505 while (buf->b_ml.ml_mfp == NULL)
4506 {
4507 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004508 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004509 buf = buf->b_prev;
4510 }
4511 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004512 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004513 break;
4514 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004515 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004516 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004517 break;
4518 case ADDR_TABS:
4519 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004520 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004521 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004522 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004523 case ADDR_OTHER:
4524 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004525 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004526 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004527#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004528 // No error for value that is too big, will use the last entry.
4529 if (eap->line2 <= 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004530 return _(e_invalid_range);
Bram Moolenaare906c502015-09-09 21:10:39 +02004531#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004532 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004533 case ADDR_QUICKFIX_VALID:
4534#ifdef FEAT_QUICKFIX
4535 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4536 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004537 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004538#endif
4539 break;
4540 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004541 case ADDR_NONE:
4542 // Will give an error elsewhere.
4543 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004544 }
4545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 return NULL;
4547}
4548
4549/*
4550 * Correct the range for zero line number, if required.
4551 */
4552 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004553correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004555 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 {
4557 if (eap->line1 == 0)
4558 eap->line1 = 1;
4559 if (eap->line2 == 0)
4560 eap->line2 = 1;
4561 }
4562}
4563
Bram Moolenaar748bf032005-02-02 23:04:36 +00004564#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004565/*
4566 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4567 * pattern. Otherwise return eap->arg.
4568 */
4569 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004570skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004571{
4572 char_u *p = eap->arg;
4573
Bram Moolenaara37420f2006-02-04 22:37:47 +00004574 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4575 || eap->cmdidx == CMD_vimgrepadd
4576 || eap->cmdidx == CMD_lvimgrepadd
4577 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004578 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004579 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004580 if (p == NULL)
4581 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004582 }
4583 return p;
4584}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004585
4586/*
4587 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4588 * in the command line, so that things like % get expanded.
4589 */
4590 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004591replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004592{
4593 char_u *new_cmdline;
4594 char_u *program;
4595 char_u *pos;
4596 char_u *ptr;
4597 int len;
4598 int i;
4599
4600 /*
4601 * Don't do it when ":vimgrep" is used for ":grep".
4602 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004603 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4604 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4605 || eap->cmdidx == CMD_grepadd
4606 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004607 && !grep_internal(eap->cmdidx))
4608 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004609 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4610 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004611 {
4612 if (*curbuf->b_p_gp == NUL)
4613 program = p_gp;
4614 else
4615 program = curbuf->b_p_gp;
4616 }
4617 else
4618 {
4619 if (*curbuf->b_p_mp == NUL)
4620 program = p_mp;
4621 else
4622 program = curbuf->b_p_mp;
4623 }
4624
4625 p = skipwhite(p);
4626
4627 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4628 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004629 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004630 i = 1;
4631 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4632 ++i;
4633 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004634 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004635 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004636 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004637 ptr = new_cmdline;
4638 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4639 {
4640 i = (int)(pos - program);
4641 STRNCPY(ptr, program, i);
4642 STRCPY(ptr += i, p);
4643 ptr += len;
4644 program = pos + 2;
4645 }
4646 STRCPY(ptr, program);
4647 }
4648 else
4649 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004650 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004651 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004652 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004653 STRCPY(new_cmdline, program);
4654 STRCAT(new_cmdline, " ");
4655 STRCAT(new_cmdline, p);
4656 }
4657 msg_make(p);
4658
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004659 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004660 vim_free(*cmdlinep);
4661 *cmdlinep = new_cmdline;
4662 p = new_cmdline;
4663 }
4664 return p;
4665}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004666#endif
4667
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668/*
4669 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004670 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 * Return FAIL for failure, OK otherwise.
4672 */
4673 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004674expand_filename(
4675 exarg_T *eap,
4676 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004677 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004679 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 char_u *repl;
4681 int srclen;
4682 char_u *p;
4683 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004684 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685
Bram Moolenaar748bf032005-02-02 23:04:36 +00004686#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004687 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004688 p = skip_grep_pat(eap);
4689#else
4690 p = eap->arg;
4691#endif
4692
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 /*
4694 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4695 * the file name contains a wildcard it should not cause expanding.
4696 * (it will be expanded anyway if there is a wildcard before replacing).
4697 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004698 has_wildcards = mch_has_wildcard(p);
4699 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004701#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004702 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004703 if (p[0] == '`' && p[1] == '=')
4704 {
4705 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004706 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004707 if (*p == '`')
4708 ++p;
4709 continue;
4710 }
4711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 /*
4713 * Quick check if this cannot be the start of a special string.
4714 * Also removes backslash before '%', '#' and '<'.
4715 */
4716 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4717 {
4718 ++p;
4719 continue;
4720 }
4721
4722 /*
4723 * Try to find a match at this position.
4724 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004725 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4726 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004727 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004729 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 {
4731 p += srclen;
4732 continue;
4733 }
4734
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004735 // Wildcards won't be expanded below, the replacement is taken
4736 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004737 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4738 {
4739 char_u *l = repl;
4740
4741 repl = expand_env_save(repl);
4742 vim_free(l);
4743 }
4744
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004745 // Need to escape white space et al. with a backslash.
4746 // Don't do this for:
4747 // - replacement that already has been escaped: "##"
4748 // - shell commands (may have to use quotes instead).
4749 // - non-unix systems when there is a single argument (spaces don't
4750 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004752 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 && eap->cmdidx != CMD_grep
4755 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004756 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004757 && eap->cmdidx != CMD_lgrep
4758 && eap->cmdidx != CMD_lgrepadd
4759 && eap->cmdidx != CMD_lmake
4760 && eap->cmdidx != CMD_make
4761 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004763 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764#endif
4765 )
4766 {
4767 char_u *l;
4768#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004769 // Don't escape a backslash here, because rem_backslash() doesn't
4770 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 static char_u *nobslash = (char_u *)" \t\"|";
4772# define ESCAPE_CHARS nobslash
4773#else
4774# define ESCAPE_CHARS escape_chars
4775#endif
4776
4777 for (l = repl; *l; ++l)
4778 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4779 {
4780 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4781 if (l != NULL)
4782 {
4783 vim_free(repl);
4784 repl = l;
4785 }
4786 break;
4787 }
4788 }
4789
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004790 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004791 if ((eap->usefilter || eap->cmdidx == CMD_bang
4792 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004793 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 {
4795 char_u *l;
4796
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004797 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 if (l != NULL)
4799 {
4800 vim_free(repl);
4801 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 }
4803 }
4804
4805 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4806 vim_free(repl);
4807 if (p == NULL)
4808 return FAIL;
4809 }
4810
4811 /*
4812 * One file argument: Expand wildcards.
4813 * Don't do this with ":r !command" or ":w !command".
4814 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004815 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 {
4817 /*
4818 * May do this twice:
4819 * 1. Replace environment variables.
4820 * 2. Replace any other wildcards, remove backslashes.
4821 */
4822 for (n = 1; n <= 2; ++n)
4823 {
4824 if (n == 2)
4825 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 /*
4827 * Halve the number of backslashes (this is Vi compatible).
4828 * For Unix and OS/2, when wildcards are expanded, this is
4829 * done by ExpandOne() below.
4830 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004831#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 if (!has_wildcards)
4833#endif
4834 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 }
4836
4837 if (has_wildcards)
4838 {
4839 if (n == 1)
4840 {
4841 /*
4842 * First loop: May expand environment variables. This
4843 * can be done much faster with expand_env() than with
4844 * something else (e.g., calling a shell).
4845 * After expanding environment variables, check again
4846 * if there are still wildcards present.
4847 */
4848 if (vim_strchr(eap->arg, '$') != NULL
4849 || vim_strchr(eap->arg, '~') != NULL)
4850 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004851 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004852 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 has_wildcards = mch_has_wildcard(NameBuff);
4854 p = NameBuff;
4855 }
4856 else
4857 p = NULL;
4858 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004859 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 {
4861 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004862 int options = WILD_LIST_NOTFOUND
4863 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864
4865 ExpandInit(&xpc);
4866 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004867 if (p_wic)
4868 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004870 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 if (p == NULL)
4872 return FAIL;
4873 }
4874 if (p != NULL)
4875 {
4876 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4877 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004878 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 vim_free(p);
4880 }
4881 }
4882 }
4883 }
4884 return OK;
4885}
4886
4887/*
4888 * Replace part of the command line, keeping eap->cmd, eap->arg and
4889 * eap->nextcmd correct.
4890 * "src" points to the part that is to be replaced, of length "srclen".
4891 * "repl" is the replacement string.
4892 * Returns a pointer to the character after the replaced string.
4893 * Returns NULL for failure.
4894 */
4895 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004896repl_cmdline(
4897 exarg_T *eap,
4898 char_u *src,
4899 int srclen,
4900 char_u *repl,
4901 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902{
4903 int len;
4904 int i;
4905 char_u *new_cmdline;
4906
4907 /*
4908 * The new command line is build in new_cmdline[].
4909 * First allocate it.
4910 * Careful: a "+cmd" argument may have been NUL terminated.
4911 */
4912 len = (int)STRLEN(repl);
4913 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004914 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004915 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004916 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004917 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918
4919 /*
4920 * Copy the stuff before the expanded part.
4921 * Copy the expanded stuff.
4922 * Copy what came after the expanded part.
4923 * Copy the next commands, if there are any.
4924 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004925 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004927
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004929 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004931 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004933 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 {
4935 i = (int)STRLEN(new_cmdline) + 1;
4936 STRCPY(new_cmdline + i, eap->nextcmd);
4937 eap->nextcmd = new_cmdline + i;
4938 }
4939 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4940 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4941 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4942 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4943 vim_free(*cmdlinep);
4944 *cmdlinep = new_cmdline;
4945
4946 return src;
4947}
4948
4949/*
4950 * Check for '|' to separate commands and '"' to start comments.
4951 */
4952 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004953separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954{
4955 char_u *p;
4956
Bram Moolenaar86b68352004-12-27 21:59:20 +00004957#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004958 p = skip_grep_pat(eap);
4959#else
4960 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004961#endif
4962
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004963 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 {
4965 if (*p == Ctrl_V)
4966 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004967 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004968 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004970 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004971 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004972 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 break;
4974 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004975
4976#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004977 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004978 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004979 {
4980 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004981 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01004982 if (*p == NUL) // stop at NUL after CTRL-V
4983 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004984 }
4985#endif
4986
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004987 // Check for '"': start of comment or '|': next command
4988 // :@" and :*" do not start a comment!
4989 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004990 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004991#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004992 && !in_vim9script()
4993#endif
4994 && !(eap->argt & EX_NOTRLCOM)
4995 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4996 || p != eap->arg)
4997 && (eap->cmdidx != CMD_redir
4998 || p != eap->arg + 1 || p[-1] != '@'))
4999#ifdef FEAT_EVAL
5000 || (*p == '#'
5001 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005002 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005003 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02005004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 || *p == '|' || *p == '\n')
5006 {
5007 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005008 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 * AND 'b' is present in 'cpoptions'.
5010 */
5011 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005012 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005014 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 --p;
5016 }
5017 else
5018 {
5019 eap->nextcmd = check_nextcmd(p);
5020 *p = NUL;
5021 break;
5022 }
5023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005025
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005026 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 del_trailing_spaces(eap->arg);
5028}
5029
5030/*
5031 * get + command from ex argument
5032 */
5033 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005034getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035{
5036 char_u *arg = *argp;
5037 char_u *command = NULL;
5038
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005039 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 {
5041 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005042 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 command = dollar_command;
5044 else
5045 {
5046 command = arg;
5047 arg = skip_cmd_arg(command, TRUE);
5048 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005049 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 }
5051
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005052 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 *argp = arg;
5054 }
5055 return command;
5056}
5057
5058/*
5059 * Find end of "+command" argument. Skip over "\ " and "\\".
5060 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005061 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005062skip_cmd_arg(
5063 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005064 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065{
5066 while (*p && !vim_isspace(*p))
5067 {
5068 if (*p == '\\' && p[1] != NUL)
5069 {
5070 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005071 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 else
5073 ++p;
5074 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005075 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 }
5077 return p;
5078}
5079
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005080 int
5081get_bad_opt(char_u *p, exarg_T *eap)
5082{
5083 if (STRICMP(p, "keep") == 0)
5084 eap->bad_char = BAD_KEEP;
5085 else if (STRICMP(p, "drop") == 0)
5086 eap->bad_char = BAD_DROP;
5087 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5088 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005089 else
5090 return FAIL;
5091 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005092}
5093
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094/*
5095 * Get "++opt=arg" argument.
5096 * Return FAIL or OK.
5097 */
5098 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005099getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100{
5101 char_u *arg = eap->arg + 2;
5102 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005103 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005106 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5108 {
5109 if (*arg == 'n')
5110 {
5111 arg += 2;
5112 eap->force_bin = FORCE_NOBIN;
5113 }
5114 else
5115 eap->force_bin = FORCE_BIN;
5116 if (!checkforcmd(&arg, "binary", 3))
5117 return FAIL;
5118 eap->arg = skipwhite(arg);
5119 return OK;
5120 }
5121
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005122 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005123 if (STRNCMP(arg, "edit", 4) == 0)
5124 {
5125 eap->read_edit = TRUE;
5126 eap->arg = skipwhite(arg + 4);
5127 return OK;
5128 }
5129
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 if (STRNCMP(arg, "ff", 2) == 0)
5131 {
5132 arg += 2;
5133 pp = &eap->force_ff;
5134 }
5135 else if (STRNCMP(arg, "fileformat", 10) == 0)
5136 {
5137 arg += 10;
5138 pp = &eap->force_ff;
5139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 else if (STRNCMP(arg, "enc", 3) == 0)
5141 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005142 if (STRNCMP(arg, "encoding", 8) == 0)
5143 arg += 8;
5144 else
5145 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 pp = &eap->force_enc;
5147 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005148 else if (STRNCMP(arg, "bad", 3) == 0)
5149 {
5150 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005151 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153
5154 if (pp == NULL || *arg != '=')
5155 return FAIL;
5156
5157 ++arg;
5158 *pp = (int)(arg - eap->cmd);
5159 arg = skip_cmd_arg(arg, FALSE);
5160 eap->arg = skipwhite(arg);
5161 *arg = NUL;
5162
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 if (pp == &eap->force_ff)
5164 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5166 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005167 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005169 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005171 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5173 *p = TOLOWER_ASC(*p);
5174 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005175 else
5176 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005177 // Check ++bad= argument. Must be a single-byte character, "keep" or
5178 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005179 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005180 return FAIL;
5181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182
5183 return OK;
5184}
5185
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005187ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188{
5189 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005190 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 * directory for security reasons.
5192 */
5193 if (secure)
5194 {
5195 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005196 eap->errmsg =
5197 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 }
5199 else if (eap->cmdidx == CMD_autocmd)
5200 do_autocmd(eap->arg, eap->forceit);
5201 else
5202 do_augroup(eap->arg, eap->forceit);
5203}
5204
5205/*
5206 * ":doautocmd": Apply the automatic commands to the current buffer.
5207 */
5208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005209ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005211 char_u *arg = eap->arg;
5212 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005213 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005214
Bram Moolenaar1610d052016-06-09 22:53:01 +02005215 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005216 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005217 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005218 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221/*
5222 * :[N]bunload[!] [N] [bufname] unload buffer
5223 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5224 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5225 */
5226 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005227ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005229 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005230 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 eap->errmsg = do_bufdel(
5232 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5233 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5234 : DOBUF_UNLOAD, eap->arg,
5235 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5236}
5237
5238/*
5239 * :[N]buffer [N] to buffer N
5240 * :[N]sbuffer [N] to buffer N
5241 */
5242 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005243ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005245 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005246 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005248 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 else
5250 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005251 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5253 else
5254 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005255 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005256 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 }
5258}
5259
5260/*
5261 * :[N]bmodified [N] to next mod. buffer
5262 * :[N]sbmodified [N] to next mod. buffer
5263 */
5264 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005265ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266{
5267 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005268 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005269 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270}
5271
5272/*
5273 * :[N]bnext [N] to next buffer
5274 * :[N]sbnext [N] split and to next buffer
5275 */
5276 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005277ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005279 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005280 return;
5281
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005283 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005284 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285}
5286
5287/*
5288 * :[N]bNext [N] to previous buffer
5289 * :[N]bprevious [N] to previous buffer
5290 * :[N]sbNext [N] split and to previous buffer
5291 * :[N]sbprevious [N] split and to previous buffer
5292 */
5293 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005294ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005296 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005297 return;
5298
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005300 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005301 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302}
5303
5304/*
5305 * :brewind to first buffer
5306 * :bfirst to first buffer
5307 * :sbrewind split and to first buffer
5308 * :sbfirst split and to first buffer
5309 */
5310 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005311ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005313 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005314 return;
5315
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005317 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005318 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319}
5320
5321/*
5322 * :blast to last buffer
5323 * :sblast split and to last buffer
5324 */
5325 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005326ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005328 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005329 return;
5330
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005332 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005333 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335
Bram Moolenaar7a092242020-04-16 22:10:49 +02005336/*
5337 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005338 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005341ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005343 int comment_char = '"';
5344
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005345#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005346 if (in_vim9script())
5347 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005348#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005349 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005350}
5351
5352/*
5353 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5354 * to "cmd_start" or has a white space character before it.
5355 */
5356 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005357ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005358{
5359 int c = *cmd;
5360
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005361 if (c == NUL || c == '|' || c == '\n')
5362 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005363#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005364 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005365 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5366 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005367 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005368#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005369 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370}
5371
5372#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5373 || defined(PROTO)
5374/*
5375 * Return the next command, after the first '|' or '\n'.
5376 * Return NULL if not found.
5377 */
5378 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005379find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380{
5381 while (*p != '|' && *p != '\n')
5382 {
5383 if (*p == NUL)
5384 return NULL;
5385 ++p;
5386 }
5387 return (p + 1);
5388}
5389#endif
5390
5391/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005392 * Check if *p is a separator between Ex commands, skipping over white space.
5393 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 */
5395 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005396check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005398 char_u *s = skipwhite(p);
5399
5400 if (*s == '|' || *s == '\n')
5401 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 else
5403 return NULL;
5404}
5405
5406/*
5407 * - if there are more files to edit
5408 * - and this is the last window
5409 * - and forceit not used
5410 * - and not repeated twice on a row
5411 * return FAIL and give error message if 'message' TRUE
5412 * return OK otherwise
5413 */
5414 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005415check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005416 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005417 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418{
5419 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5420
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005421 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005422 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 {
5424 if (message)
5425 {
5426#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005427 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5428 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005430 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005432 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5433 NGETTEXT("%d more file to edit. Quit anyway?",
5434 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5436 return OK;
5437 return FAIL;
5438 }
5439#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005440 semsg(NGETTEXT("E173: %d more file to edit",
5441 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005442 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 }
5444 return FAIL;
5445 }
5446 return OK;
5447}
5448
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449/*
5450 * Function given to ExpandGeneric() to obtain the list of command names.
5451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005453get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454{
5455 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 return cmdnames[idx].cmd_name;
5458}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005461ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462{
Bram Moolenaare6850792010-05-14 15:28:44 +02005463 if (*eap->arg == NUL)
5464 {
5465#ifdef FEAT_EVAL
5466 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5467 char_u *p = NULL;
5468
5469 if (expr != NULL)
5470 {
5471 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005472 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005473 --emsg_off;
5474 vim_free(expr);
5475 }
5476 if (p != NULL)
5477 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005478 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005479 vim_free(p);
5480 }
5481 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005482 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005483#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005484 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005485#endif
5486 }
5487 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005488 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005489
5490#ifdef FEAT_VTP
5491 else if (has_vtp_working())
5492 {
5493 // background color change requires clear + redraw
5494 update_screen(CLEAR);
5495 redrawcmd();
5496 }
5497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498}
5499
5500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005501ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502{
5503 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005504 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 do_highlight(eap->arg, eap->forceit, FALSE);
5506}
5507
5508
5509/*
5510 * Call this function if we thought we were going to exit, but we won't
5511 * (because of an error). May need to restore the terminal mode.
5512 */
5513 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005514not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515{
5516 exiting = FALSE;
5517 settmode(TMODE_RAW);
5518}
5519
Bram Moolenaar3552e742021-05-29 12:21:58 +02005520 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005521before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5522{
5523 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5524
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005525 // Bail out when autocommands closed the window.
5526 // Refuse to quit when the buffer in the last window is being closed (can
5527 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005528 if (!win_valid(wp)
5529 || curbuf_locked()
5530 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5531 return TRUE;
5532
5533 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5534 {
5535 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005536 // Refuse to quit when locked or when the window was closed or the
5537 // buffer in the last window is being closed (can only happen in
5538 // autocommands).
5539 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005540 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5541 return TRUE;
5542 }
5543
5544 return FALSE;
5545}
5546
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005548 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005549 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005550 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005552 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005553ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005555 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005556
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557#ifdef FEAT_CMDWIN
5558 if (cmdwin_type != 0)
5559 {
5560 cmdwin_result = Ctrl_C;
5561 return;
5562 }
5563#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005564 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005565 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005566 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005567 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005568 return;
5569 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005570 if (eap->addr_count > 0)
5571 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005572 int wnr = eap->line2;
5573
5574 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5575 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005576 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005577 }
5578 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005579 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005580
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005581 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005582 if (curbuf_locked())
5583 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005584
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005585 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005586 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005587 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588
5589#ifdef FEAT_NETBEANS_INTG
5590 netbeansForcedQuit = eap->forceit;
5591#endif
5592
5593 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02005594 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 */
5596 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5597 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005598 if ((!buf_hide(wp->w_buffer)
5599 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005600 | (eap->forceit ? CCGD_FORCEIT : 0)
5601 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005603 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 {
5605 not_exiting();
5606 }
5607 else
5608 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005609 // quit last window
5610 // Note: only_one_window() returns true, even so a help window is
5611 // still open. In that case only quit, if no address has been
5612 // specified. Example:
5613 // :h|wincmd w|1q - don't quit
5614 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005615 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005617 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005618#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005620#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005621 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005622 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 }
5624}
5625
5626/*
5627 * ":cquit".
5628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005630ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005632 // this does not always pass on the exit code to the Manx compiler. why?
5633 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634}
5635
5636/*
5637 * ":qall": try to quit all windows
5638 */
5639 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005640ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641{
5642# ifdef FEAT_CMDWIN
5643 if (cmdwin_type != 0)
5644 {
5645 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005646 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 else
5648 cmdwin_result = K_XF2;
5649 return;
5650 }
5651# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005652
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005653 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005654 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005655 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005656 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005657 return;
5658 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005659
5660 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005661 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005662
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005664 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 getout(0);
5666 not_exiting();
5667}
5668
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669/*
5670 * ":close": close current window, unless it is the last one
5671 */
5672 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005673ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005675 win_T *win;
5676 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005677#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005679 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005681#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005682 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005683 {
5684 if (eap->addr_count == 0)
5685 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005686 else
5687 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005688 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005689 {
5690 winnr++;
5691 if (winnr == eap->line2)
5692 break;
5693 }
5694 if (win == NULL)
5695 win = lastwin;
5696 ex_win_close(eap->forceit, win, NULL);
5697 }
5698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699}
5700
Bram Moolenaar4033c552017-09-16 20:54:51 +02005701#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005702/*
5703 * ":pclose": Close any preview window.
5704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005706ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005707{
5708 win_T *win;
5709
Bram Moolenaar79648732019-07-18 21:43:07 +02005710 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005711 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005712 if (win->w_p_pvw)
5713 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005714 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005715 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005716 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005717# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005718 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005719 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005720# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005721}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005722#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005723
Bram Moolenaarf740b292006-02-16 22:11:02 +00005724/*
5725 * Close window "win" and take care of handling closing the last window for a
5726 * modified buffer.
5727 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005729ex_win_close(
5730 int forceit,
5731 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005732 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733{
5734 int need_hide;
5735 buf_T *buf = win->w_buffer;
5736
Bram Moolenaarcf844172020-06-26 19:44:06 +02005737 // Never close the autocommand window.
5738 if (win == aucmd_win)
5739 {
5740 emsg(_(e_autocmd_close));
5741 return;
5742 }
5743
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005745 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005747#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005748 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005750 bufref_T bufref;
5751
5752 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005754 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 return;
5756 need_hide = FALSE;
5757 }
5758 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005761 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 return;
5763 }
5764 }
5765
Bram Moolenaar4033c552017-09-16 20:54:51 +02005766#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005768#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005769
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005770 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005771 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005772 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005773 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005774 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775}
5776
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005778 * Handle the argument for a tabpage related ex command.
5779 * Returns a tabpage number.
5780 * When an error is encountered then eap->errmsg is set.
5781 */
5782 static int
5783get_tabpage_arg(exarg_T *eap)
5784{
5785 int tab_number;
5786 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5787
5788 if (eap->arg && *eap->arg != NUL)
5789 {
5790 char_u *p = eap->arg;
5791 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005792 int relative = 0; // argument +N/-N means: go to N places to the
5793 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005794
5795 if (*p == '-')
5796 {
5797 relative = -1;
5798 p++;
5799 }
5800 else if (*p == '+')
5801 {
5802 relative = 1;
5803 p++;
5804 }
5805
5806 p_save = p;
5807 tab_number = getdigits(&p);
5808
5809 if (relative == 0)
5810 {
5811 if (STRCMP(p, "$") == 0)
5812 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005813 else if (STRCMP(p, "#") == 0)
5814 if (valid_tabpage(lastused_tabpage))
5815 tab_number = tabpage_index(lastused_tabpage);
5816 else
5817 {
5818 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5819 tab_number = 0;
5820 goto theend;
5821 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005822 else if (p == p_save || *p_save == '-' || *p != NUL
5823 || tab_number > LAST_TAB_NR)
5824 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005825 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005826 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005827 goto theend;
5828 }
5829 }
5830 else
5831 {
5832 if (*p_save == NUL)
5833 tab_number = 1;
5834 else if (p == p_save || *p_save == '-' || *p != NUL
5835 || tab_number == 0)
5836 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005837 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005838 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005839 goto theend;
5840 }
5841 tab_number = tab_number * relative + tabpage_index(curtab);
5842 if (!unaccept_arg0 && relative == -1)
5843 --tab_number;
5844 }
5845 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005846 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005847 }
5848 else if (eap->addr_count > 0)
5849 {
5850 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005851 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005852 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005853 tab_number = 0;
5854 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005855 else
5856 {
5857 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005858 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005859 {
5860 --tab_number;
5861 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005862 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01005863 }
5864 }
5865 }
5866 else
5867 {
5868 switch (eap->cmdidx)
5869 {
5870 case CMD_tabnext:
5871 tab_number = tabpage_index(curtab) + 1;
5872 if (tab_number > LAST_TAB_NR)
5873 tab_number = 1;
5874 break;
5875 case CMD_tabmove:
5876 tab_number = LAST_TAB_NR;
5877 break;
5878 default:
5879 tab_number = tabpage_index(curtab);
5880 }
5881 }
5882
5883theend:
5884 return tab_number;
5885}
5886
5887/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005888 * ":tabclose": close current tab page, unless it is the last one.
5889 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 */
5891 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005892ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005894 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005895 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005896
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005897# ifdef FEAT_CMDWIN
5898 if (cmdwin_type != 0)
5899 cmdwin_result = K_IGNORE;
5900 else
5901# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005902 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005903 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005904 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005906 tab_number = get_tabpage_arg(eap);
5907 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005908 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005909 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005910 if (tp == NULL)
5911 {
5912 beep_flush();
5913 return;
5914 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005915 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005916 {
5917 tabpage_close_other(tp, eap->forceit);
5918 return;
5919 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005920 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005921 tabpage_close(eap->forceit);
5922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005924}
5925
5926/*
5927 * ":tabonly": close all tab pages except the current one
5928 */
5929 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005930ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005931{
5932 tabpage_T *tp;
5933 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005934 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005935
5936# ifdef FEAT_CMDWIN
5937 if (cmdwin_type != 0)
5938 cmdwin_result = K_IGNORE;
5939 else
5940# endif
5941 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005942 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005943 else
5944 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005945 tab_number = get_tabpage_arg(eap);
5946 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005947 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005948 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005949 // Repeat this up to a 1000 times, because autocommands may
5950 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005951 for (done = 0; done < 1000; ++done)
5952 {
5953 FOR_ALL_TABPAGES(tp)
5954 if (tp->tp_topframe != topframe)
5955 {
5956 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005957 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005958 if (valid_tabpage(tp))
5959 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005960 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005961 break;
5962 }
5963 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005964 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005965 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005966 }
5967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969
5970/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005971 * Close the current tab page.
5972 */
5973 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005974tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005975{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005976 // First close all the windows but the current one. If that worked then
5977 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005978 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005979 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005980 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005981 ex_win_close(forceit, curwin, NULL);
5982# ifdef FEAT_GUI
5983 need_mouse_correct = TRUE;
5984# endif
5985}
5986
5987/*
5988 * Close tab page "tp", which is not the current tab page.
5989 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005990 * Also takes care of the tab pages line disappearing when closing the
5991 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005992 */
5993 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005994tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005995{
5996 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005997 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005998 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005999
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006000 // Limit to 1000 windows, autocommands may add a window while we close
6001 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006002 while (++done < 1000)
6003 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006004 wp = tp->tp_firstwin;
6005 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006006
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006007 // Autocommands may delete the tab page under our fingers and we may
6008 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006009 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006010 break;
6011 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006012
Bram Moolenaar29323592016-07-24 22:04:11 +02006013 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006014
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006015 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006016 if (h != tabline_height())
6017 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006018}
6019
6020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 * ":only".
6022 */
6023 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006024ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006026 win_T *wp;
6027 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028# ifdef FEAT_GUI
6029 need_mouse_correct = TRUE;
6030# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006031 if (eap->addr_count > 0)
6032 {
6033 wnr = eap->line2;
6034 for (wp = firstwin; --wnr > 0; )
6035 {
6036 if (wp->w_next == NULL)
6037 break;
6038 else
6039 wp = wp->w_next;
6040 }
6041 win_goto(wp);
6042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 close_others(TRUE, eap->forceit);
6044}
6045
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006047ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006049 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01006050 if (!eap->skip)
6051 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006052#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006053 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006054#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006055 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006056 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01006057 else
6058 {
6059 int winnr = 0;
6060 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006061
Bram Moolenaar2256c992016-11-15 21:17:07 +01006062 FOR_ALL_WINDOWS(win)
6063 {
6064 winnr++;
6065 if (winnr == eap->line2)
6066 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006067 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006068 if (win == NULL)
6069 win = lastwin;
6070 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 }
6073}
6074
6075/*
6076 * ":stop" and ":suspend": Suspend Vim.
6077 */
6078 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006079ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080{
6081 /*
6082 * Disallow suspending for "rvim".
6083 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006084 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 {
6086 if (!eap->forceit)
6087 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01006088 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 windgoto((int)Rows - 1, 0);
6090 out_char('\n');
6091 out_flush();
6092 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006093 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006095 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006097 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098#ifdef FEAT_TITLE
6099 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006100 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101#endif
6102 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006103 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006105 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006106 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 }
6108}
6109
6110/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006111 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 */
6113 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006114ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006116#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006117 if (not_in_vim9(eap) == FAIL)
6118 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120#ifdef FEAT_CMDWIN
6121 if (cmdwin_type != 0)
6122 {
6123 cmdwin_result = Ctrl_C;
6124 return;
6125 }
6126#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006127 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006128 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006129 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006130 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006131 return;
6132 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006133
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006135 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 */
6137 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6138 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006139
6140 // Write the buffer for ":wq" or when it was changed.
6141 // Trigger QuitPre and ExitPre.
6142 // Check if we can exit now, after autocommands have changed things.
6143 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6144 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006146 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 {
6148 not_exiting();
6149 }
6150 else
6151 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006152 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006154 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155# ifdef FEAT_GUI
6156 need_mouse_correct = TRUE;
6157# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006158 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006159 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 }
6161}
6162
6163/*
6164 * ":print", ":list", ":number".
6165 */
6166 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006167ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006169 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006170 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006171 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006173 for ( ;!got_int; ui_breakcheck())
6174 {
6175 print_line(eap->line1,
6176 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6177 || (eap->flags & EXFLAG_NR)),
6178 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6179 if (++eap->line1 > eap->line2)
6180 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006181 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006182 }
6183 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006184 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006185 curwin->w_cursor.lnum = eap->line2;
6186 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 }
6188
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190}
6191
6192#ifdef FEAT_BYTEOFF
6193 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006194ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195{
6196 goto_byte(eap->line2);
6197}
6198#endif
6199
6200/*
6201 * ":shell".
6202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006204ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205{
6206 do_shell(NULL, 0);
6207}
6208
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006209#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006211static int drop_busy = FALSE;
6212static int drop_filec;
6213static char_u **drop_filev = NULL;
6214static int drop_split;
6215static void (*drop_callback)(void *);
6216static void *drop_cookie;
6217
6218 static void
6219handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220{
6221 exarg_T ea;
6222 int save_msg_scroll = msg_scroll;
6223
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006224 // Setting the argument list may cause screen updates and being called
6225 // recursively. Avoid that by setting drop_busy.
6226 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006228 // Check whether the current buffer is changed. If so, we will need
6229 // to split the current window or data could be lost.
6230 // We don't need to check if the 'hidden' option is set, as in this
6231 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006232 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 {
6234 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006235 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 --emsg_off;
6237 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006238 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 if (win_split(0, 0) == FAIL)
6241 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006242 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006244 // When splitting the window, create a new alist. Otherwise the
6245 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 alist_unlink(curwin->w_alist);
6247 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 }
6249
6250 /*
6251 * Set up the new argument list.
6252 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006253 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254
6255 /*
6256 * Move to the first file.
6257 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006258 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006259 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 ea.cmd = (char_u *)"next";
6261 do_argfile(&ea, 0);
6262
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006263 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6264 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 need_start_insertmode = FALSE;
6266
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006267 // Restore msg_scroll, otherwise a following command may cause scrolling
6268 // unexpectedly. The screen will be redrawn by the caller, thus
6269 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006271
6272 if (drop_callback != NULL)
6273 drop_callback(drop_cookie);
6274
6275 drop_filev = NULL;
6276 drop_busy = FALSE;
6277}
6278
6279/*
6280 * Handle a file drop. The code is here because a drop is *nearly* like an
6281 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006282 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006283 * code is very similar to :args and hence needs access to a lot of the static
6284 * functions in this file.
6285 *
6286 * The "filev" list must have been allocated using alloc(), as should each item
6287 * in the list. This function takes over responsibility for freeing the "filev"
6288 * list.
6289 */
6290 void
6291handle_drop(
6292 int filec, // the number of files dropped
6293 char_u **filev, // the list of files dropped
6294 int split, // force splitting the window
6295 void (*callback)(void *), // to be called after setting the argument
6296 // list
6297 void *cookie) // argument for "callback" (allocated)
6298{
6299 // Cannot handle recursive drops, finish the pending one.
6300 if (drop_busy)
6301 {
6302 FreeWild(filec, filev);
6303 vim_free(cookie);
6304 return;
6305 }
6306
6307 // When calling handle_drop() more than once in a row we only use the last
6308 // one.
6309 if (drop_filev != NULL)
6310 {
6311 FreeWild(drop_filec, drop_filev);
6312 vim_free(drop_cookie);
6313 }
6314
6315 drop_filec = filec;
6316 drop_filev = filev;
6317 drop_split = split;
6318 drop_callback = callback;
6319 drop_cookie = cookie;
6320
6321 // Postpone this when:
6322 // - editing the command line
6323 // - not possible to change the current buffer
6324 // - updating the screen
6325 // As it may change buffers and window structures that are in use and cause
6326 // freed memory to be used.
6327 if (text_locked() || curbuf_locked() || updating_screen)
6328 return;
6329
6330 handle_drop_internal();
6331}
6332
6333/*
6334 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6335 * reset: Handle a postponed drop.
6336 */
6337 void
6338handle_any_postponed_drop(void)
6339{
6340 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006341 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006342 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343}
6344#endif
6345
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 * ":preserve".
6348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006350ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006352 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 ml_preserve(curbuf, TRUE);
6354}
6355
6356/*
6357 * ":recover".
6358 */
6359 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006360ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006362 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006364 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6365 | CCGD_MULTWIN
6366 | (eap->forceit ? CCGD_FORCEIT : 0)
6367 | CCGD_EXCMD)
6368
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 && (*eap->arg == NUL
6370 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006371 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 recoverymode = FALSE;
6373}
6374
6375/*
6376 * Command modifier used in a wrong way.
6377 */
6378 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006379ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006381 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382}
6383
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384/*
6385 * :sview [+command] file split window with new file, read-only
6386 * :split [[+command] file] split window with current or new file
6387 * :vsplit [[+command] file] split window vertically with current or new file
6388 * :new [[+command] file] split window with no or new file
6389 * :vnew [[+command] file] split vertically window with no or new file
6390 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006391 *
6392 * :tabedit open new Tab page with empty window
6393 * :tabedit [+command] file open new Tab page and edit "file"
6394 * :tabnew [[+command] file] just like :tabedit
6395 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 */
6397 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006398ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006400 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006401#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006403#endif
6404#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006405 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006406 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006407#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006408 int use_tab = eap->cmdidx == CMD_tabedit
6409 || eap->cmdidx == CMD_tabfind
6410 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006412 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006413 return;
6414
Bram Moolenaar4033c552017-09-16 20:54:51 +02006415#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418
Bram Moolenaar4033c552017-09-16 20:54:51 +02006419#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006420 // A ":split" in the quickfix window works like ":new". Don't want two
6421 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006422 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 {
6424 if (eap->cmdidx == CMD_split)
6425 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 if (eap->cmdidx == CMD_vsplit)
6427 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430
Bram Moolenaar4033c552017-09-16 20:54:51 +02006431#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006432 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 {
6434 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6435 FNAME_MESS, TRUE, curbuf->b_ffname);
6436 if (fname == NULL)
6437 goto theend;
6438 eap->arg = fname;
6439 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006440# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006441 else
6442# endif
6443#endif
6444#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006445 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 && eap->cmdidx != CMD_new)
6448 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006449 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006450# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006451 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006452# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006453 au_has_group((char_u *)"FileExplorer"))
6454 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006455 // No browsing supported but we do have the file explorer:
6456 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006457 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006458 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006459 }
6460 else
6461 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006462 fname = do_browse(0, (char_u *)(use_tab
6463 ? _("Edit File in new tab page")
6464 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006466 if (fname == NULL)
6467 goto theend;
6468 eap->arg = fname;
6469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006471 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006474 /*
6475 * Either open new tab page or split the window.
6476 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006477 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006478 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006479 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006480 : eap->addr_count == 0 ? 0
6481 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006482 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006483 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006484
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006485 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006486 if (curwin != old_curwin
6487 && win_valid(old_curwin)
6488 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006489 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006490 old_curwin->w_alt_fnum = curbuf->b_fnum;
6491 }
6492 }
6493 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6495 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006496 // Reset 'scrollbind' when editing another file, but keep it when
6497 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006498 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006499 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 else
6501 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 do_exedit(eap, old_curwin);
6503 }
6504
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006505# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006506 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006507# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006509# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510theend:
6511 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006512# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006514
6515/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006516 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006517 */
6518 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006519tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006520{
6521 exarg_T ea;
6522
Bram Moolenaara80faa82020-04-12 19:37:17 +02006523 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006524 ea.cmdidx = CMD_tabnew;
6525 ea.cmd = (char_u *)"tabn";
6526 ea.arg = (char_u *)"";
6527 ex_splitview(&ea);
6528}
6529
6530/*
6531 * :tabnext command
6532 */
6533 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006534ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006535{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006536 int tab_number;
6537
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006538 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006539 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006540 switch (eap->cmdidx)
6541 {
6542 case CMD_tabfirst:
6543 case CMD_tabrewind:
6544 goto_tabpage(1);
6545 break;
6546 case CMD_tablast:
6547 goto_tabpage(9999);
6548 break;
6549 case CMD_tabprevious:
6550 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006551 if (eap->arg && *eap->arg != NUL)
6552 {
6553 char_u *p = eap->arg;
6554 char_u *p_save = p;
6555
6556 tab_number = getdigits(&p);
6557 if (p == p_save || *p_save == '-' || *p != NUL
6558 || tab_number == 0)
6559 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006560 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006561 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006562 return;
6563 }
6564 }
6565 else
6566 {
6567 if (eap->addr_count == 0)
6568 tab_number = 1;
6569 else
6570 {
6571 tab_number = eap->line2;
6572 if (tab_number < 1)
6573 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006574 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006575 return;
6576 }
6577 }
6578 }
6579 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006580 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006581 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006582 tab_number = get_tabpage_arg(eap);
6583 if (eap->errmsg == NULL)
6584 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006585 break;
6586 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006587}
6588
6589/*
6590 * :tabmove command
6591 */
6592 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006593ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006594{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006595 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006596
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006597 tab_number = get_tabpage_arg(eap);
6598 if (eap->errmsg == NULL)
6599 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006600}
6601
6602/*
6603 * :tabs command: List tabs and their contents.
6604 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006605 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006606ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006607{
6608 tabpage_T *tp;
6609 win_T *wp;
6610 int tabcount = 1;
6611
6612 msg_start();
6613 msg_scroll = TRUE;
6614 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6615 {
6616 msg_putchar('\n');
6617 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006618 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006619 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006620 ui_breakcheck();
6621
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006622 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006623 wp = firstwin;
6624 else
6625 wp = tp->tp_firstwin;
6626 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6627 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006628 msg_putchar('\n');
6629 msg_putchar(wp == curwin ? '>' : ' ');
6630 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006631 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6632 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006633 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006634 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006635 else
6636 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6637 IObuff, IOSIZE, TRUE);
6638 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006639 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006640 ui_breakcheck();
6641 }
6642 }
6643}
6644
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645/*
6646 * ":mode": Set screen mode.
6647 * If no argument given, just get the screen size and redraw.
6648 */
6649 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006650ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651{
6652 if (*eap->arg == NUL)
6653 shell_resized();
6654 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006655 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656}
6657
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658/*
6659 * ":resize".
6660 * set, increment or decrement current window height
6661 */
6662 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006663ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664{
6665 int n;
6666 win_T *wp = curwin;
6667
6668 if (eap->addr_count > 0)
6669 {
6670 n = eap->line2;
6671 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6672 ;
6673 }
6674
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006675# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006677# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006679 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 {
6681 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006682 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006683 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 n = 9999;
6685 win_setwidth_win((int)n, wp);
6686 }
6687 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 {
6689 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006690 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006691 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 n = 9999;
6693 win_setheight_win((int)n, wp);
6694 }
6695}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696
6697/*
6698 * ":find [+command] <file>" command.
6699 */
6700 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006701ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702{
6703#ifdef FEAT_SEARCHPATH
6704 char_u *fname;
6705 int count;
6706
6707 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6708 TRUE, curbuf->b_ffname);
6709 if (eap->addr_count > 0)
6710 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006711 // Repeat finding the file "count" times. This matters when it
6712 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 count = eap->line2;
6714 while (fname != NULL && --count > 0)
6715 {
6716 vim_free(fname);
6717 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6718 FALSE, curbuf->b_ffname);
6719 }
6720 }
6721
6722 if (fname != NULL)
6723 {
6724 eap->arg = fname;
6725#endif
6726 do_exedit(eap, NULL);
6727#ifdef FEAT_SEARCHPATH
6728 vim_free(fname);
6729 }
6730#endif
6731}
6732
6733/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006734 * ":open" simulation: for now just work like ":visual".
6735 */
6736 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006737ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006738{
6739 regmatch_T regmatch;
6740 char_u *p;
6741
Bram Moolenaar65088802021-03-13 21:07:21 +01006742#ifdef FEAT_EVAL
6743 if (not_in_vim9(eap) == FAIL)
6744 return;
6745#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006746 curwin->w_cursor.lnum = eap->line2;
6747 beginline(BL_SOL | BL_FIX);
6748 if (*eap->arg == '/')
6749 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006750 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006751 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006752 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00006753 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006754 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006755 if (regmatch.regprog != NULL)
6756 {
6757 regmatch.rm_ic = p_ic;
6758 p = ml_get_curline();
6759 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006760 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006761 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006762 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006763 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006764 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006765 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006766 eap->arg += STRLEN(eap->arg);
6767 }
6768 check_cursor();
6769
6770 eap->cmdidx = CMD_visual;
6771 do_exedit(eap, NULL);
6772}
6773
6774/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006775 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 */
6777 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006778ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779{
6780 do_exedit(eap, NULL);
6781}
6782
6783/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006784 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006787do_exedit(
6788 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006789 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790{
6791 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006793 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006795 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6796 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006797 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 /*
6799 * ":vi" command ends Ex mode.
6800 */
6801 if (exmode_active && (eap->cmdidx == CMD_visual
6802 || eap->cmdidx == CMD_view))
6803 {
6804 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006805 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006807 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006808 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006809 if (global_busy)
6810 {
6811 int rd = RedrawingDisabled;
6812 int nwr = no_wait_return;
6813 int ms = msg_scroll;
6814#ifdef FEAT_GUI
6815 int he = hold_gui_events;
6816#endif
6817
6818 if (eap->nextcmd != NULL)
6819 {
6820 stuffReadbuff(eap->nextcmd);
6821 eap->nextcmd = NULL;
6822 }
6823
6824 if (exmode_was != EXMODE_VIM)
6825 settmode(TMODE_RAW);
6826 RedrawingDisabled = 0;
6827 no_wait_return = 0;
6828 need_wait_return = FALSE;
6829 msg_scroll = 0;
6830#ifdef FEAT_GUI
6831 hold_gui_events = 0;
6832#endif
6833 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006834 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006835
6836 main_loop(FALSE, TRUE);
6837
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006838 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006839 RedrawingDisabled = rd;
6840 no_wait_return = nwr;
6841 msg_scroll = ms;
6842#ifdef FEAT_GUI
6843 hold_gui_events = he;
6844#endif
6845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 }
6849
6850 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006851 || eap->cmdidx == CMD_tabnew
6852 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006853 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006855 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 setpcmark();
6857 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006858 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6859 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006861 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 || *eap->arg != NUL
6863#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006864 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865#endif
6866 )
6867 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006868 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6869 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006870 if (*eap->arg != NUL && curbuf_locked())
6871 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006872
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 n = readonlymode;
6874 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6875 readonlymode = TRUE;
6876 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006877 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6878 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01006879 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
6880 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6882 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006883 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6885 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6886 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006887 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006889 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006890 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006891 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
6892 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006893 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006895 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 if (old_curwin != NULL)
6897 {
6898 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006899 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006901#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006902 cleanup_T cs;
6903
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006904 // Reset the error/interrupt/exception state here so that
6905 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006906 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006907#endif
6908#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006910#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006911 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006912
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006913#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006914 // Restore the error/interrupt/exception state if not
6915 // discarded by a new aborting error, interrupt, or
6916 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006917 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 }
6920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 }
6922 else if (readonlymode && curbuf->b_nwindows == 1)
6923 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006924 // When editing an already visited buffer, 'readonly' won't be set
6925 // but the previous value is kept. With ":view" and ":sview" we
6926 // want the file to be readonly, except when another window is
6927 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 curbuf->b_p_ro = TRUE;
6929 }
6930 readonlymode = n;
6931 }
6932 else
6933 {
6934 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01006935 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936#ifdef FEAT_TITLE
6937 n = curwin->w_arg_idx_invalid;
6938#endif
6939 check_arg_idx(curwin);
6940#ifdef FEAT_TITLE
6941 if (n != curwin->w_arg_idx_invalid)
6942 maketitle();
6943#endif
6944 }
6945
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 /*
6947 * if ":split file" worked, set alternate file name in old window to new
6948 * file
6949 */
6950 if (old_curwin != NULL
6951 && *eap->arg != NUL
6952 && curwin != old_curwin
6953 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006954 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006955 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957
6958 ex_no_reprint = TRUE;
6959}
6960
6961#ifndef FEAT_GUI
6962/*
6963 * ":gui" and ":gvim" when there is no GUI.
6964 */
6965 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006966ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006968 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969}
6970#endif
6971
Bram Moolenaar4f974752019-02-17 17:44:42 +01006972#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006974ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975{
6976 gui_make_tearoff(eap->arg);
6977}
6978#endif
6979
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006980#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6981 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006983ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006985# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6986 if (gui.in_use)
6987 gui_make_popup(eap->arg, eap->forceit);
6988# ifdef FEAT_TERM_POPUP_MENU
6989 else
6990# endif
6991# endif
6992# ifdef FEAT_TERM_POPUP_MENU
6993 pum_make_popup(eap->arg, eap->forceit);
6994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995}
6996#endif
6997
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006999ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000{
7001 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007002 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007004 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005}
7006
7007/*
7008 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7009 * offset.
7010 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007013ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007016 win_T *save_curwin = curwin;
7017 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 long topline;
7019 long y;
7020 linenr_T old_linenr = curwin->w_cursor.lnum;
7021
7022 setpcmark();
7023
7024 /*
7025 * determine max topline
7026 */
7027 if (curwin->w_p_scb)
7028 {
7029 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007030 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 {
7032 if (wp->w_p_scb && wp->w_buffer)
7033 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007034 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 if (topline > y)
7036 topline = y;
7037 }
7038 }
7039 if (topline < 1)
7040 topline = 1;
7041 }
7042 else
7043 {
7044 topline = 1;
7045 }
7046
7047
7048 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007049 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007051 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 {
7053 if (curwin->w_p_scb)
7054 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007055 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 y = topline - curwin->w_topline;
7057 if (y > 0)
7058 scrollup(y, TRUE);
7059 else
7060 scrolldown(-y, TRUE);
7061 curwin->w_scbind_pos = topline;
7062 redraw_later(VALID);
7063 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 }
7066 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007067 curwin = save_curwin;
7068 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 if (curwin->w_p_scb)
7070 {
7071 did_syncbind = TRUE;
7072 checkpcmark();
7073 if (old_linenr != curwin->w_cursor.lnum)
7074 {
7075 char_u ctrl_o[2];
7076
7077 ctrl_o[0] = Ctrl_O;
7078 ctrl_o[1] = 0;
7079 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7080 }
7081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082}
7083
7084
7085 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007086ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007088 int i;
7089 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7090 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007092 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 do_bang(1, eap, FALSE, FALSE, TRUE);
7094 else
7095 {
7096 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7097 return;
7098
7099#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007100 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 {
7102 char_u *browseFile;
7103
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007104 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 NULL, NULL, NULL, curbuf);
7106 if (browseFile != NULL)
7107 {
7108 i = readfile(browseFile, NULL,
7109 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7110 vim_free(browseFile);
7111 }
7112 else
7113 i = OK;
7114 }
7115 else
7116#endif
7117 if (*eap->arg == NUL)
7118 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007119 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 return;
7121 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7122 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7123 }
7124 else
7125 {
7126 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7127 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7128 i = readfile(eap->arg, NULL,
7129 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7130
7131 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007132 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007134#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 if (!aborting())
7136#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007137 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 }
7139 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007140 {
7141 if (empty && exmode_active)
7142 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007143 // Delete the empty line that remains. Historically ex does
7144 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007145 if (eap->line2 == 0)
7146 lnum = curbuf->b_ml.ml_line_count;
7147 else
7148 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007149 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007150 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007151 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007152 if (curwin->w_cursor.lnum > 1
7153 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007154 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007155 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007156 }
7157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 }
7161}
7162
Bram Moolenaarea408852005-06-25 22:49:46 +00007163static char_u *prev_dir = NULL;
7164
7165#if defined(EXITFREE) || defined(PROTO)
7166 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007167free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007168{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007169 VIM_CLEAR(prev_dir);
7170 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007171}
7172#endif
7173
Bram Moolenaarf4258302013-06-02 18:20:17 +02007174/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007175 * Get the previous directory for the given chdir scope.
7176 */
7177 static char_u *
7178get_prevdir(cdscope_T scope)
7179{
7180 if (scope == CDSCOPE_WINDOW)
7181 return curwin->w_prevdir;
7182 else if (scope == CDSCOPE_TABPAGE)
7183 return curtab->tp_prevdir;
7184 return prev_dir;
7185}
7186
7187/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007188 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007189 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7190 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007191 */
7192 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007193post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007194{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007195 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007196 // Clear tab local directory for both :cd and :tcd
7197 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007198 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007199 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007200 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007201 char_u *pdir = get_prevdir(scope);
7202
7203 // If still in the global directory, need to remember current
7204 // directory as the global directory.
7205 if (globaldir == NULL && pdir != NULL)
7206 globaldir = vim_strsave(pdir);
7207
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007208 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007209 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007210 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007211 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007212 curtab->tp_localdir = vim_strsave(NameBuff);
7213 else
7214 curwin->w_localdir = vim_strsave(NameBuff);
7215 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007216 }
7217 else
7218 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007219 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007220 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007221 }
7222
7223 shorten_fnames(TRUE);
7224}
7225
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007226/*
7227 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007228 * chdir() function.
7229 * scope == CDSCOPE_WINDOW: changes the window-local directory
7230 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7231 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007232 * Returns TRUE if the directory is successfully changed.
7233 */
7234 int
7235changedir_func(
7236 char_u *new_dir,
7237 int forceit,
7238 cdscope_T scope)
7239{
7240 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02007241 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007242 int dir_differs;
7243 int retval = FALSE;
7244
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007245 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007246 return FALSE;
7247
7248 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7249 {
7250 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7251 return FALSE;
7252 }
7253
7254 // ":cd -": Change to previous directory
7255 if (STRCMP(new_dir, "-") == 0)
7256 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007257 pdir = get_prevdir(scope);
7258 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007259 {
7260 emsg(_("E186: No previous directory"));
7261 return FALSE;
7262 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007263 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007264 }
7265
Bram Moolenaar002bc792020-06-05 22:33:42 +02007266 // Free the previous directory
7267 tofree = get_prevdir(scope);
7268
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007269 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007270 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007271 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007272 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007273 pdir = NULL;
7274 if (scope == CDSCOPE_WINDOW)
7275 curwin->w_prevdir = pdir;
7276 else if (scope == CDSCOPE_TABPAGE)
7277 curtab->tp_prevdir = pdir;
7278 else
7279 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007280
7281#if defined(UNIX) || defined(VMS)
7282 // for UNIX ":cd" means: go to home directory
7283 if (*new_dir == NUL)
7284 {
7285 // use NameBuff for home directory name
7286# ifdef VMS
7287 char_u *p;
7288
7289 p = mch_getenv((char_u *)"SYS$LOGIN");
7290 if (p == NULL || *p == NUL) // empty is the same as not set
7291 NameBuff[0] = NUL;
7292 else
7293 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7294# else
7295 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7296# endif
7297 new_dir = NameBuff;
7298 }
7299#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02007300 dir_differs = new_dir == NULL || pdir == NULL
7301 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007302 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7303 emsg(_(e_failed));
7304 else
7305 {
7306 char_u *acmd_fname;
7307
7308 post_chdir(scope);
7309
7310 if (dir_differs)
7311 {
7312 if (scope == CDSCOPE_WINDOW)
7313 acmd_fname = (char_u *)"window";
7314 else if (scope == CDSCOPE_TABPAGE)
7315 acmd_fname = (char_u *)"tabpage";
7316 else
7317 acmd_fname = (char_u *)"global";
7318 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7319 curbuf);
7320 }
7321 retval = TRUE;
7322 }
7323 vim_free(tofree);
7324
7325 return retval;
7326}
Bram Moolenaarea408852005-06-25 22:49:46 +00007327
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007329 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007331 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007332ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007334 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335
7336 new_dir = eap->arg;
7337#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007338 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 if (*new_dir == NUL)
7340 ex_pwd(NULL);
7341 else
7342#endif
7343 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007344 cdscope_T scope = CDSCOPE_GLOBAL;
7345
7346 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7347 scope = CDSCOPE_WINDOW;
7348 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7349 scope = CDSCOPE_TABPAGE;
7350
7351 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007352 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007353 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007354 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 ex_pwd(eap);
7356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 }
7358}
7359
7360/*
7361 * ":pwd".
7362 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007364ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365{
7366 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7367 {
7368#ifdef BACKSLASH_IN_FILENAME
7369 slash_adjust(NameBuff);
7370#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007371 if (p_verbose > 0)
7372 {
7373 char *context = "global";
7374
7375 if (curwin->w_localdir != NULL)
7376 context = "window";
7377 else if (curtab->tp_localdir != NULL)
7378 context = "tabpage";
7379 smsg("[%s] %s", context, (char *)NameBuff);
7380 }
7381 else
7382 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 }
7384 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007385 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386}
7387
7388/*
7389 * ":=".
7390 */
7391 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007392ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007394 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007395 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396}
7397
7398 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007399ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007401 int n;
7402 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403
7404 if (cursor_valid())
7405 {
7406 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7407 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007408 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007410
7411 len = eap->line2;
7412 switch (*eap->arg)
7413 {
7414 case 'm': break;
7415 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007416 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007417 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007418
7419 // Hide the cursor if invoked with !
7420 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421}
7422
7423/*
7424 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007425 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 */
7427 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007428do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429{
Bram Moolenaar52953192019-08-16 10:27:13 +02007430 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007431 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007432# ifdef ELAPSED_FUNC
7433 elapsed_T start_tv;
7434
7435 // Remember at what time we started, so that we know how much longer we
7436 // should wait after waiting for a bit.
7437 ELAPSED_INIT(start_tv);
7438# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007440 if (hide_cursor)
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007441 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007442 else
7443 cursor_on();
7444
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007445 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007446 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007448 wait_now = msec - done > 1000L ? 1000L : msec - done;
7449#ifdef FEAT_TIMERS
7450 {
7451 long due_time = check_due_timer();
7452
7453 if (due_time > 0 && due_time < wait_now)
7454 wait_now = due_time;
7455 }
7456#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007457#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007458 if (has_any_channel() && wait_now > 20L)
7459 wait_now = 20L;
7460#endif
7461#ifdef FEAT_SOUND
7462 if (has_any_sound_callback() && wait_now > 20L)
7463 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007464#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007465 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007466
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007467#ifdef FEAT_JOB_CHANNEL
7468 if (has_any_channel())
7469 ui_breakcheck_force(TRUE);
7470 else
7471#endif
7472 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007473#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007474 // Process the netbeans and clientserver messages that may have been
7475 // received in the call to ui_breakcheck() when the GUI is in use. This
7476 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007477 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007478#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007479
7480# ifdef ELAPSED_FUNC
7481 // actual time passed
7482 done = ELAPSED_FUNC(start_tv);
7483# else
7484 // guestimate time passed (will actually be more)
7485 done += wait_now;
7486# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007488
7489 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7490 // input buffer, otherwise a following call to input() fails.
7491 if (got_int)
7492 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007493
7494 if (hide_cursor)
7495 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496}
7497
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498/*
7499 * ":winsize" command (obsolete).
7500 */
7501 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007502ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503{
7504 int w, h;
7505 char_u *arg = eap->arg;
7506 char_u *p;
7507
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007508 if (!isdigit(*arg))
7509 {
7510 semsg(_(e_invarg2), arg);
7511 return;
7512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 w = getdigits(&arg);
7514 arg = skipwhite(arg);
7515 p = arg;
7516 h = getdigits(&arg);
7517 if (*p != NUL && *arg == NUL)
7518 set_shellsize(w, h, TRUE);
7519 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007520 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521}
7522
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007524ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525{
7526 int xchar = NUL;
7527 char_u *p;
7528
7529 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7530 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007531 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 if (eap->arg[1] == NUL)
7533 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007534 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 return;
7536 }
7537 xchar = eap->arg[1];
7538 p = eap->arg + 2;
7539 }
7540 else
7541 p = eap->arg + 1;
7542
7543 eap->nextcmd = check_nextcmd(p);
7544 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007545 if (*p != NUL && *p != (
7546#ifdef FEAT_EVAL
7547 in_vim9script() ? '#' :
7548#endif
7549 '"')
7550 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007551 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007552 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007554 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007555 postponed_split_flags = cmdmod.cmod_split;
7556 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7558 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007559 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 }
7561}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562
Bram Moolenaar843ee412004-06-30 16:16:41 +00007563#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564/*
7565 * ":winpos".
7566 */
7567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007568ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569{
7570 int x, y;
7571 char_u *arg = eap->arg;
7572 char_u *p;
7573
7574 if (*arg == NUL)
7575 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007576# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007577# ifdef VIMDLL
7578 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7579 mch_get_winpos(&x, &y) != FAIL)
7580# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007582# else
7583 if (mch_get_winpos(&x, &y) != FAIL)
7584# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 {
7586 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007587 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 }
7589 else
7590# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007591 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 }
7593 else
7594 {
7595 x = getdigits(&arg);
7596 arg = skipwhite(arg);
7597 p = arg;
7598 y = getdigits(&arg);
7599 if (*p == NUL || *arg != NUL)
7600 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007601 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 return;
7603 }
7604# ifdef FEAT_GUI
7605 if (gui.in_use)
7606 gui_mch_set_winpos(x, y);
7607 else if (gui.starting)
7608 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007609 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 gui_win_x = x;
7611 gui_win_y = y;
7612 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007613# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 else
7615# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007616# endif
7617# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007618 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619# endif
7620# ifdef HAVE_TGETENT
7621 if (*T_CWP)
7622 term_set_winpos(x, y);
7623# endif
7624 }
7625}
7626#endif
7627
7628/*
7629 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7630 */
7631 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007632ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633{
7634 oparg_T oa;
7635
7636 clear_oparg(&oa);
7637 oa.regname = eap->regname;
7638 oa.start.lnum = eap->line1;
7639 oa.end.lnum = eap->line2;
7640 oa.line_count = eap->line2 - eap->line1 + 1;
7641 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007643 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 {
7645 setpcmark();
7646 curwin->w_cursor.lnum = eap->line1;
7647 beginline(BL_SOL | BL_FIX);
7648 }
7649
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007650 if (VIsual_active)
7651 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007652
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 switch (eap->cmdidx)
7654 {
7655 case CMD_delete:
7656 oa.op_type = OP_DELETE;
7657 op_delete(&oa);
7658 break;
7659
7660 case CMD_yank:
7661 oa.op_type = OP_YANK;
7662 (void)op_yank(&oa, FALSE, TRUE);
7663 break;
7664
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007665 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007666 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007668 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7669#else
7670 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007672 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 oa.op_type = OP_RSHIFT;
7674 else
7675 oa.op_type = OP_LSHIFT;
7676 op_shift(&oa, FALSE, eap->amount);
7677 break;
7678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007680 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681}
7682
7683/*
7684 * ":put".
7685 */
7686 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007687ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007689 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 if (eap->line2 == 0)
7691 {
7692 eap->line2 = 1;
7693 eap->forceit = TRUE;
7694 }
7695 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007696 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007697 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698}
7699
7700/*
7701 * Handle ":copy" and ":move".
7702 */
7703 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007704ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705{
7706 long n;
7707
Bram Moolenaar491799b2020-08-01 19:23:43 +02007708#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007709 if (not_in_vim9(eap) == FAIL)
7710 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007711#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007712 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007713 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 {
7715 eap->nextcmd = NULL;
7716 return;
7717 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007718 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719
7720 /*
7721 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7722 */
7723 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7724 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007725 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 return;
7727 }
7728
7729 if (eap->cmdidx == CMD_move)
7730 {
7731 if (do_move(eap->line1, eap->line2, n) == FAIL)
7732 return;
7733 }
7734 else
7735 ex_copy(eap->line1, eap->line2, n);
7736 u_clearline();
7737 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007738 ex_may_print(eap);
7739}
7740
7741/*
7742 * Print the current line if flags were given to the Ex command.
7743 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007744 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007745ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007746{
7747 if (eap->flags != 0)
7748 {
7749 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7750 (eap->flags & EXFLAG_LIST));
7751 ex_no_reprint = TRUE;
7752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753}
7754
7755/*
7756 * ":smagic" and ":snomagic".
7757 */
7758 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007759ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007761 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007763 magic_overruled = eap->cmdidx == CMD_smagic
7764 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02007765 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007766 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767}
7768
7769/*
7770 * ":join".
7771 */
7772 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007773ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774{
7775 curwin->w_cursor.lnum = eap->line1;
7776 if (eap->line1 == eap->line2)
7777 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007778 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 return;
7780 if (eap->line2 == curbuf->b_ml.ml_line_count)
7781 {
7782 beep_flush();
7783 return;
7784 }
7785 ++eap->line2;
7786 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007787 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007789 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790}
7791
7792/*
7793 * ":[addr]@r" or ":[addr]*r": execute register
7794 */
7795 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007796ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797{
7798 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007799 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800
7801 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007802 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803
7804#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007805 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806#endif
7807
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007808 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 c = *eap->arg;
7810 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7811 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007812 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007813 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7814 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007815 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 else
7819 {
7820 int save_efr = exec_from_reg;
7821
7822 exec_from_reg = TRUE;
7823
7824 /*
7825 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007826 * Continue until the stuff buffer is empty and all added characters
7827 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007829 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7831
7832 exec_from_reg = save_efr;
7833 }
7834}
7835
7836/*
7837 * ":!".
7838 */
7839 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007840ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841{
7842 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7843}
7844
7845/*
7846 * ":undo".
7847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007849ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007851 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007852 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007853 else
7854 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855}
7856
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007857#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007858 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007859ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007860{
7861 char_u hash[UNDO_HASH_SIZE];
7862
7863 u_compute_hash(hash);
7864 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7865}
7866
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007867 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007868ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007869{
7870 char_u hash[UNDO_HASH_SIZE];
7871
7872 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007873 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007874}
7875#endif
7876
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877/*
7878 * ":redo".
7879 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007881ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882{
7883 u_redo(1);
7884}
7885
7886/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007887 * ":earlier" and ":later".
7888 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007889 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007890ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007891{
7892 long count = 0;
7893 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007894 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007895 char_u *p = eap->arg;
7896
7897 if (*p == NUL)
7898 count = 1;
7899 else if (isdigit(*p))
7900 {
7901 count = getdigits(&p);
7902 switch (*p)
7903 {
7904 case 's': ++p; sec = TRUE; break;
7905 case 'm': ++p; sec = TRUE; count *= 60; break;
7906 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007907 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7908 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007909 }
7910 }
7911
7912 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007913 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007914 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007915 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7916 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007917}
7918
7919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 * ":redir": start/stop redirection.
7921 */
7922 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007923ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924{
7925 char *mode;
7926 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007927 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928
Bram Moolenaarba768492016-07-08 15:32:54 +02007929#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007930 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007931 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007932 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007933 return;
7934 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007935#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007936
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 if (STRICMP(eap->arg, "END") == 0)
7938 close_redir();
7939 else
7940 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007941 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007943 ++arg;
7944 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007946 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 mode = "a";
7948 }
7949 else
7950 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007951 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952
7953 close_redir();
7954
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007955 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007956 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 if (fname == NULL)
7958 return;
7959#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007960 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {
7962 char_u *browseFile;
7963
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007964 browseFile = do_browse(BROWSE_SAVE,
7965 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007966 fname, NULL, NULL,
7967 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007969 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 vim_free(fname);
7971 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007972 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 }
7974#endif
7975
7976 redir_fd = open_exfile(fname, eap->forceit, mode);
7977 vim_free(fname);
7978 }
7979#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007980 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007982 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007984 ++arg;
7985 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007987 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007988 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007990 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007992 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007993 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007994 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007995 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007997 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007998 if (*arg == '>')
7999 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008000 // Make register empty when not using @A-@Z and the
8001 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00008002 if (*arg == NUL && !isupper(redir_reg))
8003 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008005 }
8006 if (*arg != NUL)
8007 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008008 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008009 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008010 }
8011 }
8012 else if (*arg == '=' && arg[1] == '>')
8013 {
8014 int append;
8015
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008016 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008017 close_redir();
8018 arg += 2;
8019
8020 if (*arg == '>')
8021 {
8022 ++arg;
8023 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 }
8025 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008026 append = FALSE;
8027
8028 if (var_redir_start(skipwhite(arg), append) == OK)
8029 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 }
8031#endif
8032
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008033 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008036 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008038
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008039 // Make sure redirection is not off. Can happen for cmdline completion
8040 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008041 if (redir_fd != NULL
8042#ifdef FEAT_EVAL
8043 || redir_reg || redir_vname
8044#endif
8045 )
8046 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047}
8048
8049/*
8050 * ":redraw": force redraw
8051 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008052 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008053ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054{
8055 int r = RedrawingDisabled;
8056 int p = p_lz;
8057
8058 RedrawingDisabled = 0;
8059 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008060 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008062 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063#ifdef FEAT_TITLE
8064 if (need_maketitle)
8065 maketitle();
8066#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008067#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8068# ifdef VIMDLL
8069 if (!gui.in_use)
8070# endif
8071 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 RedrawingDisabled = r;
8074 p_lz = p;
8075
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008076 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 msg_didout = FALSE;
8078 msg_col = 0;
8079
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008080 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008081 need_wait_return = FALSE;
8082
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 out_flush();
8084}
8085
8086/*
8087 * ":redrawstatus": force redraw of status line(s)
8088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008090ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 int r = RedrawingDisabled;
8093 int p = p_lz;
8094
8095 RedrawingDisabled = 0;
8096 p_lz = FALSE;
8097 if (eap->forceit)
8098 status_redraw_all();
8099 else
8100 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008101 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 RedrawingDisabled = r;
8103 p_lz = p;
8104 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105}
8106
Bram Moolenaare12bab32019-01-08 22:02:56 +01008107/*
8108 * ":redrawtabline": force redraw of the tabline
8109 */
8110 static void
8111ex_redrawtabline(exarg_T *eap UNUSED)
8112{
8113 int r = RedrawingDisabled;
8114 int p = p_lz;
8115
8116 RedrawingDisabled = 0;
8117 p_lz = FALSE;
8118
8119 draw_tabline();
8120
8121 RedrawingDisabled = r;
8122 p_lz = p;
8123 out_flush();
8124}
8125
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008127close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128{
8129 if (redir_fd != NULL)
8130 {
8131 fclose(redir_fd);
8132 redir_fd = NULL;
8133 }
8134#ifdef FEAT_EVAL
8135 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008136 if (redir_vname)
8137 {
8138 var_redir_stop();
8139 redir_vname = 0;
8140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141#endif
8142}
8143
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008144#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008145 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008146vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008147{
8148 if (vim_mkdir(name, prot) != 0)
8149 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008150 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008151 return FAIL;
8152 }
8153 return OK;
8154}
8155#endif
8156
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157/*
8158 * Open a file for writing for an Ex command, with some checks.
8159 * Return file descriptor, or NULL on failure.
8160 */
8161 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008162open_exfile(
8163 char_u *fname,
8164 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008165 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166{
8167 FILE *fd;
8168
8169#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008170 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 if (mch_isdir(fname))
8172 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008173 semsg(_(e_src_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 return NULL;
8175 }
8176#endif
8177 if (!forceit && *mode != 'a' && vim_fexists(fname))
8178 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008179 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 return NULL;
8181 }
8182
8183 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008184 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185
8186 return fd;
8187}
8188
8189/*
8190 * ":mark" and ":k".
8191 */
8192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008193ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194{
8195 pos_T pos;
8196
Bram Moolenaar10b94212021-02-19 21:42:57 +01008197#ifdef FEAT_EVAL
8198 if (not_in_vim9(eap) == FAIL)
8199 return;
8200#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008201 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008202 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008203 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02008204 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 else
8206 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008207 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 curwin->w_cursor.lnum = eap->line2;
8209 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008210 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008211 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008212 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 }
8214}
8215
8216/*
8217 * Update w_topline, w_leftcol and the cursor position.
8218 */
8219 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008220update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008222 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 update_topline();
8224 if (!curwin->w_p_wrap)
8225 validate_cursor();
8226 update_curswant();
8227}
8228
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008230 * Save the current State and go to Normal mode.
8231 * Return TRUE if the typeahead could be saved.
8232 */
8233 int
8234save_current_state(save_state_T *sst)
8235{
8236 sst->save_msg_scroll = msg_scroll;
8237 sst->save_restart_edit = restart_edit;
8238 sst->save_msg_didout = msg_didout;
8239 sst->save_State = State;
8240 sst->save_insertmode = p_im;
8241 sst->save_finish_op = finish_op;
8242 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008243 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008244
Bram Moolenaar4324d872020-12-01 20:12:24 +01008245 msg_scroll = FALSE; // no msg scrolling in Normal mode
8246 restart_edit = 0; // don't go to Insert mode
8247 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008248
Bram Moolenaara452b802020-12-01 21:47:59 +01008249 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008250 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008251
8252 /*
8253 * Save the current typeahead. This is required to allow using ":normal"
8254 * from an event handler and makes sure we don't hang when the argument
8255 * ends with half a command.
8256 */
8257 save_typeahead(&sst->tabuf);
8258 return sst->tabuf.typebuf_valid;
8259}
8260
8261 void
8262restore_current_state(save_state_T *sst)
8263{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008264 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008265 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008266
8267 msg_scroll = sst->save_msg_scroll;
8268 restart_edit = sst->save_restart_edit;
8269 p_im = sst->save_insertmode;
8270 finish_op = sst->save_finish_op;
8271 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008272 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008273 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008274 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008275
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008276 // Restore the state (needed when called from a function executed for
8277 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008278 State = sst->save_State;
8279#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008280 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008281#endif
8282}
8283
8284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 * ":normal[!] {commands}": Execute normal mode commands.
8286 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008287 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008288ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008290 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 char_u *arg = NULL;
8292 int l;
8293 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008295 if (ex_normal_lock > 0)
8296 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008297 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008298 return;
8299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 if (ex_normal_busy >= p_mmd)
8301 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008302 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 return;
8304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 /*
8307 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8308 * this for the K_SPECIAL leading byte, otherwise special keys will not
8309 * work.
8310 */
8311 if (has_mbyte)
8312 {
8313 int len = 0;
8314
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008315 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 for (p = eap->arg; *p != NUL; ++p)
8317 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008318#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008319 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008321#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008322 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008323 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008324#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 )
8328 len += 2;
8329 }
8330 if (len > 0)
8331 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008332 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 if (arg != NULL)
8334 {
8335 len = 0;
8336 for (p = eap->arg; *p != NUL; ++p)
8337 {
8338 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008339#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 if (*p == CSI)
8341 {
8342 arg[len++] = KS_EXTRA;
8343 arg[len++] = (int)KE_CSI;
8344 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008345#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008346 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 {
8348 arg[len++] = *++p;
8349 if (*p == K_SPECIAL)
8350 {
8351 arg[len++] = KS_SPECIAL;
8352 arg[len++] = KE_FILLER;
8353 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008354#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 else if (*p == CSI)
8356 {
8357 arg[len++] = KS_EXTRA;
8358 arg[len++] = (int)KE_CSI;
8359 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 }
8362 arg[len] = NUL;
8363 }
8364 }
8365 }
8366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008368 ++ex_normal_busy;
8369 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 {
8371 /*
8372 * Repeat the :normal command for each line in the range. When no
8373 * range given, execute it just once, without positioning the cursor
8374 * first.
8375 */
8376 do
8377 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 if (eap->addr_count != 0)
8379 {
8380 curwin->w_cursor.lnum = eap->line1++;
8381 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008382 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 }
8384
Bram Moolenaar13505972019-01-24 15:04:48 +01008385 exec_normal_cmd(arg != NULL
8386 ? arg
8387 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 }
8389 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8390 }
8391
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008392 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 update_topline_cursor();
8394
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008395 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008397 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008398#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008399 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008400#endif
8401
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403}
8404
8405/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008406 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 */
8408 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008409ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008411 if (eap->forceit)
8412 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008413 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008414 if (!curwin->w_cursor.lnum)
8415 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008416 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008417 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008418#ifdef FEAT_TERMINAL
8419 // Ignore this when running in an active terminal.
8420 if (term_job_running(curbuf->b_term))
8421 return;
8422#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008423
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008424 // Ignore the command when already in Insert mode. Inserting an
8425 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008426 if (State & INSERT)
8427 return;
8428
Bram Moolenaar64969662005-12-14 21:59:55 +00008429 if (eap->cmdidx == CMD_startinsert)
8430 restart_edit = 'a';
8431 else if (eap->cmdidx == CMD_startreplace)
8432 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008434 restart_edit = 'V';
8435
8436 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008438 if (eap->cmdidx == CMD_startinsert)
8439 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008440 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008442
8443 if (VIsual_active)
8444 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445}
8446
8447/*
8448 * ":stopinsert"
8449 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008451ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452{
8453 restart_edit = 0;
8454 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008455 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008458/*
8459 * Execute normal mode command "cmd".
8460 * "remap" can be REMAP_NONE or REMAP_YES.
8461 */
8462 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008463exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008464{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008465 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008466 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008467 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008468}
Bram Moolenaar25281632016-01-21 23:32:32 +01008469
Bram Moolenaar25281632016-01-21 23:32:32 +01008470/*
8471 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008472 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008473 */
8474 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008475exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008476{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008477 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008478 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008479
Bram Moolenaar905dd902019-04-07 14:21:47 +02008480 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8481 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008482 clear_oparg(&oa);
8483 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008484 while ((!stuff_empty()
8485 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008486 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008487 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008488 {
8489 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008490#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008491 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008492 && oa.op_type == OP_NOP && oa.regname == NUL
8493 && !VIsual_active)
8494 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008495 // If terminal_loop() returns OK we got a key that is handled
8496 // in Normal model. With FAIL we first need to position the
8497 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008498 if (terminal_loop(TRUE) == OK)
8499 normal_cmd(&oa, TRUE);
8500 }
8501 else
8502#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008503 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008504 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008505 }
8506}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008507
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508#ifdef FEAT_FIND_ID
8509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008510ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511{
8512 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8513 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8514 (linenr_T)1, (linenr_T)MAXLNUM);
8515}
8516
Bram Moolenaar4033c552017-09-16 20:54:51 +02008517#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518/*
8519 * ":psearch"
8520 */
8521 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008522ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523{
8524 g_do_tagpreview = p_pvh;
8525 ex_findpat(eap);
8526 g_do_tagpreview = 0;
8527}
8528#endif
8529
8530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008531ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532{
8533 int whole = TRUE;
8534 long n;
8535 char_u *p;
8536 int action;
8537
8538 switch (cmdnames[eap->cmdidx].cmd_name[2])
8539 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008540 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8542 action = ACTION_GOTO;
8543 else
8544 action = ACTION_SHOW;
8545 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008546 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 action = ACTION_SHOW_ALL;
8548 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008549 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 action = ACTION_GOTO;
8551 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008552 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 action = ACTION_SPLIT;
8554 break;
8555 }
8556
8557 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008558 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 {
8560 n = getdigits(&eap->arg);
8561 eap->arg = skipwhite(eap->arg);
8562 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008563 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 {
8565 whole = FALSE;
8566 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008567 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 if (*p)
8569 {
8570 *p++ = NUL;
8571 p = skipwhite(p);
8572
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008573 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008574 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008575 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 else
8577 eap->nextcmd = check_nextcmd(p);
8578 }
8579 }
8580 if (!eap->skip)
8581 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8582 whole, !eap->forceit,
8583 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8584 n, action, eap->line1, eap->line2);
8585}
8586#endif
8587
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588
Bram Moolenaar4033c552017-09-16 20:54:51 +02008589#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590/*
8591 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8592 */
8593 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008594ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008596 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8598}
8599
8600/*
8601 * ":pedit"
8602 */
8603 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008604ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605{
8606 win_T *curwin_save = curwin;
8607
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008608 if (ERROR_IF_ANY_POPUP_WINDOW)
8609 return;
8610
Bram Moolenaar026587b2019-08-17 15:08:00 +02008611 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008613 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008614
Bram Moolenaar026587b2019-08-17 15:08:00 +02008615 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008617
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 if (curwin != curwin_save && win_valid(curwin_save))
8619 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008620 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 validate_cursor();
8622 redraw_later(VALID);
8623 win_enter(curwin_save, TRUE);
8624 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008625# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008626 else if (WIN_IS_POPUP(curwin))
8627 {
8628 // can't keep focus in popup window
8629 win_enter(firstwin, TRUE);
8630 }
8631# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 g_do_tagpreview = 0;
8633}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635
8636/*
8637 * ":stag", ":stselect" and ":stjump".
8638 */
8639 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008640ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641{
8642 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008643 postponed_split_flags = cmdmod.cmod_split;
8644 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8646 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008647 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649
8650/*
8651 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8652 */
8653 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008654ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655{
8656 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8657}
8658
8659 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008660ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661{
8662 int cmd;
8663
8664 switch (name[1])
8665 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008666 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008668 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008670 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008672 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008674 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008676 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008678 case 'f': // ":tfirst"
8679 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008681 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008683 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008685 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008687 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 return;
8689 }
8690#endif
8691 cmd = DT_TAG;
8692 break;
8693 }
8694
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008695 if (name[0] == 'l')
8696 {
8697#ifndef FEAT_QUICKFIX
8698 ex_ni(eap);
8699 return;
8700#else
8701 cmd = DT_LTAG;
8702#endif
8703 }
8704
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8706 eap->forceit, TRUE);
8707}
8708
8709/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008710 * Check "str" for starting with a special cmdline variable.
8711 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8712 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8713 */
8714 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008715find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008716{
8717 int len;
8718 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008719 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008720 "%",
8721#define SPEC_PERC 0
8722 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008723#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008724 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008725#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008726 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008727#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008728 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008729#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008730 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008731#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008732 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008733#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008734 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008735#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008736 "<stack>", // call stack
8737#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008738 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008739#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008740 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008741#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008742 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008743#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008744 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008745#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008746 "<SID>", // script ID: <SNR>123_
8747#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008748#ifdef FEAT_CLIENTSERVER
8749 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008750# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008751#endif
8752 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008753
K.Takataeeec2542021-06-02 13:28:16 +02008754 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008755 {
8756 len = (int)STRLEN(spec_str[i]);
8757 if (STRNCMP(src, spec_str[i], len) == 0)
8758 {
8759 *usedlen = len;
8760 return i;
8761 }
8762 }
8763 return -1;
8764}
8765
8766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 * Evaluate cmdline variables.
8768 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008769 * change "%" to curbuf->b_ffname
8770 * "#" to curwin->w_alt_fnum
8771 * "%%" to curwin->w_alt_fnum in Vim9 script
8772 * "<cword>" to word under the cursor
8773 * "<cWORD>" to WORD under the cursor
8774 * "<cexpr>" to C-expression under the cursor
8775 * "<cfile>" to path name under the cursor
8776 * "<sfile>" to sourced file name
8777 * "<stack>" to call stack
8778 * "<slnum>" to sourced file line number
8779 * "<afile>" to file name for autocommand
8780 * "<abuf>" to buffer number for autocommand
8781 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 *
8783 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8784 * "" for error without a message) and NULL is returned.
8785 * Returns an allocated string if a valid match was found.
8786 * Returns NULL if no match was found. "usedlen" then still contains the
8787 * number of characters to skip.
8788 */
8789 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008790eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008791 char_u *src, // pointer into commandline
8792 char_u *srcstart, // beginning of valid memory for src
8793 int *usedlen, // characters after src that are used
8794 linenr_T *lnump, // line number for :e command, or NULL
8795 char **errormsg, // pointer to error message
8796 int *escaped) // return value has escaped white space (can
8797 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798{
8799 int i;
8800 char_u *s;
8801 char_u *result;
8802 char_u *resultbuf = NULL;
8803 int resultlen;
8804 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008805 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008807 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810
8811 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008812 if (escaped != NULL)
8813 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814
8815 /*
8816 * Check if there is something to do.
8817 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008818 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008819 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 {
8821 *usedlen = 1;
8822 return NULL;
8823 }
8824
8825 /*
8826 * Skip when preceded with a backslash "\%" and "\#".
8827 * Note: In "\\%" the % is also not recognized!
8828 */
8829 if (src > srcstart && src[-1] == '\\')
8830 {
8831 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008832 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 return NULL;
8834 }
8835
8836 /*
8837 * word or WORD under cursor
8838 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008839 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8840 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008842 resultlen = find_ident_under_cursor(&result,
8843 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8844 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8845 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 if (resultlen == 0)
8847 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008848 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 return NULL;
8850 }
8851 }
8852
8853 /*
8854 * '#': Alternate file name
8855 * '%': Current file name
8856 * File name under the cursor
8857 * File name for autocommand
8858 * and following modifiers
8859 */
8860 else
8861 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008862 int off = 0;
8863
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 switch (spec_idx)
8865 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008866 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008867#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008868 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008871 // '%': current file
8872 if (curbuf->b_fname == NULL)
8873 {
8874 result = (char_u *)"";
8875 valid = 0; // Must have ":p:h" to be valid
8876 }
8877 else
8878 {
8879 result = curbuf->b_fname;
8880 tilde_file = STRCMP(result, "~") == 0;
8881 }
8882 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008884#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008885 // "%%" alternate file
8886 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008887#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01008888 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008889 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008890 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008892 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 result = arg_all();
8894 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008895 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008896 if (escaped != NULL)
8897 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 break;
8900 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008901 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008902 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008903 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008905 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008906 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008907 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008908 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008910 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008912 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00008913 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008914 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008915 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008916 return NULL;
8917 }
8918#ifdef FEAT_EVAL
8919 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8920 (long)i);
8921 if (result == NULL)
8922 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008923 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008924 return NULL;
8925 }
8926#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008927 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 }
8931 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008932 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008933 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
8934 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008935 buf = buflist_findnr(i);
8936 if (buf == NULL)
8937 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008938 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008939 return NULL;
8940 }
8941 if (lnump != NULL)
8942 *lnump = ECMD_LAST;
8943 if (buf->b_fname == NULL)
8944 {
8945 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008946 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008947 }
8948 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008949 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008950 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008951 tilde_file = STRCMP(result, "~") == 0;
8952 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 break;
8955
8956#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008957 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008958 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 if (result == NULL)
8960 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008961 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 return NULL;
8963 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008964 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 break;
8966#endif
8967
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008968 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008970 if (result != NULL && !autocmd_fname_full)
8971 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008972 // Still need to turn the fname into a full path. It is
8973 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008974 autocmd_fname_full = TRUE;
8975 result = FullName_save(autocmd_fname, FALSE);
8976 vim_free(autocmd_fname);
8977 autocmd_fname = result;
8978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 if (result == NULL)
8980 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008981 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 return NULL;
8983 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008984 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 break;
8986
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008987 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 if (autocmd_bufnr <= 0)
8989 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008990 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 return NULL;
8992 }
8993 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8994 result = strbuf;
8995 break;
8996
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008997 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 result = autocmd_match;
8999 if (result == NULL)
9000 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009001 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 return NULL;
9003 }
9004 break;
9005
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009006 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02009007 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02009008 result = estack_sfile(spec_idx == SPEC_SFILE
9009 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 if (result == NULL)
9011 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02009012 *errormsg = spec_idx == SPEC_SFILE
9013 ? _("E498: no :source file name to substitute for \"<sfile>\"")
9014 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 return NULL;
9016 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009017 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009019
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009020 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009021 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009022 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009023 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009024 return NULL;
9025 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009026 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009027 result = strbuf;
9028 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009029
9030#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009031 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009032 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009033 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009034 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009035 return NULL;
9036 }
9037 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009038 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009039 result = strbuf;
9040 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009041
Bram Moolenaar90944302020-08-01 20:45:11 +02009042 case SPEC_SID:
9043 if (current_sctx.sc_sid <= 0)
9044 {
9045 *errormsg = _(e_usingsid);
9046 return NULL;
9047 }
9048 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9049 result = strbuf;
9050 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009051#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009052
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009053#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009054 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009055 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9056 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 result = strbuf;
9058 break;
9059#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009060
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009061 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009062 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009063 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 }
9065
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009066 resultlen = (int)STRLEN(result); // length of new string
9067 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 {
9069 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 resultlen = (int)(s - result);
9072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 else if (!skip_mod)
9074 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009075 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 &resultlen);
9077 if (result == NULL)
9078 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009079 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 return NULL;
9081 }
9082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 }
9084
9085 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9086 {
9087 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009088 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009089 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009091 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 result = NULL;
9093 }
9094 else
9095 result = vim_strnsave(result, resultlen);
9096 vim_free(resultbuf);
9097 return result;
9098}
9099
9100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 * Expand the <sfile> string in "arg".
9102 *
9103 * Returns an allocated string, or NULL for any error.
9104 */
9105 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009106expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009108 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 int len;
9110 char_u *result;
9111 char_u *newres;
9112 char_u *repl;
9113 int srclen;
9114 char_u *p;
9115
9116 result = vim_strsave(arg);
9117 if (result == NULL)
9118 return NULL;
9119
9120 for (p = result; *p; )
9121 {
9122 if (STRNCMP(p, "<sfile>", 7) != 0)
9123 ++p;
9124 else
9125 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009126 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00009127 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 if (errormsg != NULL)
9129 {
9130 if (*errormsg)
9131 emsg(errormsg);
9132 vim_free(result);
9133 return NULL;
9134 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009135 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 {
9137 p += srclen;
9138 continue;
9139 }
9140 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9141 newres = alloc(len);
9142 if (newres == NULL)
9143 {
9144 vim_free(repl);
9145 vim_free(result);
9146 return NULL;
9147 }
9148 mch_memmove(newres, result, (size_t)(p - result));
9149 STRCPY(newres + (p - result), repl);
9150 len = (int)STRLEN(newres);
9151 STRCAT(newres, p + srclen);
9152 vim_free(repl);
9153 vim_free(result);
9154 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009155 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 }
9157 }
9158
9159 return result;
9160}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009163/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009164 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009165 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009166 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009168dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 if (fname == NULL)
9171 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009172 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173}
9174#endif
9175
9176/*
9177 * ":behave {mswin,xterm}"
9178 */
9179 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009180ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181{
9182 if (STRCMP(eap->arg, "mswin") == 0)
9183 {
9184 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9185 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9186 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9187 set_option_value((char_u *)"keymodel", 0L,
9188 (char_u *)"startsel,stopsel", 0);
9189 }
9190 else if (STRCMP(eap->arg, "xterm") == 0)
9191 {
9192 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9193 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9194 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9195 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9196 }
9197 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009198 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199}
9200
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201static int filetype_detect = FALSE;
9202static int filetype_plugin = FALSE;
9203static int filetype_indent = FALSE;
9204
9205/*
9206 * ":filetype [plugin] [indent] {on,off,detect}"
9207 * on: Load the filetype.vim file to install autocommands for file types.
9208 * off: Load the ftoff.vim file to remove all autocommands for file types.
9209 * plugin on: load filetype.vim and ftplugin.vim
9210 * plugin off: load ftplugof.vim
9211 * indent on: load filetype.vim and indent.vim
9212 * indent off: load indoff.vim
9213 */
9214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009215ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216{
9217 char_u *arg = eap->arg;
9218 int plugin = FALSE;
9219 int indent = FALSE;
9220
9221 if (*eap->arg == NUL)
9222 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009223 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009224 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 filetype_detect ? "ON" : "OFF",
9226 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9227 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9228 return;
9229 }
9230
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009231 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 for (;;)
9233 {
9234 if (STRNCMP(arg, "plugin", 6) == 0)
9235 {
9236 plugin = TRUE;
9237 arg = skipwhite(arg + 6);
9238 continue;
9239 }
9240 if (STRNCMP(arg, "indent", 6) == 0)
9241 {
9242 indent = TRUE;
9243 arg = skipwhite(arg + 6);
9244 continue;
9245 }
9246 break;
9247 }
9248 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9249 {
9250 if (*arg == 'o' || !filetype_detect)
9251 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009252 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 filetype_detect = TRUE;
9254 if (plugin)
9255 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009256 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 filetype_plugin = TRUE;
9258 }
9259 if (indent)
9260 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009261 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 filetype_indent = TRUE;
9263 }
9264 }
9265 if (*arg == 'd')
9266 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009267 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009268 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 }
9270 }
9271 else if (STRCMP(arg, "off") == 0)
9272 {
9273 if (plugin || indent)
9274 {
9275 if (plugin)
9276 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009277 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 filetype_plugin = FALSE;
9279 }
9280 if (indent)
9281 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009282 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 filetype_indent = FALSE;
9284 }
9285 }
9286 else
9287 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009288 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 filetype_detect = FALSE;
9290 }
9291 }
9292 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009293 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294}
9295
9296/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009297 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 */
9299 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009300ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301{
9302 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009303 {
9304 char_u *arg = eap->arg;
9305
9306 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9307 arg += 9;
9308
9309 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9310 if (arg != eap->arg)
9311 did_filetype = FALSE;
9312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009316ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317{
9318#ifdef FEAT_DIGRAPHS
9319 if (*eap->arg != NUL)
9320 putdigraph(eap->arg);
9321 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009322 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323#else
mityu61065042021-07-19 20:07:21 +02009324 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325#endif
9326}
9327
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009328#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9329 void
9330set_no_hlsearch(int flag)
9331{
9332 no_hlsearch = flag;
9333# ifdef FEAT_EVAL
9334 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9335# endif
9336}
9337
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338/*
9339 * ":nohlsearch"
9340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009342ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009344 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009345 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347#endif
9348
9349#ifdef FEAT_CRYPT
9350/*
9351 * ":X": Get crypt key
9352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009354ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009356 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009357 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358}
9359#endif
9360
9361#ifdef FEAT_FOLDING
9362 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009363ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364{
9365 if (foldManualAllowed(TRUE))
9366 foldCreate(eap->line1, eap->line2);
9367}
9368
9369 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009370ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371{
9372 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9373 eap->forceit, FALSE);
9374}
9375
9376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009377ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378{
9379 linenr_T lnum;
9380
Bram Moolenaar4facea32019-10-12 20:17:40 +02009381# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009382 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009383# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009384
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009385 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9387 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9388 ml_setmarked(lnum);
9389
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009390 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009392 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009393# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009394 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009395# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396}
9397#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009398
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009399#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009400/*
9401 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9402 * instead of a quickfix command.
9403 */
9404 int
9405is_loclist_cmd(int cmdidx)
9406{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009407 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009408 return FALSE;
9409 return cmdnames[cmdidx].cmd_name[0] == 'l';
9410}
9411#endif
9412
Bram Moolenaar4facea32019-10-12 20:17:40 +02009413#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009414 int
9415get_pressedreturn(void)
9416{
9417 return ex_pressedreturn;
9418}
9419
9420 void
9421set_pressedreturn(int val)
9422{
9423 ex_pressedreturn = val;
9424}
9425#endif