blob: 06bff807612386c60a72ef96dcb840d95e4d5799 [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 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200601 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100602do_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 Moolenaar217e1b82019-12-01 21:41:28 +0100667 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
668 // location for storing error messages to be converted to an exception.
669 // This ensures that the do_errthrow() call in do_one_cmd() does not
670 // combine the messages stored by an earlier invocation of do_one_cmd()
671 // with the command name of the later one. This would happen when
672 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 saved_msg_list = msg_list;
674 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675#endif
676
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100677 // It's possible to create an endless loop with ":execute", catch that
678 // here. The value of 200 allows nested function calls, ":source", etc.
679 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100680 if (call_depth >= 200
681#ifdef FEAT_EVAL
682 && call_depth >= p_mfd
683#endif
684 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100686 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100688 // When converting to an exception, we do not include the command name
689 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100690 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 msg_list = saved_msg_list;
692#endif
693 return FAIL;
694 }
695 ++call_depth;
696
697#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100698 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 cstack.cs_idx = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
701
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100702 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100704 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100705 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000706 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 ++ex_nesting_level;
708
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100709 // Get the function or script name and the address where the next breakpoint
710 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000711 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {
713 fname = func_name(real_cookie);
714 breakpoint = func_breakpoint(real_cookie);
715 dbg_tick = func_dbg_tick(real_cookie);
716 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100717 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100719 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 breakpoint = source_breakpoint(real_cookie);
721 dbg_tick = source_dbg_tick(real_cookie);
722 }
723
724 /*
725 * Initialize "force_abort" and "suppress_errthrow" at the top level.
726 */
727 if (!recursive)
728 {
729 force_abort = FALSE;
730 suppress_errthrow = FALSE;
731 }
732
733 /*
734 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000735 * exception handling (used when debugging). Otherwise clear it to avoid
736 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000738 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000739 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000740 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200741 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742
743 initial_trylevel = trylevel;
744
745 /*
746 * "did_throw" will be set to TRUE when an exception is being thrown.
747 */
748 did_throw = FALSE;
749#endif
750 /*
751 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000752 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 * If force_abort is set, we cancel everything.
754 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100755#ifdef FEAT_EVAL
756 did_emsg_cumul += did_emsg;
757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 did_emsg = FALSE;
759
760 /*
761 * KeyTyped is only set when calling vgetc(). Reset it here when not
762 * calling vgetc() (sourced command lines).
763 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100764 if (!(flags & DOCMD_KEYTYPED)
765 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 KeyTyped = FALSE;
767
768 /*
769 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000770 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 * - for multiple commands on one line, separated with '|'
772 * - when repeating until there are no more lines (for ":source")
773 */
774 next_cmdline = cmdline;
775 do
776 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000777#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100778 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000779#endif
780
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100781 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 if (next_cmdline == NULL
783#ifdef FEAT_EVAL
784 && !force_abort
785 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000786 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787#endif
788 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100789 {
790#ifdef FEAT_EVAL
791 did_emsg_cumul += did_emsg;
792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
796 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000797 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100798 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 * 3. If a line is given: Make a copy, so we can mess with it.
800 */
801
802#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100803 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000804 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100806 // Each '|' separated command is stored separately in lines_ga, to
807 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100808 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100810 // Check if a function has returned or, unless it has an unclosed
811 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000814# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000815 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000816 func_line_end(real_cookie);
817# endif
818 if (func_has_ended(real_cookie))
819 {
820 retval = FAIL;
821 break;
822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000824#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000825 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100826 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000827 script_line_end();
828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100830 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100831 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 {
833 retval = FAIL;
834 break;
835 }
836
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100837 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 if (breakpoint != NULL && dbg_tick != NULL
839 && *dbg_tick != debug_tick)
840 {
841 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100842 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100843 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 *dbg_tick = debug_tick;
845 }
846
847 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100848 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100850 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100852 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100854 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100855 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100857 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100858 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 *dbg_tick = debug_tick;
860 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000861# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000862 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000863 {
864 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100865 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100866 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000867 script_line_start();
868 }
869# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871#endif
872
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100873 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (next_cmdline == NULL)
875 {
876 /*
877 * Need to set msg_didout for the first line after an ":if",
878 * otherwise the ":if" will be overwritten.
879 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100880 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100882 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883#ifdef FEAT_EVAL
884 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
885#else
886 0
887#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100888 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
889 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100891 // Don't call wait_return for aborted command line. The NULL
892 // returned for the end of a sourced file or executed function
893 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (KeyTyped && !(flags & DOCMD_REPEAT))
895 need_wait_return = FALSE;
896 retval = FAIL;
897 break;
898 }
899 used_getline = TRUE;
900
901 /*
902 * Keep the first typed line. Clear it when more lines are typed.
903 */
904 if (flags & DOCMD_KEEPLINE)
905 {
906 vim_free(repeat_cmdline);
907 if (count == 0)
908 repeat_cmdline = vim_strsave(next_cmdline);
909 else
910 repeat_cmdline = NULL;
911 }
912 }
913
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100914 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 else if (cmdline_copy == NULL)
916 {
917 next_cmdline = vim_strsave(next_cmdline);
918 if (next_cmdline == NULL)
919 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200920 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 retval = FAIL;
922 break;
923 }
924 }
925 cmdline_copy = next_cmdline;
926
927#ifdef FEAT_EVAL
928 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200929 * Inside a while/for loop, and when the command looks like a ":while"
930 * or ":for", the line is stored, because we may need it later when
931 * looping.
932 *
933 * When there is a '|' and another command, it is stored separately,
934 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000935 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200936 *
937 * Pass a different "fgetline" function to do_one_cmd() below,
938 * that it stores lines in or reads them from "lines_ga". Makes it
939 * possible to define a function inside a while/for loop and handles
940 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200942 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200944 cmd_getline = get_loop_line;
945 cmd_cookie = (void *)&cmd_loop_cookie;
946 cmd_loop_cookie.lines_gap = &lines_ga;
947 cmd_loop_cookie.current_line = current_line;
948 cmd_loop_cookie.getline = fgetline;
949 cmd_loop_cookie.cookie = cookie;
950 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
951
952 // Save the current line when encountering it the first time.
953 if (current_line == lines_ga.ga_len
954 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 {
956 retval = FAIL;
957 break;
958 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200959 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200961 else
962 {
963 cmd_getline = fgetline;
964 cmd_cookie = cookie;
965 }
966
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 did_endif = FALSE;
968#endif
969
970 if (count++ == 0)
971 {
972 /*
973 * All output from the commands is put below each other, without
974 * waiting for a return. Don't do this when executing commands
975 * from a script or when being called recursive (e.g. for ":e
976 * +command file").
977 */
978 if (!(flags & DOCMD_NOWAIT) && !recursive)
979 {
980 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100981 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100983 msg_scroll = TRUE; // put messages below each other
984 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 ++RedrawingDisabled;
986 did_inc = TRUE;
987 }
988 }
989
Bram Moolenaar823654b2020-05-29 23:03:09 +0200990 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100991 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992
993 /*
994 * 2. Execute one '|' separated command.
995 * do_one_cmd() will return NULL if there is no trailing '|'.
996 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
997 */
998 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100999 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000#ifdef FEAT_EVAL
1001 &cstack,
1002#endif
1003 cmd_getline, cmd_cookie);
1004 --recursive;
1005
1006#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001007 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001008 // Use "current_line" from "cmd_loop_cookie", it may have been
1009 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001010 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011#endif
1012
1013 if (next_cmdline == NULL)
1014 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001015 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001016
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 /*
1018 * If the command was typed, remember it for the ':' register.
1019 * Do this AFTER executing the command to make :@: work.
1020 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001021 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 && new_last_cmdline != NULL)
1023 {
1024 vim_free(last_cmdline);
1025 last_cmdline = new_last_cmdline;
1026 new_last_cmdline = NULL;
1027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 }
1029 else
1030 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001031 // need to copy the command after the '|' to cmdline_copy, for the
1032 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001033 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 next_cmdline = cmdline_copy;
1035 }
1036
1037
1038#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001039 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001041 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001043 {
1044 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001048 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 {
1050 ++current_line;
1051
1052 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001053 * An ":endwhile", ":endfor" and ":continue" is handled here.
1054 * If we were executing commands, jump back to the ":while" or
1055 * ":for".
1056 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001058 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001060 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001062 // Jump back to the matching ":while" or ":for". Be careful
1063 // not to use a cs_line[] from an entry that isn't a ":while"
1064 // or ":for": It would make "current_line" invalid and can
1065 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 if (!did_emsg && !got_int && !did_throw
1067 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 && (cstack.cs_flags[cstack.cs_idx]
1069 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 && cstack.cs_line[cstack.cs_idx] >= 0
1071 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1072 {
1073 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001074 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001075 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001076 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001078 // Check for the next breakpoint at or after the ":while"
1079 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 if (breakpoint != NULL)
1081 {
1082 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001083 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 fname,
1085 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1086 *dbg_tick = debug_tick;
1087 }
1088 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001089 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001091 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001093 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1094 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 }
1096 }
1097
1098 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001099 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001101 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001103 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001104 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 }
1106 }
1107
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001108 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001109 if (breakpoint != NULL && has_watchexpr())
1110 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001111 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001112 *dbg_tick = debug_tick;
1113 }
1114
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 /*
1116 * When not inside any ":while" loop, clear remembered lines.
1117 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001118 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 {
1120 if (lines_ga.ga_len > 0)
1121 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001122 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1124 free_cmdlines(&lines_ga);
1125 }
1126 current_line = 0;
1127 }
1128
1129 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001130 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1131 * being restored at the ":endtry". Reset them here and set the
1132 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1133 * This includes the case where a missing ":endif", ":endwhile" or
1134 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001136 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001138 cstack.cs_lflags &= ~CSL_HAD_FINA;
1139 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1140 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 did_throw ? (void *)current_exception : NULL);
1142 did_emsg = got_int = did_throw = FALSE;
1143 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1144 }
1145
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001146 // Update global "trylevel" for recursive calls to do_cmdline() from
1147 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 trylevel = initial_trylevel + cstack.cs_trylevel;
1149
1150 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001151 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 * files) is aborted because of an error, an interrupt, or an uncaught
1153 * exception, cancel everything. If it is left normally, reset
1154 * force_abort to get the non-EH compatible abortion behavior for
1155 * the rest of the script.
1156 */
1157 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1158 force_abort = FALSE;
1159
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001160 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001162#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
1164 }
1165 /*
1166 * Continue executing command lines when:
1167 * - no CTRL-C typed, no aborting error, no exception thrown or try
1168 * conditionals need to be checked for executing finally clauses or
1169 * catching an interrupt exception
1170 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001171 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 * looping for ":source" command or function call.
1173 */
1174 while (!((got_int
1175#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001176 || (did_emsg && (force_abort || in_vim9script()))
1177 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178#endif
1179 )
1180#ifdef FEAT_EVAL
1181 && cstack.cs_trylevel == 0
1182#endif
1183 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001184 && !(did_emsg
1185#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001186 // Keep going when inside try/catch, so that the error can be
1187 // deal with, except when it is a syntax error, it may cause
1188 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001189 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1190#endif
1191 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001192 && (getline_equal(fgetline, cookie, getexmodeline)
1193 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 && (next_cmdline != NULL
1195#ifdef FEAT_EVAL
1196 || cstack.cs_idx >= 0
1197#endif
1198 || (flags & DOCMD_REPEAT)));
1199
1200 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001201 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202#ifdef FEAT_EVAL
1203 free_cmdlines(&lines_ga);
1204 ga_clear(&lines_ga);
1205
1206 if (cstack.cs_idx >= 0)
1207 {
1208 /*
1209 * If a sourced file or executed function ran to its end, report the
1210 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001211 * In Vim9 script do not give a second error, executing aborts after
1212 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 */
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001214 if (!got_int && !did_throw && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001215 && ((getline_equal(fgetline, cookie, getsourceline)
1216 && !source_finished(fgetline, cookie))
1217 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 && !func_has_ended(real_cookie))))
1219 {
1220 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001221 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001223 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001224 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001225 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001227 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 }
1229
1230 /*
1231 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1232 * ":endtry" in a sourced file or executed function. If the try
1233 * conditional is in its finally clause, ignore anything pending.
1234 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001235 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 */
1237 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001238 {
1239 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1240
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001241 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001242 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001243 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1244 &cstack.cs_looplevel);
1245 }
1246 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 trylevel = initial_trylevel;
1248 }
1249
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001250 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1251 // lack was reported above and the error message is to be converted to an
1252 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001253 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 ? (char_u *)"endfunction" : (char_u *)NULL);
1255
1256 if (trylevel == 0)
1257 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001258 // Just in case did_throw got set but current_exception wasn't.
1259 if (current_exception == NULL)
1260 did_throw = FALSE;
1261
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 /*
1263 * When an exception is being thrown out of the outermost try
1264 * conditional, discard the uncaught exception, disable the conversion
1265 * of interrupts or errors to exceptions, and ensure that no more
1266 * commands are executed.
1267 */
1268 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001269 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
1271 /*
1272 * On an interrupt or an aborting error not converted to an exception,
1273 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001274 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 * when force_abort is set and did_emsg unset in case of an interrupt
1276 * from a finally clause after an error.
1277 */
1278 else if (got_int || (did_emsg && force_abort))
1279 suppress_errthrow = TRUE;
1280 }
1281
1282 /*
1283 * The current cstack will be freed when do_cmdline() returns. An uncaught
1284 * exception will have to be rethrown in the previous cstack. If a function
1285 * has just returned or a script file was just finished and the previous
1286 * cstack belongs to the same function or, respectively, script file, it
1287 * will have to be checked for finally clauses to be executed due to the
1288 * ":return" or ":finish". This is done in do_one_cmd().
1289 */
1290 if (did_throw)
1291 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001292 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001294 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 && ex_nesting_level > func_level(real_cookie) + 1))
1296 {
1297 if (!did_throw)
1298 check_cstack = TRUE;
1299 }
1300 else
1301 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001302 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001303 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 --ex_nesting_level;
1305 /*
1306 * Go to debug mode when returning from a function in which we are
1307 * single-stepping.
1308 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001309 if ((getline_equal(fgetline, cookie, getsourceline)
1310 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001312 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 ? (char_u *)_("End of sourced file")
1314 : (char_u *)_("End of function"));
1315 }
1316
1317 /*
1318 * Restore the exception environment (done after returning from the
1319 * debugger).
1320 */
1321 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001322 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323
1324 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001325
1326 // Cleanup if "cs_emsg_silent_list" remains.
1327 if (cstack.cs_emsg_silent_list != NULL)
1328 {
1329 eslist_T *elem, *temp;
1330
1331 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1332 {
1333 temp = elem->next;
1334 vim_free(elem);
1335 }
1336 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001337#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
1339 /*
1340 * If there was too much output to fit on the command line, ask the user to
1341 * hit return before redrawing the screen. With the ":global" command we do
1342 * this only once after the command is finished.
1343 */
1344 if (did_inc)
1345 {
1346 --RedrawingDisabled;
1347 --no_wait_return;
1348 msg_scroll = FALSE;
1349
1350 /*
1351 * When just finished an ":if"-":else" which was typed, no need to
1352 * wait for hit-return. Also for an error situation.
1353 */
1354 if (retval == FAIL
1355#ifdef FEAT_EVAL
1356 || (did_endif && KeyTyped && !did_emsg)
1357#endif
1358 )
1359 {
1360 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001361 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 }
1363 else if (need_wait_return)
1364 {
1365 /*
1366 * The msg_start() above clears msg_didout. The wait_return we do
1367 * here should not overwrite the command that may be shown before
1368 * doing that.
1369 */
1370 msg_didout |= msg_didout_before_start;
1371 wait_return(FALSE);
1372 }
1373 }
1374
Bram Moolenaar2a942252012-11-28 23:03:07 +01001375#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001376 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001377#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 /*
1379 * Reset if_level, in case a sourced script file contains more ":if" than
1380 * ":endif" (could be ":if x | foo | endif").
1381 */
1382 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001383#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001384
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 --call_depth;
1386 return retval;
1387}
1388
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001389#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001390/*
1391 * Handle when "did_throw" is set after executing commands.
1392 */
1393 void
1394handle_did_throw()
1395{
1396 char *p = NULL;
1397 msglist_T *messages = NULL;
Bram Moolenaar9e0ee592021-07-31 22:17:28 +02001398 ESTACK_CHECK_DECLARATION
Bram Moolenaar620c9592021-07-31 21:32:31 +02001399
1400 /*
1401 * If the uncaught exception is a user exception, report it as an
1402 * error. If it is an error exception, display the saved error
1403 * message now. For an interrupt exception, do nothing; the
1404 * interrupt message is given elsewhere.
1405 */
1406 switch (current_exception->type)
1407 {
1408 case ET_USER:
1409 vim_snprintf((char *)IObuff, IOSIZE,
1410 _("E605: Exception not caught: %s"),
1411 current_exception->value);
1412 p = (char *)vim_strsave(IObuff);
1413 break;
1414 case ET_ERROR:
1415 messages = current_exception->messages;
1416 current_exception->messages = NULL;
1417 break;
1418 case ET_INTERRUPT:
1419 break;
1420 }
1421
1422 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1423 current_exception->throw_lnum);
1424 ESTACK_CHECK_SETUP
1425 current_exception->throw_name = NULL;
1426
1427 discard_current_exception(); // uses IObuff if 'verbose'
1428 suppress_errthrow = TRUE;
1429 force_abort = TRUE;
1430
1431 if (messages != NULL)
1432 {
1433 do
1434 {
1435 msglist_T *next = messages->next;
1436 int save_compiling = estack_compiling;
1437
1438 estack_compiling = messages->msg_compiling;
1439 emsg(messages->msg);
1440 vim_free(messages->msg);
1441 vim_free(messages->sfile);
1442 vim_free(messages);
1443 messages = next;
1444 estack_compiling = save_compiling;
1445 }
1446 while (messages != NULL);
1447 }
1448 else if (p != NULL)
1449 {
1450 emsg(p);
1451 vim_free(p);
1452 }
1453 vim_free(SOURCING_NAME);
1454 ESTACK_CHECK_NOW
1455 estack_pop();
1456}
1457
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001459 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 */
1461 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001462get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001464 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 wcmd_T *wp;
1466 char_u *line;
1467
1468 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1469 {
1470 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001471 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001473 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001475 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001477 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001478 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 ++cp->current_line;
1480
1481 return line;
1482 }
1483
1484 KeyTyped = FALSE;
1485 ++cp->current_line;
1486 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001487 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 return vim_strsave(wp->line);
1489}
1490
1491/*
1492 * Store a line in "gap" so that a ":while" loop can execute it again.
1493 */
1494 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001495store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496{
1497 if (ga_grow(gap, 1) == FAIL)
1498 return FAIL;
1499 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001500 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 return OK;
1503}
1504
1505/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001506 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 */
1508 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001509free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510{
1511 while (gap->ga_len > 0)
1512 {
1513 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1514 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 }
1516}
1517#endif
1518
1519/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001520 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1521 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001524getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001525 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001526 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001527 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528{
1529#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001530 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001531 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001533 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1534 // function that's originally used to obtain the lines. This may be
1535 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001536 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001537 cp = (struct loop_cookie *)cookie;
1538 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 {
1540 gp = cp->getline;
1541 cp = cp->cookie;
1542 }
1543 return gp == func;
1544#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001545 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546#endif
1547}
1548
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001550 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 * getline function. Otherwise return "cookie".
1552 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001554getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001555 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001556 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557{
Bram Moolenaar13505972019-01-24 15:04:48 +01001558#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001559 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001560 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001562 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1563 // cookie that's originally used to obtain the lines. This may be nested
1564 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001565 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001566 cp = (struct loop_cookie *)cookie;
1567 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 {
1569 gp = cp->getline;
1570 cp = cp->cookie;
1571 }
1572 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001573#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001576}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
Bram Moolenaard5053d02020-06-28 15:51:16 +02001578#if defined(FEAT_EVAL) || defined(PROT)
1579/*
1580 * Get the next line source line without advancing.
1581 */
1582 char_u *
1583getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001584 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001585 void *cookie) // argument for fgetline()
1586{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001587 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001588 struct loop_cookie *cp;
1589 wcmd_T *wp;
1590
1591 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1592 // cookie that's originally used to obtain the lines. This may be nested
1593 // several levels.
1594 gp = fgetline;
1595 cp = (struct loop_cookie *)cookie;
1596 while (gp == get_loop_line)
1597 {
1598 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1599 {
1600 // executing lines a second time, use the stored copy
1601 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1602 return wp->line;
1603 }
1604 gp = cp->getline;
1605 cp = cp->cookie;
1606 }
1607 if (gp == getsourceline)
1608 return source_nextline(cp);
1609 return NULL;
1610}
1611#endif
1612
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001613
1614/*
1615 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1616 * ":bwipeout", etc.
1617 * Returns the buffer number.
1618 */
1619 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001620compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001621{
1622 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001623 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001624 int count = offset;
1625
1626 buf = firstbuf;
1627 while (buf->b_next != NULL && buf->b_fnum < lnum)
1628 buf = buf->b_next;
1629 while (count != 0)
1630 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001631 count += (offset < 0) ? 1 : -1;
1632 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1633 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001634 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001635 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001636 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001637 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001638 while (buf->b_ml.ml_mfp == NULL)
1639 {
1640 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1641 if (nextbuf == NULL)
1642 break;
1643 buf = nextbuf;
1644 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001645 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001646 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001647 if (addr_type == ADDR_LOADED_BUFFERS)
1648 while (buf->b_ml.ml_mfp == NULL)
1649 {
1650 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1651 if (nextbuf == NULL)
1652 break;
1653 buf = nextbuf;
1654 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001655 return buf->b_fnum;
1656}
1657
Bram Moolenaarb7316892019-05-01 18:08:42 +02001658/*
1659 * Return the window number of "win".
1660 * When "win" is NULL return the number of windows.
1661 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001662 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001663current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001664{
1665 win_T *wp;
1666 int nr = 0;
1667
Bram Moolenaar29323592016-07-24 22:04:11 +02001668 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001669 {
1670 ++nr;
1671 if (wp == win)
1672 break;
1673 }
1674 return nr;
1675}
1676
1677 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001678current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001679{
1680 tabpage_T *tp;
1681 int nr = 0;
1682
Bram Moolenaar29323592016-07-24 22:04:11 +02001683 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001684 {
1685 ++nr;
1686 if (tp == tab)
1687 break;
1688 }
1689 return nr;
1690}
1691
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001692 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001693comment_start(char_u *p, int starts_with_colon UNUSED)
1694{
1695#ifdef FEAT_EVAL
1696 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001697 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001698#endif
1699 return *p == '"';
1700}
1701
Bram Moolenaarf240e182014-11-27 18:33:02 +01001702# define CURRENT_WIN_NR current_win_nr(curwin)
1703# define LAST_WIN_NR current_win_nr(NULL)
1704# define CURRENT_TAB_NR current_tab_nr(curtab)
1705# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001706
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707/*
1708 * Execute one Ex command.
1709 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001710 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1711 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 *
1713 * 1. skip comment lines and leading space
1714 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001715 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001716 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001717 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001718 * 6. parse arguments
1719 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001721 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 *
1723 * This function may be called recursively!
1724 */
1725#if (_MSC_VER == 1200)
1726/*
Bram Moolenaared203462004-06-16 11:19:22 +00001727 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001729 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730#endif
1731 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001732do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001733 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001734 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001736 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001738 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001739 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001741 char_u *p;
1742 linenr_T lnum;
1743 long n;
1744 char *errormsg = NULL; // error message
1745 char_u *after_modifier = NULL;
1746 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001747 cmdmod_T save_cmdmod;
1748 int save_reg_executing = reg_executing;
1749 int ni; // set when Not Implemented
1750 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001751 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001752#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001753 int may_have_range;
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001754 int vim9script;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001755 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001756#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001757 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758
Bram Moolenaara80faa82020-04-12 19:37:17 +02001759 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 ea.line1 = 1;
1761 ea.line2 = 1;
1762#ifdef FEAT_EVAL
1763 ++ex_nesting_level;
1764#endif
1765
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001766 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 if (quitmore
1768#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001769 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001770 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001771#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001772 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001773 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 --quitmore;
1775
1776 /*
1777 * Reset browse, confirm, etc.. They are restored when returning, for
1778 * recursive calls.
1779 */
1780 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001782 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001783 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1784 goto doend;
1785
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001786/*
1787 * 1. Skip comment lines and leading white space and colons.
1788 * 2. Handle command modifiers.
1789 */
1790 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001792 ea.cmdlinep = cmdlinep;
1793 ea.getline = fgetline;
1794 ea.cookie = cookie;
1795#ifdef FEAT_EVAL
1796 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001797 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001799 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001800 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001801 apply_cmdmod(&cmdmod);
Bram Moolenaar2379f872021-02-14 14:07:34 +01001802#ifdef FEAT_EVAL
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001803 vim9script = in_vim9script();
Bram Moolenaar2379f872021-02-14 14:07:34 +01001804#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001805 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
1807#ifdef FEAT_EVAL
1808 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1809 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1810#else
1811 ea.skip = (if_level > 0);
1812#endif
1813
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001815 * 3. Skip over the range to find the command. Let "p" point to after it.
1816 *
1817 * We need the command to know what kind of range it uses.
1818 */
1819 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001820#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001821 // In Vim9 script a colon is required before the range. This may also be
1822 // after command modifiers.
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001823 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001824 {
1825 may_have_range = FALSE;
1826 for (p = ea.cmd; p >= *cmdlinep; --p)
1827 {
1828 if (*p == ':')
1829 may_have_range = TRUE;
1830 if (p < ea.cmd && !VIM_ISWHITE(*p))
1831 break;
1832 }
1833 }
1834 else
1835 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001836 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001837#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001838 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839
1840#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001841 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001842 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001843 if (ea.cmd == cmd + 1 && *cmd == '$')
1844 // should be "$VAR = val"
1845 --ea.cmd;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001846 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001847 if (ea.cmdidx == CMD_SIZE)
1848 {
1849 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1850
1851 // If a ':' before the range is missing, give a clearer error
1852 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001853 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001854 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001855 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001856 goto doend;
1857 }
1858 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001859 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860 else
1861#endif
1862 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001863
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001864#ifdef FEAT_EVAL
1865# ifdef FEAT_PROFILE
1866 // Count this line for profiling if skip is TRUE.
1867 if (do_profiling == PROF_YES
1868 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1869 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1870 {
1871 int skip = did_emsg || got_int || did_throw;
1872
1873 if (ea.cmdidx == CMD_catch)
1874 skip = !skip && !(cstack->cs_idx >= 0
1875 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1876 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1877 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1878 skip = skip || !(cstack->cs_idx >= 0
1879 && !(cstack->cs_flags[cstack->cs_idx]
1880 & (CSF_ACTIVE | CSF_TRUE)));
1881 else if (ea.cmdidx == CMD_finally)
1882 skip = FALSE;
1883 else if (ea.cmdidx != CMD_endif
1884 && ea.cmdidx != CMD_endfor
1885 && ea.cmdidx != CMD_endtry
1886 && ea.cmdidx != CMD_endwhile)
1887 skip = ea.skip;
1888
1889 if (!skip)
1890 {
1891 if (getline_equal(fgetline, cookie, get_func_line))
1892 func_line_exec(getline_cookie(fgetline, cookie));
1893 else if (getline_equal(fgetline, cookie, getsourceline))
1894 script_line_exec();
1895 }
1896 }
1897# endif
1898
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001899 // May go to debug mode. If this happens and the ">quit" debug command is
1900 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001901 dbg_check_breakpoint(&ea);
1902 if (!ea.skip && got_int)
1903 {
1904 ea.skip = TRUE;
1905 (void)do_intthrow(cstack);
1906 }
1907#endif
1908
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001909/*
1910 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 *
1912 * where 'addr' is:
1913 *
1914 * % (entire file)
1915 * $ [+-NUM]
1916 * 'x [+-NUM] (where x denotes a currently defined mark)
1917 * . [+-NUM]
1918 * [+-NUM]..
1919 * NUM
1920 *
1921 * The ea.cmd pointer is updated to point to the first character following the
1922 * range spec. If an initial address is found, but no second, the upper bound
1923 * is equal to the lower.
1924 */
1925
Bram Moolenaar25190db2019-05-04 15:05:28 +02001926 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001927 if (!IS_USER_CMDIDX(ea.cmdidx))
1928 {
1929 if (ea.cmdidx != CMD_SIZE)
1930 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1931 else
1932 ea.addr_type = ADDR_LINES;
1933
Bram Moolenaar25190db2019-05-04 15:05:28 +02001934 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001935 if (ea.cmdidx == CMD_wincmd && p != NULL)
1936 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001937#ifdef FEAT_QUICKFIX
1938 // :.cc in quickfix window uses line number
1939 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1940 ea.addr_type = ADDR_OTHER;
1941#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001942 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001943
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001944 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001945#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001946 if (!may_have_range)
1947 ea.line1 = ea.line2 = default_address(&ea);
1948 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001949#endif
1950 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1951 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001954 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 */
1956
1957 /*
1958 * Skip ':' and any white space
1959 */
1960 ea.cmd = skipwhite(ea.cmd);
1961 while (*ea.cmd == ':')
1962 ea.cmd = skipwhite(ea.cmd + 1);
1963
1964 /*
1965 * If we got a line, but no command, then go to the line.
1966 * If we find a '|' or '\n' we set ea.nextcmd.
1967 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001968 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1969 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 {
1971 /*
1972 * strange vi behaviour:
1973 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001974 * ":3|..." prints line 3 (not in Vim9 script)
1975 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001977 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 goto doend;
Bram Moolenaarb8554302021-02-15 21:30:30 +01001979 if ((*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
1980#ifdef FEAT_EVAL
1981 && !vim9script
1982#endif
1983 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 {
1985 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001986 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if ((errormsg = invalid_range(&ea)) == NULL)
1988 {
1989 correct_range(&ea);
1990 ex_print(&ea);
1991 }
1992 }
1993 else if (ea.addr_count != 0)
1994 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001995 if (ea.line2 > curbuf->b_ml.ml_line_count)
1996 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001997 // With '-' in 'cpoptions' a line number past the file is an
1998 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001999 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2000 ea.line2 = -1;
2001 else
2002 ea.line2 = curbuf->b_ml.ml_line_count;
2003 }
2004
2005 if (ea.line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002006 errormsg = _(e_invalid_range);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 else
2008 {
2009 if (ea.line2 == 0)
2010 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 else
2012 curwin->w_cursor.lnum = ea.line2;
2013 beginline(BL_SOL | BL_FIX);
2014 }
2015 }
2016 goto doend;
2017 }
2018
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002019 // If this looks like an undefined user command and there are CmdUndefined
2020 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002021 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2022 && ASCII_ISUPPER(*ea.cmd)
2023 && has_cmdundefined())
2024 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002025 int ret;
2026
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002027 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002028 while (ASCII_ISALNUM(*p))
2029 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002030 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002031 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2032 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002033 // If the autocommands did something and didn't cause an error, try
2034 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002035 p = (ret
2036#ifdef FEAT_EVAL
2037 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002038#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002040 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002041
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 if (p == NULL)
2043 {
2044 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002045 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 goto doend;
2047 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002048 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002049 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2050 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 {
2052 errormsg = uc_fun_cmd();
2053 goto doend;
2054 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002055
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 if (ea.cmdidx == CMD_SIZE)
2057 {
2058 if (!ea.skip)
2059 {
2060 STRCPY(IObuff, _("E492: Not an editor command"));
2061 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002062 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002063 // If the modifier was parsed OK the error must be in the
2064 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002065 if (after_modifier != NULL)
2066 append_command(after_modifier);
2067 else
2068 append_command(*cmdlinep);
2069 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002070 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002071 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 }
2073 goto doend;
2074 }
2075
Bram Moolenaar958636c2014-10-21 20:01:58 +02002076 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2077 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002078#ifdef HAVE_EX_SCRIPT_NI
2079 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2080#endif
2081 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082
2083#ifndef FEAT_EVAL
2084 /*
2085 * When the expression evaluation is disabled, recognize the ":if" and
2086 * ":endif" commands and ignore everything in between it.
2087 */
2088 if (ea.cmdidx == CMD_if)
2089 ++if_level;
2090 if (if_level)
2091 {
2092 if (ea.cmdidx == CMD_endif)
2093 --if_level;
2094 goto doend;
2095 }
2096
2097#endif
2098
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002099 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002100 if (*p == '!' && ea.cmdidx != CMD_substitute
2101 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
2103 ++p;
2104 ea.forceit = TRUE;
2105 }
2106 else
2107 ea.forceit = FALSE;
2108
2109/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002110 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002112 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002113 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114
2115 if (!ea.skip)
2116 {
2117#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002118 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002120 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002121 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 goto doend;
2123 }
2124#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002125 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002126 {
2127 errormsg = _("E981: Command not allowed in rvim");
2128 goto doend;
2129 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002130 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002132 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002133 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 goto doend;
2135 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002136
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002137 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002139#ifdef FEAT_CMDWIN
2140 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2141 {
2142 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002143 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002144 goto doend;
2145 }
2146#endif
2147 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2148 {
2149 // Command not allowed when text is locked
2150 errormsg = _(get_text_locked_msg());
2151 goto doend;
2152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002154
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002155 // Disallow editing another buffer when "curbuf_lock" is set.
2156 // Do allow ":checktime" (it is postponed).
2157 // Do allow ":edit" (check for an argument later).
2158 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002159 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002160 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002161 && ea.cmdidx != CMD_edit
2162 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002163 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002164 && curbuf_locked())
2165 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002167 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002169 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002170 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 goto doend;
2172 }
2173 }
2174
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002175 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002177 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 goto doend;
2179 }
2180
2181 /*
2182 * Don't complain about the range if it is not used
2183 * (could happen if line_count is accidentally set to 0).
2184 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002185 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 {
2187 /*
2188 * If the range is backwards, ask for confirmation and, if given, swap
2189 * ea.line1 & ea.line2 so it's forwards again.
2190 * When global command is busy, don't ask, will fail below.
2191 */
2192 if (!global_busy && ea.line1 > ea.line2)
2193 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002194 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002196 if (sourcing || exmode_active)
2197 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002198 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002199 goto doend;
2200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 if (ask_yesno((char_u *)
2202 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002203 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 }
2205 lnum = ea.line1;
2206 ea.line1 = ea.line2;
2207 ea.line2 = lnum;
2208 }
2209 if ((errormsg = invalid_range(&ea)) != NULL)
2210 goto doend;
2211 }
2212
Bram Moolenaarb7316892019-05-01 18:08:42 +02002213 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2214 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 ea.line2 = 1;
2216
2217 correct_range(&ea);
2218
2219#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002220 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002221 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002223 // Put the first line at the start of a closed fold, put the last line
2224 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 (void)hasFolding(ea.line1, &ea.line1, NULL);
2226 (void)hasFolding(ea.line2, NULL, &ea.line2);
2227 }
2228#endif
2229
2230#ifdef FEAT_QUICKFIX
2231 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002232 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 * option here, so things like % get expanded.
2234 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002235 p = replace_makeprg(&ea, p, cmdlinep);
2236 if (p == NULL)
2237 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238#endif
2239
2240 /*
2241 * Skip to start of argument.
2242 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2243 */
2244 if (ea.cmdidx == CMD_bang)
2245 ea.arg = p;
2246 else
2247 ea.arg = skipwhite(p);
2248
Bram Moolenaar379fb762018-08-30 15:58:28 +02002249 // ":file" cannot be run with an argument when "curbuf_lock" is set
2250 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2251 goto doend;
2252
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 /*
2254 * Check for "++opt=val" argument.
2255 * Must be first, allow ":w ++enc=utf8 !cmd"
2256 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002257 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2259 if (getargopt(&ea) == FAIL && !ni)
2260 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002261 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 goto doend;
2263 }
2264
2265 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2266 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002267 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002269 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002271 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 goto doend;
2273 }
2274 ea.arg = skipwhite(ea.arg + 1);
2275 ea.append = TRUE;
2276 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002277 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 {
2279 ++ea.arg;
2280 ea.usefilter = TRUE;
2281 }
2282 }
2283
2284 if (ea.cmdidx == CMD_read)
2285 {
2286 if (ea.forceit)
2287 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002288 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 ea.forceit = FALSE;
2290 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002291 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 {
2293 ++ea.arg;
2294 ea.usefilter = TRUE;
2295 }
2296 }
2297
2298 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2299 {
2300 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002301 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
2303 ++ea.arg;
2304 ++ea.amount;
2305 }
2306 ea.arg = skipwhite(ea.arg);
2307 }
2308
2309 /*
2310 * Check for "+command" argument, before checking for next command.
2311 * Don't do this for ":read !cmd" and ":write !cmd".
2312 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002313 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2315
2316 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002317 * For commands that do not use '|' inside their argument: Check for '|' to
2318 * separate commands and '"' or '#' to start comments.
2319 *
2320 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002321 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002322 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002323 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002325 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2326 {
2327 separate_nextcmd(&ea);
2328 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002329 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002330 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002331 || ea.cmdidx == CMD_global
2332 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002333 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002334#ifdef FEAT_EVAL
2335 || inside_block(&ea)
2336#endif
2337 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
2339 for (p = ea.arg; *p; ++p)
2340 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002341 // Remove one backslash before a newline, so that it's possible to
2342 // pass a newline to the shell and also a newline that is preceded
2343 // with a backslash. This makes it impossible to end a shell
2344 // command in a backslash, but that doesn't appear useful.
2345 // Halving the number of backslashes is incompatible with previous
2346 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002348 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 else if (*p == '\n')
2350 {
2351 ea.nextcmd = p + 1;
2352 *p = NUL;
2353 break;
2354 }
2355 }
2356 }
2357
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002358 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002359 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002361 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002362 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002364 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002365 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002366 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002368#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002369 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002370 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002372 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002373 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 }
2375#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002376 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2377 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002378 {
2379 ea.regname = *ea.arg++;
2380#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002381 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002382 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2383 {
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002384 set_expr_line(vim_strsave(ea.arg), &ea);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002385 ea.arg += STRLEN(ea.arg);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002386 did_set_expr_line = TRUE;
Bram Moolenaar85de2062011-05-05 14:26:41 +02002387 }
2388#endif
2389 ea.arg = skipwhite(ea.arg);
2390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
2392
2393 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002394 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 * count, it's a buffer name.
2396 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002397 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002398 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002399 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 {
2401 n = getdigits(&ea.arg);
2402 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002403 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002405 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 goto doend;
2407 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002408 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {
2410 ea.line2 = n;
2411 if (ea.addr_count == 0)
2412 ea.addr_count = 1;
2413 }
2414 else
2415 {
2416 ea.line1 = ea.line2;
2417 ea.line2 += n - 1;
2418 ++ea.addr_count;
2419 /*
2420 * Be vi compatible: no error message for out of range.
2421 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002422 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 ea.line2 = curbuf->b_ml.ml_line_count;
2424 }
2425 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002426
2427 /*
2428 * Check for flags: 'l', 'p' and '#'.
2429 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002430 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002431 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002432 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2433 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002435 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002436 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 goto doend;
2438 }
2439
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002440 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002442 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 goto doend;
2444 }
2445
2446#ifdef FEAT_EVAL
2447 /*
2448 * Skip the command when it's not going to be executed.
2449 * The commands like :if, :endif, etc. always need to be executed.
2450 * Also make an exception for commands that handle a trailing command
2451 * themselves.
2452 */
2453 if (ea.skip)
2454 {
2455 switch (ea.cmdidx)
2456 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002457 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 case CMD_while:
2459 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002460 case CMD_for:
2461 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 case CMD_if:
2463 case CMD_elseif:
2464 case CMD_else:
2465 case CMD_endif:
2466 case CMD_try:
2467 case CMD_catch:
2468 case CMD_finally:
2469 case CMD_endtry:
2470 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002471 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 break;
2473
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002474 // Commands that handle '|' themselves. Check: A command should
2475 // either have the EX_TRLBAR flag, appear in this list or appear in
2476 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 case CMD_aboveleft:
2478 case CMD_and:
2479 case CMD_belowright:
2480 case CMD_botright:
2481 case CMD_browse:
2482 case CMD_call:
2483 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002484 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 case CMD_delfunction:
2486 case CMD_djump:
2487 case CMD_dlist:
2488 case CMD_dsearch:
2489 case CMD_dsplit:
2490 case CMD_echo:
2491 case CMD_echoerr:
2492 case CMD_echomsg:
2493 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002494 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002496 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002497 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 case CMD_help:
2499 case CMD_hide:
2500 case CMD_ijump:
2501 case CMD_ilist:
2502 case CMD_isearch:
2503 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002504 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 case CMD_keepjumps:
2506 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002507 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 case CMD_leftabove:
2509 case CMD_let:
2510 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002511 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002512 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002514 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002515 case CMD_noautocmd:
2516 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 case CMD_perl:
2518 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002519 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002520 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002521 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 case CMD_return:
2523 case CMD_rightbelow:
2524 case CMD_ruby:
2525 case CMD_silent:
2526 case CMD_smagic:
2527 case CMD_snomagic:
2528 case CMD_substitute:
2529 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002530 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 case CMD_tcl:
2532 case CMD_throw:
2533 case CMD_tilde:
2534 case CMD_topleft:
2535 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002536 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002537 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 case CMD_verbose:
2539 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002540 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 break;
2542
2543 default: goto doend;
2544 }
2545 }
2546#endif
2547
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002548 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 {
2550 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2551 goto doend;
2552 }
2553
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 /*
2555 * Accept buffer name. Cannot be used at the same time with a buffer
2556 * number. Don't do this for a user command.
2557 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002558 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002559 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 {
2561 /*
2562 * :bdelete, :bwipeout and :bunload take several arguments, separated
2563 * by spaces: find next space (skipping over escaped characters).
2564 * The others take one argument: ignore trailing spaces.
2565 */
2566 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2567 || ea.cmdidx == CMD_bunload)
2568 p = skiptowhite_esc(ea.arg);
2569 else
2570 {
2571 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002572 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 --p;
2574 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002575 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002576 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002577 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 goto doend;
2579 ea.addr_count = 1;
2580 ea.arg = skipwhite(p);
2581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002583 // The :try command saves the emsg_silent flag, reset it here when
2584 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002585 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002586 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002587 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002588 if (emsg_silent < 0)
2589 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002590 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002591 }
2592
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002594 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596
Bram Moolenaar958636c2014-10-21 20:01:58 +02002597 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 {
2599 /*
2600 * Execute a user-defined command.
2601 */
2602 do_ucmd(&ea);
2603 }
2604 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 {
2606 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002607 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 */
2609 ea.errmsg = NULL;
2610 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2611 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002612 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 }
2614
2615#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002617 // Not if this was a command that wasn't executed or :endif.
Bram Moolenaar16531552020-02-08 16:00:46 +01002618 if (getline_equal(ea.getline, ea.cookie, getsourceline)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002619 && current_sctx.sc_sid > 0
2620 && ea.cmdidx != CMD_endif
2621 && (cstack->cs_idx < 0
2622 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002623 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 /*
2626 * If the command just executed called do_cmdline(), any throw or ":return"
2627 * or ":finish" encountered there must also check the cstack of the still
2628 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2629 * exception, or reanimate a returned function or finished script file and
2630 * return or finish it again.
2631 */
2632 if (need_rethrow)
2633 do_throw(cstack);
2634 else if (check_cstack)
2635 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002636 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002638 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 && current_func_returned())
2640 do_return(&ea, TRUE, FALSE, NULL);
2641 }
2642 need_rethrow = check_cstack = FALSE;
2643#endif
2644
2645doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002646 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002647 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002649 curwin->w_cursor.col = 0;
2650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
2652 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2653 {
2654 if (sourcing)
2655 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002656 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 {
2658 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002659 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002661 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 }
2663 emsg(errormsg);
2664 }
2665#ifdef FEAT_EVAL
2666 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002667 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2668 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002669
2670 if (did_set_expr_line)
2671 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672#endif
2673
Bram Moolenaare1004402020-10-24 20:49:43 +02002674 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002676 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002678 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 ea.nextcmd = NULL;
2680
2681#ifdef FEAT_EVAL
2682 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002683 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684#endif
2685
2686 return ea.nextcmd;
2687}
2688#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002689 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690#endif
2691
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002692static char ex_error_buf[MSG_BUF_LEN];
2693
2694/*
2695 * Return an error message with argument included.
2696 * Uses a static buffer, only the last error will be kept.
2697 * "msg" will be translated, caller should use N_().
2698 */
2699 char *
2700ex_errmsg(char *msg, char_u *arg)
2701{
2702 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2703 return ex_error_buf;
2704}
2705
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002707 * Check for an Ex command with optional tail.
2708 * If there is a match advance "pp" to the argument and return TRUE.
2709 * If "noparen" is TRUE do not recognize the command followed by "(".
2710 */
2711 static int
2712checkforcmd_opt(
2713 char_u **pp, // start of command
2714 char *cmd, // name of command
2715 int len, // required length
2716 int noparen)
2717{
2718 int i;
2719
2720 for (i = 0; cmd[i] != NUL; ++i)
2721 if (((char_u *)cmd)[i] != (*pp)[i])
2722 break;
2723 if (i >= len && !isalpha((*pp)[i])
2724 && (*pp)[i] != '_' && (!noparen || (*pp)[i] != '('))
2725 {
2726 *pp = skipwhite(*pp + i);
2727 return TRUE;
2728 }
2729 return FALSE;
2730}
2731
2732/*
2733 * Check for an Ex command with optional tail.
2734 * If there is a match advance "pp" to the argument and return TRUE.
2735 */
2736 int
2737checkforcmd(
2738 char_u **pp, // start of command
2739 char *cmd, // name of command
2740 int len) // required length
2741{
2742 return checkforcmd_opt(pp, cmd, len, FALSE);
2743}
2744
2745/*
2746 * Check for an Ex command with optional tail, not followed by "(".
2747 * If there is a match advance "pp" to the argument and return TRUE.
2748 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002749 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002750checkforcmd_noparen(
2751 char_u **pp, // start of command
2752 char *cmd, // name of command
2753 int len) // required length
2754{
2755 return checkforcmd_opt(pp, cmd, len, TRUE);
2756}
2757
2758/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002759 * Parse and skip over command modifiers:
2760 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002761 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002762 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002763 * When "skip_only" is TRUE the global variables are not changed, except for
2764 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002765 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2766 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002767 * Call apply_cmdmod() to get the side effects of the modifiers:
2768 * - Increment "sandbox" for ":sandbox"
2769 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002770 * - set msg_silent for ":silent"
2771 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002772 * Return FAIL when the command is not to be executed.
2773 * May set "errormsg" to an error message.
2774 */
2775 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002776parse_command_modifiers(
2777 exarg_T *eap,
2778 char **errormsg,
2779 cmdmod_T *cmod,
2780 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002781{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002782 char_u *p;
2783 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002784
Bram Moolenaare1004402020-10-24 20:49:43 +02002785 CLEAR_POINTER(cmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002786
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002787 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002788 for (;;)
2789 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002790 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002791 {
2792 if (*eap->cmd == ':')
2793 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002794 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002795 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002796
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002797 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002798 if (*eap->cmd == NUL && exmode_active
2799 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2800 || getline_equal(eap->getline, eap->cookie, getexline))
2801 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2802 {
2803 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002804 if (!skip_only)
2805 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002806 }
2807
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002808 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002809 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002810 {
2811 // a comment ends at a NL
2812 if (eap->nextcmd == NULL)
2813 {
2814 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2815 if (eap->nextcmd != NULL)
2816 ++eap->nextcmd;
2817 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002818 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002819 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002820 if (*eap->cmd == NUL)
2821 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002822 if (!skip_only)
2823 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002824 return FAIL;
2825 }
2826
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002827 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002828
2829 // In Vim9 script a variable can shadow a command modifier:
2830 // verbose = 123
2831 // verbose += 123
2832 // silent! verbose = func()
2833 // verbose.member = 2
2834 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002835 // But not:
2836 // verbose [a, b] = list
Bram Moolenaar17126b12021-01-07 22:03:02 +01002837 if (in_vim9script())
2838 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002839 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002840
2841 for (s = p; ASCII_ISALPHA(*s); ++s)
2842 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002843 n = skipwhite(s);
2844 if (vim_strchr((char_u *)".=", *n) != NULL
2845 || *s == '['
2846 || (*n != NUL && n[1] == '='))
Bram Moolenaar17126b12021-01-07 22:03:02 +01002847 break;
2848 }
2849
Bram Moolenaareffed932018-08-14 13:38:17 +02002850 switch (*p)
2851 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002852 // When adding an entry, also modify cmd_exists().
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002853 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002854 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002855 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002856 continue;
2857
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002858 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002859 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002860 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002861 continue;
2862 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002863 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002864 {
2865#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002866 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002867#endif
2868 continue;
2869 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002870 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002871 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002872 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002873 continue;
2874
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002875 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002876 break;
2877#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002878 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002879#endif
2880 continue;
2881
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002882 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002883 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002884 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002885 continue;
2886 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002887 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002888 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002889 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002890 continue;
2891 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002892 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002893 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002894 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002895 continue;
2896 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002897 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002898 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002899 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002900 continue;
2901
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002902 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002903 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02002904 char_u *reg_pat;
2905 char_u *nulp = NULL;
2906 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002907
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002908 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaareffed932018-08-14 13:38:17 +02002909 || *p == NUL || ends_excmd(*p))
2910 break;
2911 if (*p == '!')
2912 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002913 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002914 p = skipwhite(p + 1);
2915 if (*p == NUL || ends_excmd(*p))
2916 break;
2917 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002918#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002919 // Avoid that "filter(arg)" is recognized.
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +01002920 if (in_vim9script() && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002921 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002922#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002923 if (skip_only)
2924 p = skip_vimgrep_pat(p, NULL, NULL);
2925 else
2926 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02002927 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
2928 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02002929 if (p == NULL || *p == NUL)
2930 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002931 if (!skip_only)
2932 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002933 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002934 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002935 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002936 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02002937 // restore the character overwritten by NUL
2938 if (nulp != NULL)
2939 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002940 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002941 eap->cmd = p;
2942 continue;
2943 }
2944
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002945 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002946 case 'h': if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02002947 || *p == NUL || ends_excmd(*p))
2948 break;
2949 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002950 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002951 continue;
2952
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002953 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002954 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002955 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002956 continue;
2957 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002958 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
2959 {
2960 if (ends_excmd2(p, eap->cmd))
2961 {
2962 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02002963 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002964 return FAIL;
2965 }
2966 cmod->cmod_flags |= CMOD_LEGACY;
2967 continue;
2968 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002969
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002970 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002971 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002972 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002973 continue;
2974
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002975 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002976 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002977 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002978 continue;
2979 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002980 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002981 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002982 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002983 continue;
2984
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002985 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02002986 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002987 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002988 continue;
2989
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002990 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002991 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002992 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02002993 continue;
2994 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002995 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002996 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002997 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002998 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2999 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003000 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003001 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003002 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003003 }
3004 continue;
3005
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003006 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003007 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003008 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003009 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003010 long tabnr = get_address(eap, &eap->cmd,
3011 ADDR_TABS, eap->skip,
3012 skip_only, FALSE, 1);
3013 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003014 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003015 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003016 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003017 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3018 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003019 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003020 return FAIL;
3021 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003022 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003023 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003024 }
3025 eap->cmd = p;
3026 continue;
3027 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003028 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003029 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003030 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003031 continue;
3032
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003033 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003034 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003035 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003036 continue;
3037
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003038 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003039 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003040 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003041 continue;
3042 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003043 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003044 {
3045 if (ends_excmd2(p, eap->cmd))
3046 {
3047 *errormsg =
3048 _(e_vim9cmd_must_be_followed_by_command);
3049 return FAIL;
3050 }
3051 cmod->cmod_flags |= CMOD_VIM9CMD;
3052 continue;
3053 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003054 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003055 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003056 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02003057 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003058 else
Bram Moolenaare1004402020-10-24 20:49:43 +02003059 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003060 eap->cmd = p;
3061 continue;
3062 }
3063 break;
3064 }
3065
3066 return OK;
3067}
3068
3069/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003070 * Return TRUE if "cmod" has anything set.
3071 */
3072 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003073has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003074{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003075 return (cmod->cmod_flags != 0 && (!ignore_silent
3076 || (cmod->cmod_flags
3077 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003078 || cmod->cmod_split != 0
3079 || cmod->cmod_verbose != 0
3080 || cmod->cmod_tab != 0
3081 || cmod->cmod_filter_regmatch.regprog != NULL;
3082}
3083
3084/*
3085 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3086 */
3087 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003088cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003089{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003090 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003091 {
3092 emsg(_(e_misplaced_command_modifier));
3093 return TRUE;
3094 }
3095 return FALSE;
3096}
3097
3098/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003099 * Apply the command modifiers. Saves current state in "cmdmod", call
3100 * undo_cmdmod() later.
3101 */
3102 void
3103apply_cmdmod(cmdmod_T *cmod)
3104{
3105#ifdef HAVE_SANDBOX
3106 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3107 {
3108 ++sandbox;
3109 cmod->cmod_did_sandbox = TRUE;
3110 }
3111#endif
3112 if (cmod->cmod_verbose > 0)
3113 {
3114 if (cmod->cmod_verbose_save == 0)
3115 cmod->cmod_verbose_save = p_verbose + 1;
3116 p_verbose = cmod->cmod_verbose;
3117 }
3118
3119 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3120 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003121 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003122 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003123 cmod->cmod_save_msg_scroll = msg_scroll;
3124 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003125 if (cmod->cmod_flags & CMOD_SILENT)
3126 ++msg_silent;
3127 if (cmod->cmod_flags & CMOD_UNSILENT)
3128 msg_silent = 0;
3129
3130 if (cmod->cmod_flags & CMOD_ERRSILENT)
3131 {
3132 ++emsg_silent;
3133 ++cmod->cmod_did_esilent;
3134 }
3135
Bram Moolenaare1004402020-10-24 20:49:43 +02003136 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003137 {
3138 // Set 'eventignore' to "all".
3139 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003140 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003141 set_string_option_direct((char_u *)"ei", -1,
3142 (char_u *)"all", OPT_FREE, SID_NONE);
3143 }
3144}
3145
3146/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003147 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003148 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003149 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003150undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003151{
Bram Moolenaare1004402020-10-24 20:49:43 +02003152 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003153 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003154 p_verbose = cmod->cmod_verbose_save - 1;
3155 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003156 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003157
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003158#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003159 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003160 {
3161 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003162 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003163 }
3164#endif
3165
Bram Moolenaare1004402020-10-24 20:49:43 +02003166 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003167 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003168 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003169 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3170 OPT_FREE, SID_NONE);
3171 free_string_option(cmod->cmod_save_ei);
3172 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003173 }
3174
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003175 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003176
Bram Moolenaare1004402020-10-24 20:49:43 +02003177 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003178 {
3179 // messages could be enabled for a serious error, need to check if the
3180 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003181 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3182 msg_silent = cmod->cmod_save_msg_silent - 1;
3183 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003184 if (emsg_silent < 0)
3185 emsg_silent = 0;
3186 // Restore msg_scroll, it's set by file I/O commands, even when no
3187 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003188 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003189
3190 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3191 // somewhere in the line. Put it back in the first column.
3192 if (redirecting())
3193 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003194
Bram Moolenaare1004402020-10-24 20:49:43 +02003195 cmod->cmod_save_msg_silent = 0;
3196 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003197 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003198}
3199
3200/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003201 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003202 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003203 * Return FAIL and set "errormsg" or return OK.
3204 */
3205 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003206parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003207{
3208 int address_count = 1;
3209 linenr_T lnum;
3210
3211 // Repeat for all ',' or ';' separated addresses.
3212 for (;;)
3213 {
3214 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003215 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003216 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003217 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003218 eap->addr_count == 0, address_count++);
3219 if (eap->cmd == NULL) // error detected
3220 return FAIL;
3221 if (lnum == MAXLNUM)
3222 {
3223 if (*eap->cmd == '%') // '%' - all lines
3224 {
3225 ++eap->cmd;
3226 switch (eap->addr_type)
3227 {
3228 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003229 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003230 eap->line1 = 1;
3231 eap->line2 = curbuf->b_ml.ml_line_count;
3232 break;
3233 case ADDR_LOADED_BUFFERS:
3234 {
3235 buf_T *buf = firstbuf;
3236
3237 while (buf->b_next != NULL
3238 && buf->b_ml.ml_mfp == NULL)
3239 buf = buf->b_next;
3240 eap->line1 = buf->b_fnum;
3241 buf = lastbuf;
3242 while (buf->b_prev != NULL
3243 && buf->b_ml.ml_mfp == NULL)
3244 buf = buf->b_prev;
3245 eap->line2 = buf->b_fnum;
3246 break;
3247 }
3248 case ADDR_BUFFERS:
3249 eap->line1 = firstbuf->b_fnum;
3250 eap->line2 = lastbuf->b_fnum;
3251 break;
3252 case ADDR_WINDOWS:
3253 case ADDR_TABS:
3254 if (IS_USER_CMDIDX(eap->cmdidx))
3255 {
3256 eap->line1 = 1;
3257 eap->line2 = eap->addr_type == ADDR_WINDOWS
3258 ? LAST_WIN_NR : LAST_TAB_NR;
3259 }
3260 else
3261 {
3262 // there is no Vim command which uses '%' and
3263 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003264 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003265 return FAIL;
3266 }
3267 break;
3268 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003269 case ADDR_UNSIGNED:
3270 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003271 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003272 return FAIL;
3273 case ADDR_ARGUMENTS:
3274 if (ARGCOUNT == 0)
3275 eap->line1 = eap->line2 = 0;
3276 else
3277 {
3278 eap->line1 = 1;
3279 eap->line2 = ARGCOUNT;
3280 }
3281 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003282 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003283#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003284 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003285 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003286 if (eap->line2 == 0)
3287 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003288#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003289 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003290 case ADDR_NONE:
3291 // Will give an error later if a range is found.
3292 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003293 }
3294 ++eap->addr_count;
3295 }
3296 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3297 {
3298 pos_T *fp;
3299
3300 // '*' - visual area
3301 if (eap->addr_type != ADDR_LINES)
3302 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003303 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003304 return FAIL;
3305 }
3306
3307 ++eap->cmd;
3308 if (!eap->skip)
3309 {
3310 fp = getmark('<', FALSE);
3311 if (check_mark(fp) == FAIL)
3312 return FAIL;
3313 eap->line1 = fp->lnum;
3314 fp = getmark('>', FALSE);
3315 if (check_mark(fp) == FAIL)
3316 return FAIL;
3317 eap->line2 = fp->lnum;
3318 ++eap->addr_count;
3319 }
3320 }
3321 }
3322 else
3323 eap->line2 = lnum;
3324 eap->addr_count++;
3325
3326 if (*eap->cmd == ';')
3327 {
3328 if (!eap->skip)
3329 {
3330 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003331 // Don't leave the cursor on an illegal line or column, but do
3332 // accept zero as address, so 0;/PATTERN/ works correctly.
3333 if (eap->line2 > 0)
3334 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003335 }
3336 }
3337 else if (*eap->cmd != ',')
3338 break;
3339 ++eap->cmd;
3340 }
3341
3342 // One address given: set start and end lines.
3343 if (eap->addr_count == 1)
3344 {
3345 eap->line1 = eap->line2;
3346 // ... but only implicit: really no address given
3347 if (lnum == MAXLNUM)
3348 eap->addr_count = 0;
3349 }
3350 return OK;
3351}
3352
3353/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003354 * Append "cmd" to the error message in IObuff.
3355 * Takes care of limiting the length and handling 0xa0, which would be
3356 * invisible otherwise.
3357 */
3358 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003359append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003360{
3361 char_u *s = cmd;
3362 char_u *d;
3363
3364 STRCAT(IObuff, ": ");
3365 d = IObuff + STRLEN(IObuff);
3366 while (*s != NUL && d - IObuff < IOSIZE - 7)
3367 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003368 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003369 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003370 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003371 STRCPY(d, "<a0>");
3372 d += 4;
3373 }
3374 else
3375 MB_COPY_CHAR(s, d);
3376 }
3377 *d = NUL;
3378}
3379
3380/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003381 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3382 * the name. Otherwise just return "start".
3383 */
3384 char_u *
3385skip_option_env_lead(char_u *start)
3386{
3387 char_u *name = start;
3388
3389 if (*start == '&')
3390 {
3391 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3392 name += 3;
3393 else
3394 name += 1;
3395 }
3396 else if (*start == '$')
3397 name += 1;
3398 return name;
3399}
3400
3401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003403 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003404 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003405 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 *
3407 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3408 * assignment without "let". Sets eap->cmdidx to the command while returning
3409 * "eap->cmd".
3410 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 * Returns NULL for an ambiguous user command.
3412 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 char_u *
3414find_ex_command(
3415 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003416 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003417 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003418 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419{
3420 int len;
3421 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003422 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003423#ifndef FEAT_EVAL
3424 int vim9 = FALSE;
3425#else
3426 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 /*
3429 * Recognize a Vim9 script function/method call and assignment:
3430 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3431 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003432 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003433 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003435 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003437 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003438 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003440 int oplen;
3441 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003442 char_u *swp;
3443
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003444 if (*eap->cmd == '&'
3445 || *eap->cmd == '$'
3446 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003447 && (valid_yank_reg(eap->cmd[1], FALSE)
3448 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003449 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003450 if (*eap->cmd == '&')
3451 {
3452 p = eap->cmd + 1;
3453 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3454 p += 2;
3455 p = to_name_end(p, FALSE);
3456 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003457 else if (*eap->cmd == '$')
3458 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003459 else
3460 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003461 if (ends_excmd(*skipwhite(p)))
3462 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003463 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3464 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003465 eap->cmdidx = CMD_eval;
3466 return eap->cmd;
3467 }
3468 // "&option" can be followed by "->" or "=", check below
3469 }
3470
3471 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003473 if (
3474 // "(..." is an expression.
3475 // "funcname(" is always a function call.
3476 *p == '('
3477 || (p == eap->cmd
3478 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003479 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003480 *eap->cmd == '{'
3481 // "'string'->func()" is an expression.
3482 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003483 // '"string"->func()' is an expression.
3484 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003485 // "g:varname" is an expression.
3486 || eap->cmd[1] == ':'
3487 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003488 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003489 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003490 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003491 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3492 {
3493 // "{" by itself is the start of a block.
3494 eap->cmdidx = CMD_block;
3495 return eap->cmd + 1;
3496 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003497 eap->cmdidx = CMD_eval;
3498 return eap->cmd;
3499 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003500
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003501 if (p != eap->cmd && (
3502 // "varname[]" is an expression.
3503 *p == '['
3504 // "varname.key" is an expression.
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02003505 || (*p == '.' && (ASCII_ISALPHA(p[1])
3506 || p[1] == '_'))))
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003507 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003508 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003509
3510 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003511 // Skip over the whole thing, it can be:
3512 // name.member = val
3513 // name[a : b] = val
3514 // name[idx] = val
3515 // name[idx].member = val
3516 // etc.
3517 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003518 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003519 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003520 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003521 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003522 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003523 || (after[0] == '.' && after[1] == '.'
3524 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003525 eap->cmdidx = CMD_var;
3526 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003527 --emsg_silent;
3528 return eap->cmd;
3529 }
3530
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003531 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3532 // an assignment.
3533 // If there is no line break inside the "[...]" then "p" is
3534 // advanced to after the "]" by to_name_const_end(): check if a "="
3535 // follows.
3536 // If "[...]" has a line break "p" still points at the "[" and it
3537 // can't be an assignment.
3538 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003539 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003540 char_u *eq;
3541
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003542 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003543 if (p == eap->cmd && *p == '[')
3544 {
3545 int count = 0;
3546 int semicolon = FALSE;
3547
3548 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3549 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003550 eq = p;
3551 if (eq != NULL)
3552 {
3553 eq = skipwhite(eq);
3554 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3555 ++eq;
3556 }
3557 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003558 {
3559 eap->cmdidx = CMD_eval;
3560 return eap->cmd;
3561 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003562 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003563 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003564 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003565 return eap->cmd;
3566 }
3567 }
3568
3569 // Recognize an assignment if we recognize the variable name:
3570 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003571 // "@r = expr"
3572 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003573 // "var = expr" where "var" is a variable name or we are skipping
3574 // (variable declaration might have been skipped).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003575 oplen = assignment_len(skipwhite(p), &heredoc);
3576 if (oplen > 0)
3577 {
3578 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3579 || *eap->cmd == '&'
3580 || *eap->cmd == '$'
3581 || *eap->cmd == '@'
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003582 || eap->skip
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003583 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003584 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003585 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003586 return eap->cmd;
3587 }
3588 }
3589
3590 // Recognize using a type for a w:, b:, t: or g: variable:
3591 // "w:varname: number = 123".
3592 if (eap->cmd[1] == ':' && *p == ':')
3593 {
3594 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003595 return eap->cmd;
3596 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003597 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003598
3599 // If it is an ID it might be a variable with an operator on the next
3600 // line, if the variable exists it can't be an Ex command.
3601 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003602 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003603 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3604 {
3605 eap->cmdidx = CMD_eval;
3606 return eap->cmd;
3607 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003608
3609 // Check for "++nr" and "--nr".
3610 if (p == eap->cmd && p[0] == p[1] && (*p == '+' || *p == '-'))
3611 {
3612 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3613 return eap->cmd + 2;
3614 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 }
3616#endif
3617
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 /*
3619 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003620 * Exceptions:
Bram Moolenaar10b94212021-02-19 21:42:57 +01003621 * - The 'k' command can directly be followed by any character.
3622 * But it is not used in Vim9 script.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003624 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003626 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 */
3628 p = eap->cmd;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003629 if (!vim9 && *p == 'k')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 {
3631 eap->cmdidx = CMD_k;
3632 ++p;
3633 }
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003634 else if (!vim9
3635 && p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003636 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3637 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 || p[1] == 'g'
3639 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3640 || p[1] == 'I'
3641 || (p[1] == 'r' && p[2] != 'e')))
3642 {
3643 eap->cmdidx = CMD_substitute;
3644 ++p;
3645 }
3646 else
3647 {
3648 while (ASCII_ISALPHA(*p))
3649 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003650 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003651 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003653 while (ASCII_ISALNUM(*p))
3654 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 }
3656 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3657 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003658 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 ++p;
3660 while (ASCII_ISALPHA(*p))
3661 ++p;
3662 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003663
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003664 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003665 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 ++p;
3667 len = (int)(p - eap->cmd);
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003668 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003669 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003670 // Check for ":dl", ":dell", etc. to ":deletel": that's
3671 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003672 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003673 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003674 break;
3675 if (i == len - 1)
3676 {
3677 --len;
3678 if (p[-1] == 'l')
3679 eap->flags |= EXFLAG_LIST;
3680 else
3681 eap->flags |= EXFLAG_PRINT;
3682 }
3683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003685 if (ASCII_ISLOWER(eap->cmd[0]))
3686 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003687 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003688 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003689
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003690 if (command_count != (int)CMD_SIZE)
3691 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003692 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003693 getout(1);
3694 }
3695
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003696 // Use a precomputed index for fast look-up in cmdnames[]
3697 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003698 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3699 if (ASCII_ISLOWER(c2))
3700 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3701 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003702 else if (ASCII_ISUPPER(eap->cmd[0]))
3703 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003705 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706
3707 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3708 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3709 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3710 (size_t)len) == 0)
3711 {
3712#ifdef FEAT_EVAL
3713 if (full != NULL
3714 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3715 *full = TRUE;
3716#endif
3717 break;
3718 }
3719
Bram Moolenaar95388e32020-11-20 21:07:00 +01003720 // Not not recognize ":*" as the star command unless '*' is in
3721 // 'cpoptions'.
3722 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3723 p = eap->cmd;
3724
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003725 // Look for a user defined command as a last resort. Let ":Print" be
3726 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003727 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3728 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003730 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 while (ASCII_ISALNUM(*p))
3732 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003733 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003735 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 eap->cmdidx = CMD_SIZE;
3737 }
3738
Bram Moolenaar373863e2020-09-26 17:20:53 +02003739 // ":fina" means ":finally" for backwards compatibility.
3740 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3741 eap->cmdidx = CMD_finally;
3742
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003743#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003744 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003745 && vim9
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +02003746 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003747 && (eap->cmdidx < 0 ||
3748 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003749 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003750 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3751
3752 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003753 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003754 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003755 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003756#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003757
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 return p;
3759}
3760
3761#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003762static struct cmdmod
3763{
3764 char *name;
3765 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003766 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003767} cmdmods[] = {
3768 {"aboveleft", 3, FALSE},
3769 {"belowright", 3, FALSE},
3770 {"botright", 2, FALSE},
3771 {"browse", 3, FALSE},
3772 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003773 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003774 {"hide", 3, FALSE},
3775 {"keepalt", 5, FALSE},
3776 {"keepjumps", 5, FALSE},
3777 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003778 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003779 {"leftabove", 5, FALSE},
3780 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003781 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003782 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003783 {"rightbelow", 6, FALSE},
3784 {"sandbox", 3, FALSE},
3785 {"silent", 3, FALSE},
3786 {"tab", 3, TRUE},
3787 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003788 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003789 {"verbose", 4, TRUE},
3790 {"vertical", 4, FALSE},
3791};
3792
3793/*
3794 * Return length of a command modifier (including optional count).
3795 * Return zero when it's not a modifier.
3796 */
3797 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003798modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003799{
3800 int i, j;
3801 char_u *p = cmd;
3802
3803 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02003804 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02003805 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003806 {
3807 for (j = 0; p[j] != NUL; ++j)
3808 if (p[j] != cmdmods[i].name[j])
3809 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003810 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003811 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003812 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003813 }
3814 return 0;
3815}
3816
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817/*
3818 * Return > 0 if an Ex command "name" exists.
3819 * Return 2 if there is an exact match.
3820 * Return 3 if there is an ambiguous match.
3821 */
3822 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003823cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824{
3825 exarg_T ea;
3826 int full = FALSE;
3827 int i;
3828 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003829 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003831 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02003832 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 {
3834 for (j = 0; name[j] != NUL; ++j)
3835 if (name[j] != cmdmods[i].name[j])
3836 break;
3837 if (name[j] == NUL && j >= cmdmods[i].minlen)
3838 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3839 }
3840
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003841 // Check built-in commands and user defined commands.
3842 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003843 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003846 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003848 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3849 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003850 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003851 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3853}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003854
3855/*
3856 * "fullcommand" function
3857 */
3858 void
3859f_fullcommand(typval_T *argvars, typval_T *rettv)
3860{
3861 exarg_T ea;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003862 char_u *name;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003863 char_u *p;
3864
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003865 rettv->v_type = VAR_STRING;
3866 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003867
3868 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
3869 return;
3870
3871 name = argvars[0].vval.v_string;
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003872 if (name == NULL)
3873 return;
3874
3875 while (*name != NUL && *name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003876 name++;
3877 name = skip_range(name, TRUE, NULL);
3878
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003879 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
3880 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003881 ea.addr_count = 0;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003882 p = find_ex_command(&ea, NULL, NULL, NULL);
3883 if (p == NULL || ea.cmdidx == CMD_SIZE)
3884 return;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003885 if (in_vim9script())
3886 {
3887 int res;
3888
3889 ++emsg_silent;
3890 res = not_in_vim9(&ea);
3891 --emsg_silent;
3892
3893 if (res == FAIL)
3894 return;
3895 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003896
3897 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02003898 ? get_user_command_name(ea.useridx, ea.cmdidx)
3899 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003900}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901#endif
3902
Bram Moolenaard0190392019-08-23 21:17:35 +02003903 cmdidx_T
3904excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905{
Bram Moolenaard0190392019-08-23 21:17:35 +02003906 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907
Bram Moolenaard0190392019-08-23 21:17:35 +02003908 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3909 idx = (cmdidx_T)((int)idx + 1))
3910 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 break;
3912
Bram Moolenaard0190392019-08-23 21:17:35 +02003913 return idx;
3914}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915
Bram Moolenaard0190392019-08-23 21:17:35 +02003916 long
3917excmd_get_argt(cmdidx_T idx)
3918{
3919 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920}
3921
3922/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003923 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 *
3925 * Backslashed delimiters after / or ? will be skipped, and commands will
3926 * not be expanded between /'s and ?'s or after "'".
3927 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003928 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 * Returns the "cmd" pointer advanced to beyond the range.
3930 */
3931 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003932skip_range(
3933 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003934 int skip_star, // skip "*" used for Visual range
3935 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003937 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003939 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003941 if (*cmd == '\\')
3942 {
3943 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3944 ++cmd;
3945 else
3946 break;
3947 }
3948 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 {
3950 if (*++cmd == NUL && ctx != NULL)
3951 *ctx = EXPAND_NOTHING;
3952 }
3953 else if (*cmd == '/' || *cmd == '?')
3954 {
3955 delim = *cmd++;
3956 while (*cmd != NUL && *cmd != delim)
3957 if (*cmd++ == '\\' && *cmd != NUL)
3958 ++cmd;
3959 if (*cmd == NUL && ctx != NULL)
3960 *ctx = EXPAND_NOTHING;
3961 }
3962 if (*cmd != NUL)
3963 ++cmd;
3964 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003965
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003966 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003967 while (*cmd == ':')
3968 cmd = skipwhite(cmd + 1);
3969
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003970 // Skip "*" used for Visual range.
3971 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3972 cmd = skipwhite(cmd + 1);
3973
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 return cmd;
3975}
3976
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003977 static void
3978addr_error(cmd_addr_T addr_type)
3979{
3980 if (addr_type == ADDR_NONE)
3981 emsg(_(e_norange));
3982 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02003983 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003984}
3985
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003987 * Return the default address for an address type.
3988 */
3989 static linenr_T
3990default_address(exarg_T *eap)
3991{
3992 linenr_T lnum = 0;
3993
3994 switch (eap->addr_type)
3995 {
3996 case ADDR_LINES:
3997 case ADDR_OTHER:
3998 // Default is the cursor line number. Avoid using an invalid
3999 // line number though.
4000 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4001 lnum = curbuf->b_ml.ml_line_count;
4002 else
4003 lnum = curwin->w_cursor.lnum;
4004 break;
4005 case ADDR_WINDOWS:
4006 lnum = CURRENT_WIN_NR;
4007 break;
4008 case ADDR_ARGUMENTS:
4009 lnum = curwin->w_arg_idx + 1;
4010 if (lnum > ARGCOUNT)
4011 lnum = ARGCOUNT;
4012 break;
4013 case ADDR_LOADED_BUFFERS:
4014 case ADDR_BUFFERS:
4015 lnum = curbuf->b_fnum;
4016 break;
4017 case ADDR_TABS:
4018 lnum = CURRENT_TAB_NR;
4019 break;
4020 case ADDR_TABS_RELATIVE:
4021 case ADDR_UNSIGNED:
4022 lnum = 1;
4023 break;
4024 case ADDR_QUICKFIX:
4025#ifdef FEAT_QUICKFIX
4026 lnum = qf_get_cur_idx(eap);
4027#endif
4028 break;
4029 case ADDR_QUICKFIX_VALID:
4030#ifdef FEAT_QUICKFIX
4031 lnum = qf_get_cur_valid_idx(eap);
4032#endif
4033 break;
4034 case ADDR_NONE:
4035 // Will give an error later if a range is found.
4036 break;
4037 }
4038 return lnum;
4039}
4040
4041/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004042 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 *
4044 * Set ptr to the next character after the part that was interpreted.
4045 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004046 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 *
4048 * Return MAXLNUM when no Ex address was found.
4049 */
4050 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004051get_address(
4052 exarg_T *eap UNUSED,
4053 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004054 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004055 int skip, // only skip the address, don't use it
4056 int silent, // no errors or side effects
4057 int to_other_file, // flag: may jump to other file
4058 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059{
4060 int c;
4061 int i;
4062 long n;
4063 char_u *cmd;
4064 pos_T pos;
4065 pos_T *fp;
4066 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004067 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068
4069 cmd = skipwhite(*ptr);
4070 lnum = MAXLNUM;
4071 do
4072 {
4073 switch (*cmd)
4074 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004075 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004076 ++cmd;
4077 switch (addr_type)
4078 {
4079 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004080 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 lnum = curwin->w_cursor.lnum;
4082 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004083 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004084 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004085 break;
4086 case ADDR_ARGUMENTS:
4087 lnum = curwin->w_arg_idx + 1;
4088 break;
4089 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004090 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004091 lnum = curbuf->b_fnum;
4092 break;
4093 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004094 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004095 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004096 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004097 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004098 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004099 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004100 cmd = NULL;
4101 goto error;
4102 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004103 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004104#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004105 lnum = qf_get_cur_idx(eap);
4106#endif
4107 break;
4108 case ADDR_QUICKFIX_VALID:
4109#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004110 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004111#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004112 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004113 }
4114 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004116 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004117 ++cmd;
4118 switch (addr_type)
4119 {
4120 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004121 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 lnum = curbuf->b_ml.ml_line_count;
4123 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004124 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004125 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004126 break;
4127 case ADDR_ARGUMENTS:
4128 lnum = ARGCOUNT;
4129 break;
4130 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004131 buf = lastbuf;
4132 while (buf->b_ml.ml_mfp == NULL)
4133 {
4134 if (buf->b_prev == NULL)
4135 break;
4136 buf = buf->b_prev;
4137 }
4138 lnum = buf->b_fnum;
4139 break;
4140 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004141 lnum = lastbuf->b_fnum;
4142 break;
4143 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004144 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004145 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004146 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004147 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004148 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004149 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004150 cmd = NULL;
4151 goto error;
4152 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004153 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004154#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004155 lnum = qf_get_size(eap);
4156 if (lnum == 0)
4157 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004158#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004159 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004160 case ADDR_QUICKFIX_VALID:
4161#ifdef FEAT_QUICKFIX
4162 lnum = qf_get_valid_size(eap);
4163 if (lnum == 0)
4164 lnum = 1;
4165#endif
4166 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004167 }
4168 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004170 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004171 if (*++cmd == NUL)
4172 {
4173 cmd = NULL;
4174 goto error;
4175 }
4176 if (addr_type != ADDR_LINES)
4177 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004178 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004179 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004180 goto error;
4181 }
4182 if (skip)
4183 ++cmd;
4184 else
4185 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004186 // Only accept a mark in another file when it is
4187 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004188 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4189 ++cmd;
4190 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004191 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004192 lnum = curwin->w_cursor.lnum;
4193 else
4194 {
4195 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 {
4197 cmd = NULL;
4198 goto error;
4199 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004200 lnum = fp->lnum;
4201 }
4202 }
4203 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204
4205 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004206 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004207 c = *cmd++;
4208 if (addr_type != ADDR_LINES)
4209 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004210 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004211 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004212 goto error;
4213 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004214 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004215 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004216 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004217 if (*cmd == c)
4218 ++cmd;
4219 }
4220 else
4221 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004222 int flags;
4223
4224 pos = curwin->w_cursor; // save curwin->w_cursor
4225
4226 // When '/' or '?' follows another address, start from
4227 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004228 if (lnum != MAXLNUM)
4229 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004230
4231 // Start a forward search at the end of the line (unless
4232 // before the first line).
4233 // Start a backward search at the start of the line.
4234 // This makes sure we never match in the current
4235 // line, and can match anywhere in the
4236 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004237 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004238 curwin->w_cursor.col = MAXCOL;
4239 else
4240 curwin->w_cursor.col = 0;
4241 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004242 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004243 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004244 {
4245 curwin->w_cursor = pos;
4246 cmd = NULL;
4247 goto error;
4248 }
4249 lnum = curwin->w_cursor.lnum;
4250 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004251 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004252 cmd += searchcmdlen;
4253 }
4254 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004256 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004257 ++cmd;
4258 if (addr_type != ADDR_LINES)
4259 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004260 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004261 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004262 goto error;
4263 }
4264 if (*cmd == '&')
4265 i = RE_SUBST;
4266 else if (*cmd == '?' || *cmd == '/')
4267 i = RE_SEARCH;
4268 else
4269 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004270 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004271 cmd = NULL;
4272 goto error;
4273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004275 if (!skip)
4276 {
4277 /*
4278 * When search follows another address, start from
4279 * there.
4280 */
4281 if (lnum != MAXLNUM)
4282 pos.lnum = lnum;
4283 else
4284 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004286 /*
4287 * Start the search just like for the above
4288 * do_search().
4289 */
4290 if (*cmd != '?')
4291 pos.col = MAXCOL;
4292 else
4293 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004294 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004295 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004296 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004297 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004298 lnum = pos.lnum;
4299 else
4300 {
4301 cmd = NULL;
4302 goto error;
4303 }
4304 }
4305 ++cmd;
4306 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
4308 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004309 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004310 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 }
4312
4313 for (;;)
4314 {
4315 cmd = skipwhite(cmd);
4316 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4317 break;
4318
4319 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004320 {
4321 switch (addr_type)
4322 {
4323 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004324 case ADDR_OTHER:
4325 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004326 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004327 break;
4328 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004329 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004330 break;
4331 case ADDR_ARGUMENTS:
4332 lnum = curwin->w_arg_idx + 1;
4333 break;
4334 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004335 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004336 lnum = curbuf->b_fnum;
4337 break;
4338 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004339 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004340 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004341 case ADDR_TABS_RELATIVE:
4342 lnum = 1;
4343 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004344 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004345#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004346 lnum = qf_get_cur_idx(eap);
4347#endif
4348 break;
4349 case ADDR_QUICKFIX_VALID:
4350#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004351 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004352#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004353 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004354 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004355 case ADDR_UNSIGNED:
4356 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004357 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004358 }
4359 }
4360
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004362 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 else
4364 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004365 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 n = 1;
4367 else
4368 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004369
4370 if (addr_type == ADDR_TABS_RELATIVE)
4371 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004372 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004373 cmd = NULL;
4374 goto error;
4375 }
4376 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004377 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004378 lnum = compute_buffer_local_count(
4379 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004381 {
4382#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004383 // Relative line addressing, need to adjust for folded lines
4384 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004385 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4386 && address_count >= 2)
4387 (void)hasFolding(lnum, NULL, &lnum);
4388#endif
4389 if (i == '-')
4390 lnum -= n;
4391 else
4392 lnum += n;
4393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 }
4395 } while (*cmd == '/' || *cmd == '?');
4396
4397error:
4398 *ptr = cmd;
4399 return lnum;
4400}
4401
4402/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004403 * Set eap->line1 and eap->line2 to the whole range.
4404 * Used for commands with the EX_DFLALL flag and no range given.
4405 */
4406 static void
4407address_default_all(exarg_T *eap)
4408{
4409 eap->line1 = 1;
4410 switch (eap->addr_type)
4411 {
4412 case ADDR_LINES:
4413 case ADDR_OTHER:
4414 eap->line2 = curbuf->b_ml.ml_line_count;
4415 break;
4416 case ADDR_LOADED_BUFFERS:
4417 {
4418 buf_T *buf = firstbuf;
4419
4420 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4421 buf = buf->b_next;
4422 eap->line1 = buf->b_fnum;
4423 buf = lastbuf;
4424 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4425 buf = buf->b_prev;
4426 eap->line2 = buf->b_fnum;
4427 }
4428 break;
4429 case ADDR_BUFFERS:
4430 eap->line1 = firstbuf->b_fnum;
4431 eap->line2 = lastbuf->b_fnum;
4432 break;
4433 case ADDR_WINDOWS:
4434 eap->line2 = LAST_WIN_NR;
4435 break;
4436 case ADDR_TABS:
4437 eap->line2 = LAST_TAB_NR;
4438 break;
4439 case ADDR_TABS_RELATIVE:
4440 eap->line2 = 1;
4441 break;
4442 case ADDR_ARGUMENTS:
4443 if (ARGCOUNT == 0)
4444 eap->line1 = eap->line2 = 0;
4445 else
4446 eap->line2 = ARGCOUNT;
4447 break;
4448 case ADDR_QUICKFIX_VALID:
4449#ifdef FEAT_QUICKFIX
4450 eap->line2 = qf_get_valid_size(eap);
4451 if (eap->line2 == 0)
4452 eap->line2 = 1;
4453#endif
4454 break;
4455 case ADDR_NONE:
4456 case ADDR_UNSIGNED:
4457 case ADDR_QUICKFIX:
4458 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4459 break;
4460 }
4461}
4462
4463
4464/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004465 * Get flags from an Ex command argument.
4466 */
4467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004468get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004469{
4470 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4471 {
4472 if (*eap->arg == 'l')
4473 eap->flags |= EXFLAG_LIST;
4474 else if (*eap->arg == 'p')
4475 eap->flags |= EXFLAG_PRINT;
4476 else
4477 eap->flags |= EXFLAG_NR;
4478 eap->arg = skipwhite(eap->arg + 1);
4479 }
4480}
4481
4482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 * Function called for command which is Not Implemented. NI!
4484 */
4485 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004486ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487{
4488 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004489 eap->errmsg =
4490 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491}
4492
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004493#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494/*
4495 * Function called for script command which is Not Implemented. NI!
4496 * Skips over ":perl <<EOF" constructs.
4497 */
4498 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004499ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500{
4501 if (!eap->skip)
4502 ex_ni(eap);
4503 else
4504 vim_free(script_get(eap, eap->arg));
4505}
4506#endif
4507
4508/*
4509 * Check range in Ex command for validity.
4510 * Return NULL when valid, error message when invalid.
4511 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004512 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004513invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004515 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004516
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 if ( eap->line1 < 0
4518 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004519 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004520 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004521
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004522 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004523 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004524 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004525 {
4526 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004527 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004528#ifdef FEAT_DIFF
4529 + (eap->cmdidx == CMD_diffget)
4530#endif
4531 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004532 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004533 break;
4534 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004535 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004536 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004537 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004538 break;
4539 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004540 // Only a boundary check, not whether the buffers actually
4541 // exist.
4542 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004543 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004544 break;
4545 case ADDR_LOADED_BUFFERS:
4546 buf = firstbuf;
4547 while (buf->b_ml.ml_mfp == NULL)
4548 {
4549 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004550 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004551 buf = buf->b_next;
4552 }
4553 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004554 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004555 buf = lastbuf;
4556 while (buf->b_ml.ml_mfp == NULL)
4557 {
4558 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004559 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004560 buf = buf->b_prev;
4561 }
4562 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004563 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004564 break;
4565 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004566 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004567 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004568 break;
4569 case ADDR_TABS:
4570 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004571 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004572 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004573 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004574 case ADDR_OTHER:
4575 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004576 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004577 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004578#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004579 // No error for value that is too big, will use the last entry.
4580 if (eap->line2 <= 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004581 return _(e_invalid_range);
Bram Moolenaare906c502015-09-09 21:10:39 +02004582#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004583 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004584 case ADDR_QUICKFIX_VALID:
4585#ifdef FEAT_QUICKFIX
4586 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4587 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004588 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004589#endif
4590 break;
4591 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004592 case ADDR_NONE:
4593 // Will give an error elsewhere.
4594 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004595 }
4596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 return NULL;
4598}
4599
4600/*
4601 * Correct the range for zero line number, if required.
4602 */
4603 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004604correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004606 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 {
4608 if (eap->line1 == 0)
4609 eap->line1 = 1;
4610 if (eap->line2 == 0)
4611 eap->line2 = 1;
4612 }
4613}
4614
Bram Moolenaar748bf032005-02-02 23:04:36 +00004615#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004616/*
4617 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4618 * pattern. Otherwise return eap->arg.
4619 */
4620 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004621skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004622{
4623 char_u *p = eap->arg;
4624
Bram Moolenaara37420f2006-02-04 22:37:47 +00004625 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4626 || eap->cmdidx == CMD_vimgrepadd
4627 || eap->cmdidx == CMD_lvimgrepadd
4628 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004629 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004630 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004631 if (p == NULL)
4632 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004633 }
4634 return p;
4635}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004636
4637/*
4638 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4639 * in the command line, so that things like % get expanded.
4640 */
4641 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004642replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004643{
4644 char_u *new_cmdline;
4645 char_u *program;
4646 char_u *pos;
4647 char_u *ptr;
4648 int len;
4649 int i;
4650
4651 /*
4652 * Don't do it when ":vimgrep" is used for ":grep".
4653 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004654 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4655 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4656 || eap->cmdidx == CMD_grepadd
4657 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004658 && !grep_internal(eap->cmdidx))
4659 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004660 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4661 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004662 {
4663 if (*curbuf->b_p_gp == NUL)
4664 program = p_gp;
4665 else
4666 program = curbuf->b_p_gp;
4667 }
4668 else
4669 {
4670 if (*curbuf->b_p_mp == NUL)
4671 program = p_mp;
4672 else
4673 program = curbuf->b_p_mp;
4674 }
4675
4676 p = skipwhite(p);
4677
4678 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4679 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004680 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004681 i = 1;
4682 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4683 ++i;
4684 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004685 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004686 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004687 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004688 ptr = new_cmdline;
4689 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4690 {
4691 i = (int)(pos - program);
4692 STRNCPY(ptr, program, i);
4693 STRCPY(ptr += i, p);
4694 ptr += len;
4695 program = pos + 2;
4696 }
4697 STRCPY(ptr, program);
4698 }
4699 else
4700 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004701 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004702 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004703 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004704 STRCPY(new_cmdline, program);
4705 STRCAT(new_cmdline, " ");
4706 STRCAT(new_cmdline, p);
4707 }
4708 msg_make(p);
4709
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004710 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004711 vim_free(*cmdlinep);
4712 *cmdlinep = new_cmdline;
4713 p = new_cmdline;
4714 }
4715 return p;
4716}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004717#endif
4718
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719/*
4720 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004721 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 * Return FAIL for failure, OK otherwise.
4723 */
4724 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004725expand_filename(
4726 exarg_T *eap,
4727 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004728 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004730 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 char_u *repl;
4732 int srclen;
4733 char_u *p;
4734 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004735 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736
Bram Moolenaar748bf032005-02-02 23:04:36 +00004737#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004738 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004739 p = skip_grep_pat(eap);
4740#else
4741 p = eap->arg;
4742#endif
4743
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 /*
4745 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4746 * the file name contains a wildcard it should not cause expanding.
4747 * (it will be expanded anyway if there is a wildcard before replacing).
4748 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004749 has_wildcards = mch_has_wildcard(p);
4750 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004752#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004753 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004754 if (p[0] == '`' && p[1] == '=')
4755 {
4756 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004757 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004758 if (*p == '`')
4759 ++p;
4760 continue;
4761 }
4762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 /*
4764 * Quick check if this cannot be the start of a special string.
4765 * Also removes backslash before '%', '#' and '<'.
4766 */
4767 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4768 {
4769 ++p;
4770 continue;
4771 }
4772
4773 /*
4774 * Try to find a match at this position.
4775 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004776 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4777 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004778 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004780 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 {
4782 p += srclen;
4783 continue;
4784 }
4785
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004786 // Wildcards won't be expanded below, the replacement is taken
4787 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004788 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4789 {
4790 char_u *l = repl;
4791
4792 repl = expand_env_save(repl);
4793 vim_free(l);
4794 }
4795
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004796 // Need to escape white space et al. with a backslash.
4797 // Don't do this for:
4798 // - replacement that already has been escaped: "##"
4799 // - shell commands (may have to use quotes instead).
4800 // - non-unix systems when there is a single argument (spaces don't
4801 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004803 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 && eap->cmdidx != CMD_grep
4806 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004807 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004808 && eap->cmdidx != CMD_lgrep
4809 && eap->cmdidx != CMD_lgrepadd
4810 && eap->cmdidx != CMD_lmake
4811 && eap->cmdidx != CMD_make
4812 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004814 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815#endif
4816 )
4817 {
4818 char_u *l;
4819#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004820 // Don't escape a backslash here, because rem_backslash() doesn't
4821 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 static char_u *nobslash = (char_u *)" \t\"|";
4823# define ESCAPE_CHARS nobslash
4824#else
4825# define ESCAPE_CHARS escape_chars
4826#endif
4827
4828 for (l = repl; *l; ++l)
4829 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4830 {
4831 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4832 if (l != NULL)
4833 {
4834 vim_free(repl);
4835 repl = l;
4836 }
4837 break;
4838 }
4839 }
4840
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004841 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004842 if ((eap->usefilter || eap->cmdidx == CMD_bang
4843 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004844 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 {
4846 char_u *l;
4847
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004848 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 if (l != NULL)
4850 {
4851 vim_free(repl);
4852 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
4854 }
4855
4856 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4857 vim_free(repl);
4858 if (p == NULL)
4859 return FAIL;
4860 }
4861
4862 /*
4863 * One file argument: Expand wildcards.
4864 * Don't do this with ":r !command" or ":w !command".
4865 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004866 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 {
4868 /*
4869 * May do this twice:
4870 * 1. Replace environment variables.
4871 * 2. Replace any other wildcards, remove backslashes.
4872 */
4873 for (n = 1; n <= 2; ++n)
4874 {
4875 if (n == 2)
4876 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 /*
4878 * Halve the number of backslashes (this is Vi compatible).
4879 * For Unix and OS/2, when wildcards are expanded, this is
4880 * done by ExpandOne() below.
4881 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004882#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 if (!has_wildcards)
4884#endif
4885 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 }
4887
4888 if (has_wildcards)
4889 {
4890 if (n == 1)
4891 {
4892 /*
4893 * First loop: May expand environment variables. This
4894 * can be done much faster with expand_env() than with
4895 * something else (e.g., calling a shell).
4896 * After expanding environment variables, check again
4897 * if there are still wildcards present.
4898 */
4899 if (vim_strchr(eap->arg, '$') != NULL
4900 || vim_strchr(eap->arg, '~') != NULL)
4901 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004902 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004903 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 has_wildcards = mch_has_wildcard(NameBuff);
4905 p = NameBuff;
4906 }
4907 else
4908 p = NULL;
4909 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004910 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 {
4912 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004913 int options = WILD_LIST_NOTFOUND
4914 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915
4916 ExpandInit(&xpc);
4917 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004918 if (p_wic)
4919 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004921 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 if (p == NULL)
4923 return FAIL;
4924 }
4925 if (p != NULL)
4926 {
4927 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4928 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004929 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 vim_free(p);
4931 }
4932 }
4933 }
4934 }
4935 return OK;
4936}
4937
4938/*
4939 * Replace part of the command line, keeping eap->cmd, eap->arg and
4940 * eap->nextcmd correct.
4941 * "src" points to the part that is to be replaced, of length "srclen".
4942 * "repl" is the replacement string.
4943 * Returns a pointer to the character after the replaced string.
4944 * Returns NULL for failure.
4945 */
4946 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004947repl_cmdline(
4948 exarg_T *eap,
4949 char_u *src,
4950 int srclen,
4951 char_u *repl,
4952 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953{
4954 int len;
4955 int i;
4956 char_u *new_cmdline;
4957
4958 /*
4959 * The new command line is build in new_cmdline[].
4960 * First allocate it.
4961 * Careful: a "+cmd" argument may have been NUL terminated.
4962 */
4963 len = (int)STRLEN(repl);
4964 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004965 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004966 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004967 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004968 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969
4970 /*
4971 * Copy the stuff before the expanded part.
4972 * Copy the expanded stuff.
4973 * Copy what came after the expanded part.
4974 * Copy the next commands, if there are any.
4975 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004976 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004978
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004980 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004982 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004984 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
4986 i = (int)STRLEN(new_cmdline) + 1;
4987 STRCPY(new_cmdline + i, eap->nextcmd);
4988 eap->nextcmd = new_cmdline + i;
4989 }
4990 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4991 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4992 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4993 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4994 vim_free(*cmdlinep);
4995 *cmdlinep = new_cmdline;
4996
4997 return src;
4998}
4999
5000/*
5001 * Check for '|' to separate commands and '"' to start comments.
5002 */
5003 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005004separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005{
5006 char_u *p;
5007
Bram Moolenaar86b68352004-12-27 21:59:20 +00005008#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005009 p = skip_grep_pat(eap);
5010#else
5011 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005012#endif
5013
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005014 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 {
5016 if (*p == Ctrl_V)
5017 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005018 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005019 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005021 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005022 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005023 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 break;
5025 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005026
5027#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005028 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005029 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005030 {
5031 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005032 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005033 if (*p == NUL) // stop at NUL after CTRL-V
5034 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005035 }
5036#endif
5037
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005038 // Check for '"': start of comment or '|': next command
5039 // :@" and :*" do not start a comment!
5040 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005041 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02005042#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005043 && !in_vim9script()
5044#endif
5045 && !(eap->argt & EX_NOTRLCOM)
5046 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5047 || p != eap->arg)
5048 && (eap->cmdidx != CMD_redir
5049 || p != eap->arg + 1 || p[-1] != '@'))
5050#ifdef FEAT_EVAL
5051 || (*p == '#'
5052 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005053 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005054 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02005055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 || *p == '|' || *p == '\n')
5057 {
5058 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005059 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 * AND 'b' is present in 'cpoptions'.
5061 */
5062 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005063 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005065 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 --p;
5067 }
5068 else
5069 {
5070 eap->nextcmd = check_nextcmd(p);
5071 *p = NUL;
5072 break;
5073 }
5074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005076
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005077 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 del_trailing_spaces(eap->arg);
5079}
5080
5081/*
5082 * get + command from ex argument
5083 */
5084 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005085getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086{
5087 char_u *arg = *argp;
5088 char_u *command = NULL;
5089
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005090 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 {
5092 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005093 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 command = dollar_command;
5095 else
5096 {
5097 command = arg;
5098 arg = skip_cmd_arg(command, TRUE);
5099 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005100 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 }
5102
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005103 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 *argp = arg;
5105 }
5106 return command;
5107}
5108
5109/*
5110 * Find end of "+command" argument. Skip over "\ " and "\\".
5111 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005112 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005113skip_cmd_arg(
5114 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005115 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116{
5117 while (*p && !vim_isspace(*p))
5118 {
5119 if (*p == '\\' && p[1] != NUL)
5120 {
5121 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005122 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 else
5124 ++p;
5125 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005126 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
5128 return p;
5129}
5130
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005131 int
5132get_bad_opt(char_u *p, exarg_T *eap)
5133{
5134 if (STRICMP(p, "keep") == 0)
5135 eap->bad_char = BAD_KEEP;
5136 else if (STRICMP(p, "drop") == 0)
5137 eap->bad_char = BAD_DROP;
5138 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5139 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005140 else
5141 return FAIL;
5142 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005143}
5144
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145/*
5146 * Get "++opt=arg" argument.
5147 * Return FAIL or OK.
5148 */
5149 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005150getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151{
5152 char_u *arg = eap->arg + 2;
5153 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005154 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005157 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5159 {
5160 if (*arg == 'n')
5161 {
5162 arg += 2;
5163 eap->force_bin = FORCE_NOBIN;
5164 }
5165 else
5166 eap->force_bin = FORCE_BIN;
5167 if (!checkforcmd(&arg, "binary", 3))
5168 return FAIL;
5169 eap->arg = skipwhite(arg);
5170 return OK;
5171 }
5172
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005173 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005174 if (STRNCMP(arg, "edit", 4) == 0)
5175 {
5176 eap->read_edit = TRUE;
5177 eap->arg = skipwhite(arg + 4);
5178 return OK;
5179 }
5180
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 if (STRNCMP(arg, "ff", 2) == 0)
5182 {
5183 arg += 2;
5184 pp = &eap->force_ff;
5185 }
5186 else if (STRNCMP(arg, "fileformat", 10) == 0)
5187 {
5188 arg += 10;
5189 pp = &eap->force_ff;
5190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 else if (STRNCMP(arg, "enc", 3) == 0)
5192 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005193 if (STRNCMP(arg, "encoding", 8) == 0)
5194 arg += 8;
5195 else
5196 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 pp = &eap->force_enc;
5198 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005199 else if (STRNCMP(arg, "bad", 3) == 0)
5200 {
5201 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005202 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204
5205 if (pp == NULL || *arg != '=')
5206 return FAIL;
5207
5208 ++arg;
5209 *pp = (int)(arg - eap->cmd);
5210 arg = skip_cmd_arg(arg, FALSE);
5211 eap->arg = skipwhite(arg);
5212 *arg = NUL;
5213
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 if (pp == &eap->force_ff)
5215 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5217 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005218 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005220 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005222 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5224 *p = TOLOWER_ASC(*p);
5225 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005226 else
5227 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005228 // Check ++bad= argument. Must be a single-byte character, "keep" or
5229 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005230 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005231 return FAIL;
5232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233
5234 return OK;
5235}
5236
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005238ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239{
5240 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005241 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 * directory for security reasons.
5243 */
5244 if (secure)
5245 {
5246 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005247 eap->errmsg =
5248 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
5250 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005251 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 else
5253 do_augroup(eap->arg, eap->forceit);
5254}
5255
5256/*
5257 * ":doautocmd": Apply the automatic commands to the current buffer.
5258 */
5259 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005260ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005262 char_u *arg = eap->arg;
5263 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005264 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005265
Bram Moolenaar1610d052016-06-09 22:53:01 +02005266 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005267 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005268 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005269 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272/*
5273 * :[N]bunload[!] [N] [bufname] unload buffer
5274 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5275 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5276 */
5277 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005278ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005280 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005281 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 eap->errmsg = do_bufdel(
5283 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5284 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5285 : DOBUF_UNLOAD, eap->arg,
5286 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5287}
5288
5289/*
5290 * :[N]buffer [N] to buffer N
5291 * :[N]sbuffer [N] to buffer N
5292 */
5293 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005294ex_buffer(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 Moolenaar815b76b2019-06-01 14:15:52 +02005297 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005299 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 else
5301 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005302 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5304 else
5305 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005306 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005307 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 }
5309}
5310
5311/*
5312 * :[N]bmodified [N] to next mod. buffer
5313 * :[N]sbmodified [N] to next mod. buffer
5314 */
5315 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005316ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
5318 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005319 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005320 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321}
5322
5323/*
5324 * :[N]bnext [N] to next buffer
5325 * :[N]sbnext [N] split and to next buffer
5326 */
5327 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005328ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005330 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005331 return;
5332
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005334 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005335 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336}
5337
5338/*
5339 * :[N]bNext [N] to previous buffer
5340 * :[N]bprevious [N] to previous buffer
5341 * :[N]sbNext [N] split and to previous buffer
5342 * :[N]sbprevious [N] split and to previous buffer
5343 */
5344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005345ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005347 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005348 return;
5349
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005351 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005352 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353}
5354
5355/*
5356 * :brewind to first buffer
5357 * :bfirst to first buffer
5358 * :sbrewind split and to first buffer
5359 * :sbfirst split and to first buffer
5360 */
5361 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005362ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005364 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005365 return;
5366
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005368 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005369 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370}
5371
5372/*
5373 * :blast to last buffer
5374 * :sblast split and to last buffer
5375 */
5376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005377ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005379 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005380 return;
5381
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005383 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005384 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386
Bram Moolenaar7a092242020-04-16 22:10:49 +02005387/*
5388 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005389 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005390 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005392ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005394 int comment_char = '"';
5395
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005396#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005397 if (in_vim9script())
5398 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005399#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005400 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005401}
5402
5403/*
5404 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5405 * to "cmd_start" or has a white space character before it.
5406 */
5407 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005408ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005409{
5410 int c = *cmd;
5411
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005412 if (c == NUL || c == '|' || c == '\n')
5413 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005414#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005415 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005416 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5417 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005418 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005419#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005420 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421}
5422
5423#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5424 || defined(PROTO)
5425/*
5426 * Return the next command, after the first '|' or '\n'.
5427 * Return NULL if not found.
5428 */
5429 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005430find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431{
5432 while (*p != '|' && *p != '\n')
5433 {
5434 if (*p == NUL)
5435 return NULL;
5436 ++p;
5437 }
5438 return (p + 1);
5439}
5440#endif
5441
5442/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005443 * Check if *p is a separator between Ex commands, skipping over white space.
5444 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 */
5446 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005447check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005449 char_u *s = skipwhite(p);
5450
5451 if (*s == '|' || *s == '\n')
5452 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 else
5454 return NULL;
5455}
5456
5457/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005458 * If "eap->nextcmd" is not set, check for a next command at "p".
5459 */
5460 void
5461set_nextcmd(exarg_T *eap, char_u *arg)
5462{
5463 char_u *p = check_nextcmd(arg);
5464
5465 if (eap->nextcmd == NULL)
5466 eap->nextcmd = p;
5467 else if (p != NULL)
5468 // cannot use "| command" inside a {} block
5469 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5470}
5471
5472/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 * - if there are more files to edit
5474 * - and this is the last window
5475 * - and forceit not used
5476 * - and not repeated twice on a row
5477 * return FAIL and give error message if 'message' TRUE
5478 * return OK otherwise
5479 */
5480 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005481check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005482 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005483 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484{
5485 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5486
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005487 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005488 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 {
5490 if (message)
5491 {
5492#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005493 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5494 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005496 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005498 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5499 NGETTEXT("%d more file to edit. Quit anyway?",
5500 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5502 return OK;
5503 return FAIL;
5504 }
5505#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005506 semsg(NGETTEXT("E173: %d more file to edit",
5507 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005508 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 }
5510 return FAIL;
5511 }
5512 return OK;
5513}
5514
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515/*
5516 * Function given to ExpandGeneric() to obtain the list of command names.
5517 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005519get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520{
5521 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005522 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 return cmdnames[idx].cmd_name;
5524}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005527ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528{
Bram Moolenaare6850792010-05-14 15:28:44 +02005529 if (*eap->arg == NUL)
5530 {
5531#ifdef FEAT_EVAL
5532 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5533 char_u *p = NULL;
5534
5535 if (expr != NULL)
5536 {
5537 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005538 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005539 --emsg_off;
5540 vim_free(expr);
5541 }
5542 if (p != NULL)
5543 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005544 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005545 vim_free(p);
5546 }
5547 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005548 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005549#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005550 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005551#endif
5552 }
5553 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005554 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005555
5556#ifdef FEAT_VTP
5557 else if (has_vtp_working())
5558 {
5559 // background color change requires clear + redraw
5560 update_screen(CLEAR);
5561 redrawcmd();
5562 }
5563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564}
5565
5566 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005567ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568{
5569 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005570 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 do_highlight(eap->arg, eap->forceit, FALSE);
5572}
5573
5574
5575/*
5576 * Call this function if we thought we were going to exit, but we won't
5577 * (because of an error). May need to restore the terminal mode.
5578 */
5579 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005580not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
5582 exiting = FALSE;
5583 settmode(TMODE_RAW);
5584}
5585
Bram Moolenaar3552e742021-05-29 12:21:58 +02005586 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005587before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5588{
5589 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5590
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005591 // Bail out when autocommands closed the window.
5592 // Refuse to quit when the buffer in the last window is being closed (can
5593 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005594 if (!win_valid(wp)
5595 || curbuf_locked()
5596 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5597 return TRUE;
5598
5599 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5600 {
5601 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005602 // Refuse to quit when locked or when the window was closed or the
5603 // buffer in the last window is being closed (can only happen in
5604 // autocommands).
5605 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005606 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5607 return TRUE;
5608 }
5609
5610 return FALSE;
5611}
5612
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005614 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005615 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005616 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005618 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005619ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005621 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005622
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623#ifdef FEAT_CMDWIN
5624 if (cmdwin_type != 0)
5625 {
5626 cmdwin_result = Ctrl_C;
5627 return;
5628 }
5629#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005630 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005631 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005632 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005633 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005634 return;
5635 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005636 if (eap->addr_count > 0)
5637 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005638 int wnr = eap->line2;
5639
5640 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5641 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005642 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005643 }
5644 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005645 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005646
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005647 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005648 if (curbuf_locked())
5649 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005650
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005651 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005652 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005653 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654
5655#ifdef FEAT_NETBEANS_INTG
5656 netbeansForcedQuit = eap->forceit;
5657#endif
5658
5659 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02005660 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 */
5662 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5663 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005664 if ((!buf_hide(wp->w_buffer)
5665 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005666 | (eap->forceit ? CCGD_FORCEIT : 0)
5667 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005669 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 {
5671 not_exiting();
5672 }
5673 else
5674 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005675 // quit last window
5676 // Note: only_one_window() returns true, even so a help window is
5677 // still open. In that case only quit, if no address has been
5678 // specified. Example:
5679 // :h|wincmd w|1q - don't quit
5680 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005681 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005683 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005684#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005686#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005687 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005688 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 }
5690}
5691
5692/*
5693 * ":cquit".
5694 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005696ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005698 // this does not always pass on the exit code to the Manx compiler. why?
5699 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700}
5701
5702/*
5703 * ":qall": try to quit all windows
5704 */
5705 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005706ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707{
5708# ifdef FEAT_CMDWIN
5709 if (cmdwin_type != 0)
5710 {
5711 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005712 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 else
5714 cmdwin_result = K_XF2;
5715 return;
5716 }
5717# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005718
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005719 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005720 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005721 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005722 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005723 return;
5724 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005725
5726 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005727 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005728
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005730 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 getout(0);
5732 not_exiting();
5733}
5734
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735/*
5736 * ":close": close current window, unless it is the last one
5737 */
5738 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005739ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005741 win_T *win;
5742 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005743#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005745 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005747#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005748 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005749 {
5750 if (eap->addr_count == 0)
5751 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005752 else
5753 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005754 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005755 {
5756 winnr++;
5757 if (winnr == eap->line2)
5758 break;
5759 }
5760 if (win == NULL)
5761 win = lastwin;
5762 ex_win_close(eap->forceit, win, NULL);
5763 }
5764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765}
5766
Bram Moolenaar4033c552017-09-16 20:54:51 +02005767#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005768/*
5769 * ":pclose": Close any preview window.
5770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005772ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005773{
5774 win_T *win;
5775
Bram Moolenaar79648732019-07-18 21:43:07 +02005776 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005777 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005778 if (win->w_p_pvw)
5779 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005780 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005781 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005782 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005783# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005784 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005785 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005786# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005787}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005788#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005789
Bram Moolenaarf740b292006-02-16 22:11:02 +00005790/*
5791 * Close window "win" and take care of handling closing the last window for a
5792 * modified buffer.
5793 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005794 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005795ex_win_close(
5796 int forceit,
5797 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005798 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
5800 int need_hide;
5801 buf_T *buf = win->w_buffer;
5802
Bram Moolenaarcf844172020-06-26 19:44:06 +02005803 // Never close the autocommand window.
5804 if (win == aucmd_win)
5805 {
5806 emsg(_(e_autocmd_close));
5807 return;
5808 }
5809
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005811 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005813#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005814 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005816 bufref_T bufref;
5817
5818 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005820 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 return;
5822 need_hide = FALSE;
5823 }
5824 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005827 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 return;
5829 }
5830 }
5831
Bram Moolenaar4033c552017-09-16 20:54:51 +02005832#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005834#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005835
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005836 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005837 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005838 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005839 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005840 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841}
5842
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005844 * Handle the argument for a tabpage related ex command.
5845 * Returns a tabpage number.
5846 * When an error is encountered then eap->errmsg is set.
5847 */
5848 static int
5849get_tabpage_arg(exarg_T *eap)
5850{
5851 int tab_number;
5852 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5853
5854 if (eap->arg && *eap->arg != NUL)
5855 {
5856 char_u *p = eap->arg;
5857 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005858 int relative = 0; // argument +N/-N means: go to N places to the
5859 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005860
5861 if (*p == '-')
5862 {
5863 relative = -1;
5864 p++;
5865 }
5866 else if (*p == '+')
5867 {
5868 relative = 1;
5869 p++;
5870 }
5871
5872 p_save = p;
5873 tab_number = getdigits(&p);
5874
5875 if (relative == 0)
5876 {
5877 if (STRCMP(p, "$") == 0)
5878 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005879 else if (STRCMP(p, "#") == 0)
5880 if (valid_tabpage(lastused_tabpage))
5881 tab_number = tabpage_index(lastused_tabpage);
5882 else
5883 {
5884 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5885 tab_number = 0;
5886 goto theend;
5887 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005888 else if (p == p_save || *p_save == '-' || *p != NUL
5889 || tab_number > LAST_TAB_NR)
5890 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005891 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005892 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005893 goto theend;
5894 }
5895 }
5896 else
5897 {
5898 if (*p_save == NUL)
5899 tab_number = 1;
5900 else if (p == p_save || *p_save == '-' || *p != NUL
5901 || tab_number == 0)
5902 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005903 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005904 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005905 goto theend;
5906 }
5907 tab_number = tab_number * relative + tabpage_index(curtab);
5908 if (!unaccept_arg0 && relative == -1)
5909 --tab_number;
5910 }
5911 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005912 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005913 }
5914 else if (eap->addr_count > 0)
5915 {
5916 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005917 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005918 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005919 tab_number = 0;
5920 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005921 else
5922 {
5923 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005924 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005925 {
5926 --tab_number;
5927 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005928 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01005929 }
5930 }
5931 }
5932 else
5933 {
5934 switch (eap->cmdidx)
5935 {
5936 case CMD_tabnext:
5937 tab_number = tabpage_index(curtab) + 1;
5938 if (tab_number > LAST_TAB_NR)
5939 tab_number = 1;
5940 break;
5941 case CMD_tabmove:
5942 tab_number = LAST_TAB_NR;
5943 break;
5944 default:
5945 tab_number = tabpage_index(curtab);
5946 }
5947 }
5948
5949theend:
5950 return tab_number;
5951}
5952
5953/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005954 * ":tabclose": close current tab page, unless it is the last one.
5955 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 */
5957 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005958ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005960 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005961 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005962
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005963# ifdef FEAT_CMDWIN
5964 if (cmdwin_type != 0)
5965 cmdwin_result = K_IGNORE;
5966 else
5967# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005968 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005969 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005970 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005972 tab_number = get_tabpage_arg(eap);
5973 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005974 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005975 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005976 if (tp == NULL)
5977 {
5978 beep_flush();
5979 return;
5980 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005981 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005982 {
5983 tabpage_close_other(tp, eap->forceit);
5984 return;
5985 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005986 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005987 tabpage_close(eap->forceit);
5988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005990}
5991
5992/*
5993 * ":tabonly": close all tab pages except the current one
5994 */
5995 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005996ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005997{
5998 tabpage_T *tp;
5999 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006000 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006001
6002# ifdef FEAT_CMDWIN
6003 if (cmdwin_type != 0)
6004 cmdwin_result = K_IGNORE;
6005 else
6006# endif
6007 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006008 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006009 else
6010 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006011 tab_number = get_tabpage_arg(eap);
6012 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006013 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006014 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006015 // Repeat this up to a 1000 times, because autocommands may
6016 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006017 for (done = 0; done < 1000; ++done)
6018 {
6019 FOR_ALL_TABPAGES(tp)
6020 if (tp->tp_topframe != topframe)
6021 {
6022 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006023 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006024 if (valid_tabpage(tp))
6025 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006026 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006027 break;
6028 }
6029 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006030 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006031 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006032 }
6033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035
6036/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006037 * Close the current tab page.
6038 */
6039 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006040tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006041{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006042 // First close all the windows but the current one. If that worked then
6043 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006044 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006045 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006046 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006047 ex_win_close(forceit, curwin, NULL);
6048# ifdef FEAT_GUI
6049 need_mouse_correct = TRUE;
6050# endif
6051}
6052
6053/*
6054 * Close tab page "tp", which is not the current tab page.
6055 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006056 * Also takes care of the tab pages line disappearing when closing the
6057 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006058 */
6059 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006060tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006061{
6062 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006063 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006064 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006065
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006066 // Limit to 1000 windows, autocommands may add a window while we close
6067 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006068 while (++done < 1000)
6069 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006070 wp = tp->tp_firstwin;
6071 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006072
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006073 // Autocommands may delete the tab page under our fingers and we may
6074 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006075 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006076 break;
6077 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006078
Bram Moolenaar29323592016-07-24 22:04:11 +02006079 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006080
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006081 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006082 if (h != tabline_height())
6083 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006084}
6085
6086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 * ":only".
6088 */
6089 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006090ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006092 win_T *wp;
6093 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094# ifdef FEAT_GUI
6095 need_mouse_correct = TRUE;
6096# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006097 if (eap->addr_count > 0)
6098 {
6099 wnr = eap->line2;
6100 for (wp = firstwin; --wnr > 0; )
6101 {
6102 if (wp->w_next == NULL)
6103 break;
6104 else
6105 wp = wp->w_next;
6106 }
6107 win_goto(wp);
6108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 close_others(TRUE, eap->forceit);
6110}
6111
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006113ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006115 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01006116 if (!eap->skip)
6117 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006118#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006119 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006120#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006121 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006122 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01006123 else
6124 {
6125 int winnr = 0;
6126 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006127
Bram Moolenaar2256c992016-11-15 21:17:07 +01006128 FOR_ALL_WINDOWS(win)
6129 {
6130 winnr++;
6131 if (winnr == eap->line2)
6132 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006133 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006134 if (win == NULL)
6135 win = lastwin;
6136 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 }
6139}
6140
6141/*
6142 * ":stop" and ":suspend": Suspend Vim.
6143 */
6144 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006145ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146{
6147 /*
6148 * Disallow suspending for "rvim".
6149 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006150 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 {
6152 if (!eap->forceit)
6153 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01006154 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 windgoto((int)Rows - 1, 0);
6156 out_char('\n');
6157 out_flush();
6158 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006159 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006161 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006163 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164#ifdef FEAT_TITLE
6165 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006166 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167#endif
6168 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006169 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006171 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006172 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 }
6174}
6175
6176/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006177 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 */
6179 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006180ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006182#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006183 if (not_in_vim9(eap) == FAIL)
6184 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186#ifdef FEAT_CMDWIN
6187 if (cmdwin_type != 0)
6188 {
6189 cmdwin_result = Ctrl_C;
6190 return;
6191 }
6192#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006193 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006194 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006195 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006196 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006197 return;
6198 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006199
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006201 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 */
6203 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6204 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006205
6206 // Write the buffer for ":wq" or when it was changed.
6207 // Trigger QuitPre and ExitPre.
6208 // Check if we can exit now, after autocommands have changed things.
6209 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6210 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006212 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 {
6214 not_exiting();
6215 }
6216 else
6217 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006218 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006220 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221# ifdef FEAT_GUI
6222 need_mouse_correct = TRUE;
6223# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006224 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006225 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 }
6227}
6228
6229/*
6230 * ":print", ":list", ":number".
6231 */
6232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006233ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006235 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006236 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006237 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006239 for ( ;!got_int; ui_breakcheck())
6240 {
6241 print_line(eap->line1,
6242 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6243 || (eap->flags & EXFLAG_NR)),
6244 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6245 if (++eap->line1 > eap->line2)
6246 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006247 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006248 }
6249 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006250 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006251 curwin->w_cursor.lnum = eap->line2;
6252 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 }
6254
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256}
6257
6258#ifdef FEAT_BYTEOFF
6259 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006260ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261{
6262 goto_byte(eap->line2);
6263}
6264#endif
6265
6266/*
6267 * ":shell".
6268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006270ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271{
6272 do_shell(NULL, 0);
6273}
6274
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006275#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006277static int drop_busy = FALSE;
6278static int drop_filec;
6279static char_u **drop_filev = NULL;
6280static int drop_split;
6281static void (*drop_callback)(void *);
6282static void *drop_cookie;
6283
6284 static void
6285handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286{
6287 exarg_T ea;
6288 int save_msg_scroll = msg_scroll;
6289
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006290 // Setting the argument list may cause screen updates and being called
6291 // recursively. Avoid that by setting drop_busy.
6292 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006294 // Check whether the current buffer is changed. If so, we will need
6295 // to split the current window or data could be lost.
6296 // We don't need to check if the 'hidden' option is set, as in this
6297 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006298 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 {
6300 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006301 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 --emsg_off;
6303 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006304 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 if (win_split(0, 0) == FAIL)
6307 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006308 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006310 // When splitting the window, create a new alist. Otherwise the
6311 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 alist_unlink(curwin->w_alist);
6313 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 }
6315
6316 /*
6317 * Set up the new argument list.
6318 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006319 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320
6321 /*
6322 * Move to the first file.
6323 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006324 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006325 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 ea.cmd = (char_u *)"next";
6327 do_argfile(&ea, 0);
6328
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006329 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6330 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 need_start_insertmode = FALSE;
6332
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006333 // Restore msg_scroll, otherwise a following command may cause scrolling
6334 // unexpectedly. The screen will be redrawn by the caller, thus
6335 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006337
6338 if (drop_callback != NULL)
6339 drop_callback(drop_cookie);
6340
6341 drop_filev = NULL;
6342 drop_busy = FALSE;
6343}
6344
6345/*
6346 * Handle a file drop. The code is here because a drop is *nearly* like an
6347 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006348 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006349 * code is very similar to :args and hence needs access to a lot of the static
6350 * functions in this file.
6351 *
6352 * The "filev" list must have been allocated using alloc(), as should each item
6353 * in the list. This function takes over responsibility for freeing the "filev"
6354 * list.
6355 */
6356 void
6357handle_drop(
6358 int filec, // the number of files dropped
6359 char_u **filev, // the list of files dropped
6360 int split, // force splitting the window
6361 void (*callback)(void *), // to be called after setting the argument
6362 // list
6363 void *cookie) // argument for "callback" (allocated)
6364{
6365 // Cannot handle recursive drops, finish the pending one.
6366 if (drop_busy)
6367 {
6368 FreeWild(filec, filev);
6369 vim_free(cookie);
6370 return;
6371 }
6372
6373 // When calling handle_drop() more than once in a row we only use the last
6374 // one.
6375 if (drop_filev != NULL)
6376 {
6377 FreeWild(drop_filec, drop_filev);
6378 vim_free(drop_cookie);
6379 }
6380
6381 drop_filec = filec;
6382 drop_filev = filev;
6383 drop_split = split;
6384 drop_callback = callback;
6385 drop_cookie = cookie;
6386
6387 // Postpone this when:
6388 // - editing the command line
6389 // - not possible to change the current buffer
6390 // - updating the screen
6391 // As it may change buffers and window structures that are in use and cause
6392 // freed memory to be used.
6393 if (text_locked() || curbuf_locked() || updating_screen)
6394 return;
6395
6396 handle_drop_internal();
6397}
6398
6399/*
6400 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6401 * reset: Handle a postponed drop.
6402 */
6403 void
6404handle_any_postponed_drop(void)
6405{
6406 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006407 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006408 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409}
6410#endif
6411
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 * ":preserve".
6414 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006416ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006418 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 ml_preserve(curbuf, TRUE);
6420}
6421
6422/*
6423 * ":recover".
6424 */
6425 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006426ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006428 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006430 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6431 | CCGD_MULTWIN
6432 | (eap->forceit ? CCGD_FORCEIT : 0)
6433 | CCGD_EXCMD)
6434
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 && (*eap->arg == NUL
6436 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006437 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 recoverymode = FALSE;
6439}
6440
6441/*
6442 * Command modifier used in a wrong way.
6443 */
6444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006445ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006447 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448}
6449
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450/*
6451 * :sview [+command] file split window with new file, read-only
6452 * :split [[+command] file] split window with current or new file
6453 * :vsplit [[+command] file] split window vertically with current or new file
6454 * :new [[+command] file] split window with no or new file
6455 * :vnew [[+command] file] split vertically window with no or new file
6456 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006457 *
6458 * :tabedit open new Tab page with empty window
6459 * :tabedit [+command] file open new Tab page and edit "file"
6460 * :tabnew [[+command] file] just like :tabedit
6461 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 */
6463 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006464ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006466 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006467#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006469#endif
6470#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006471 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006472 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006473#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006474 int use_tab = eap->cmdidx == CMD_tabedit
6475 || eap->cmdidx == CMD_tabfind
6476 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006478 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006479 return;
6480
Bram Moolenaar4033c552017-09-16 20:54:51 +02006481#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484
Bram Moolenaar4033c552017-09-16 20:54:51 +02006485#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006486 // A ":split" in the quickfix window works like ":new". Don't want two
6487 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006488 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 {
6490 if (eap->cmdidx == CMD_split)
6491 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 if (eap->cmdidx == CMD_vsplit)
6493 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496
Bram Moolenaar4033c552017-09-16 20:54:51 +02006497#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006498 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 {
6500 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6501 FNAME_MESS, TRUE, curbuf->b_ffname);
6502 if (fname == NULL)
6503 goto theend;
6504 eap->arg = fname;
6505 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006506# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006507 else
6508# endif
6509#endif
6510#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006511 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 && eap->cmdidx != CMD_new)
6514 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006515 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006516# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006517 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006518# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006519 au_has_group((char_u *)"FileExplorer"))
6520 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006521 // No browsing supported but we do have the file explorer:
6522 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006523 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006524 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006525 }
6526 else
6527 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006528 fname = do_browse(0, (char_u *)(use_tab
6529 ? _("Edit File in new tab page")
6530 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006532 if (fname == NULL)
6533 goto theend;
6534 eap->arg = fname;
6535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006537 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006540 /*
6541 * Either open new tab page or split the window.
6542 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006543 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006544 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006545 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006546 : eap->addr_count == 0 ? 0
6547 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006548 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006549 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006550
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006551 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006552 if (curwin != old_curwin
6553 && win_valid(old_curwin)
6554 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006555 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006556 old_curwin->w_alt_fnum = curbuf->b_fnum;
6557 }
6558 }
6559 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6561 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006562 // Reset 'scrollbind' when editing another file, but keep it when
6563 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006564 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006565 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 else
6567 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 do_exedit(eap, old_curwin);
6569 }
6570
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006571# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006572 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006573# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006575# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576theend:
6577 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006578# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006580
6581/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006582 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006583 */
6584 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006585tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006586{
6587 exarg_T ea;
6588
Bram Moolenaara80faa82020-04-12 19:37:17 +02006589 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006590 ea.cmdidx = CMD_tabnew;
6591 ea.cmd = (char_u *)"tabn";
6592 ea.arg = (char_u *)"";
6593 ex_splitview(&ea);
6594}
6595
6596/*
6597 * :tabnext command
6598 */
6599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006600ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006601{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006602 int tab_number;
6603
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006604 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006605 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006606 switch (eap->cmdidx)
6607 {
6608 case CMD_tabfirst:
6609 case CMD_tabrewind:
6610 goto_tabpage(1);
6611 break;
6612 case CMD_tablast:
6613 goto_tabpage(9999);
6614 break;
6615 case CMD_tabprevious:
6616 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006617 if (eap->arg && *eap->arg != NUL)
6618 {
6619 char_u *p = eap->arg;
6620 char_u *p_save = p;
6621
6622 tab_number = getdigits(&p);
6623 if (p == p_save || *p_save == '-' || *p != NUL
6624 || tab_number == 0)
6625 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006626 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006627 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006628 return;
6629 }
6630 }
6631 else
6632 {
6633 if (eap->addr_count == 0)
6634 tab_number = 1;
6635 else
6636 {
6637 tab_number = eap->line2;
6638 if (tab_number < 1)
6639 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006640 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006641 return;
6642 }
6643 }
6644 }
6645 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006646 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006647 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006648 tab_number = get_tabpage_arg(eap);
6649 if (eap->errmsg == NULL)
6650 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006651 break;
6652 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006653}
6654
6655/*
6656 * :tabmove command
6657 */
6658 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006659ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006660{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006661 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006662
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006663 tab_number = get_tabpage_arg(eap);
6664 if (eap->errmsg == NULL)
6665 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006666}
6667
6668/*
6669 * :tabs command: List tabs and their contents.
6670 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006671 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006672ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006673{
6674 tabpage_T *tp;
6675 win_T *wp;
6676 int tabcount = 1;
6677
6678 msg_start();
6679 msg_scroll = TRUE;
6680 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6681 {
6682 msg_putchar('\n');
6683 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006684 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006685 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006686 ui_breakcheck();
6687
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006688 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006689 wp = firstwin;
6690 else
6691 wp = tp->tp_firstwin;
6692 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6693 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006694 msg_putchar('\n');
6695 msg_putchar(wp == curwin ? '>' : ' ');
6696 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006697 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6698 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006699 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006700 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006701 else
6702 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6703 IObuff, IOSIZE, TRUE);
6704 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006705 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006706 ui_breakcheck();
6707 }
6708 }
6709}
6710
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711/*
6712 * ":mode": Set screen mode.
6713 * If no argument given, just get the screen size and redraw.
6714 */
6715 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006716ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717{
6718 if (*eap->arg == NUL)
6719 shell_resized();
6720 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006721 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722}
6723
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724/*
6725 * ":resize".
6726 * set, increment or decrement current window height
6727 */
6728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006729ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730{
6731 int n;
6732 win_T *wp = curwin;
6733
6734 if (eap->addr_count > 0)
6735 {
6736 n = eap->line2;
6737 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6738 ;
6739 }
6740
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006741# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006743# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006745 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 {
6747 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006748 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006749 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 n = 9999;
6751 win_setwidth_win((int)n, wp);
6752 }
6753 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 {
6755 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006756 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006757 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 n = 9999;
6759 win_setheight_win((int)n, wp);
6760 }
6761}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762
6763/*
6764 * ":find [+command] <file>" command.
6765 */
6766 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006767ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768{
6769#ifdef FEAT_SEARCHPATH
6770 char_u *fname;
6771 int count;
6772
6773 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6774 TRUE, curbuf->b_ffname);
6775 if (eap->addr_count > 0)
6776 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006777 // Repeat finding the file "count" times. This matters when it
6778 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 count = eap->line2;
6780 while (fname != NULL && --count > 0)
6781 {
6782 vim_free(fname);
6783 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6784 FALSE, curbuf->b_ffname);
6785 }
6786 }
6787
6788 if (fname != NULL)
6789 {
6790 eap->arg = fname;
6791#endif
6792 do_exedit(eap, NULL);
6793#ifdef FEAT_SEARCHPATH
6794 vim_free(fname);
6795 }
6796#endif
6797}
6798
6799/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006800 * ":open" simulation: for now just work like ":visual".
6801 */
6802 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006803ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006804{
6805 regmatch_T regmatch;
6806 char_u *p;
6807
Bram Moolenaar65088802021-03-13 21:07:21 +01006808#ifdef FEAT_EVAL
6809 if (not_in_vim9(eap) == FAIL)
6810 return;
6811#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006812 curwin->w_cursor.lnum = eap->line2;
6813 beginline(BL_SOL | BL_FIX);
6814 if (*eap->arg == '/')
6815 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006816 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006817 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006818 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00006819 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006820 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006821 if (regmatch.regprog != NULL)
6822 {
6823 regmatch.rm_ic = p_ic;
6824 p = ml_get_curline();
6825 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006826 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006827 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006828 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006829 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006830 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006831 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006832 eap->arg += STRLEN(eap->arg);
6833 }
6834 check_cursor();
6835
6836 eap->cmdidx = CMD_visual;
6837 do_exedit(eap, NULL);
6838}
6839
6840/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006841 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 */
6843 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006844ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845{
6846 do_exedit(eap, NULL);
6847}
6848
6849/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006850 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006853do_exedit(
6854 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006855 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856{
6857 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006859 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006861 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6862 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006863 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 /*
6865 * ":vi" command ends Ex mode.
6866 */
6867 if (exmode_active && (eap->cmdidx == CMD_visual
6868 || eap->cmdidx == CMD_view))
6869 {
6870 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006871 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006873 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006874 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006875 if (global_busy)
6876 {
6877 int rd = RedrawingDisabled;
6878 int nwr = no_wait_return;
6879 int ms = msg_scroll;
6880#ifdef FEAT_GUI
6881 int he = hold_gui_events;
6882#endif
6883
6884 if (eap->nextcmd != NULL)
6885 {
6886 stuffReadbuff(eap->nextcmd);
6887 eap->nextcmd = NULL;
6888 }
6889
6890 if (exmode_was != EXMODE_VIM)
6891 settmode(TMODE_RAW);
6892 RedrawingDisabled = 0;
6893 no_wait_return = 0;
6894 need_wait_return = FALSE;
6895 msg_scroll = 0;
6896#ifdef FEAT_GUI
6897 hold_gui_events = 0;
6898#endif
6899 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006900 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006901
6902 main_loop(FALSE, TRUE);
6903
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006904 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006905 RedrawingDisabled = rd;
6906 no_wait_return = nwr;
6907 msg_scroll = ms;
6908#ifdef FEAT_GUI
6909 hold_gui_events = he;
6910#endif
6911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 }
6915
6916 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006917 || eap->cmdidx == CMD_tabnew
6918 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006919 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006921 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 setpcmark();
6923 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006924 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6925 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006927 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 || *eap->arg != NUL
6929#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006930 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931#endif
6932 )
6933 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006934 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6935 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006936 if (*eap->arg != NUL && curbuf_locked())
6937 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006938
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 n = readonlymode;
6940 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6941 readonlymode = TRUE;
6942 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006943 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6944 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01006945 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
6946 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6948 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006949 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6951 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6952 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006953 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006955 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006956 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006957 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
6958 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006959 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006961 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 if (old_curwin != NULL)
6963 {
6964 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006965 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006967#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006968 cleanup_T cs;
6969
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006970 // Reset the error/interrupt/exception state here so that
6971 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006972 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006973#endif
6974#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006976#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006977 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006978
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006979#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006980 // Restore the error/interrupt/exception state if not
6981 // discarded by a new aborting error, interrupt, or
6982 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006983 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 }
6986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 }
6988 else if (readonlymode && curbuf->b_nwindows == 1)
6989 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006990 // When editing an already visited buffer, 'readonly' won't be set
6991 // but the previous value is kept. With ":view" and ":sview" we
6992 // want the file to be readonly, except when another window is
6993 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 curbuf->b_p_ro = TRUE;
6995 }
6996 readonlymode = n;
6997 }
6998 else
6999 {
7000 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007001 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002#ifdef FEAT_TITLE
7003 n = curwin->w_arg_idx_invalid;
7004#endif
7005 check_arg_idx(curwin);
7006#ifdef FEAT_TITLE
7007 if (n != curwin->w_arg_idx_invalid)
7008 maketitle();
7009#endif
7010 }
7011
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 /*
7013 * if ":split file" worked, set alternate file name in old window to new
7014 * file
7015 */
7016 if (old_curwin != NULL
7017 && *eap->arg != NUL
7018 && curwin != old_curwin
7019 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007020 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007021 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023
7024 ex_no_reprint = TRUE;
7025}
7026
7027#ifndef FEAT_GUI
7028/*
7029 * ":gui" and ":gvim" when there is no GUI.
7030 */
7031 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007032ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007034 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035}
7036#endif
7037
Bram Moolenaar4f974752019-02-17 17:44:42 +01007038#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007040ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041{
7042 gui_make_tearoff(eap->arg);
7043}
7044#endif
7045
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007046#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7047 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007049ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007051# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7052 if (gui.in_use)
7053 gui_make_popup(eap->arg, eap->forceit);
7054# ifdef FEAT_TERM_POPUP_MENU
7055 else
7056# endif
7057# endif
7058# ifdef FEAT_TERM_POPUP_MENU
7059 pum_make_popup(eap->arg, eap->forceit);
7060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061}
7062#endif
7063
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007065ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066{
7067 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007068 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007070 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071}
7072
7073/*
7074 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7075 * offset.
7076 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7077 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007079ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007082 win_T *save_curwin = curwin;
7083 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 long topline;
7085 long y;
7086 linenr_T old_linenr = curwin->w_cursor.lnum;
7087
7088 setpcmark();
7089
7090 /*
7091 * determine max topline
7092 */
7093 if (curwin->w_p_scb)
7094 {
7095 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007096 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 {
7098 if (wp->w_p_scb && wp->w_buffer)
7099 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007100 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 if (topline > y)
7102 topline = y;
7103 }
7104 }
7105 if (topline < 1)
7106 topline = 1;
7107 }
7108 else
7109 {
7110 topline = 1;
7111 }
7112
7113
7114 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007115 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007117 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 {
7119 if (curwin->w_p_scb)
7120 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007121 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 y = topline - curwin->w_topline;
7123 if (y > 0)
7124 scrollup(y, TRUE);
7125 else
7126 scrolldown(-y, TRUE);
7127 curwin->w_scbind_pos = topline;
7128 redraw_later(VALID);
7129 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 }
7132 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007133 curwin = save_curwin;
7134 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 if (curwin->w_p_scb)
7136 {
7137 did_syncbind = TRUE;
7138 checkpcmark();
7139 if (old_linenr != curwin->w_cursor.lnum)
7140 {
7141 char_u ctrl_o[2];
7142
7143 ctrl_o[0] = Ctrl_O;
7144 ctrl_o[1] = 0;
7145 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7146 }
7147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148}
7149
7150
7151 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007152ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007154 int i;
7155 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7156 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007158 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 do_bang(1, eap, FALSE, FALSE, TRUE);
7160 else
7161 {
7162 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7163 return;
7164
7165#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007166 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 {
7168 char_u *browseFile;
7169
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007170 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 NULL, NULL, NULL, curbuf);
7172 if (browseFile != NULL)
7173 {
7174 i = readfile(browseFile, NULL,
7175 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7176 vim_free(browseFile);
7177 }
7178 else
7179 i = OK;
7180 }
7181 else
7182#endif
7183 if (*eap->arg == NUL)
7184 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007185 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 return;
7187 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7188 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7189 }
7190 else
7191 {
7192 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7193 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7194 i = readfile(eap->arg, NULL,
7195 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7196
7197 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007198 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007200#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 if (!aborting())
7202#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007203 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 }
7205 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007206 {
7207 if (empty && exmode_active)
7208 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007209 // Delete the empty line that remains. Historically ex does
7210 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007211 if (eap->line2 == 0)
7212 lnum = curbuf->b_ml.ml_line_count;
7213 else
7214 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007215 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007216 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007217 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007218 if (curwin->w_cursor.lnum > 1
7219 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007220 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007221 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007222 }
7223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 }
7227}
7228
Bram Moolenaarea408852005-06-25 22:49:46 +00007229static char_u *prev_dir = NULL;
7230
7231#if defined(EXITFREE) || defined(PROTO)
7232 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007233free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007234{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007235 VIM_CLEAR(prev_dir);
7236 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007237}
7238#endif
7239
Bram Moolenaarf4258302013-06-02 18:20:17 +02007240/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007241 * Get the previous directory for the given chdir scope.
7242 */
7243 static char_u *
7244get_prevdir(cdscope_T scope)
7245{
7246 if (scope == CDSCOPE_WINDOW)
7247 return curwin->w_prevdir;
7248 else if (scope == CDSCOPE_TABPAGE)
7249 return curtab->tp_prevdir;
7250 return prev_dir;
7251}
7252
7253/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007254 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007255 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7256 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007257 */
7258 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007259post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007260{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007261 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007262 // Clear tab local directory for both :cd and :tcd
7263 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007264 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007265 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007266 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007267 char_u *pdir = get_prevdir(scope);
7268
7269 // If still in the global directory, need to remember current
7270 // directory as the global directory.
7271 if (globaldir == NULL && pdir != NULL)
7272 globaldir = vim_strsave(pdir);
7273
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007274 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007275 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007276 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007277 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007278 curtab->tp_localdir = vim_strsave(NameBuff);
7279 else
7280 curwin->w_localdir = vim_strsave(NameBuff);
7281 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007282 }
7283 else
7284 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007285 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007286 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007287 }
7288
7289 shorten_fnames(TRUE);
7290}
7291
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007292/*
7293 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007294 * chdir() function.
7295 * scope == CDSCOPE_WINDOW: changes the window-local directory
7296 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7297 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007298 * Returns TRUE if the directory is successfully changed.
7299 */
7300 int
7301changedir_func(
7302 char_u *new_dir,
7303 int forceit,
7304 cdscope_T scope)
7305{
7306 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02007307 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007308 int dir_differs;
7309 int retval = FALSE;
7310
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007311 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007312 return FALSE;
7313
7314 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7315 {
7316 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7317 return FALSE;
7318 }
7319
7320 // ":cd -": Change to previous directory
7321 if (STRCMP(new_dir, "-") == 0)
7322 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007323 pdir = get_prevdir(scope);
7324 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007325 {
7326 emsg(_("E186: No previous directory"));
7327 return FALSE;
7328 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007329 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007330 }
7331
Bram Moolenaar002bc792020-06-05 22:33:42 +02007332 // Free the previous directory
7333 tofree = get_prevdir(scope);
7334
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007335 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007336 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007337 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007338 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007339 pdir = NULL;
7340 if (scope == CDSCOPE_WINDOW)
7341 curwin->w_prevdir = pdir;
7342 else if (scope == CDSCOPE_TABPAGE)
7343 curtab->tp_prevdir = pdir;
7344 else
7345 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007346
7347#if defined(UNIX) || defined(VMS)
7348 // for UNIX ":cd" means: go to home directory
7349 if (*new_dir == NUL)
7350 {
7351 // use NameBuff for home directory name
7352# ifdef VMS
7353 char_u *p;
7354
7355 p = mch_getenv((char_u *)"SYS$LOGIN");
7356 if (p == NULL || *p == NUL) // empty is the same as not set
7357 NameBuff[0] = NUL;
7358 else
7359 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7360# else
7361 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7362# endif
7363 new_dir = NameBuff;
7364 }
7365#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02007366 dir_differs = new_dir == NULL || pdir == NULL
7367 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007368 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7369 emsg(_(e_failed));
7370 else
7371 {
7372 char_u *acmd_fname;
7373
7374 post_chdir(scope);
7375
7376 if (dir_differs)
7377 {
7378 if (scope == CDSCOPE_WINDOW)
7379 acmd_fname = (char_u *)"window";
7380 else if (scope == CDSCOPE_TABPAGE)
7381 acmd_fname = (char_u *)"tabpage";
7382 else
7383 acmd_fname = (char_u *)"global";
7384 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7385 curbuf);
7386 }
7387 retval = TRUE;
7388 }
7389 vim_free(tofree);
7390
7391 return retval;
7392}
Bram Moolenaarea408852005-06-25 22:49:46 +00007393
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007395 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007397 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007398ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007400 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401
7402 new_dir = eap->arg;
7403#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007404 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 if (*new_dir == NUL)
7406 ex_pwd(NULL);
7407 else
7408#endif
7409 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007410 cdscope_T scope = CDSCOPE_GLOBAL;
7411
7412 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7413 scope = CDSCOPE_WINDOW;
7414 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7415 scope = CDSCOPE_TABPAGE;
7416
7417 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007418 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007419 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007420 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 ex_pwd(eap);
7422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 }
7424}
7425
7426/*
7427 * ":pwd".
7428 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007430ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431{
7432 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7433 {
7434#ifdef BACKSLASH_IN_FILENAME
7435 slash_adjust(NameBuff);
7436#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007437 if (p_verbose > 0)
7438 {
7439 char *context = "global";
7440
7441 if (curwin->w_localdir != NULL)
7442 context = "window";
7443 else if (curtab->tp_localdir != NULL)
7444 context = "tabpage";
7445 smsg("[%s] %s", context, (char *)NameBuff);
7446 }
7447 else
7448 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 }
7450 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007451 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452}
7453
7454/*
7455 * ":=".
7456 */
7457 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007458ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007460 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007461 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462}
7463
7464 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007465ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007467 int n;
7468 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469
7470 if (cursor_valid())
7471 {
7472 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7473 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007474 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007476
7477 len = eap->line2;
7478 switch (*eap->arg)
7479 {
7480 case 'm': break;
7481 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007482 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007483 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007484
7485 // Hide the cursor if invoked with !
7486 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487}
7488
7489/*
7490 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007491 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 */
7493 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007494do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495{
Bram Moolenaar52953192019-08-16 10:27:13 +02007496 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007497 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007498# ifdef ELAPSED_FUNC
7499 elapsed_T start_tv;
7500
7501 // Remember at what time we started, so that we know how much longer we
7502 // should wait after waiting for a bit.
7503 ELAPSED_INIT(start_tv);
7504# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007506 if (hide_cursor)
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007507 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007508 else
7509 cursor_on();
7510
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007511 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007512 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007514 wait_now = msec - done > 1000L ? 1000L : msec - done;
7515#ifdef FEAT_TIMERS
7516 {
7517 long due_time = check_due_timer();
7518
7519 if (due_time > 0 && due_time < wait_now)
7520 wait_now = due_time;
7521 }
7522#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007523#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007524 if (has_any_channel() && wait_now > 20L)
7525 wait_now = 20L;
7526#endif
7527#ifdef FEAT_SOUND
7528 if (has_any_sound_callback() && wait_now > 20L)
7529 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007530#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007531 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007532
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007533#ifdef FEAT_JOB_CHANNEL
7534 if (has_any_channel())
7535 ui_breakcheck_force(TRUE);
7536 else
7537#endif
7538 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007539#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007540 // Process the netbeans and clientserver messages that may have been
7541 // received in the call to ui_breakcheck() when the GUI is in use. This
7542 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007543 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007544#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007545
7546# ifdef ELAPSED_FUNC
7547 // actual time passed
7548 done = ELAPSED_FUNC(start_tv);
7549# else
7550 // guestimate time passed (will actually be more)
7551 done += wait_now;
7552# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007554
7555 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7556 // input buffer, otherwise a following call to input() fails.
7557 if (got_int)
7558 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007559
7560 if (hide_cursor)
7561 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562}
7563
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564/*
7565 * ":winsize" command (obsolete).
7566 */
7567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007568ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569{
7570 int w, h;
7571 char_u *arg = eap->arg;
7572 char_u *p;
7573
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007574 if (!isdigit(*arg))
7575 {
7576 semsg(_(e_invarg2), arg);
7577 return;
7578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 w = getdigits(&arg);
7580 arg = skipwhite(arg);
7581 p = arg;
7582 h = getdigits(&arg);
7583 if (*p != NUL && *arg == NUL)
7584 set_shellsize(w, h, TRUE);
7585 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007586 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587}
7588
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007590ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591{
7592 int xchar = NUL;
7593 char_u *p;
7594
7595 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7596 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007597 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 if (eap->arg[1] == NUL)
7599 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007600 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 return;
7602 }
7603 xchar = eap->arg[1];
7604 p = eap->arg + 2;
7605 }
7606 else
7607 p = eap->arg + 1;
7608
Bram Moolenaar63b91732021-08-05 20:40:03 +02007609 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007611 if (*p != NUL && *p != (
7612#ifdef FEAT_EVAL
7613 in_vim9script() ? '#' :
7614#endif
7615 '"')
7616 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007617 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007618 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007620 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007621 postponed_split_flags = cmdmod.cmod_split;
7622 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7624 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007625 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 }
7627}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628
Bram Moolenaar843ee412004-06-30 16:16:41 +00007629#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630/*
7631 * ":winpos".
7632 */
7633 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007634ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635{
7636 int x, y;
7637 char_u *arg = eap->arg;
7638 char_u *p;
7639
7640 if (*arg == NUL)
7641 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007642# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007643# ifdef VIMDLL
7644 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7645 mch_get_winpos(&x, &y) != FAIL)
7646# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007648# else
7649 if (mch_get_winpos(&x, &y) != FAIL)
7650# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {
7652 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007653 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 }
7655 else
7656# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007657 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 }
7659 else
7660 {
7661 x = getdigits(&arg);
7662 arg = skipwhite(arg);
7663 p = arg;
7664 y = getdigits(&arg);
7665 if (*p == NUL || *arg != NUL)
7666 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007667 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 return;
7669 }
7670# ifdef FEAT_GUI
7671 if (gui.in_use)
7672 gui_mch_set_winpos(x, y);
7673 else if (gui.starting)
7674 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007675 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 gui_win_x = x;
7677 gui_win_y = y;
7678 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007679# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 else
7681# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007682# endif
7683# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007684 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685# endif
7686# ifdef HAVE_TGETENT
7687 if (*T_CWP)
7688 term_set_winpos(x, y);
7689# endif
7690 }
7691}
7692#endif
7693
7694/*
7695 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7696 */
7697 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007698ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699{
7700 oparg_T oa;
7701
7702 clear_oparg(&oa);
7703 oa.regname = eap->regname;
7704 oa.start.lnum = eap->line1;
7705 oa.end.lnum = eap->line2;
7706 oa.line_count = eap->line2 - eap->line1 + 1;
7707 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007709 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 {
7711 setpcmark();
7712 curwin->w_cursor.lnum = eap->line1;
7713 beginline(BL_SOL | BL_FIX);
7714 }
7715
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007716 if (VIsual_active)
7717 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007718
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 switch (eap->cmdidx)
7720 {
7721 case CMD_delete:
7722 oa.op_type = OP_DELETE;
7723 op_delete(&oa);
7724 break;
7725
7726 case CMD_yank:
7727 oa.op_type = OP_YANK;
7728 (void)op_yank(&oa, FALSE, TRUE);
7729 break;
7730
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007731 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007732 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007734 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7735#else
7736 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007738 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 oa.op_type = OP_RSHIFT;
7740 else
7741 oa.op_type = OP_LSHIFT;
7742 op_shift(&oa, FALSE, eap->amount);
7743 break;
7744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007746 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747}
7748
7749/*
7750 * ":put".
7751 */
7752 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007753ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007755 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 if (eap->line2 == 0)
7757 {
7758 eap->line2 = 1;
7759 eap->forceit = TRUE;
7760 }
7761 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007762 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007763 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764}
7765
7766/*
7767 * Handle ":copy" and ":move".
7768 */
7769 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007770ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771{
7772 long n;
7773
Bram Moolenaar491799b2020-08-01 19:23:43 +02007774#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007775 if (not_in_vim9(eap) == FAIL)
7776 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007777#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007778 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007779 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 {
7781 eap->nextcmd = NULL;
7782 return;
7783 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007784 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785
7786 /*
7787 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7788 */
7789 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7790 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007791 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 return;
7793 }
7794
7795 if (eap->cmdidx == CMD_move)
7796 {
7797 if (do_move(eap->line1, eap->line2, n) == FAIL)
7798 return;
7799 }
7800 else
7801 ex_copy(eap->line1, eap->line2, n);
7802 u_clearline();
7803 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007804 ex_may_print(eap);
7805}
7806
7807/*
7808 * Print the current line if flags were given to the Ex command.
7809 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007810 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007811ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007812{
7813 if (eap->flags != 0)
7814 {
7815 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7816 (eap->flags & EXFLAG_LIST));
7817 ex_no_reprint = TRUE;
7818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819}
7820
7821/*
7822 * ":smagic" and ":snomagic".
7823 */
7824 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007825ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007827 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007829 magic_overruled = eap->cmdidx == CMD_smagic
7830 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02007831 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007832 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833}
7834
7835/*
7836 * ":join".
7837 */
7838 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007839ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840{
7841 curwin->w_cursor.lnum = eap->line1;
7842 if (eap->line1 == eap->line2)
7843 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007844 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 return;
7846 if (eap->line2 == curbuf->b_ml.ml_line_count)
7847 {
7848 beep_flush();
7849 return;
7850 }
7851 ++eap->line2;
7852 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007853 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007855 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856}
7857
7858/*
7859 * ":[addr]@r" or ":[addr]*r": execute register
7860 */
7861 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007862ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863{
7864 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007865 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866
7867 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007868 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869
7870#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007871 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872#endif
7873
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007874 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 c = *eap->arg;
7876 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7877 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007878 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007879 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7880 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007881 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 else
7885 {
7886 int save_efr = exec_from_reg;
7887
7888 exec_from_reg = TRUE;
7889
7890 /*
7891 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007892 * Continue until the stuff buffer is empty and all added characters
7893 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007895 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7897
7898 exec_from_reg = save_efr;
7899 }
7900}
7901
7902/*
7903 * ":!".
7904 */
7905 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007906ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907{
7908 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7909}
7910
7911/*
7912 * ":undo".
7913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007915ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007917 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007918 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007919 else
7920 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921}
7922
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007923#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007924 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007925ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007926{
7927 char_u hash[UNDO_HASH_SIZE];
7928
7929 u_compute_hash(hash);
7930 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7931}
7932
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007933 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007934ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007935{
7936 char_u hash[UNDO_HASH_SIZE];
7937
7938 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007939 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007940}
7941#endif
7942
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943/*
7944 * ":redo".
7945 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007947ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948{
7949 u_redo(1);
7950}
7951
7952/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007953 * ":earlier" and ":later".
7954 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007955 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007956ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007957{
7958 long count = 0;
7959 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007960 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007961 char_u *p = eap->arg;
7962
7963 if (*p == NUL)
7964 count = 1;
7965 else if (isdigit(*p))
7966 {
7967 count = getdigits(&p);
7968 switch (*p)
7969 {
7970 case 's': ++p; sec = TRUE; break;
7971 case 'm': ++p; sec = TRUE; count *= 60; break;
7972 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007973 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7974 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007975 }
7976 }
7977
7978 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007979 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007980 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007981 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7982 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007983}
7984
7985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 * ":redir": start/stop redirection.
7987 */
7988 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007989ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990{
7991 char *mode;
7992 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007993 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994
Bram Moolenaarba768492016-07-08 15:32:54 +02007995#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007996 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007997 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007998 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007999 return;
8000 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008001#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008002
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 if (STRICMP(eap->arg, "END") == 0)
8004 close_redir();
8005 else
8006 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008007 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008009 ++arg;
8010 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008012 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 mode = "a";
8014 }
8015 else
8016 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008017 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018
8019 close_redir();
8020
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008021 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008022 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 if (fname == NULL)
8024 return;
8025#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008026 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 {
8028 char_u *browseFile;
8029
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008030 browseFile = do_browse(BROWSE_SAVE,
8031 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008032 fname, NULL, NULL,
8033 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008035 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 vim_free(fname);
8037 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008038 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 }
8040#endif
8041
8042 redir_fd = open_exfile(fname, eap->forceit, mode);
8043 vim_free(fname);
8044 }
8045#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008046 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008048 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008050 ++arg;
8051 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008053 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008054 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008056 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008058 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008059 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008060 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008061 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008063 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008064 if (*arg == '>')
8065 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008066 // Make register empty when not using @A-@Z and the
8067 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00008068 if (*arg == NUL && !isupper(redir_reg))
8069 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008071 }
8072 if (*arg != NUL)
8073 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008074 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008075 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008076 }
8077 }
8078 else if (*arg == '=' && arg[1] == '>')
8079 {
8080 int append;
8081
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008082 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008083 close_redir();
8084 arg += 2;
8085
8086 if (*arg == '>')
8087 {
8088 ++arg;
8089 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 }
8091 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008092 append = FALSE;
8093
8094 if (var_redir_start(skipwhite(arg), append) == OK)
8095 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 }
8097#endif
8098
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008099 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008102 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008104
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008105 // Make sure redirection is not off. Can happen for cmdline completion
8106 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008107 if (redir_fd != NULL
8108#ifdef FEAT_EVAL
8109 || redir_reg || redir_vname
8110#endif
8111 )
8112 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113}
8114
8115/*
8116 * ":redraw": force redraw
8117 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008118 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008119ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120{
8121 int r = RedrawingDisabled;
8122 int p = p_lz;
8123
8124 RedrawingDisabled = 0;
8125 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008126 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008128 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129#ifdef FEAT_TITLE
8130 if (need_maketitle)
8131 maketitle();
8132#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008133#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8134# ifdef VIMDLL
8135 if (!gui.in_use)
8136# endif
8137 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 RedrawingDisabled = r;
8140 p_lz = p;
8141
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008142 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 msg_didout = FALSE;
8144 msg_col = 0;
8145
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008146 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008147 need_wait_return = FALSE;
8148
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 out_flush();
8150}
8151
8152/*
8153 * ":redrawstatus": force redraw of status line(s)
8154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008156ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 int r = RedrawingDisabled;
8159 int p = p_lz;
8160
8161 RedrawingDisabled = 0;
8162 p_lz = FALSE;
8163 if (eap->forceit)
8164 status_redraw_all();
8165 else
8166 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008167 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 RedrawingDisabled = r;
8169 p_lz = p;
8170 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171}
8172
Bram Moolenaare12bab32019-01-08 22:02:56 +01008173/*
8174 * ":redrawtabline": force redraw of the tabline
8175 */
8176 static void
8177ex_redrawtabline(exarg_T *eap UNUSED)
8178{
8179 int r = RedrawingDisabled;
8180 int p = p_lz;
8181
8182 RedrawingDisabled = 0;
8183 p_lz = FALSE;
8184
8185 draw_tabline();
8186
8187 RedrawingDisabled = r;
8188 p_lz = p;
8189 out_flush();
8190}
8191
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008193close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194{
8195 if (redir_fd != NULL)
8196 {
8197 fclose(redir_fd);
8198 redir_fd = NULL;
8199 }
8200#ifdef FEAT_EVAL
8201 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008202 if (redir_vname)
8203 {
8204 var_redir_stop();
8205 redir_vname = 0;
8206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207#endif
8208}
8209
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008210#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008211 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008212vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008213{
8214 if (vim_mkdir(name, prot) != 0)
8215 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008216 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008217 return FAIL;
8218 }
8219 return OK;
8220}
8221#endif
8222
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223/*
8224 * Open a file for writing for an Ex command, with some checks.
8225 * Return file descriptor, or NULL on failure.
8226 */
8227 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008228open_exfile(
8229 char_u *fname,
8230 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008231 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232{
8233 FILE *fd;
8234
8235#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008236 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 if (mch_isdir(fname))
8238 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008239 semsg(_(e_src_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 return NULL;
8241 }
8242#endif
8243 if (!forceit && *mode != 'a' && vim_fexists(fname))
8244 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008245 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 return NULL;
8247 }
8248
8249 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008250 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251
8252 return fd;
8253}
8254
8255/*
8256 * ":mark" and ":k".
8257 */
8258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008259ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260{
8261 pos_T pos;
8262
Bram Moolenaar10b94212021-02-19 21:42:57 +01008263#ifdef FEAT_EVAL
8264 if (not_in_vim9(eap) == FAIL)
8265 return;
8266#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008267 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008268 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008269 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02008270 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 else
8272 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008273 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 curwin->w_cursor.lnum = eap->line2;
8275 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008276 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008277 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008278 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 }
8280}
8281
8282/*
8283 * Update w_topline, w_leftcol and the cursor position.
8284 */
8285 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008286update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008288 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 update_topline();
8290 if (!curwin->w_p_wrap)
8291 validate_cursor();
8292 update_curswant();
8293}
8294
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008296 * Save the current State and go to Normal mode.
8297 * Return TRUE if the typeahead could be saved.
8298 */
8299 int
8300save_current_state(save_state_T *sst)
8301{
8302 sst->save_msg_scroll = msg_scroll;
8303 sst->save_restart_edit = restart_edit;
8304 sst->save_msg_didout = msg_didout;
8305 sst->save_State = State;
8306 sst->save_insertmode = p_im;
8307 sst->save_finish_op = finish_op;
8308 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008309 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008310
Bram Moolenaar4324d872020-12-01 20:12:24 +01008311 msg_scroll = FALSE; // no msg scrolling in Normal mode
8312 restart_edit = 0; // don't go to Insert mode
8313 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008314
Bram Moolenaara452b802020-12-01 21:47:59 +01008315 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008316 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008317
8318 /*
8319 * Save the current typeahead. This is required to allow using ":normal"
8320 * from an event handler and makes sure we don't hang when the argument
8321 * ends with half a command.
8322 */
8323 save_typeahead(&sst->tabuf);
8324 return sst->tabuf.typebuf_valid;
8325}
8326
8327 void
8328restore_current_state(save_state_T *sst)
8329{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008330 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008331 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008332
8333 msg_scroll = sst->save_msg_scroll;
8334 restart_edit = sst->save_restart_edit;
8335 p_im = sst->save_insertmode;
8336 finish_op = sst->save_finish_op;
8337 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008338 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008339 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008340 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008341
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008342 // Restore the state (needed when called from a function executed for
8343 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008344 State = sst->save_State;
8345#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008346 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008347#endif
8348}
8349
8350/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 * ":normal[!] {commands}": Execute normal mode commands.
8352 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008353 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008354ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008356 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 char_u *arg = NULL;
8358 int l;
8359 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008361 if (ex_normal_lock > 0)
8362 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008363 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008364 return;
8365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 if (ex_normal_busy >= p_mmd)
8367 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008368 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 return;
8370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 /*
8373 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8374 * this for the K_SPECIAL leading byte, otherwise special keys will not
8375 * work.
8376 */
8377 if (has_mbyte)
8378 {
8379 int len = 0;
8380
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008381 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 for (p = eap->arg; *p != NUL; ++p)
8383 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008384#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008385 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008387#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008388 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008389 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008390#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 )
8394 len += 2;
8395 }
8396 if (len > 0)
8397 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008398 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 if (arg != NULL)
8400 {
8401 len = 0;
8402 for (p = eap->arg; *p != NUL; ++p)
8403 {
8404 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008405#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 if (*p == CSI)
8407 {
8408 arg[len++] = KS_EXTRA;
8409 arg[len++] = (int)KE_CSI;
8410 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008411#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008412 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 {
8414 arg[len++] = *++p;
8415 if (*p == K_SPECIAL)
8416 {
8417 arg[len++] = KS_SPECIAL;
8418 arg[len++] = KE_FILLER;
8419 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008420#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 else if (*p == CSI)
8422 {
8423 arg[len++] = KS_EXTRA;
8424 arg[len++] = (int)KE_CSI;
8425 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 }
8428 arg[len] = NUL;
8429 }
8430 }
8431 }
8432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008434 ++ex_normal_busy;
8435 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 {
8437 /*
8438 * Repeat the :normal command for each line in the range. When no
8439 * range given, execute it just once, without positioning the cursor
8440 * first.
8441 */
8442 do
8443 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 if (eap->addr_count != 0)
8445 {
8446 curwin->w_cursor.lnum = eap->line1++;
8447 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008448 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 }
8450
Bram Moolenaar13505972019-01-24 15:04:48 +01008451 exec_normal_cmd(arg != NULL
8452 ? arg
8453 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 }
8455 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8456 }
8457
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008458 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 update_topline_cursor();
8460
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008461 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008463 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008464#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008465 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008466#endif
8467
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469}
8470
8471/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008472 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 */
8474 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008475ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008477 if (eap->forceit)
8478 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008479 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008480 if (!curwin->w_cursor.lnum)
8481 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008482 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008483 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008484#ifdef FEAT_TERMINAL
8485 // Ignore this when running in an active terminal.
8486 if (term_job_running(curbuf->b_term))
8487 return;
8488#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008489
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008490 // Ignore the command when already in Insert mode. Inserting an
8491 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008492 if (State & INSERT)
8493 return;
8494
Bram Moolenaar64969662005-12-14 21:59:55 +00008495 if (eap->cmdidx == CMD_startinsert)
8496 restart_edit = 'a';
8497 else if (eap->cmdidx == CMD_startreplace)
8498 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008500 restart_edit = 'V';
8501
8502 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008504 if (eap->cmdidx == CMD_startinsert)
8505 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008506 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008508
8509 if (VIsual_active)
8510 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511}
8512
8513/*
8514 * ":stopinsert"
8515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008517ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518{
8519 restart_edit = 0;
8520 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008521 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008524/*
8525 * Execute normal mode command "cmd".
8526 * "remap" can be REMAP_NONE or REMAP_YES.
8527 */
8528 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008529exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008530{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008531 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008532 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008533 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008534}
Bram Moolenaar25281632016-01-21 23:32:32 +01008535
Bram Moolenaar25281632016-01-21 23:32:32 +01008536/*
8537 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008538 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008539 */
8540 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008541exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008542{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008543 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008544 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008545
Bram Moolenaar905dd902019-04-07 14:21:47 +02008546 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8547 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008548 clear_oparg(&oa);
8549 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008550 while ((!stuff_empty()
8551 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008552 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008553 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008554 {
8555 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008556#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008557 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008558 && oa.op_type == OP_NOP && oa.regname == NUL
8559 && !VIsual_active)
8560 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008561 // If terminal_loop() returns OK we got a key that is handled
8562 // in Normal model. With FAIL we first need to position the
8563 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008564 if (terminal_loop(TRUE) == OK)
8565 normal_cmd(&oa, TRUE);
8566 }
8567 else
8568#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008569 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008570 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008571 }
8572}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008573
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574#ifdef FEAT_FIND_ID
8575 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008576ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577{
8578 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8579 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8580 (linenr_T)1, (linenr_T)MAXLNUM);
8581}
8582
Bram Moolenaar4033c552017-09-16 20:54:51 +02008583#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584/*
8585 * ":psearch"
8586 */
8587 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008588ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589{
8590 g_do_tagpreview = p_pvh;
8591 ex_findpat(eap);
8592 g_do_tagpreview = 0;
8593}
8594#endif
8595
8596 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008597ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598{
8599 int whole = TRUE;
8600 long n;
8601 char_u *p;
8602 int action;
8603
8604 switch (cmdnames[eap->cmdidx].cmd_name[2])
8605 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008606 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8608 action = ACTION_GOTO;
8609 else
8610 action = ACTION_SHOW;
8611 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008612 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 action = ACTION_SHOW_ALL;
8614 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008615 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 action = ACTION_GOTO;
8617 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008618 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 action = ACTION_SPLIT;
8620 break;
8621 }
8622
8623 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008624 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 {
8626 n = getdigits(&eap->arg);
8627 eap->arg = skipwhite(eap->arg);
8628 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008629 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 {
8631 whole = FALSE;
8632 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008633 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 if (*p)
8635 {
8636 *p++ = NUL;
8637 p = skipwhite(p);
8638
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008639 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008640 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008641 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02008643 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 }
8645 }
8646 if (!eap->skip)
8647 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8648 whole, !eap->forceit,
8649 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8650 n, action, eap->line1, eap->line2);
8651}
8652#endif
8653
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654
Bram Moolenaar4033c552017-09-16 20:54:51 +02008655#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656/*
8657 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8658 */
8659 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008660ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008662 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8664}
8665
8666/*
8667 * ":pedit"
8668 */
8669 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008670ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671{
8672 win_T *curwin_save = curwin;
8673
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008674 if (ERROR_IF_ANY_POPUP_WINDOW)
8675 return;
8676
Bram Moolenaar026587b2019-08-17 15:08:00 +02008677 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008679 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008680
Bram Moolenaar026587b2019-08-17 15:08:00 +02008681 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008683
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 if (curwin != curwin_save && win_valid(curwin_save))
8685 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008686 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 validate_cursor();
8688 redraw_later(VALID);
8689 win_enter(curwin_save, TRUE);
8690 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008691# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008692 else if (WIN_IS_POPUP(curwin))
8693 {
8694 // can't keep focus in popup window
8695 win_enter(firstwin, TRUE);
8696 }
8697# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 g_do_tagpreview = 0;
8699}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701
8702/*
8703 * ":stag", ":stselect" and ":stjump".
8704 */
8705 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008706ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707{
8708 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008709 postponed_split_flags = cmdmod.cmod_split;
8710 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8712 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008713 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715
8716/*
8717 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8718 */
8719 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008720ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721{
8722 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8723}
8724
8725 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008726ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727{
8728 int cmd;
8729
8730 switch (name[1])
8731 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008732 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008734 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008736 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008738 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008740 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008742 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008744 case 'f': // ":tfirst"
8745 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008747 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008749 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008751 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008753 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 return;
8755 }
8756#endif
8757 cmd = DT_TAG;
8758 break;
8759 }
8760
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008761 if (name[0] == 'l')
8762 {
8763#ifndef FEAT_QUICKFIX
8764 ex_ni(eap);
8765 return;
8766#else
8767 cmd = DT_LTAG;
8768#endif
8769 }
8770
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8772 eap->forceit, TRUE);
8773}
8774
8775/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008776 * Check "str" for starting with a special cmdline variable.
8777 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8778 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8779 */
8780 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008781find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008782{
8783 int len;
8784 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008785 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008786 "%",
8787#define SPEC_PERC 0
8788 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008789#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008790 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008791#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008792 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008793#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008794 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008795#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008796 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008797#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008798 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008799#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008800 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008801#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008802 "<stack>", // call stack
8803#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008804 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008805#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008806 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008807#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008808 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008809#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008810 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008811#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008812 "<SID>", // script ID: <SNR>123_
8813#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008814#ifdef FEAT_CLIENTSERVER
8815 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008816# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008817#endif
8818 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008819
K.Takataeeec2542021-06-02 13:28:16 +02008820 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008821 {
8822 len = (int)STRLEN(spec_str[i]);
8823 if (STRNCMP(src, spec_str[i], len) == 0)
8824 {
8825 *usedlen = len;
8826 return i;
8827 }
8828 }
8829 return -1;
8830}
8831
8832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 * Evaluate cmdline variables.
8834 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008835 * change "%" to curbuf->b_ffname
8836 * "#" to curwin->w_alt_fnum
8837 * "%%" to curwin->w_alt_fnum in Vim9 script
8838 * "<cword>" to word under the cursor
8839 * "<cWORD>" to WORD under the cursor
8840 * "<cexpr>" to C-expression under the cursor
8841 * "<cfile>" to path name under the cursor
8842 * "<sfile>" to sourced file name
8843 * "<stack>" to call stack
8844 * "<slnum>" to sourced file line number
8845 * "<afile>" to file name for autocommand
8846 * "<abuf>" to buffer number for autocommand
8847 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 *
8849 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8850 * "" for error without a message) and NULL is returned.
8851 * Returns an allocated string if a valid match was found.
8852 * Returns NULL if no match was found. "usedlen" then still contains the
8853 * number of characters to skip.
8854 */
8855 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008856eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008857 char_u *src, // pointer into commandline
8858 char_u *srcstart, // beginning of valid memory for src
8859 int *usedlen, // characters after src that are used
8860 linenr_T *lnump, // line number for :e command, or NULL
8861 char **errormsg, // pointer to error message
8862 int *escaped) // return value has escaped white space (can
8863 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864{
8865 int i;
8866 char_u *s;
8867 char_u *result;
8868 char_u *resultbuf = NULL;
8869 int resultlen;
8870 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008871 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008873 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876
8877 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008878 if (escaped != NULL)
8879 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880
8881 /*
8882 * Check if there is something to do.
8883 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008884 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008885 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 {
8887 *usedlen = 1;
8888 return NULL;
8889 }
8890
8891 /*
8892 * Skip when preceded with a backslash "\%" and "\#".
8893 * Note: In "\\%" the % is also not recognized!
8894 */
8895 if (src > srcstart && src[-1] == '\\')
8896 {
8897 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008898 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 return NULL;
8900 }
8901
8902 /*
8903 * word or WORD under cursor
8904 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008905 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8906 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008908 resultlen = find_ident_under_cursor(&result,
8909 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8910 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8911 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 if (resultlen == 0)
8913 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008914 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 return NULL;
8916 }
8917 }
8918
8919 /*
8920 * '#': Alternate file name
8921 * '%': Current file name
8922 * File name under the cursor
8923 * File name for autocommand
8924 * and following modifiers
8925 */
8926 else
8927 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008928 int off = 0;
8929
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 switch (spec_idx)
8931 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008932 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008933#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008934 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008937 // '%': current file
8938 if (curbuf->b_fname == NULL)
8939 {
8940 result = (char_u *)"";
8941 valid = 0; // Must have ":p:h" to be valid
8942 }
8943 else
8944 {
8945 result = curbuf->b_fname;
8946 tilde_file = STRCMP(result, "~") == 0;
8947 }
8948 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008950#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008951 // "%%" alternate file
8952 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008953#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01008954 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008955 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008956 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008958 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 result = arg_all();
8960 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008961 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008962 if (escaped != NULL)
8963 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 break;
8966 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008967 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008968 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008969 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008971 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008972 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008973 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008974 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008976 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008978 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00008979 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008980 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008981 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008982 return NULL;
8983 }
8984#ifdef FEAT_EVAL
8985 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8986 (long)i);
8987 if (result == NULL)
8988 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008989 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008990 return NULL;
8991 }
8992#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008993 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 }
8997 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008998 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008999 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9000 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009001 buf = buflist_findnr(i);
9002 if (buf == NULL)
9003 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009004 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00009005 return NULL;
9006 }
9007 if (lnump != NULL)
9008 *lnump = ECMD_LAST;
9009 if (buf->b_fname == NULL)
9010 {
9011 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009012 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009013 }
9014 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009015 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009016 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009017 tilde_file = STRCMP(result, "~") == 0;
9018 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 break;
9021
9022#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009023 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009024 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 if (result == NULL)
9026 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009027 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 return NULL;
9029 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009030 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 break;
9032#endif
9033
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009034 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009036 if (result != NULL && !autocmd_fname_full)
9037 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009038 // Still need to turn the fname into a full path. It is
9039 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009040 autocmd_fname_full = TRUE;
9041 result = FullName_save(autocmd_fname, FALSE);
9042 vim_free(autocmd_fname);
9043 autocmd_fname = result;
9044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 if (result == NULL)
9046 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009047 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 return NULL;
9049 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009050 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 break;
9052
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009053 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 if (autocmd_bufnr <= 0)
9055 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009056 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 return NULL;
9058 }
9059 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9060 result = strbuf;
9061 break;
9062
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009063 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 result = autocmd_match;
9065 if (result == NULL)
9066 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009067 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 return NULL;
9069 }
9070 break;
9071
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009072 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02009073 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02009074 result = estack_sfile(spec_idx == SPEC_SFILE
9075 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 if (result == NULL)
9077 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02009078 *errormsg = spec_idx == SPEC_SFILE
9079 ? _("E498: no :source file name to substitute for \"<sfile>\"")
9080 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 return NULL;
9082 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009083 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009085
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009086 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009087 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009088 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009089 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009090 return NULL;
9091 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009092 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009093 result = strbuf;
9094 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009095
9096#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009097 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009098 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009099 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009100 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009101 return NULL;
9102 }
9103 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009104 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009105 result = strbuf;
9106 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009107
Bram Moolenaar90944302020-08-01 20:45:11 +02009108 case SPEC_SID:
9109 if (current_sctx.sc_sid <= 0)
9110 {
9111 *errormsg = _(e_usingsid);
9112 return NULL;
9113 }
9114 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9115 result = strbuf;
9116 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009117#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009118
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009119#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009120 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009121 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9122 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 result = strbuf;
9124 break;
9125#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009126
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009127 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009128 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009129 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 }
9131
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009132 resultlen = (int)STRLEN(result); // length of new string
9133 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 {
9135 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 resultlen = (int)(s - result);
9138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 else if (!skip_mod)
9140 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009141 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 &resultlen);
9143 if (result == NULL)
9144 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009145 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 return NULL;
9147 }
9148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 }
9150
9151 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9152 {
9153 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009154 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009155 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009157 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 result = NULL;
9159 }
9160 else
9161 result = vim_strnsave(result, resultlen);
9162 vim_free(resultbuf);
9163 return result;
9164}
9165
9166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 * Expand the <sfile> string in "arg".
9168 *
9169 * Returns an allocated string, or NULL for any error.
9170 */
9171 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009172expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009174 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 int len;
9176 char_u *result;
9177 char_u *newres;
9178 char_u *repl;
9179 int srclen;
9180 char_u *p;
9181
9182 result = vim_strsave(arg);
9183 if (result == NULL)
9184 return NULL;
9185
9186 for (p = result; *p; )
9187 {
9188 if (STRNCMP(p, "<sfile>", 7) != 0)
9189 ++p;
9190 else
9191 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009192 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00009193 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 if (errormsg != NULL)
9195 {
9196 if (*errormsg)
9197 emsg(errormsg);
9198 vim_free(result);
9199 return NULL;
9200 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009201 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 {
9203 p += srclen;
9204 continue;
9205 }
9206 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9207 newres = alloc(len);
9208 if (newres == NULL)
9209 {
9210 vim_free(repl);
9211 vim_free(result);
9212 return NULL;
9213 }
9214 mch_memmove(newres, result, (size_t)(p - result));
9215 STRCPY(newres + (p - result), repl);
9216 len = (int)STRLEN(newres);
9217 STRCAT(newres, p + srclen);
9218 vim_free(repl);
9219 vim_free(result);
9220 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009221 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 }
9223 }
9224
9225 return result;
9226}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009229/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009230 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009231 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009232 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009234dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 if (fname == NULL)
9237 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009238 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239}
9240#endif
9241
9242/*
9243 * ":behave {mswin,xterm}"
9244 */
9245 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009246ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247{
9248 if (STRCMP(eap->arg, "mswin") == 0)
9249 {
9250 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9251 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9252 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9253 set_option_value((char_u *)"keymodel", 0L,
9254 (char_u *)"startsel,stopsel", 0);
9255 }
9256 else if (STRCMP(eap->arg, "xterm") == 0)
9257 {
9258 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9259 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9260 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9261 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9262 }
9263 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009264 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265}
9266
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267static int filetype_detect = FALSE;
9268static int filetype_plugin = FALSE;
9269static int filetype_indent = FALSE;
9270
9271/*
9272 * ":filetype [plugin] [indent] {on,off,detect}"
9273 * on: Load the filetype.vim file to install autocommands for file types.
9274 * off: Load the ftoff.vim file to remove all autocommands for file types.
9275 * plugin on: load filetype.vim and ftplugin.vim
9276 * plugin off: load ftplugof.vim
9277 * indent on: load filetype.vim and indent.vim
9278 * indent off: load indoff.vim
9279 */
9280 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009281ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282{
9283 char_u *arg = eap->arg;
9284 int plugin = FALSE;
9285 int indent = FALSE;
9286
9287 if (*eap->arg == NUL)
9288 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009289 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009290 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 filetype_detect ? "ON" : "OFF",
9292 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9293 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9294 return;
9295 }
9296
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009297 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 for (;;)
9299 {
9300 if (STRNCMP(arg, "plugin", 6) == 0)
9301 {
9302 plugin = TRUE;
9303 arg = skipwhite(arg + 6);
9304 continue;
9305 }
9306 if (STRNCMP(arg, "indent", 6) == 0)
9307 {
9308 indent = TRUE;
9309 arg = skipwhite(arg + 6);
9310 continue;
9311 }
9312 break;
9313 }
9314 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9315 {
9316 if (*arg == 'o' || !filetype_detect)
9317 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009318 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 filetype_detect = TRUE;
9320 if (plugin)
9321 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009322 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 filetype_plugin = TRUE;
9324 }
9325 if (indent)
9326 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009327 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 filetype_indent = TRUE;
9329 }
9330 }
9331 if (*arg == 'd')
9332 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009333 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009334 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 }
9336 }
9337 else if (STRCMP(arg, "off") == 0)
9338 {
9339 if (plugin || indent)
9340 {
9341 if (plugin)
9342 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009343 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 filetype_plugin = FALSE;
9345 }
9346 if (indent)
9347 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009348 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 filetype_indent = FALSE;
9350 }
9351 }
9352 else
9353 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009354 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 filetype_detect = FALSE;
9356 }
9357 }
9358 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009359 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360}
9361
9362/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009363 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 */
9365 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009366ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367{
9368 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009369 {
9370 char_u *arg = eap->arg;
9371
9372 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9373 arg += 9;
9374
9375 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9376 if (arg != eap->arg)
9377 did_filetype = FALSE;
9378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009382ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383{
9384#ifdef FEAT_DIGRAPHS
9385 if (*eap->arg != NUL)
9386 putdigraph(eap->arg);
9387 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009388 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389#else
mityu61065042021-07-19 20:07:21 +02009390 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391#endif
9392}
9393
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009394#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9395 void
9396set_no_hlsearch(int flag)
9397{
9398 no_hlsearch = flag;
9399# ifdef FEAT_EVAL
9400 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9401# endif
9402}
9403
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404/*
9405 * ":nohlsearch"
9406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009408ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009410 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009411 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413#endif
9414
9415#ifdef FEAT_CRYPT
9416/*
9417 * ":X": Get crypt key
9418 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009420ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009422 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009423 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424}
9425#endif
9426
9427#ifdef FEAT_FOLDING
9428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009429ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430{
9431 if (foldManualAllowed(TRUE))
9432 foldCreate(eap->line1, eap->line2);
9433}
9434
9435 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009436ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437{
9438 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9439 eap->forceit, FALSE);
9440}
9441
9442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009443ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444{
9445 linenr_T lnum;
9446
Bram Moolenaar4facea32019-10-12 20:17:40 +02009447# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009448 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009449# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009450
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009451 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9453 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9454 ml_setmarked(lnum);
9455
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009456 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009458 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009459# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009460 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009461# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462}
9463#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009464
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009465#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009466/*
9467 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9468 * instead of a quickfix command.
9469 */
9470 int
9471is_loclist_cmd(int cmdidx)
9472{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009473 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009474 return FALSE;
9475 return cmdnames[cmdidx].cmd_name[0] == 'l';
9476}
9477#endif
9478
Bram Moolenaar4facea32019-10-12 20:17:40 +02009479#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009480 int
9481get_pressedreturn(void)
9482{
9483 return ex_pressedreturn;
9484}
9485
9486 void
9487set_pressedreturn(int val)
9488{
9489 ex_pressedreturn = val;
9490}
9491#endif