blob: 85fecbbbf1e30385908db20458ffce07c4437413 [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
290# define ex_finally ex_ni
291# define ex_finish ex_ni
292# define ex_function ex_ni
293# define ex_if ex_ni
294# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200295# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200296# define ex_lockvar ex_ni
297# define ex_oldfiles ex_ni
298# define ex_options ex_ni
299# define ex_packadd ex_ni
300# define ex_packloadall ex_ni
301# define ex_return ex_ni
302# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# define ex_throw ex_ni
304# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000306# define ex_unlockvar ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200307# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100308# define ex_import ex_ni
309# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200311#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_loadview ex_ni
313#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200314#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315# define ex_viminfo ex_ni
316#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_filetype(exarg_T *eap);
319static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000321# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322# define ex_diffpatch ex_ni
323# define ex_diffgetput ex_ni
324# define ex_diffsplit ex_ni
325# define ex_diffthis ex_ni
326# define ex_diffupdate ex_ni
327#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100328static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100330static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#else
332# define ex_nohlsearch ex_ni
333# define ex_match ex_ni
334#endif
335#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100336static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337#else
338# define ex_X ex_ni
339#endif
340#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100341static void ex_fold(exarg_T *eap);
342static void ex_foldopen(exarg_T *eap);
343static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344#else
345# define ex_fold ex_ni
346# define ex_foldopen ex_ni
347# define ex_folddo ex_ni
348#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100349#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350# define ex_language ex_ni
351#endif
352#ifndef FEAT_SIGNS
353# define ex_sign ex_ni
354#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000355#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200356# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000357# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200358# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361#ifndef FEAT_JUMPLIST
362# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200363# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364# define ex_changes ex_ni
365#endif
366
Bram Moolenaar05159a02005-02-26 23:04:13 +0000367#ifndef FEAT_PROFILE
368# define ex_profile ex_ni
369#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200370#ifndef FEAT_TERMINAL
371# define ex_terminal ex_ni
372#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200373#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
374# define ex_xrestore ex_ni
375#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100376#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200377# define ex_popupclear ex_ni
378#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000379
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380/*
381 * Declare cmdnames[].
382 */
383#define DO_DECLARE_EXCMD
384#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200385#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100386
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387static char_u dollar_command[2] = {'$', 0};
388
389
390#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100391// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392typedef struct
393{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100394 char_u *line; // command line
395 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396} wcmd_T;
397
398/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000399 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000401 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000403struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100405 garray_T *lines_gap; // growarray with line info
406 int current_line; // last read line from growarray
407 int repeating; // TRUE when looping a second time
408 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200409 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 void *cookie;
411};
412
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200413static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100414static int store_loop_line(garray_T *gap, char_u *line);
415static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000416
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100417// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000418struct dbg_stuff
419{
420 int trylevel;
421 int force_abort;
422 except_T *caught_stack;
423 char_u *vv_exception;
424 char_u *vv_throwpoint;
425 int did_emsg;
426 int got_int;
427 int did_throw;
428 int need_rethrow;
429 int check_cstack;
430 except_T *current_exception;
431};
432
Bram Moolenaared203462004-06-16 11:19:22 +0000433 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100434save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000435{
436 dsp->trylevel = trylevel; trylevel = 0;
437 dsp->force_abort = force_abort; force_abort = FALSE;
438 dsp->caught_stack = caught_stack; caught_stack = NULL;
439 dsp->vv_exception = v_exception(NULL);
440 dsp->vv_throwpoint = v_throwpoint(NULL);
441
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100442 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000443 dsp->did_emsg = did_emsg; did_emsg = FALSE;
444 dsp->got_int = got_int; got_int = FALSE;
445 dsp->did_throw = did_throw; did_throw = FALSE;
446 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
447 dsp->check_cstack = check_cstack; check_cstack = FALSE;
448 dsp->current_exception = current_exception; current_exception = NULL;
449}
450
451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100452restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000453{
454 suppress_errthrow = FALSE;
455 trylevel = dsp->trylevel;
456 force_abort = dsp->force_abort;
457 caught_stack = dsp->caught_stack;
458 (void)v_exception(dsp->vv_exception);
459 (void)v_throwpoint(dsp->vv_throwpoint);
460 did_emsg = dsp->did_emsg;
461 got_int = dsp->got_int;
462 did_throw = dsp->did_throw;
463 need_rethrow = dsp->need_rethrow;
464 check_cstack = dsp->check_cstack;
465 current_exception = dsp->current_exception;
466}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467#endif
468
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469/*
470 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
471 * command is given.
472 */
473 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100474do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100475 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476{
477 int save_msg_scroll;
478 int prev_msg_row;
479 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100480 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000481
482 if (improved)
483 exmode_active = EXMODE_VIM;
484 else
485 exmode_active = EXMODE_NORMAL;
486 State = NORMAL;
487
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100488 // When using ":global /pat/ visual" and then "Q" we return to continue
489 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000490 if (global_busy)
491 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492
493 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100494 ++RedrawingDisabled; // don't redisplay the window
495 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100497 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 ++hold_gui_events;
499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500
Bram Moolenaar32526b32019-01-19 17:43:09 +0100501 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 while (exmode_active)
503 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100504 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000505 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
506 {
507 exmode_active = FALSE;
508 break;
509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 msg_scroll = TRUE;
511 need_wait_return = FALSE;
512 ex_pressedreturn = FALSE;
513 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100514 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 prev_msg_row = msg_row;
516 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 if (improved)
518 {
519 cmdline_row = msg_row;
520 do_cmdline(NULL, getexline, NULL, 0);
521 }
522 else
523 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
524 lines_left = Rows - 1;
525
Bram Moolenaardf177f62005-02-22 08:39:57 +0000526 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100527 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000529 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100530 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000531 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000533 if (ex_pressedreturn)
534 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100535 // go up one line, to overwrite the ":<CR>" line, so the
536 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000537 msg_row = prev_msg_row;
538 if (prev_msg_row == Rows - 1)
539 msg_row--;
540 }
541 msg_col = 0;
542 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
543 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100546 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 {
548 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100549 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000550 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100551 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 }
554
555#ifdef FEAT_GUI
556 --hold_gui_events;
557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 --RedrawingDisabled;
559 --no_wait_return;
560 update_screen(CLEAR);
561 need_wait_return = FALSE;
562 msg_scroll = save_msg_scroll;
563}
564
565/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200566 * Print the executed command for when 'verbose' is set.
567 * When "lnum" is 0 only print the command.
568 */
569 static void
570msg_verbose_cmd(linenr_T lnum, char_u *cmd)
571{
572 ++no_wait_return;
573 verbose_enter_scroll();
574
575 if (lnum == 0)
576 smsg(_("Executing: %s"), cmd);
577 else
578 smsg(_("line %ld: %s"), (long)lnum, cmd);
579 if (msg_silent == 0)
580 msg_puts("\n"); // don't overwrite this
581
582 verbose_leave_scroll();
583 --no_wait_return;
584}
585
586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 * Execute a simple command line. Used for translated commands like "*".
588 */
589 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100590do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591{
592 return do_cmdline(cmd, NULL, NULL,
593 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
594}
595
596/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100597 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
598 * This allows for using a range without ":" in Vim9 script.
599 */
600 int
601do_cmd_argument(char_u *cmd)
602{
603 return do_cmdline(cmd, NULL, NULL,
604 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
605}
606
607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 * do_cmdline(): execute one Ex command line
609 *
610 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100611 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 * 2. Split up in parts separated with '|'.
613 *
614 * This function can be called recursively!
615 *
616 * flags:
617 * DOCMD_VERBOSE - The command will be included in the error message.
618 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100619 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 * DOCMD_KEYTYPED - Don't reset KeyTyped.
621 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
622 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
623 *
624 * return FAIL if cmdline could not be executed, OK otherwise
625 */
626 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100627do_cmdline(
628 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200629 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100630 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100631 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100633 char_u *next_cmdline; // next cmd to execute
634 char_u *cmdline_copy = NULL; // copy of cmd line
635 int used_getline = FALSE; // used "fgetline" to obtain command
636 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100638 int count = 0; // line number count
639 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 int retval = OK;
641#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100642 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100643 garray_T lines_ga; // keep lines for ":while"/":for"
644 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200645 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100646 char_u *fname = NULL; // function or script name
647 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
648 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
649 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200651 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200652 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100654 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200655 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000657 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000659 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100661# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662# define cmd_cookie cookie
663#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100664 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200665#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100666 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100668 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
669 // location for storing error messages to be converted to an exception.
670 // This ensures that the do_errthrow() call in do_one_cmd() does not
671 // combine the messages stored by an earlier invocation of do_one_cmd()
672 // with the command name of the later one. This would happen when
673 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 saved_msg_list = msg_list;
675 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676#endif
677
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100678 // It's possible to create an endless loop with ":execute", catch that
679 // here. The value of 200 allows nested function calls, ":source", etc.
680 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100681 if (call_depth >= 200
682#ifdef FEAT_EVAL
683 && call_depth >= p_mfd
684#endif
685 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100687 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100689 // When converting to an exception, we do not include the command name
690 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100691 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 msg_list = saved_msg_list;
693#endif
694 return FAIL;
695 }
696 ++call_depth;
697
698#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100699 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 cstack.cs_idx = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
702
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100703 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100705 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100706 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000707 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 ++ex_nesting_level;
709
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100710 // Get the function or script name and the address where the next breakpoint
711 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000712 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {
714 fname = func_name(real_cookie);
715 breakpoint = func_breakpoint(real_cookie);
716 dbg_tick = func_dbg_tick(real_cookie);
717 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100718 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100720 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 breakpoint = source_breakpoint(real_cookie);
722 dbg_tick = source_dbg_tick(real_cookie);
723 }
724
725 /*
726 * Initialize "force_abort" and "suppress_errthrow" at the top level.
727 */
728 if (!recursive)
729 {
730 force_abort = FALSE;
731 suppress_errthrow = FALSE;
732 }
733
734 /*
735 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000736 * exception handling (used when debugging). Otherwise clear it to avoid
737 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000739 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000740 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000741 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200742 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
744 initial_trylevel = trylevel;
745
746 /*
747 * "did_throw" will be set to TRUE when an exception is being thrown.
748 */
749 did_throw = FALSE;
750#endif
751 /*
752 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000753 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 * If force_abort is set, we cancel everything.
755 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100756#ifdef FEAT_EVAL
757 did_emsg_cumul += did_emsg;
758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 did_emsg = FALSE;
760
761 /*
762 * KeyTyped is only set when calling vgetc(). Reset it here when not
763 * calling vgetc() (sourced command lines).
764 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100765 if (!(flags & DOCMD_KEYTYPED)
766 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 KeyTyped = FALSE;
768
769 /*
770 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000771 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 * - for multiple commands on one line, separated with '|'
773 * - when repeating until there are no more lines (for ":source")
774 */
775 next_cmdline = cmdline;
776 do
777 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000778#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100779 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000780#endif
781
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100782 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 if (next_cmdline == NULL
784#ifdef FEAT_EVAL
785 && !force_abort
786 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000787 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788#endif
789 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100790 {
791#ifdef FEAT_EVAL
792 did_emsg_cumul += did_emsg;
793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
797 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000798 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100799 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 * 3. If a line is given: Make a copy, so we can mess with it.
801 */
802
803#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100804 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000805 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100807 // Each '|' separated command is stored separately in lines_ga, to
808 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100809 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100811 // Check if a function has returned or, unless it has an unclosed
812 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000813 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000815# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000816 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000817 func_line_end(real_cookie);
818# endif
819 if (func_has_ended(real_cookie))
820 {
821 retval = FAIL;
822 break;
823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000825#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000826 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000828 script_line_end();
829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100831 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100832 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 {
834 retval = FAIL;
835 break;
836 }
837
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100838 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (breakpoint != NULL && dbg_tick != NULL
840 && *dbg_tick != debug_tick)
841 {
842 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100843 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100844 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 *dbg_tick = debug_tick;
846 }
847
848 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100849 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100851 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100853 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100855 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100856 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100858 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100859 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 *dbg_tick = debug_tick;
861 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000862# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000863 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000864 {
865 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100866 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100867 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000868 script_line_start();
869 }
870# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872#endif
873
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100874 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (next_cmdline == NULL)
876 {
877 /*
878 * Need to set msg_didout for the first line after an ":if",
879 * otherwise the ":if" will be overwritten.
880 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100881 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100883 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#ifdef FEAT_EVAL
885 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
886#else
887 0
888#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100889 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
890 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100892 // Don't call wait_return for aborted command line. The NULL
893 // returned for the end of a sourced file or executed function
894 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 if (KeyTyped && !(flags & DOCMD_REPEAT))
896 need_wait_return = FALSE;
897 retval = FAIL;
898 break;
899 }
900 used_getline = TRUE;
901
902 /*
903 * Keep the first typed line. Clear it when more lines are typed.
904 */
905 if (flags & DOCMD_KEEPLINE)
906 {
907 vim_free(repeat_cmdline);
908 if (count == 0)
909 repeat_cmdline = vim_strsave(next_cmdline);
910 else
911 repeat_cmdline = NULL;
912 }
913 }
914
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100915 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 else if (cmdline_copy == NULL)
917 {
918 next_cmdline = vim_strsave(next_cmdline);
919 if (next_cmdline == NULL)
920 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100921 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 retval = FAIL;
923 break;
924 }
925 }
926 cmdline_copy = next_cmdline;
927
928#ifdef FEAT_EVAL
929 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200930 * Inside a while/for loop, and when the command looks like a ":while"
931 * or ":for", the line is stored, because we may need it later when
932 * looping.
933 *
934 * When there is a '|' and another command, it is stored separately,
935 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000936 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200937 *
938 * Pass a different "fgetline" function to do_one_cmd() below,
939 * that it stores lines in or reads them from "lines_ga". Makes it
940 * possible to define a function inside a while/for loop and handles
941 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200943 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200945 cmd_getline = get_loop_line;
946 cmd_cookie = (void *)&cmd_loop_cookie;
947 cmd_loop_cookie.lines_gap = &lines_ga;
948 cmd_loop_cookie.current_line = current_line;
949 cmd_loop_cookie.getline = fgetline;
950 cmd_loop_cookie.cookie = cookie;
951 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
952
953 // Save the current line when encountering it the first time.
954 if (current_line == lines_ga.ga_len
955 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 {
957 retval = FAIL;
958 break;
959 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200960 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200962 else
963 {
964 cmd_getline = fgetline;
965 cmd_cookie = cookie;
966 }
967
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 did_endif = FALSE;
969#endif
970
971 if (count++ == 0)
972 {
973 /*
974 * All output from the commands is put below each other, without
975 * waiting for a return. Don't do this when executing commands
976 * from a script or when being called recursive (e.g. for ":e
977 * +command file").
978 */
979 if (!(flags & DOCMD_NOWAIT) && !recursive)
980 {
981 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100982 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100984 msg_scroll = TRUE; // put messages below each other
985 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 ++RedrawingDisabled;
987 did_inc = TRUE;
988 }
989 }
990
Bram Moolenaar823654b2020-05-29 23:03:09 +0200991 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100992 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
994 /*
995 * 2. Execute one '|' separated command.
996 * do_one_cmd() will return NULL if there is no trailing '|'.
997 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
998 */
999 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001000 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001#ifdef FEAT_EVAL
1002 &cstack,
1003#endif
1004 cmd_getline, cmd_cookie);
1005 --recursive;
1006
1007#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001008 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001009 // Use "current_line" from "cmd_loop_cookie", it may have been
1010 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001011 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012#endif
1013
1014 if (next_cmdline == NULL)
1015 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001016 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001017
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 /*
1019 * If the command was typed, remember it for the ':' register.
1020 * Do this AFTER executing the command to make :@: work.
1021 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001022 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 && new_last_cmdline != NULL)
1024 {
1025 vim_free(last_cmdline);
1026 last_cmdline = new_last_cmdline;
1027 new_last_cmdline = NULL;
1028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 }
1030 else
1031 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001032 // need to copy the command after the '|' to cmdline_copy, for the
1033 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001034 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 next_cmdline = cmdline_copy;
1036 }
1037
1038
1039#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001040 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001042 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001044 {
1045 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001049 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 {
1051 ++current_line;
1052
1053 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001054 * An ":endwhile", ":endfor" and ":continue" is handled here.
1055 * If we were executing commands, jump back to the ":while" or
1056 * ":for".
1057 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001059 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001061 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001063 // Jump back to the matching ":while" or ":for". Be careful
1064 // not to use a cs_line[] from an entry that isn't a ":while"
1065 // or ":for": It would make "current_line" invalid and can
1066 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 if (!did_emsg && !got_int && !did_throw
1068 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001069 && (cstack.cs_flags[cstack.cs_idx]
1070 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 && cstack.cs_line[cstack.cs_idx] >= 0
1072 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1073 {
1074 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001075 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001076 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001077 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001079 // Check for the next breakpoint at or after the ":while"
1080 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 if (breakpoint != NULL)
1082 {
1083 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001084 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 fname,
1086 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1087 *dbg_tick = debug_tick;
1088 }
1089 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001090 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001092 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001094 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1095 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 }
1097 }
1098
1099 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001100 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001102 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001104 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001105 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 }
1107 }
1108
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001109 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001110 if (breakpoint != NULL && has_watchexpr())
1111 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001112 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001113 *dbg_tick = debug_tick;
1114 }
1115
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 /*
1117 * When not inside any ":while" loop, clear remembered lines.
1118 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001119 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 {
1121 if (lines_ga.ga_len > 0)
1122 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001123 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1125 free_cmdlines(&lines_ga);
1126 }
1127 current_line = 0;
1128 }
1129
1130 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1132 * being restored at the ":endtry". Reset them here and set the
1133 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1134 * This includes the case where a missing ":endif", ":endwhile" or
1135 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001137 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001139 cstack.cs_lflags &= ~CSL_HAD_FINA;
1140 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1141 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 did_throw ? (void *)current_exception : NULL);
1143 did_emsg = got_int = did_throw = FALSE;
1144 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1145 }
1146
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001147 // Update global "trylevel" for recursive calls to do_cmdline() from
1148 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 trylevel = initial_trylevel + cstack.cs_trylevel;
1150
1151 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001152 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 * files) is aborted because of an error, an interrupt, or an uncaught
1154 * exception, cancel everything. If it is left normally, reset
1155 * force_abort to get the non-EH compatible abortion behavior for
1156 * the rest of the script.
1157 */
1158 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1159 force_abort = FALSE;
1160
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001161 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001163#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164
1165 }
1166 /*
1167 * Continue executing command lines when:
1168 * - no CTRL-C typed, no aborting error, no exception thrown or try
1169 * conditionals need to be checked for executing finally clauses or
1170 * catching an interrupt exception
1171 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001172 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 * looping for ":source" command or function call.
1174 */
1175 while (!((got_int
1176#ifdef FEAT_EVAL
1177 || (did_emsg && force_abort) || did_throw
1178#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.
1211 */
1212 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001213 && ((getline_equal(fgetline, cookie, getsourceline)
1214 && !source_finished(fgetline, cookie))
1215 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 && !func_has_ended(real_cookie))))
1217 {
1218 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001219 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001221 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001222 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001223 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001225 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 }
1227
1228 /*
1229 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1230 * ":endtry" in a sourced file or executed function. If the try
1231 * conditional is in its finally clause, ignore anything pending.
1232 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001233 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 */
1235 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001236 {
1237 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1238
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001239 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001240 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001241 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1242 &cstack.cs_looplevel);
1243 }
1244 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 trylevel = initial_trylevel;
1246 }
1247
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001248 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1249 // lack was reported above and the error message is to be converted to an
1250 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001251 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 ? (char_u *)"endfunction" : (char_u *)NULL);
1253
1254 if (trylevel == 0)
1255 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001256 // Just in case did_throw got set but current_exception wasn't.
1257 if (current_exception == NULL)
1258 did_throw = FALSE;
1259
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 /*
1261 * When an exception is being thrown out of the outermost try
1262 * conditional, discard the uncaught exception, disable the conversion
1263 * of interrupts or errors to exceptions, and ensure that no more
1264 * commands are executed.
1265 */
1266 if (did_throw)
1267 {
Bram Moolenaar033135e2021-01-14 21:40:22 +01001268 char *p = NULL;
Bram Moolenaare8c46602021-04-05 22:27:37 +02001269 msglist_T *messages = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
1271 /*
1272 * If the uncaught exception is a user exception, report it as an
1273 * error. If it is an error exception, display the saved error
1274 * message now. For an interrupt exception, do nothing; the
1275 * interrupt message is given elsewhere.
1276 */
1277 switch (current_exception->type)
1278 {
1279 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001280 vim_snprintf((char *)IObuff, IOSIZE,
1281 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 current_exception->value);
Bram Moolenaar033135e2021-01-14 21:40:22 +01001283 p = (char *)vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 break;
1285 case ET_ERROR:
1286 messages = current_exception->messages;
1287 current_exception->messages = NULL;
1288 break;
1289 case ET_INTERRUPT:
1290 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001293 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1294 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001295 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 current_exception->throw_name = NULL;
1297
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001298 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 suppress_errthrow = TRUE;
1300 force_abort = TRUE;
1301
1302 if (messages != NULL)
1303 {
1304 do
1305 {
Bram Moolenaare8c46602021-04-05 22:27:37 +02001306 msglist_T *next = messages->next;
1307 int save_compiling = estack_compiling;
1308
1309 estack_compiling = messages->msg_compiling;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 emsg(messages->msg);
1311 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001312 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 vim_free(messages);
1314 messages = next;
Bram Moolenaare8c46602021-04-05 22:27:37 +02001315 estack_compiling = save_compiling;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 }
1317 while (messages != NULL);
1318 }
1319 else if (p != NULL)
1320 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001321 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 vim_free(p);
1323 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001324 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001325 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001326 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 }
1328
1329 /*
1330 * On an interrupt or an aborting error not converted to an exception,
1331 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001332 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 * when force_abort is set and did_emsg unset in case of an interrupt
1334 * from a finally clause after an error.
1335 */
1336 else if (got_int || (did_emsg && force_abort))
1337 suppress_errthrow = TRUE;
1338 }
1339
1340 /*
1341 * The current cstack will be freed when do_cmdline() returns. An uncaught
1342 * exception will have to be rethrown in the previous cstack. If a function
1343 * has just returned or a script file was just finished and the previous
1344 * cstack belongs to the same function or, respectively, script file, it
1345 * will have to be checked for finally clauses to be executed due to the
1346 * ":return" or ":finish". This is done in do_one_cmd().
1347 */
1348 if (did_throw)
1349 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001350 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001352 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 && ex_nesting_level > func_level(real_cookie) + 1))
1354 {
1355 if (!did_throw)
1356 check_cstack = TRUE;
1357 }
1358 else
1359 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001360 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001361 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 --ex_nesting_level;
1363 /*
1364 * Go to debug mode when returning from a function in which we are
1365 * single-stepping.
1366 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001367 if ((getline_equal(fgetline, cookie, getsourceline)
1368 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001370 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 ? (char_u *)_("End of sourced file")
1372 : (char_u *)_("End of function"));
1373 }
1374
1375 /*
1376 * Restore the exception environment (done after returning from the
1377 * debugger).
1378 */
1379 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001380 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
1382 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001383
1384 // Cleanup if "cs_emsg_silent_list" remains.
1385 if (cstack.cs_emsg_silent_list != NULL)
1386 {
1387 eslist_T *elem, *temp;
1388
1389 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1390 {
1391 temp = elem->next;
1392 vim_free(elem);
1393 }
1394 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001395#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396
1397 /*
1398 * If there was too much output to fit on the command line, ask the user to
1399 * hit return before redrawing the screen. With the ":global" command we do
1400 * this only once after the command is finished.
1401 */
1402 if (did_inc)
1403 {
1404 --RedrawingDisabled;
1405 --no_wait_return;
1406 msg_scroll = FALSE;
1407
1408 /*
1409 * When just finished an ":if"-":else" which was typed, no need to
1410 * wait for hit-return. Also for an error situation.
1411 */
1412 if (retval == FAIL
1413#ifdef FEAT_EVAL
1414 || (did_endif && KeyTyped && !did_emsg)
1415#endif
1416 )
1417 {
1418 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001419 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 }
1421 else if (need_wait_return)
1422 {
1423 /*
1424 * The msg_start() above clears msg_didout. The wait_return we do
1425 * here should not overwrite the command that may be shown before
1426 * doing that.
1427 */
1428 msg_didout |= msg_didout_before_start;
1429 wait_return(FALSE);
1430 }
1431 }
1432
Bram Moolenaar2a942252012-11-28 23:03:07 +01001433#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001434 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001435#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 /*
1437 * Reset if_level, in case a sourced script file contains more ":if" than
1438 * ":endif" (could be ":if x | foo | endif").
1439 */
1440 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001441#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001442
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 --call_depth;
1444 return retval;
1445}
1446
1447#ifdef FEAT_EVAL
1448/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001449 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 */
1451 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001452get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001454 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 wcmd_T *wp;
1456 char_u *line;
1457
1458 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1459 {
1460 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001461 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001463 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001465 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001467 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001468 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 ++cp->current_line;
1470
1471 return line;
1472 }
1473
1474 KeyTyped = FALSE;
1475 ++cp->current_line;
1476 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001477 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 return vim_strsave(wp->line);
1479}
1480
1481/*
1482 * Store a line in "gap" so that a ":while" loop can execute it again.
1483 */
1484 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001485store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486{
1487 if (ga_grow(gap, 1) == FAIL)
1488 return FAIL;
1489 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001490 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 return OK;
1493}
1494
1495/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001496 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 */
1498 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001499free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500{
1501 while (gap->ga_len > 0)
1502 {
1503 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1504 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 }
1506}
1507#endif
1508
1509/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001510 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1511 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001514getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001515 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001516 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001517 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
1519#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001520 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001521 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001523 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1524 // function that's originally used to obtain the lines. This may be
1525 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001526 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001527 cp = (struct loop_cookie *)cookie;
1528 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 {
1530 gp = cp->getline;
1531 cp = cp->cookie;
1532 }
1533 return gp == func;
1534#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001535 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536#endif
1537}
1538
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001540 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 * getline function. Otherwise return "cookie".
1542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001544getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001545 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001546 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
Bram Moolenaar13505972019-01-24 15:04:48 +01001548#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001549 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001550 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001552 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1553 // cookie that's originally used to obtain the lines. This may be nested
1554 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001555 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001556 cp = (struct loop_cookie *)cookie;
1557 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 {
1559 gp = cp->getline;
1560 cp = cp->cookie;
1561 }
1562 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001563#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001566}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567
Bram Moolenaard5053d02020-06-28 15:51:16 +02001568#if defined(FEAT_EVAL) || defined(PROT)
1569/*
1570 * Get the next line source line without advancing.
1571 */
1572 char_u *
1573getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001574 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001575 void *cookie) // argument for fgetline()
1576{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001577 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001578 struct loop_cookie *cp;
1579 wcmd_T *wp;
1580
1581 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1582 // cookie that's originally used to obtain the lines. This may be nested
1583 // several levels.
1584 gp = fgetline;
1585 cp = (struct loop_cookie *)cookie;
1586 while (gp == get_loop_line)
1587 {
1588 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1589 {
1590 // executing lines a second time, use the stored copy
1591 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1592 return wp->line;
1593 }
1594 gp = cp->getline;
1595 cp = cp->cookie;
1596 }
1597 if (gp == getsourceline)
1598 return source_nextline(cp);
1599 return NULL;
1600}
1601#endif
1602
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001603
1604/*
1605 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1606 * ":bwipeout", etc.
1607 * Returns the buffer number.
1608 */
1609 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001610compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001611{
1612 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001613 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001614 int count = offset;
1615
1616 buf = firstbuf;
1617 while (buf->b_next != NULL && buf->b_fnum < lnum)
1618 buf = buf->b_next;
1619 while (count != 0)
1620 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001621 count += (offset < 0) ? 1 : -1;
1622 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1623 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001624 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001625 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001626 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001627 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001628 while (buf->b_ml.ml_mfp == NULL)
1629 {
1630 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1631 if (nextbuf == NULL)
1632 break;
1633 buf = nextbuf;
1634 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001635 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001636 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001637 if (addr_type == ADDR_LOADED_BUFFERS)
1638 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 return buf->b_fnum;
1646}
1647
Bram Moolenaarb7316892019-05-01 18:08:42 +02001648/*
1649 * Return the window number of "win".
1650 * When "win" is NULL return the number of windows.
1651 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001652 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001653current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001654{
1655 win_T *wp;
1656 int nr = 0;
1657
Bram Moolenaar29323592016-07-24 22:04:11 +02001658 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001659 {
1660 ++nr;
1661 if (wp == win)
1662 break;
1663 }
1664 return nr;
1665}
1666
1667 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001668current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001669{
1670 tabpage_T *tp;
1671 int nr = 0;
1672
Bram Moolenaar29323592016-07-24 22:04:11 +02001673 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001674 {
1675 ++nr;
1676 if (tp == tab)
1677 break;
1678 }
1679 return nr;
1680}
1681
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001682 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001683comment_start(char_u *p, int starts_with_colon UNUSED)
1684{
1685#ifdef FEAT_EVAL
1686 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001687 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001688#endif
1689 return *p == '"';
1690}
1691
Bram Moolenaarf240e182014-11-27 18:33:02 +01001692# define CURRENT_WIN_NR current_win_nr(curwin)
1693# define LAST_WIN_NR current_win_nr(NULL)
1694# define CURRENT_TAB_NR current_tab_nr(curtab)
1695# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001696
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697/*
1698 * Execute one Ex command.
1699 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001700 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1701 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 *
1703 * 1. skip comment lines and leading space
1704 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001705 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001706 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001707 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001708 * 6. parse arguments
1709 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001711 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 *
1713 * This function may be called recursively!
1714 */
1715#if (_MSC_VER == 1200)
1716/*
Bram Moolenaared203462004-06-16 11:19:22 +00001717 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001719 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720#endif
1721 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001722do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001723 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001724 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001726 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001728 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001729 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001731 char_u *p;
1732 linenr_T lnum;
1733 long n;
1734 char *errormsg = NULL; // error message
1735 char_u *after_modifier = NULL;
1736 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001737 cmdmod_T save_cmdmod;
1738 int save_reg_executing = reg_executing;
1739 int ni; // set when Not Implemented
1740 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001741 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001742#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001743 int may_have_range;
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001744 int vim9script;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001745 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001746#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001747 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
Bram Moolenaara80faa82020-04-12 19:37:17 +02001749 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 ea.line1 = 1;
1751 ea.line2 = 1;
1752#ifdef FEAT_EVAL
1753 ++ex_nesting_level;
1754#endif
1755
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001756 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 if (quitmore
1758#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001759 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001760 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001761#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001762 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001763 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 --quitmore;
1765
1766 /*
1767 * Reset browse, confirm, etc.. They are restored when returning, for
1768 * recursive calls.
1769 */
1770 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001772 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001773 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1774 goto doend;
1775
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001776/*
1777 * 1. Skip comment lines and leading white space and colons.
1778 * 2. Handle command modifiers.
1779 */
1780 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001782 ea.cmdlinep = cmdlinep;
1783 ea.getline = fgetline;
1784 ea.cookie = cookie;
1785#ifdef FEAT_EVAL
1786 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001787 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001789 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001790 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001791 apply_cmdmod(&cmdmod);
Bram Moolenaar2379f872021-02-14 14:07:34 +01001792#ifdef FEAT_EVAL
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001793 vim9script = in_vim9script();
Bram Moolenaar2379f872021-02-14 14:07:34 +01001794#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001795 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796
1797#ifdef FEAT_EVAL
1798 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1799 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1800#else
1801 ea.skip = (if_level > 0);
1802#endif
1803
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001805 * 3. Skip over the range to find the command. Let "p" point to after it.
1806 *
1807 * We need the command to know what kind of range it uses.
1808 */
1809 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001810#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001811 // In Vim9 script a colon is required before the range. This may also be
1812 // after command modifiers.
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001813 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001814 {
1815 may_have_range = FALSE;
1816 for (p = ea.cmd; p >= *cmdlinep; --p)
1817 {
1818 if (*p == ':')
1819 may_have_range = TRUE;
1820 if (p < ea.cmd && !VIM_ISWHITE(*p))
1821 break;
1822 }
1823 }
1824 else
1825 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001826 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001827#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001828 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829
1830#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001831 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001832 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001833 if (ea.cmd == cmd + 1 && *cmd == '$')
1834 // should be "$VAR = val"
1835 --ea.cmd;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001836 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001837 if (ea.cmdidx == CMD_SIZE)
1838 {
1839 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1840
1841 // If a ':' before the range is missing, give a clearer error
1842 // message.
1843 if (ar > ea.cmd)
1844 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001845 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001846 goto doend;
1847 }
1848 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001849 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 else
1851#endif
1852 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001853
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001854#ifdef FEAT_EVAL
1855# ifdef FEAT_PROFILE
1856 // Count this line for profiling if skip is TRUE.
1857 if (do_profiling == PROF_YES
1858 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1859 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1860 {
1861 int skip = did_emsg || got_int || did_throw;
1862
1863 if (ea.cmdidx == CMD_catch)
1864 skip = !skip && !(cstack->cs_idx >= 0
1865 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1866 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1867 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1868 skip = skip || !(cstack->cs_idx >= 0
1869 && !(cstack->cs_flags[cstack->cs_idx]
1870 & (CSF_ACTIVE | CSF_TRUE)));
1871 else if (ea.cmdidx == CMD_finally)
1872 skip = FALSE;
1873 else if (ea.cmdidx != CMD_endif
1874 && ea.cmdidx != CMD_endfor
1875 && ea.cmdidx != CMD_endtry
1876 && ea.cmdidx != CMD_endwhile)
1877 skip = ea.skip;
1878
1879 if (!skip)
1880 {
1881 if (getline_equal(fgetline, cookie, get_func_line))
1882 func_line_exec(getline_cookie(fgetline, cookie));
1883 else if (getline_equal(fgetline, cookie, getsourceline))
1884 script_line_exec();
1885 }
1886 }
1887# endif
1888
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001889 // May go to debug mode. If this happens and the ">quit" debug command is
1890 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001891 dbg_check_breakpoint(&ea);
1892 if (!ea.skip && got_int)
1893 {
1894 ea.skip = TRUE;
1895 (void)do_intthrow(cstack);
1896 }
1897#endif
1898
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001899/*
1900 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 *
1902 * where 'addr' is:
1903 *
1904 * % (entire file)
1905 * $ [+-NUM]
1906 * 'x [+-NUM] (where x denotes a currently defined mark)
1907 * . [+-NUM]
1908 * [+-NUM]..
1909 * NUM
1910 *
1911 * The ea.cmd pointer is updated to point to the first character following the
1912 * range spec. If an initial address is found, but no second, the upper bound
1913 * is equal to the lower.
1914 */
1915
Bram Moolenaar25190db2019-05-04 15:05:28 +02001916 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001917 if (!IS_USER_CMDIDX(ea.cmdidx))
1918 {
1919 if (ea.cmdidx != CMD_SIZE)
1920 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1921 else
1922 ea.addr_type = ADDR_LINES;
1923
Bram Moolenaar25190db2019-05-04 15:05:28 +02001924 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001925 if (ea.cmdidx == CMD_wincmd && p != NULL)
1926 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001927#ifdef FEAT_QUICKFIX
1928 // :.cc in quickfix window uses line number
1929 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1930 ea.addr_type = ADDR_OTHER;
1931#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001932 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001933
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001934 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001935#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001936 if (!may_have_range)
1937 ea.line1 = ea.line2 = default_address(&ea);
1938 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001939#endif
1940 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1941 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001944 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 */
1946
1947 /*
1948 * Skip ':' and any white space
1949 */
1950 ea.cmd = skipwhite(ea.cmd);
1951 while (*ea.cmd == ':')
1952 ea.cmd = skipwhite(ea.cmd + 1);
1953
1954 /*
1955 * If we got a line, but no command, then go to the line.
1956 * If we find a '|' or '\n' we set ea.nextcmd.
1957 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001958 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1959 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 {
1961 /*
1962 * strange vi behaviour:
1963 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001964 * ":3|..." prints line 3 (not in Vim9 script)
1965 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001967 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 goto doend;
Bram Moolenaarb8554302021-02-15 21:30:30 +01001969 if ((*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
1970#ifdef FEAT_EVAL
1971 && !vim9script
1972#endif
1973 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 {
1975 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001976 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 if ((errormsg = invalid_range(&ea)) == NULL)
1978 {
1979 correct_range(&ea);
1980 ex_print(&ea);
1981 }
1982 }
1983 else if (ea.addr_count != 0)
1984 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001985 if (ea.line2 > curbuf->b_ml.ml_line_count)
1986 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001987 // With '-' in 'cpoptions' a line number past the file is an
1988 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001989 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1990 ea.line2 = -1;
1991 else
1992 ea.line2 = curbuf->b_ml.ml_line_count;
1993 }
1994
1995 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001996 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 else
1998 {
1999 if (ea.line2 == 0)
2000 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 else
2002 curwin->w_cursor.lnum = ea.line2;
2003 beginline(BL_SOL | BL_FIX);
2004 }
2005 }
2006 goto doend;
2007 }
2008
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002009 // If this looks like an undefined user command and there are CmdUndefined
2010 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002011 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2012 && ASCII_ISUPPER(*ea.cmd)
2013 && has_cmdundefined())
2014 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002015 int ret;
2016
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002017 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002018 while (ASCII_ISALNUM(*p))
2019 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002020 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002021 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2022 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002023 // If the autocommands did something and didn't cause an error, try
2024 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002025 p = (ret
2026#ifdef FEAT_EVAL
2027 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002028#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002030 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002031
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 if (p == NULL)
2033 {
2034 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002035 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 goto doend;
2037 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002038 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002039 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2040 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {
2042 errormsg = uc_fun_cmd();
2043 goto doend;
2044 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002045
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 if (ea.cmdidx == CMD_SIZE)
2047 {
2048 if (!ea.skip)
2049 {
2050 STRCPY(IObuff, _("E492: Not an editor command"));
2051 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002052 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002053 // If the modifier was parsed OK the error must be in the
2054 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002055 if (after_modifier != NULL)
2056 append_command(after_modifier);
2057 else
2058 append_command(*cmdlinep);
2059 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002060 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002061 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 }
2063 goto doend;
2064 }
2065
Bram Moolenaar958636c2014-10-21 20:01:58 +02002066 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2067 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002068#ifdef HAVE_EX_SCRIPT_NI
2069 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2070#endif
2071 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072
2073#ifndef FEAT_EVAL
2074 /*
2075 * When the expression evaluation is disabled, recognize the ":if" and
2076 * ":endif" commands and ignore everything in between it.
2077 */
2078 if (ea.cmdidx == CMD_if)
2079 ++if_level;
2080 if (if_level)
2081 {
2082 if (ea.cmdidx == CMD_endif)
2083 --if_level;
2084 goto doend;
2085 }
2086
2087#endif
2088
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002089 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002090 if (*p == '!' && ea.cmdidx != CMD_substitute
2091 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
2093 ++p;
2094 ea.forceit = TRUE;
2095 }
2096 else
2097 ea.forceit = FALSE;
2098
2099/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002100 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002102 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002103 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104
2105 if (!ea.skip)
2106 {
2107#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002108 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002110 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002111 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 goto doend;
2113 }
2114#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002115 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002116 {
2117 errormsg = _("E981: Command not allowed in rvim");
2118 goto doend;
2119 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002120 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002122 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002123 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 goto doend;
2125 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002126
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002127 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002129#ifdef FEAT_CMDWIN
2130 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2131 {
2132 // Command not allowed in the command line window
2133 errormsg = _(e_cmdwin);
2134 goto doend;
2135 }
2136#endif
2137 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2138 {
2139 // Command not allowed when text is locked
2140 errormsg = _(get_text_locked_msg());
2141 goto doend;
2142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002144
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002145 // Disallow editing another buffer when "curbuf_lock" is set.
2146 // Do allow ":checktime" (it is postponed).
2147 // Do allow ":edit" (check for an argument later).
2148 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002149 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002150 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002151 && ea.cmdidx != CMD_edit
2152 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002153 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002154 && curbuf_locked())
2155 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002157 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002159 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002160 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 goto doend;
2162 }
2163 }
2164
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002165 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002167 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 goto doend;
2169 }
2170
2171 /*
2172 * Don't complain about the range if it is not used
2173 * (could happen if line_count is accidentally set to 0).
2174 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002175 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
2177 /*
2178 * If the range is backwards, ask for confirmation and, if given, swap
2179 * ea.line1 & ea.line2 so it's forwards again.
2180 * When global command is busy, don't ask, will fail below.
2181 */
2182 if (!global_busy && ea.line1 > ea.line2)
2183 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002184 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002186 if (sourcing || exmode_active)
2187 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002188 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002189 goto doend;
2190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 if (ask_yesno((char_u *)
2192 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002193 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 }
2195 lnum = ea.line1;
2196 ea.line1 = ea.line2;
2197 ea.line2 = lnum;
2198 }
2199 if ((errormsg = invalid_range(&ea)) != NULL)
2200 goto doend;
2201 }
2202
Bram Moolenaarb7316892019-05-01 18:08:42 +02002203 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2204 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 ea.line2 = 1;
2206
2207 correct_range(&ea);
2208
2209#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002210 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002211 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002213 // Put the first line at the start of a closed fold, put the last line
2214 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 (void)hasFolding(ea.line1, &ea.line1, NULL);
2216 (void)hasFolding(ea.line2, NULL, &ea.line2);
2217 }
2218#endif
2219
2220#ifdef FEAT_QUICKFIX
2221 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002222 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 * option here, so things like % get expanded.
2224 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002225 p = replace_makeprg(&ea, p, cmdlinep);
2226 if (p == NULL)
2227 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228#endif
2229
2230 /*
2231 * Skip to start of argument.
2232 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2233 */
2234 if (ea.cmdidx == CMD_bang)
2235 ea.arg = p;
2236 else
2237 ea.arg = skipwhite(p);
2238
Bram Moolenaar379fb762018-08-30 15:58:28 +02002239 // ":file" cannot be run with an argument when "curbuf_lock" is set
2240 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2241 goto doend;
2242
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 /*
2244 * Check for "++opt=val" argument.
2245 * Must be first, allow ":w ++enc=utf8 !cmd"
2246 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002247 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2249 if (getargopt(&ea) == FAIL && !ni)
2250 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002251 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 goto doend;
2253 }
2254
2255 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2256 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002257 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002259 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002261 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 goto doend;
2263 }
2264 ea.arg = skipwhite(ea.arg + 1);
2265 ea.append = TRUE;
2266 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002267 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 {
2269 ++ea.arg;
2270 ea.usefilter = TRUE;
2271 }
2272 }
2273
2274 if (ea.cmdidx == CMD_read)
2275 {
2276 if (ea.forceit)
2277 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002278 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 ea.forceit = FALSE;
2280 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002281 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 {
2283 ++ea.arg;
2284 ea.usefilter = TRUE;
2285 }
2286 }
2287
2288 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2289 {
2290 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002291 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 {
2293 ++ea.arg;
2294 ++ea.amount;
2295 }
2296 ea.arg = skipwhite(ea.arg);
2297 }
2298
2299 /*
2300 * Check for "+command" argument, before checking for next command.
2301 * Don't do this for ":read !cmd" and ":write !cmd".
2302 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002303 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2305
2306 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002307 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 * Don't do this for ":read !cmd" and ":write !cmd".
2309 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002310 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 separate_nextcmd(&ea);
2312
2313 /*
2314 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002315 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2316 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002318 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002319 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002320 || ea.cmdidx == CMD_global
2321 || ea.cmdidx == CMD_vglobal
2322 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
2324 for (p = ea.arg; *p; ++p)
2325 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002326 // Remove one backslash before a newline, so that it's possible to
2327 // pass a newline to the shell and also a newline that is preceded
2328 // with a backslash. This makes it impossible to end a shell
2329 // command in a backslash, but that doesn't appear useful.
2330 // Halving the number of backslashes is incompatible with previous
2331 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002333 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 else if (*p == '\n')
2335 {
2336 ea.nextcmd = p + 1;
2337 *p = NUL;
2338 break;
2339 }
2340 }
2341 }
2342
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002343 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002344 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002346 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002347 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002349 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002350 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002351 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002353#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002354 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002355 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002357 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002358 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 }
2360#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002361 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2362 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002363 {
2364 ea.regname = *ea.arg++;
2365#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002366 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002367 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2368 {
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002369 set_expr_line(vim_strsave(ea.arg), &ea);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002370 ea.arg += STRLEN(ea.arg);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002371 did_set_expr_line = TRUE;
Bram Moolenaar85de2062011-05-05 14:26:41 +02002372 }
2373#endif
2374 ea.arg = skipwhite(ea.arg);
2375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 }
2377
2378 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002379 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 * count, it's a buffer name.
2381 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002382 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2383 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002384 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 {
2386 n = getdigits(&ea.arg);
2387 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002388 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002390 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 goto doend;
2392 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002393 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 {
2395 ea.line2 = n;
2396 if (ea.addr_count == 0)
2397 ea.addr_count = 1;
2398 }
2399 else
2400 {
2401 ea.line1 = ea.line2;
2402 ea.line2 += n - 1;
2403 ++ea.addr_count;
2404 /*
2405 * Be vi compatible: no error message for out of range.
2406 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002407 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 ea.line2 = curbuf->b_ml.ml_line_count;
2409 }
2410 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002411
2412 /*
2413 * Check for flags: 'l', 'p' and '#'.
2414 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002415 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002416 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002417 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2418 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002420 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002421 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 goto doend;
2423 }
2424
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002425 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002427 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 goto doend;
2429 }
2430
2431#ifdef FEAT_EVAL
2432 /*
2433 * Skip the command when it's not going to be executed.
2434 * The commands like :if, :endif, etc. always need to be executed.
2435 * Also make an exception for commands that handle a trailing command
2436 * themselves.
2437 */
2438 if (ea.skip)
2439 {
2440 switch (ea.cmdidx)
2441 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002442 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 case CMD_while:
2444 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002445 case CMD_for:
2446 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 case CMD_if:
2448 case CMD_elseif:
2449 case CMD_else:
2450 case CMD_endif:
2451 case CMD_try:
2452 case CMD_catch:
2453 case CMD_finally:
2454 case CMD_endtry:
2455 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002456 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 break;
2458
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002459 // Commands that handle '|' themselves. Check: A command should
2460 // either have the EX_TRLBAR flag, appear in this list or appear in
2461 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 case CMD_aboveleft:
2463 case CMD_and:
2464 case CMD_belowright:
2465 case CMD_botright:
2466 case CMD_browse:
2467 case CMD_call:
2468 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002469 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 case CMD_delfunction:
2471 case CMD_djump:
2472 case CMD_dlist:
2473 case CMD_dsearch:
2474 case CMD_dsplit:
2475 case CMD_echo:
2476 case CMD_echoerr:
2477 case CMD_echomsg:
2478 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002479 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002481 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002482 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 case CMD_help:
2484 case CMD_hide:
2485 case CMD_ijump:
2486 case CMD_ilist:
2487 case CMD_isearch:
2488 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002489 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 case CMD_keepjumps:
2491 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002492 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 case CMD_leftabove:
2494 case CMD_let:
2495 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002496 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002497 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002499 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002500 case CMD_noautocmd:
2501 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 case CMD_perl:
2503 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002504 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002505 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002506 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 case CMD_return:
2508 case CMD_rightbelow:
2509 case CMD_ruby:
2510 case CMD_silent:
2511 case CMD_smagic:
2512 case CMD_snomagic:
2513 case CMD_substitute:
2514 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002515 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 case CMD_tcl:
2517 case CMD_throw:
2518 case CMD_tilde:
2519 case CMD_topleft:
2520 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002521 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002522 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 case CMD_verbose:
2524 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002525 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 break;
2527
2528 default: goto doend;
2529 }
2530 }
2531#endif
2532
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002533 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 {
2535 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2536 goto doend;
2537 }
2538
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 /*
2540 * Accept buffer name. Cannot be used at the same time with a buffer
2541 * number. Don't do this for a user command.
2542 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002543 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002544 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 {
2546 /*
2547 * :bdelete, :bwipeout and :bunload take several arguments, separated
2548 * by spaces: find next space (skipping over escaped characters).
2549 * The others take one argument: ignore trailing spaces.
2550 */
2551 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2552 || ea.cmdidx == CMD_bunload)
2553 p = skiptowhite_esc(ea.arg);
2554 else
2555 {
2556 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002557 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 --p;
2559 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002560 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002561 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002562 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 goto doend;
2564 ea.addr_count = 1;
2565 ea.arg = skipwhite(p);
2566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002568 // The :try command saves the emsg_silent flag, reset it here when
2569 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002570 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002571 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002572 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002573 if (emsg_silent < 0)
2574 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002575 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002576 }
2577
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002579 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581
Bram Moolenaar958636c2014-10-21 20:01:58 +02002582 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 {
2584 /*
2585 * Execute a user-defined command.
2586 */
2587 do_ucmd(&ea);
2588 }
2589 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 {
2591 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002592 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 */
2594 ea.errmsg = NULL;
2595 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2596 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002597 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 }
2599
2600#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002602 // Not if this was a command that wasn't executed or :endif.
Bram Moolenaar16531552020-02-08 16:00:46 +01002603 if (getline_equal(ea.getline, ea.cookie, getsourceline)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002604 && current_sctx.sc_sid > 0
2605 && ea.cmdidx != CMD_endif
2606 && (cstack->cs_idx < 0
2607 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002608 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 /*
2611 * If the command just executed called do_cmdline(), any throw or ":return"
2612 * or ":finish" encountered there must also check the cstack of the still
2613 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2614 * exception, or reanimate a returned function or finished script file and
2615 * return or finish it again.
2616 */
2617 if (need_rethrow)
2618 do_throw(cstack);
2619 else if (check_cstack)
2620 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002621 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002623 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 && current_func_returned())
2625 do_return(&ea, TRUE, FALSE, NULL);
2626 }
2627 need_rethrow = check_cstack = FALSE;
2628#endif
2629
2630doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002631 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002632 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002634 curwin->w_cursor.col = 0;
2635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636
2637 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2638 {
2639 if (sourcing)
2640 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002641 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 {
2643 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002644 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002646 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 }
2648 emsg(errormsg);
2649 }
2650#ifdef FEAT_EVAL
2651 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002652 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2653 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002654
2655 if (did_set_expr_line)
2656 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657#endif
2658
Bram Moolenaare1004402020-10-24 20:49:43 +02002659 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002661 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002663 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 ea.nextcmd = NULL;
2665
2666#ifdef FEAT_EVAL
2667 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002668 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669#endif
2670
2671 return ea.nextcmd;
2672}
2673#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002674 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675#endif
2676
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002677static char ex_error_buf[MSG_BUF_LEN];
2678
2679/*
2680 * Return an error message with argument included.
2681 * Uses a static buffer, only the last error will be kept.
2682 * "msg" will be translated, caller should use N_().
2683 */
2684 char *
2685ex_errmsg(char *msg, char_u *arg)
2686{
2687 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2688 return ex_error_buf;
2689}
2690
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002692 * Check for an Ex command with optional tail.
2693 * If there is a match advance "pp" to the argument and return TRUE.
2694 * If "noparen" is TRUE do not recognize the command followed by "(".
2695 */
2696 static int
2697checkforcmd_opt(
2698 char_u **pp, // start of command
2699 char *cmd, // name of command
2700 int len, // required length
2701 int noparen)
2702{
2703 int i;
2704
2705 for (i = 0; cmd[i] != NUL; ++i)
2706 if (((char_u *)cmd)[i] != (*pp)[i])
2707 break;
2708 if (i >= len && !isalpha((*pp)[i])
2709 && (*pp)[i] != '_' && (!noparen || (*pp)[i] != '('))
2710 {
2711 *pp = skipwhite(*pp + i);
2712 return TRUE;
2713 }
2714 return FALSE;
2715}
2716
2717/*
2718 * Check for an Ex command with optional tail.
2719 * If there is a match advance "pp" to the argument and return TRUE.
2720 */
2721 int
2722checkforcmd(
2723 char_u **pp, // start of command
2724 char *cmd, // name of command
2725 int len) // required length
2726{
2727 return checkforcmd_opt(pp, cmd, len, FALSE);
2728}
2729
2730/*
2731 * Check for an Ex command with optional tail, not followed by "(".
2732 * If there is a match advance "pp" to the argument and return TRUE.
2733 */
2734 static int
2735checkforcmd_noparen(
2736 char_u **pp, // start of command
2737 char *cmd, // name of command
2738 int len) // required length
2739{
2740 return checkforcmd_opt(pp, cmd, len, TRUE);
2741}
2742
2743/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002744 * Parse and skip over command modifiers:
2745 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002746 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002747 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002748 * When "skip_only" is TRUE the global variables are not changed, except for
2749 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002750 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2751 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002752 * Call apply_cmdmod() to get the side effects of the modifiers:
2753 * - Increment "sandbox" for ":sandbox"
2754 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002755 * - set msg_silent for ":silent"
2756 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002757 * Return FAIL when the command is not to be executed.
2758 * May set "errormsg" to an error message.
2759 */
2760 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002761parse_command_modifiers(
2762 exarg_T *eap,
2763 char **errormsg,
2764 cmdmod_T *cmod,
2765 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002766{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002767 char_u *p;
2768 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002769
Bram Moolenaare1004402020-10-24 20:49:43 +02002770 CLEAR_POINTER(cmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002771
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002772 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002773 for (;;)
2774 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002775 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002776 {
2777 if (*eap->cmd == ':')
2778 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002779 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002780 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002781
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002782 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002783 if (*eap->cmd == NUL && exmode_active
2784 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2785 || getline_equal(eap->getline, eap->cookie, getexline))
2786 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2787 {
2788 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002789 if (!skip_only)
2790 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002791 }
2792
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002793 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002794 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002795 return FAIL;
2796 if (*eap->cmd == NUL)
2797 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002798 if (!skip_only)
2799 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002800 return FAIL;
2801 }
2802
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002803 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002804
2805 // In Vim9 script a variable can shadow a command modifier:
2806 // verbose = 123
2807 // verbose += 123
2808 // silent! verbose = func()
2809 // verbose.member = 2
2810 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002811 // But not:
2812 // verbose [a, b] = list
Bram Moolenaar17126b12021-01-07 22:03:02 +01002813 if (in_vim9script())
2814 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002815 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002816
2817 for (s = p; ASCII_ISALPHA(*s); ++s)
2818 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002819 n = skipwhite(s);
2820 if (vim_strchr((char_u *)".=", *n) != NULL
2821 || *s == '['
2822 || (*n != NUL && n[1] == '='))
Bram Moolenaar17126b12021-01-07 22:03:02 +01002823 break;
2824 }
2825
Bram Moolenaareffed932018-08-14 13:38:17 +02002826 switch (*p)
2827 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002828 // When adding an entry, also modify cmd_exists().
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002829 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002830 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002831 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002832 continue;
2833
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002834 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002835 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002836 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002837 continue;
2838 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002839 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002840 {
2841#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002842 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002843#endif
2844 continue;
2845 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002846 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002847 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002848 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002849 continue;
2850
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002851 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002852 break;
2853#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002854 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002855#endif
2856 continue;
2857
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002858 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002859 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002860 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002861 continue;
2862 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002863 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002864 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002865 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002866 continue;
2867 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002868 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002869 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002870 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002871 continue;
2872 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002873 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002874 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002875 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002876 continue;
2877
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002878 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002879 {
2880 char_u *reg_pat;
2881
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002882 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaareffed932018-08-14 13:38:17 +02002883 || *p == NUL || ends_excmd(*p))
2884 break;
2885 if (*p == '!')
2886 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002887 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002888 p = skipwhite(p + 1);
2889 if (*p == NUL || ends_excmd(*p))
2890 break;
2891 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002892#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002893 // Avoid that "filter(arg)" is recognized.
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +01002894 if (in_vim9script() && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002895 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002896#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002897 if (skip_only)
2898 p = skip_vimgrep_pat(p, NULL, NULL);
2899 else
2900 // NOTE: This puts a NUL after the pattern.
2901 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002902 if (p == NULL || *p == NUL)
2903 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002904 if (!skip_only)
2905 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002906 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002907 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002908 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002909 break;
2910 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002911 eap->cmd = p;
2912 continue;
2913 }
2914
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002915 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002916 case 'h': if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02002917 || *p == NUL || ends_excmd(*p))
2918 break;
2919 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002920 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002921 continue;
2922
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002923 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002924 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002925 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002926 continue;
2927 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002928 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
2929 {
2930 if (ends_excmd2(p, eap->cmd))
2931 {
2932 *errormsg =
2933 _(e_vim9cmd_must_be_followed_by_command);
2934 return FAIL;
2935 }
2936 cmod->cmod_flags |= CMOD_LEGACY;
2937 continue;
2938 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002939
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002940 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002941 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002942 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002943 continue;
2944
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002945 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002946 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002947 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002948 continue;
2949 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002950 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002951 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002952 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002953 continue;
2954
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002955 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02002956 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002957 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002958 continue;
2959
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002960 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002961 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002962 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02002963 continue;
2964 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002965 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002966 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002967 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002968 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2969 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002970 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002971 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02002972 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002973 }
2974 continue;
2975
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002976 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002977 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002978 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002979 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002980 long tabnr = get_address(eap, &eap->cmd,
2981 ADDR_TABS, eap->skip,
2982 skip_only, FALSE, 1);
2983 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02002984 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01002985 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002986 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002987 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2988 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002989 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002990 return FAIL;
2991 }
Bram Moolenaare1004402020-10-24 20:49:43 +02002992 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002993 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002994 }
2995 eap->cmd = p;
2996 continue;
2997 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002998 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002999 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003000 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003001 continue;
3002
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003003 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003004 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003005 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003006 continue;
3007
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003008 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003009 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003010 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003011 continue;
3012 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003013 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003014 {
3015 if (ends_excmd2(p, eap->cmd))
3016 {
3017 *errormsg =
3018 _(e_vim9cmd_must_be_followed_by_command);
3019 return FAIL;
3020 }
3021 cmod->cmod_flags |= CMOD_VIM9CMD;
3022 continue;
3023 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003024 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003025 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003026 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02003027 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003028 else
Bram Moolenaare1004402020-10-24 20:49:43 +02003029 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003030 eap->cmd = p;
3031 continue;
3032 }
3033 break;
3034 }
3035
3036 return OK;
3037}
3038
3039/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003040 * Return TRUE if "cmod" has anything set.
3041 */
3042 int
3043has_cmdmod(cmdmod_T *cmod)
3044{
3045 return cmod->cmod_flags != 0
3046 || cmod->cmod_split != 0
3047 || cmod->cmod_verbose != 0
3048 || cmod->cmod_tab != 0
3049 || cmod->cmod_filter_regmatch.regprog != NULL;
3050}
3051
3052/*
3053 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3054 */
3055 int
3056cmdmod_error(void)
3057{
3058 if (in_vim9script() && has_cmdmod(&cmdmod))
3059 {
3060 emsg(_(e_misplaced_command_modifier));
3061 return TRUE;
3062 }
3063 return FALSE;
3064}
3065
3066/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003067 * Apply the command modifiers. Saves current state in "cmdmod", call
3068 * undo_cmdmod() later.
3069 */
3070 void
3071apply_cmdmod(cmdmod_T *cmod)
3072{
3073#ifdef HAVE_SANDBOX
3074 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3075 {
3076 ++sandbox;
3077 cmod->cmod_did_sandbox = TRUE;
3078 }
3079#endif
3080 if (cmod->cmod_verbose > 0)
3081 {
3082 if (cmod->cmod_verbose_save == 0)
3083 cmod->cmod_verbose_save = p_verbose + 1;
3084 p_verbose = cmod->cmod_verbose;
3085 }
3086
3087 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3088 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003089 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003090 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003091 cmod->cmod_save_msg_scroll = msg_scroll;
3092 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003093 if (cmod->cmod_flags & CMOD_SILENT)
3094 ++msg_silent;
3095 if (cmod->cmod_flags & CMOD_UNSILENT)
3096 msg_silent = 0;
3097
3098 if (cmod->cmod_flags & CMOD_ERRSILENT)
3099 {
3100 ++emsg_silent;
3101 ++cmod->cmod_did_esilent;
3102 }
3103
Bram Moolenaare1004402020-10-24 20:49:43 +02003104 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003105 {
3106 // Set 'eventignore' to "all".
3107 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003108 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003109 set_string_option_direct((char_u *)"ei", -1,
3110 (char_u *)"all", OPT_FREE, SID_NONE);
3111 }
3112}
3113
3114/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003115 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003116 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003117 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003118undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003119{
Bram Moolenaare1004402020-10-24 20:49:43 +02003120 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003121 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003122 p_verbose = cmod->cmod_verbose_save - 1;
3123 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003124 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003125
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003126#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003127 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003128 {
3129 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003130 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003131 }
3132#endif
3133
Bram Moolenaare1004402020-10-24 20:49:43 +02003134 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003135 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003136 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003137 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3138 OPT_FREE, SID_NONE);
3139 free_string_option(cmod->cmod_save_ei);
3140 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003141 }
3142
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003143 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003144
Bram Moolenaare1004402020-10-24 20:49:43 +02003145 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003146 {
3147 // messages could be enabled for a serious error, need to check if the
3148 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003149 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3150 msg_silent = cmod->cmod_save_msg_silent - 1;
3151 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003152 if (emsg_silent < 0)
3153 emsg_silent = 0;
3154 // Restore msg_scroll, it's set by file I/O commands, even when no
3155 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003156 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003157
3158 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3159 // somewhere in the line. Put it back in the first column.
3160 if (redirecting())
3161 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003162
Bram Moolenaare1004402020-10-24 20:49:43 +02003163 cmod->cmod_save_msg_silent = 0;
3164 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003165 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003166}
3167
3168/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003169 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003170 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003171 * Return FAIL and set "errormsg" or return OK.
3172 */
3173 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003174parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003175{
3176 int address_count = 1;
3177 linenr_T lnum;
3178
3179 // Repeat for all ',' or ';' separated addresses.
3180 for (;;)
3181 {
3182 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003183 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003184 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003185 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003186 eap->addr_count == 0, address_count++);
3187 if (eap->cmd == NULL) // error detected
3188 return FAIL;
3189 if (lnum == MAXLNUM)
3190 {
3191 if (*eap->cmd == '%') // '%' - all lines
3192 {
3193 ++eap->cmd;
3194 switch (eap->addr_type)
3195 {
3196 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003197 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003198 eap->line1 = 1;
3199 eap->line2 = curbuf->b_ml.ml_line_count;
3200 break;
3201 case ADDR_LOADED_BUFFERS:
3202 {
3203 buf_T *buf = firstbuf;
3204
3205 while (buf->b_next != NULL
3206 && buf->b_ml.ml_mfp == NULL)
3207 buf = buf->b_next;
3208 eap->line1 = buf->b_fnum;
3209 buf = lastbuf;
3210 while (buf->b_prev != NULL
3211 && buf->b_ml.ml_mfp == NULL)
3212 buf = buf->b_prev;
3213 eap->line2 = buf->b_fnum;
3214 break;
3215 }
3216 case ADDR_BUFFERS:
3217 eap->line1 = firstbuf->b_fnum;
3218 eap->line2 = lastbuf->b_fnum;
3219 break;
3220 case ADDR_WINDOWS:
3221 case ADDR_TABS:
3222 if (IS_USER_CMDIDX(eap->cmdidx))
3223 {
3224 eap->line1 = 1;
3225 eap->line2 = eap->addr_type == ADDR_WINDOWS
3226 ? LAST_WIN_NR : LAST_TAB_NR;
3227 }
3228 else
3229 {
3230 // there is no Vim command which uses '%' and
3231 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003232 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003233 return FAIL;
3234 }
3235 break;
3236 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003237 case ADDR_UNSIGNED:
3238 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003239 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003240 return FAIL;
3241 case ADDR_ARGUMENTS:
3242 if (ARGCOUNT == 0)
3243 eap->line1 = eap->line2 = 0;
3244 else
3245 {
3246 eap->line1 = 1;
3247 eap->line2 = ARGCOUNT;
3248 }
3249 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003250 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003251#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003252 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003253 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003254 if (eap->line2 == 0)
3255 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003256#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003257 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003258 case ADDR_NONE:
3259 // Will give an error later if a range is found.
3260 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003261 }
3262 ++eap->addr_count;
3263 }
3264 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3265 {
3266 pos_T *fp;
3267
3268 // '*' - visual area
3269 if (eap->addr_type != ADDR_LINES)
3270 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003271 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003272 return FAIL;
3273 }
3274
3275 ++eap->cmd;
3276 if (!eap->skip)
3277 {
3278 fp = getmark('<', FALSE);
3279 if (check_mark(fp) == FAIL)
3280 return FAIL;
3281 eap->line1 = fp->lnum;
3282 fp = getmark('>', FALSE);
3283 if (check_mark(fp) == FAIL)
3284 return FAIL;
3285 eap->line2 = fp->lnum;
3286 ++eap->addr_count;
3287 }
3288 }
3289 }
3290 else
3291 eap->line2 = lnum;
3292 eap->addr_count++;
3293
3294 if (*eap->cmd == ';')
3295 {
3296 if (!eap->skip)
3297 {
3298 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003299 // Don't leave the cursor on an illegal line or column, but do
3300 // accept zero as address, so 0;/PATTERN/ works correctly.
3301 if (eap->line2 > 0)
3302 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003303 }
3304 }
3305 else if (*eap->cmd != ',')
3306 break;
3307 ++eap->cmd;
3308 }
3309
3310 // One address given: set start and end lines.
3311 if (eap->addr_count == 1)
3312 {
3313 eap->line1 = eap->line2;
3314 // ... but only implicit: really no address given
3315 if (lnum == MAXLNUM)
3316 eap->addr_count = 0;
3317 }
3318 return OK;
3319}
3320
3321/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003322 * Append "cmd" to the error message in IObuff.
3323 * Takes care of limiting the length and handling 0xa0, which would be
3324 * invisible otherwise.
3325 */
3326 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003327append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003328{
3329 char_u *s = cmd;
3330 char_u *d;
3331
3332 STRCAT(IObuff, ": ");
3333 d = IObuff + STRLEN(IObuff);
3334 while (*s != NUL && d - IObuff < IOSIZE - 7)
3335 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003336 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003337 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003338 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003339 STRCPY(d, "<a0>");
3340 d += 4;
3341 }
3342 else
3343 MB_COPY_CHAR(s, d);
3344 }
3345 *d = NUL;
3346}
3347
3348/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003349 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3350 * the name. Otherwise just return "start".
3351 */
3352 char_u *
3353skip_option_env_lead(char_u *start)
3354{
3355 char_u *name = start;
3356
3357 if (*start == '&')
3358 {
3359 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3360 name += 3;
3361 else
3362 name += 1;
3363 }
3364 else if (*start == '$')
3365 name += 1;
3366 return name;
3367}
3368
3369/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003371 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003372 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003373 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 *
3375 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3376 * assignment without "let". Sets eap->cmdidx to the command while returning
3377 * "eap->cmd".
3378 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 * Returns NULL for an ambiguous user command.
3380 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 char_u *
3382find_ex_command(
3383 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003384 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003385 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003386 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387{
3388 int len;
3389 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003390 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392#ifdef FEAT_EVAL
3393 /*
3394 * Recognize a Vim9 script function/method call and assignment:
3395 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3396 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003397 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003398 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003400 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401
Bram Moolenaar83144542020-08-02 20:40:43 +02003402 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003403 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003405 int oplen;
3406 int heredoc;
3407 char_u *swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003409 if (
3410 // "(..." is an expression.
3411 // "funcname(" is always a function call.
3412 *p == '('
3413 || (p == eap->cmd
3414 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003415 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003416 *eap->cmd == '{'
3417 // "'string'->func()" is an expression.
3418 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003419 // '"string"->func()' is an expression.
3420 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003421 // "g:varname" is an expression.
3422 || eap->cmd[1] == ':'
3423 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003424 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003425 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003426 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003427 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3428 {
3429 // "{" by itself is the start of a block.
3430 eap->cmdidx = CMD_block;
3431 return eap->cmd + 1;
3432 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003433 eap->cmdidx = CMD_eval;
3434 return eap->cmd;
3435 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003436
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003437 if (p != eap->cmd && (
3438 // "varname[]" is an expression.
3439 *p == '['
3440 // "varname.key" is an expression.
3441 || (*p == '.' && ASCII_ISALPHA(p[1]))))
3442 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003443 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003444
3445 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003446 // Skip over the whole thing, it can be:
3447 // name.member = val
3448 // name[a : b] = val
3449 // name[idx] = val
3450 // name[idx].member = val
3451 // etc.
3452 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003453 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003454 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003455 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003456 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003457 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003458 || (after[0] == '.' && after[1] == '.'
3459 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003460 eap->cmdidx = CMD_var;
3461 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003462 --emsg_silent;
3463 return eap->cmd;
3464 }
3465
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003466 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3467 // an assignment.
3468 // If there is no line break inside the "[...]" then "p" is
3469 // advanced to after the "]" by to_name_const_end(): check if a "="
3470 // follows.
3471 // If "[...]" has a line break "p" still points at the "[" and it
3472 // can't be an assignment.
3473 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003474 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003475 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003476 if (p == eap->cmd && *p == '[')
3477 {
3478 int count = 0;
3479 int semicolon = FALSE;
3480
3481 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3482 }
3483 if (p == NULL || p == eap->cmd || *skipwhite(p) != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003484 {
3485 eap->cmdidx = CMD_eval;
3486 return eap->cmd;
3487 }
3488 if (p > eap->cmd && *skipwhite(p) == '=')
3489 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003490 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003491 return eap->cmd;
3492 }
3493 }
3494
3495 // Recognize an assignment if we recognize the variable name:
3496 // "g:var = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003497 // "var = expr" where "var" is a variable name or we are skipping
3498 // (variable declaration might have been skipped).
Bram Moolenaar83144542020-08-02 20:40:43 +02003499 if (*eap->cmd == '@')
3500 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003501 oplen = assignment_len(skipwhite(p), &heredoc);
3502 if (oplen > 0)
3503 {
3504 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3505 || *eap->cmd == '&'
3506 || *eap->cmd == '$'
3507 || *eap->cmd == '@'
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003508 || eap->skip
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003509 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003510 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003511 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003512 return eap->cmd;
3513 }
3514 }
3515
3516 // Recognize using a type for a w:, b:, t: or g: variable:
3517 // "w:varname: number = 123".
3518 if (eap->cmd[1] == ':' && *p == ':')
3519 {
3520 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003521 return eap->cmd;
3522 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003523 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003524
3525 // If it is an ID it might be a variable with an operator on the next
3526 // line, if the variable exists it can't be an Ex command.
3527 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003528 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003529 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3530 {
3531 eap->cmdidx = CMD_eval;
3532 return eap->cmd;
3533 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003534
3535 // Check for "++nr" and "--nr".
3536 if (p == eap->cmd && p[0] == p[1] && (*p == '+' || *p == '-'))
3537 {
3538 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3539 return eap->cmd + 2;
3540 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 }
3542#endif
3543
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 /*
3545 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003546 * Exceptions:
Bram Moolenaar10b94212021-02-19 21:42:57 +01003547 * - The 'k' command can directly be followed by any character.
3548 * But it is not used in Vim9 script.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003550 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003552 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 */
3554 p = eap->cmd;
3555 if (*p == 'k')
3556 {
3557 eap->cmdidx = CMD_k;
3558 ++p;
3559 }
3560 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003561 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3562 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 || p[1] == 'g'
3564 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3565 || p[1] == 'I'
3566 || (p[1] == 'r' && p[2] != 'e')))
3567 {
3568 eap->cmdidx = CMD_substitute;
3569 ++p;
3570 }
3571 else
3572 {
3573 while (ASCII_ISALPHA(*p))
3574 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003575 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003576 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003578 while (ASCII_ISALNUM(*p))
3579 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580 }
3581 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3582 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003583 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 ++p;
3585 while (ASCII_ISALPHA(*p))
3586 ++p;
3587 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003588
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003589 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003590 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 ++p;
3592 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003593 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3594 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003595 // Check for ":dl", ":dell", etc. to ":deletel": that's
3596 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003597 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003598 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003599 break;
3600 if (i == len - 1)
3601 {
3602 --len;
3603 if (p[-1] == 'l')
3604 eap->flags |= EXFLAG_LIST;
3605 else
3606 eap->flags |= EXFLAG_PRINT;
3607 }
3608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003610 if (ASCII_ISLOWER(eap->cmd[0]))
3611 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003612 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003613 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003614
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003615 if (command_count != (int)CMD_SIZE)
3616 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003617 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003618 getout(1);
3619 }
3620
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003621 // Use a precomputed index for fast look-up in cmdnames[]
3622 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003623 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3624 if (ASCII_ISLOWER(c2))
3625 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3626 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003627 else if (ASCII_ISUPPER(eap->cmd[0]))
3628 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003630 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631
3632 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3633 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3634 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3635 (size_t)len) == 0)
3636 {
3637#ifdef FEAT_EVAL
3638 if (full != NULL
3639 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3640 *full = TRUE;
3641#endif
3642 break;
3643 }
3644
Bram Moolenaar95388e32020-11-20 21:07:00 +01003645 // Not not recognize ":*" as the star command unless '*' is in
3646 // 'cpoptions'.
3647 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3648 p = eap->cmd;
3649
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003650 // Look for a user defined command as a last resort. Let ":Print" be
3651 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003652 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3653 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003655 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 while (ASCII_ISALNUM(*p))
3657 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003658 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003660 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 eap->cmdidx = CMD_SIZE;
3662 }
3663
Bram Moolenaar373863e2020-09-26 17:20:53 +02003664 // ":fina" means ":finally" for backwards compatibility.
3665 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3666 eap->cmdidx = CMD_finally;
3667
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003668#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003669 if (eap->cmdidx < CMD_SIZE
3670 && in_vim9script()
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003671 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003672 && (eap->cmdidx < 0 ||
3673 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003674 {
3675 semsg(_(e_command_not_followed_by_white_space_str), eap->cmd);
3676 eap->cmdidx = CMD_SIZE;
3677 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003678#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003679
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 return p;
3681}
3682
3683#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003684static struct cmdmod
3685{
3686 char *name;
3687 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003688 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003689} cmdmods[] = {
3690 {"aboveleft", 3, FALSE},
3691 {"belowright", 3, FALSE},
3692 {"botright", 2, FALSE},
3693 {"browse", 3, FALSE},
3694 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003695 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003696 {"hide", 3, FALSE},
3697 {"keepalt", 5, FALSE},
3698 {"keepjumps", 5, FALSE},
3699 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003700 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003701 {"leftabove", 5, FALSE},
3702 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003703 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003704 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003705 {"rightbelow", 6, FALSE},
3706 {"sandbox", 3, FALSE},
3707 {"silent", 3, FALSE},
3708 {"tab", 3, TRUE},
3709 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003710 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003711 {"verbose", 4, TRUE},
3712 {"vertical", 4, FALSE},
3713};
3714
3715/*
3716 * Return length of a command modifier (including optional count).
3717 * Return zero when it's not a modifier.
3718 */
3719 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003720modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003721{
3722 int i, j;
3723 char_u *p = cmd;
3724
3725 if (VIM_ISDIGIT(*cmd))
3726 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003727 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003728 {
3729 for (j = 0; p[j] != NUL; ++j)
3730 if (p[j] != cmdmods[i].name[j])
3731 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003732 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003733 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003734 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003735 }
3736 return 0;
3737}
3738
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739/*
3740 * Return > 0 if an Ex command "name" exists.
3741 * Return 2 if there is an exact match.
3742 * Return 3 if there is an ambiguous match.
3743 */
3744 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003745cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746{
3747 exarg_T ea;
3748 int full = FALSE;
3749 int i;
3750 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003751 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003753 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003754 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 {
3756 for (j = 0; name[j] != NUL; ++j)
3757 if (name[j] != cmdmods[i].name[j])
3758 break;
3759 if (name[j] == NUL && j >= cmdmods[i].minlen)
3760 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3761 }
3762
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003763 // Check built-in commands and user defined commands.
3764 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003765 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003768 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003770 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3771 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003772 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003773 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3775}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003776
3777/*
3778 * "fullcommand" function
3779 */
3780 void
3781f_fullcommand(typval_T *argvars, typval_T *rettv)
3782{
3783 exarg_T ea;
3784 char_u *name = argvars[0].vval.v_string;
3785 char_u *p;
3786
3787 while (name[0] != NUL && name[0] == ':')
3788 name++;
3789 name = skip_range(name, TRUE, NULL);
3790
3791 rettv->v_type = VAR_STRING;
3792
3793 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
3794 ea.cmdidx = (cmdidx_T)0;
3795 p = find_ex_command(&ea, NULL, NULL, NULL);
3796 if (p == NULL || ea.cmdidx == CMD_SIZE)
3797 return;
3798
3799 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
3800 ? get_user_commands(NULL, ea.useridx)
3801 : cmdnames[ea.cmdidx].cmd_name);
3802}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803#endif
3804
Bram Moolenaard0190392019-08-23 21:17:35 +02003805 cmdidx_T
3806excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807{
Bram Moolenaard0190392019-08-23 21:17:35 +02003808 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809
Bram Moolenaard0190392019-08-23 21:17:35 +02003810 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3811 idx = (cmdidx_T)((int)idx + 1))
3812 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 break;
3814
Bram Moolenaard0190392019-08-23 21:17:35 +02003815 return idx;
3816}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817
Bram Moolenaard0190392019-08-23 21:17:35 +02003818 long
3819excmd_get_argt(cmdidx_T idx)
3820{
3821 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822}
3823
3824/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003825 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 *
3827 * Backslashed delimiters after / or ? will be skipped, and commands will
3828 * not be expanded between /'s and ?'s or after "'".
3829 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003830 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 * Returns the "cmd" pointer advanced to beyond the range.
3832 */
3833 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003834skip_range(
3835 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003836 int skip_star, // skip "*" used for Visual range
3837 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003839 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003841 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003843 if (*cmd == '\\')
3844 {
3845 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3846 ++cmd;
3847 else
3848 break;
3849 }
3850 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 {
3852 if (*++cmd == NUL && ctx != NULL)
3853 *ctx = EXPAND_NOTHING;
3854 }
3855 else if (*cmd == '/' || *cmd == '?')
3856 {
3857 delim = *cmd++;
3858 while (*cmd != NUL && *cmd != delim)
3859 if (*cmd++ == '\\' && *cmd != NUL)
3860 ++cmd;
3861 if (*cmd == NUL && ctx != NULL)
3862 *ctx = EXPAND_NOTHING;
3863 }
3864 if (*cmd != NUL)
3865 ++cmd;
3866 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003867
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003868 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003869 while (*cmd == ':')
3870 cmd = skipwhite(cmd + 1);
3871
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003872 // Skip "*" used for Visual range.
3873 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3874 cmd = skipwhite(cmd + 1);
3875
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 return cmd;
3877}
3878
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003879 static void
3880addr_error(cmd_addr_T addr_type)
3881{
3882 if (addr_type == ADDR_NONE)
3883 emsg(_(e_norange));
3884 else
3885 emsg(_(e_invrange));
3886}
3887
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003889 * Return the default address for an address type.
3890 */
3891 static linenr_T
3892default_address(exarg_T *eap)
3893{
3894 linenr_T lnum = 0;
3895
3896 switch (eap->addr_type)
3897 {
3898 case ADDR_LINES:
3899 case ADDR_OTHER:
3900 // Default is the cursor line number. Avoid using an invalid
3901 // line number though.
3902 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3903 lnum = curbuf->b_ml.ml_line_count;
3904 else
3905 lnum = curwin->w_cursor.lnum;
3906 break;
3907 case ADDR_WINDOWS:
3908 lnum = CURRENT_WIN_NR;
3909 break;
3910 case ADDR_ARGUMENTS:
3911 lnum = curwin->w_arg_idx + 1;
3912 if (lnum > ARGCOUNT)
3913 lnum = ARGCOUNT;
3914 break;
3915 case ADDR_LOADED_BUFFERS:
3916 case ADDR_BUFFERS:
3917 lnum = curbuf->b_fnum;
3918 break;
3919 case ADDR_TABS:
3920 lnum = CURRENT_TAB_NR;
3921 break;
3922 case ADDR_TABS_RELATIVE:
3923 case ADDR_UNSIGNED:
3924 lnum = 1;
3925 break;
3926 case ADDR_QUICKFIX:
3927#ifdef FEAT_QUICKFIX
3928 lnum = qf_get_cur_idx(eap);
3929#endif
3930 break;
3931 case ADDR_QUICKFIX_VALID:
3932#ifdef FEAT_QUICKFIX
3933 lnum = qf_get_cur_valid_idx(eap);
3934#endif
3935 break;
3936 case ADDR_NONE:
3937 // Will give an error later if a range is found.
3938 break;
3939 }
3940 return lnum;
3941}
3942
3943/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003944 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 *
3946 * Set ptr to the next character after the part that was interpreted.
3947 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003948 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 *
3950 * Return MAXLNUM when no Ex address was found.
3951 */
3952 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003953get_address(
3954 exarg_T *eap UNUSED,
3955 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003956 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003957 int skip, // only skip the address, don't use it
3958 int silent, // no errors or side effects
3959 int to_other_file, // flag: may jump to other file
3960 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961{
3962 int c;
3963 int i;
3964 long n;
3965 char_u *cmd;
3966 pos_T pos;
3967 pos_T *fp;
3968 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003969 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970
3971 cmd = skipwhite(*ptr);
3972 lnum = MAXLNUM;
3973 do
3974 {
3975 switch (*cmd)
3976 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003977 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003978 ++cmd;
3979 switch (addr_type)
3980 {
3981 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003982 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 lnum = curwin->w_cursor.lnum;
3984 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003985 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003986 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003987 break;
3988 case ADDR_ARGUMENTS:
3989 lnum = curwin->w_arg_idx + 1;
3990 break;
3991 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003992 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003993 lnum = curbuf->b_fnum;
3994 break;
3995 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003996 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003997 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003998 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003999 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004000 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004001 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004002 cmd = NULL;
4003 goto error;
4004 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004005 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004006#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004007 lnum = qf_get_cur_idx(eap);
4008#endif
4009 break;
4010 case ADDR_QUICKFIX_VALID:
4011#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004012 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004013#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004014 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004015 }
4016 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004018 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004019 ++cmd;
4020 switch (addr_type)
4021 {
4022 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004023 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 lnum = curbuf->b_ml.ml_line_count;
4025 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004026 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004027 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004028 break;
4029 case ADDR_ARGUMENTS:
4030 lnum = ARGCOUNT;
4031 break;
4032 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004033 buf = lastbuf;
4034 while (buf->b_ml.ml_mfp == NULL)
4035 {
4036 if (buf->b_prev == NULL)
4037 break;
4038 buf = buf->b_prev;
4039 }
4040 lnum = buf->b_fnum;
4041 break;
4042 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004043 lnum = lastbuf->b_fnum;
4044 break;
4045 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004046 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004047 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004048 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004049 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004050 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004051 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004052 cmd = NULL;
4053 goto error;
4054 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004055 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004056#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004057 lnum = qf_get_size(eap);
4058 if (lnum == 0)
4059 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004060#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004061 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004062 case ADDR_QUICKFIX_VALID:
4063#ifdef FEAT_QUICKFIX
4064 lnum = qf_get_valid_size(eap);
4065 if (lnum == 0)
4066 lnum = 1;
4067#endif
4068 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004069 }
4070 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004072 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004073 if (*++cmd == NUL)
4074 {
4075 cmd = NULL;
4076 goto error;
4077 }
4078 if (addr_type != ADDR_LINES)
4079 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004080 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004081 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004082 goto error;
4083 }
4084 if (skip)
4085 ++cmd;
4086 else
4087 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004088 // Only accept a mark in another file when it is
4089 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004090 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4091 ++cmd;
4092 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004093 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004094 lnum = curwin->w_cursor.lnum;
4095 else
4096 {
4097 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 {
4099 cmd = NULL;
4100 goto error;
4101 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004102 lnum = fp->lnum;
4103 }
4104 }
4105 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106
4107 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004108 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004109 c = *cmd++;
4110 if (addr_type != ADDR_LINES)
4111 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004112 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004113 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004114 goto error;
4115 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004116 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004117 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004118 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004119 if (*cmd == c)
4120 ++cmd;
4121 }
4122 else
4123 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004124 int flags;
4125
4126 pos = curwin->w_cursor; // save curwin->w_cursor
4127
4128 // When '/' or '?' follows another address, start from
4129 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004130 if (lnum != MAXLNUM)
4131 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004132
4133 // Start a forward search at the end of the line (unless
4134 // before the first line).
4135 // Start a backward search at the start of the line.
4136 // This makes sure we never match in the current
4137 // line, and can match anywhere in the
4138 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004139 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004140 curwin->w_cursor.col = MAXCOL;
4141 else
4142 curwin->w_cursor.col = 0;
4143 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004144 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004145 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004146 {
4147 curwin->w_cursor = pos;
4148 cmd = NULL;
4149 goto error;
4150 }
4151 lnum = curwin->w_cursor.lnum;
4152 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004153 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004154 cmd += searchcmdlen;
4155 }
4156 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004158 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004159 ++cmd;
4160 if (addr_type != ADDR_LINES)
4161 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004162 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004163 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004164 goto error;
4165 }
4166 if (*cmd == '&')
4167 i = RE_SUBST;
4168 else if (*cmd == '?' || *cmd == '/')
4169 i = RE_SEARCH;
4170 else
4171 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004172 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004173 cmd = NULL;
4174 goto error;
4175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004177 if (!skip)
4178 {
4179 /*
4180 * When search follows another address, start from
4181 * there.
4182 */
4183 if (lnum != MAXLNUM)
4184 pos.lnum = lnum;
4185 else
4186 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004188 /*
4189 * Start the search just like for the above
4190 * do_search().
4191 */
4192 if (*cmd != '?')
4193 pos.col = MAXCOL;
4194 else
4195 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004196 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004197 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004198 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004199 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004200 lnum = pos.lnum;
4201 else
4202 {
4203 cmd = NULL;
4204 goto error;
4205 }
4206 }
4207 ++cmd;
4208 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209
4210 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004211 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004212 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 }
4214
4215 for (;;)
4216 {
4217 cmd = skipwhite(cmd);
4218 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4219 break;
4220
4221 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004222 {
4223 switch (addr_type)
4224 {
4225 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004226 case ADDR_OTHER:
4227 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004228 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004229 break;
4230 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004231 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004232 break;
4233 case ADDR_ARGUMENTS:
4234 lnum = curwin->w_arg_idx + 1;
4235 break;
4236 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004237 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004238 lnum = curbuf->b_fnum;
4239 break;
4240 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004241 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004242 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004243 case ADDR_TABS_RELATIVE:
4244 lnum = 1;
4245 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004246 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004247#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004248 lnum = qf_get_cur_idx(eap);
4249#endif
4250 break;
4251 case ADDR_QUICKFIX_VALID:
4252#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004253 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004254#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004255 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004256 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004257 case ADDR_UNSIGNED:
4258 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004259 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004260 }
4261 }
4262
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004264 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 else
4266 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004267 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 n = 1;
4269 else
4270 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004271
4272 if (addr_type == ADDR_TABS_RELATIVE)
4273 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004274 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004275 cmd = NULL;
4276 goto error;
4277 }
4278 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004279 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004280 lnum = compute_buffer_local_count(
4281 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004283 {
4284#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004285 // Relative line addressing, need to adjust for folded lines
4286 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004287 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4288 && address_count >= 2)
4289 (void)hasFolding(lnum, NULL, &lnum);
4290#endif
4291 if (i == '-')
4292 lnum -= n;
4293 else
4294 lnum += n;
4295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 }
4297 } while (*cmd == '/' || *cmd == '?');
4298
4299error:
4300 *ptr = cmd;
4301 return lnum;
4302}
4303
4304/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004305 * Set eap->line1 and eap->line2 to the whole range.
4306 * Used for commands with the EX_DFLALL flag and no range given.
4307 */
4308 static void
4309address_default_all(exarg_T *eap)
4310{
4311 eap->line1 = 1;
4312 switch (eap->addr_type)
4313 {
4314 case ADDR_LINES:
4315 case ADDR_OTHER:
4316 eap->line2 = curbuf->b_ml.ml_line_count;
4317 break;
4318 case ADDR_LOADED_BUFFERS:
4319 {
4320 buf_T *buf = firstbuf;
4321
4322 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4323 buf = buf->b_next;
4324 eap->line1 = buf->b_fnum;
4325 buf = lastbuf;
4326 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4327 buf = buf->b_prev;
4328 eap->line2 = buf->b_fnum;
4329 }
4330 break;
4331 case ADDR_BUFFERS:
4332 eap->line1 = firstbuf->b_fnum;
4333 eap->line2 = lastbuf->b_fnum;
4334 break;
4335 case ADDR_WINDOWS:
4336 eap->line2 = LAST_WIN_NR;
4337 break;
4338 case ADDR_TABS:
4339 eap->line2 = LAST_TAB_NR;
4340 break;
4341 case ADDR_TABS_RELATIVE:
4342 eap->line2 = 1;
4343 break;
4344 case ADDR_ARGUMENTS:
4345 if (ARGCOUNT == 0)
4346 eap->line1 = eap->line2 = 0;
4347 else
4348 eap->line2 = ARGCOUNT;
4349 break;
4350 case ADDR_QUICKFIX_VALID:
4351#ifdef FEAT_QUICKFIX
4352 eap->line2 = qf_get_valid_size(eap);
4353 if (eap->line2 == 0)
4354 eap->line2 = 1;
4355#endif
4356 break;
4357 case ADDR_NONE:
4358 case ADDR_UNSIGNED:
4359 case ADDR_QUICKFIX:
4360 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4361 break;
4362 }
4363}
4364
4365
4366/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004367 * Get flags from an Ex command argument.
4368 */
4369 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004370get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004371{
4372 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4373 {
4374 if (*eap->arg == 'l')
4375 eap->flags |= EXFLAG_LIST;
4376 else if (*eap->arg == 'p')
4377 eap->flags |= EXFLAG_PRINT;
4378 else
4379 eap->flags |= EXFLAG_NR;
4380 eap->arg = skipwhite(eap->arg + 1);
4381 }
4382}
4383
4384/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 * Function called for command which is Not Implemented. NI!
4386 */
4387 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004388ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389{
4390 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004391 eap->errmsg =
4392 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393}
4394
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004395#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396/*
4397 * Function called for script command which is Not Implemented. NI!
4398 * Skips over ":perl <<EOF" constructs.
4399 */
4400 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004401ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402{
4403 if (!eap->skip)
4404 ex_ni(eap);
4405 else
4406 vim_free(script_get(eap, eap->arg));
4407}
4408#endif
4409
4410/*
4411 * Check range in Ex command for validity.
4412 * Return NULL when valid, error message when invalid.
4413 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004414 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004415invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004417 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004418
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 if ( eap->line1 < 0
4420 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004421 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004422 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004423
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004424 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004425 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004426 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004427 {
4428 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004429 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004430#ifdef FEAT_DIFF
4431 + (eap->cmdidx == CMD_diffget)
4432#endif
4433 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004434 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004435 break;
4436 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004437 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004438 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004439 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004440 break;
4441 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004442 // Only a boundary check, not whether the buffers actually
4443 // exist.
4444 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004445 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004446 break;
4447 case ADDR_LOADED_BUFFERS:
4448 buf = firstbuf;
4449 while (buf->b_ml.ml_mfp == NULL)
4450 {
4451 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004452 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004453 buf = buf->b_next;
4454 }
4455 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004456 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004457 buf = lastbuf;
4458 while (buf->b_ml.ml_mfp == NULL)
4459 {
4460 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004461 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004462 buf = buf->b_prev;
4463 }
4464 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004465 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004466 break;
4467 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004468 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004469 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004470 break;
4471 case ADDR_TABS:
4472 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004473 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004474 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004475 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004476 case ADDR_OTHER:
4477 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004478 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004479 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004480#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004481 // No error for value that is too big, will use the last entry.
4482 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004483 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004484#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004485 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004486 case ADDR_QUICKFIX_VALID:
4487#ifdef FEAT_QUICKFIX
4488 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4489 || eap->line2 < 0)
4490 return _(e_invrange);
4491#endif
4492 break;
4493 case ADDR_UNSIGNED:
4494 if (eap->line2 < 0)
4495 return _(e_invrange);
4496 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004497 case ADDR_NONE:
4498 // Will give an error elsewhere.
4499 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004500 }
4501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 return NULL;
4503}
4504
4505/*
4506 * Correct the range for zero line number, if required.
4507 */
4508 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004509correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004511 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 {
4513 if (eap->line1 == 0)
4514 eap->line1 = 1;
4515 if (eap->line2 == 0)
4516 eap->line2 = 1;
4517 }
4518}
4519
Bram Moolenaar748bf032005-02-02 23:04:36 +00004520#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004521/*
4522 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4523 * pattern. Otherwise return eap->arg.
4524 */
4525 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004526skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004527{
4528 char_u *p = eap->arg;
4529
Bram Moolenaara37420f2006-02-04 22:37:47 +00004530 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4531 || eap->cmdidx == CMD_vimgrepadd
4532 || eap->cmdidx == CMD_lvimgrepadd
4533 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004534 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004535 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004536 if (p == NULL)
4537 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004538 }
4539 return p;
4540}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004541
4542/*
4543 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4544 * in the command line, so that things like % get expanded.
4545 */
4546 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004547replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004548{
4549 char_u *new_cmdline;
4550 char_u *program;
4551 char_u *pos;
4552 char_u *ptr;
4553 int len;
4554 int i;
4555
4556 /*
4557 * Don't do it when ":vimgrep" is used for ":grep".
4558 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004559 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4560 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4561 || eap->cmdidx == CMD_grepadd
4562 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004563 && !grep_internal(eap->cmdidx))
4564 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004565 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4566 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004567 {
4568 if (*curbuf->b_p_gp == NUL)
4569 program = p_gp;
4570 else
4571 program = curbuf->b_p_gp;
4572 }
4573 else
4574 {
4575 if (*curbuf->b_p_mp == NUL)
4576 program = p_mp;
4577 else
4578 program = curbuf->b_p_mp;
4579 }
4580
4581 p = skipwhite(p);
4582
4583 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4584 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004585 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004586 i = 1;
4587 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4588 ++i;
4589 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004590 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004591 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004592 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004593 ptr = new_cmdline;
4594 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4595 {
4596 i = (int)(pos - program);
4597 STRNCPY(ptr, program, i);
4598 STRCPY(ptr += i, p);
4599 ptr += len;
4600 program = pos + 2;
4601 }
4602 STRCPY(ptr, program);
4603 }
4604 else
4605 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004606 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004607 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004608 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004609 STRCPY(new_cmdline, program);
4610 STRCAT(new_cmdline, " ");
4611 STRCAT(new_cmdline, p);
4612 }
4613 msg_make(p);
4614
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004615 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004616 vim_free(*cmdlinep);
4617 *cmdlinep = new_cmdline;
4618 p = new_cmdline;
4619 }
4620 return p;
4621}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004622#endif
4623
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624/*
4625 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004626 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 * Return FAIL for failure, OK otherwise.
4628 */
4629 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004630expand_filename(
4631 exarg_T *eap,
4632 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004633 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004635 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 char_u *repl;
4637 int srclen;
4638 char_u *p;
4639 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004640 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
Bram Moolenaar748bf032005-02-02 23:04:36 +00004642#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004643 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004644 p = skip_grep_pat(eap);
4645#else
4646 p = eap->arg;
4647#endif
4648
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 /*
4650 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4651 * the file name contains a wildcard it should not cause expanding.
4652 * (it will be expanded anyway if there is a wildcard before replacing).
4653 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004654 has_wildcards = mch_has_wildcard(p);
4655 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004657#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004658 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004659 if (p[0] == '`' && p[1] == '=')
4660 {
4661 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004662 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004663 if (*p == '`')
4664 ++p;
4665 continue;
4666 }
4667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 /*
4669 * Quick check if this cannot be the start of a special string.
4670 * Also removes backslash before '%', '#' and '<'.
4671 */
4672 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4673 {
4674 ++p;
4675 continue;
4676 }
4677
4678 /*
4679 * Try to find a match at this position.
4680 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004681 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4682 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004683 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004685 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 {
4687 p += srclen;
4688 continue;
4689 }
4690
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004691 // Wildcards won't be expanded below, the replacement is taken
4692 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004693 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4694 {
4695 char_u *l = repl;
4696
4697 repl = expand_env_save(repl);
4698 vim_free(l);
4699 }
4700
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004701 // Need to escape white space et al. with a backslash.
4702 // Don't do this for:
4703 // - replacement that already has been escaped: "##"
4704 // - shell commands (may have to use quotes instead).
4705 // - non-unix systems when there is a single argument (spaces don't
4706 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004708 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 && eap->cmdidx != CMD_grep
4711 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004712 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004713 && eap->cmdidx != CMD_lgrep
4714 && eap->cmdidx != CMD_lgrepadd
4715 && eap->cmdidx != CMD_lmake
4716 && eap->cmdidx != CMD_make
4717 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004719 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720#endif
4721 )
4722 {
4723 char_u *l;
4724#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004725 // Don't escape a backslash here, because rem_backslash() doesn't
4726 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 static char_u *nobslash = (char_u *)" \t\"|";
4728# define ESCAPE_CHARS nobslash
4729#else
4730# define ESCAPE_CHARS escape_chars
4731#endif
4732
4733 for (l = repl; *l; ++l)
4734 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4735 {
4736 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4737 if (l != NULL)
4738 {
4739 vim_free(repl);
4740 repl = l;
4741 }
4742 break;
4743 }
4744 }
4745
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004746 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004747 if ((eap->usefilter || eap->cmdidx == CMD_bang
4748 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004749 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 {
4751 char_u *l;
4752
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004753 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 if (l != NULL)
4755 {
4756 vim_free(repl);
4757 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 }
4759 }
4760
4761 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4762 vim_free(repl);
4763 if (p == NULL)
4764 return FAIL;
4765 }
4766
4767 /*
4768 * One file argument: Expand wildcards.
4769 * Don't do this with ":r !command" or ":w !command".
4770 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004771 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 {
4773 /*
4774 * May do this twice:
4775 * 1. Replace environment variables.
4776 * 2. Replace any other wildcards, remove backslashes.
4777 */
4778 for (n = 1; n <= 2; ++n)
4779 {
4780 if (n == 2)
4781 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 /*
4783 * Halve the number of backslashes (this is Vi compatible).
4784 * For Unix and OS/2, when wildcards are expanded, this is
4785 * done by ExpandOne() below.
4786 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004787#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 if (!has_wildcards)
4789#endif
4790 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 }
4792
4793 if (has_wildcards)
4794 {
4795 if (n == 1)
4796 {
4797 /*
4798 * First loop: May expand environment variables. This
4799 * can be done much faster with expand_env() than with
4800 * something else (e.g., calling a shell).
4801 * After expanding environment variables, check again
4802 * if there are still wildcards present.
4803 */
4804 if (vim_strchr(eap->arg, '$') != NULL
4805 || vim_strchr(eap->arg, '~') != NULL)
4806 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004807 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004808 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 has_wildcards = mch_has_wildcard(NameBuff);
4810 p = NameBuff;
4811 }
4812 else
4813 p = NULL;
4814 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004815 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 {
4817 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004818 int options = WILD_LIST_NOTFOUND
4819 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820
4821 ExpandInit(&xpc);
4822 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004823 if (p_wic)
4824 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004826 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 if (p == NULL)
4828 return FAIL;
4829 }
4830 if (p != NULL)
4831 {
4832 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4833 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004834 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 vim_free(p);
4836 }
4837 }
4838 }
4839 }
4840 return OK;
4841}
4842
4843/*
4844 * Replace part of the command line, keeping eap->cmd, eap->arg and
4845 * eap->nextcmd correct.
4846 * "src" points to the part that is to be replaced, of length "srclen".
4847 * "repl" is the replacement string.
4848 * Returns a pointer to the character after the replaced string.
4849 * Returns NULL for failure.
4850 */
4851 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004852repl_cmdline(
4853 exarg_T *eap,
4854 char_u *src,
4855 int srclen,
4856 char_u *repl,
4857 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858{
4859 int len;
4860 int i;
4861 char_u *new_cmdline;
4862
4863 /*
4864 * The new command line is build in new_cmdline[].
4865 * First allocate it.
4866 * Careful: a "+cmd" argument may have been NUL terminated.
4867 */
4868 len = (int)STRLEN(repl);
4869 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004870 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004871 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004872 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004873 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874
4875 /*
4876 * Copy the stuff before the expanded part.
4877 * Copy the expanded stuff.
4878 * Copy what came after the expanded part.
4879 * Copy the next commands, if there are any.
4880 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004881 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004883
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004885 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004887 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004889 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 {
4891 i = (int)STRLEN(new_cmdline) + 1;
4892 STRCPY(new_cmdline + i, eap->nextcmd);
4893 eap->nextcmd = new_cmdline + i;
4894 }
4895 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4896 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4897 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4898 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4899 vim_free(*cmdlinep);
4900 *cmdlinep = new_cmdline;
4901
4902 return src;
4903}
4904
4905/*
4906 * Check for '|' to separate commands and '"' to start comments.
4907 */
4908 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004909separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910{
4911 char_u *p;
4912
Bram Moolenaar86b68352004-12-27 21:59:20 +00004913#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004914 p = skip_grep_pat(eap);
4915#else
4916 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004917#endif
4918
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004919 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 {
4921 if (*p == Ctrl_V)
4922 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004923 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004924 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004926 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004927 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004928 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 break;
4930 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004931
4932#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004933 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004934 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004935 {
4936 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004937 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01004938 if (*p == NUL) // stop at NUL after CTRL-V
4939 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004940 }
4941#endif
4942
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004943 // Check for '"': start of comment or '|': next command
4944 // :@" and :*" do not start a comment!
4945 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004946 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004947#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004948 && !in_vim9script()
4949#endif
4950 && !(eap->argt & EX_NOTRLCOM)
4951 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4952 || p != eap->arg)
4953 && (eap->cmdidx != CMD_redir
4954 || p != eap->arg + 1 || p[-1] != '@'))
4955#ifdef FEAT_EVAL
4956 || (*p == '#'
4957 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004958 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004959 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 || *p == '|' || *p == '\n')
4962 {
4963 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004964 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 * AND 'b' is present in 'cpoptions'.
4966 */
4967 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004968 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004970 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 --p;
4972 }
4973 else
4974 {
4975 eap->nextcmd = check_nextcmd(p);
4976 *p = NUL;
4977 break;
4978 }
4979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004981
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004982 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 del_trailing_spaces(eap->arg);
4984}
4985
4986/*
4987 * get + command from ex argument
4988 */
4989 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004990getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991{
4992 char_u *arg = *argp;
4993 char_u *command = NULL;
4994
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004995 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 {
4997 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004998 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 command = dollar_command;
5000 else
5001 {
5002 command = arg;
5003 arg = skip_cmd_arg(command, TRUE);
5004 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005005 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 }
5007
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005008 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 *argp = arg;
5010 }
5011 return command;
5012}
5013
5014/*
5015 * Find end of "+command" argument. Skip over "\ " and "\\".
5016 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005017 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005018skip_cmd_arg(
5019 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005020 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021{
5022 while (*p && !vim_isspace(*p))
5023 {
5024 if (*p == '\\' && p[1] != NUL)
5025 {
5026 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005027 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 else
5029 ++p;
5030 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005031 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 }
5033 return p;
5034}
5035
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005036 int
5037get_bad_opt(char_u *p, exarg_T *eap)
5038{
5039 if (STRICMP(p, "keep") == 0)
5040 eap->bad_char = BAD_KEEP;
5041 else if (STRICMP(p, "drop") == 0)
5042 eap->bad_char = BAD_DROP;
5043 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5044 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005045 else
5046 return FAIL;
5047 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005048}
5049
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050/*
5051 * Get "++opt=arg" argument.
5052 * Return FAIL or OK.
5053 */
5054 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005055getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056{
5057 char_u *arg = eap->arg + 2;
5058 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005059 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005062 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5064 {
5065 if (*arg == 'n')
5066 {
5067 arg += 2;
5068 eap->force_bin = FORCE_NOBIN;
5069 }
5070 else
5071 eap->force_bin = FORCE_BIN;
5072 if (!checkforcmd(&arg, "binary", 3))
5073 return FAIL;
5074 eap->arg = skipwhite(arg);
5075 return OK;
5076 }
5077
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005078 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005079 if (STRNCMP(arg, "edit", 4) == 0)
5080 {
5081 eap->read_edit = TRUE;
5082 eap->arg = skipwhite(arg + 4);
5083 return OK;
5084 }
5085
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 if (STRNCMP(arg, "ff", 2) == 0)
5087 {
5088 arg += 2;
5089 pp = &eap->force_ff;
5090 }
5091 else if (STRNCMP(arg, "fileformat", 10) == 0)
5092 {
5093 arg += 10;
5094 pp = &eap->force_ff;
5095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 else if (STRNCMP(arg, "enc", 3) == 0)
5097 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005098 if (STRNCMP(arg, "encoding", 8) == 0)
5099 arg += 8;
5100 else
5101 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 pp = &eap->force_enc;
5103 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005104 else if (STRNCMP(arg, "bad", 3) == 0)
5105 {
5106 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005107 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109
5110 if (pp == NULL || *arg != '=')
5111 return FAIL;
5112
5113 ++arg;
5114 *pp = (int)(arg - eap->cmd);
5115 arg = skip_cmd_arg(arg, FALSE);
5116 eap->arg = skipwhite(arg);
5117 *arg = NUL;
5118
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 if (pp == &eap->force_ff)
5120 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5122 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005123 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005125 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005127 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5129 *p = TOLOWER_ASC(*p);
5130 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005131 else
5132 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005133 // Check ++bad= argument. Must be a single-byte character, "keep" or
5134 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005135 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005136 return FAIL;
5137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138
5139 return OK;
5140}
5141
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005143ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144{
5145 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005146 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 * directory for security reasons.
5148 */
5149 if (secure)
5150 {
5151 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005152 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
5154 else if (eap->cmdidx == CMD_autocmd)
5155 do_autocmd(eap->arg, eap->forceit);
5156 else
5157 do_augroup(eap->arg, eap->forceit);
5158}
5159
5160/*
5161 * ":doautocmd": Apply the automatic commands to the current buffer.
5162 */
5163 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005164ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005166 char_u *arg = eap->arg;
5167 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005168 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005169
Bram Moolenaar1610d052016-06-09 22:53:01 +02005170 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005171 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005172 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005173 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176/*
5177 * :[N]bunload[!] [N] [bufname] unload buffer
5178 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5179 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5180 */
5181 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005182ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005184 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005185 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 eap->errmsg = do_bufdel(
5187 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5188 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5189 : DOBUF_UNLOAD, eap->arg,
5190 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5191}
5192
5193/*
5194 * :[N]buffer [N] to buffer N
5195 * :[N]sbuffer [N] to buffer N
5196 */
5197 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005198ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005200 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005201 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005203 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 else
5205 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005206 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5208 else
5209 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005210 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005211 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 }
5213}
5214
5215/*
5216 * :[N]bmodified [N] to next mod. buffer
5217 * :[N]sbmodified [N] to next mod. buffer
5218 */
5219 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005220ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221{
5222 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005223 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005224 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225}
5226
5227/*
5228 * :[N]bnext [N] to next buffer
5229 * :[N]sbnext [N] split and to next buffer
5230 */
5231 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005232ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005234 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005235 return;
5236
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005238 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005239 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240}
5241
5242/*
5243 * :[N]bNext [N] to previous buffer
5244 * :[N]bprevious [N] to previous buffer
5245 * :[N]sbNext [N] split and to previous buffer
5246 * :[N]sbprevious [N] split and to previous buffer
5247 */
5248 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005249ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005251 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005252 return;
5253
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005255 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005256 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257}
5258
5259/*
5260 * :brewind to first buffer
5261 * :bfirst to first buffer
5262 * :sbrewind split and to first buffer
5263 * :sbfirst split and to first buffer
5264 */
5265 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005266ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005268 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005269 return;
5270
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005272 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005273 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274}
5275
5276/*
5277 * :blast to last buffer
5278 * :sblast split and to last buffer
5279 */
5280 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005281ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005283 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005284 return;
5285
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005287 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005288 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290
Bram Moolenaar7a092242020-04-16 22:10:49 +02005291/*
5292 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005293 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005296ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005298 int comment_char = '"';
5299
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005300#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005301 if (in_vim9script())
5302 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005303#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005304 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005305}
5306
5307/*
5308 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5309 * to "cmd_start" or has a white space character before it.
5310 */
5311 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005312ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005313{
5314 int c = *cmd;
5315
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005316 if (c == NUL || c == '|' || c == '\n')
5317 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005318#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005319 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005320 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5321 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005322 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005323#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005324 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325}
5326
5327#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5328 || defined(PROTO)
5329/*
5330 * Return the next command, after the first '|' or '\n'.
5331 * Return NULL if not found.
5332 */
5333 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005334find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335{
5336 while (*p != '|' && *p != '\n')
5337 {
5338 if (*p == NUL)
5339 return NULL;
5340 ++p;
5341 }
5342 return (p + 1);
5343}
5344#endif
5345
5346/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005347 * Check if *p is a separator between Ex commands, skipping over white space.
5348 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 */
5350 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005351check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005353 char_u *s = skipwhite(p);
5354
5355 if (*s == '|' || *s == '\n')
5356 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 else
5358 return NULL;
5359}
5360
5361/*
5362 * - if there are more files to edit
5363 * - and this is the last window
5364 * - and forceit not used
5365 * - and not repeated twice on a row
5366 * return FAIL and give error message if 'message' TRUE
5367 * return OK otherwise
5368 */
5369 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005370check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005371 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005372 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373{
5374 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5375
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005376 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005377 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 {
5379 if (message)
5380 {
5381#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005382 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5383 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005385 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005387 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5388 NGETTEXT("%d more file to edit. Quit anyway?",
5389 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5391 return OK;
5392 return FAIL;
5393 }
5394#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005395 semsg(NGETTEXT("E173: %d more file to edit",
5396 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005397 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 }
5399 return FAIL;
5400 }
5401 return OK;
5402}
5403
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404/*
5405 * Function given to ExpandGeneric() to obtain the list of command names.
5406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005408get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409{
5410 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 return cmdnames[idx].cmd_name;
5413}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005416ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417{
Bram Moolenaare6850792010-05-14 15:28:44 +02005418 if (*eap->arg == NUL)
5419 {
5420#ifdef FEAT_EVAL
5421 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5422 char_u *p = NULL;
5423
5424 if (expr != NULL)
5425 {
5426 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005427 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005428 --emsg_off;
5429 vim_free(expr);
5430 }
5431 if (p != NULL)
5432 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005433 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005434 vim_free(p);
5435 }
5436 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005437 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005438#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005439 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005440#endif
5441 }
5442 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005443 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005444
5445#ifdef FEAT_VTP
5446 else if (has_vtp_working())
5447 {
5448 // background color change requires clear + redraw
5449 update_screen(CLEAR);
5450 redrawcmd();
5451 }
5452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453}
5454
5455 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005456ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457{
5458 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005459 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 do_highlight(eap->arg, eap->forceit, FALSE);
5461}
5462
5463
5464/*
5465 * Call this function if we thought we were going to exit, but we won't
5466 * (because of an error). May need to restore the terminal mode.
5467 */
5468 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005469not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470{
5471 exiting = FALSE;
5472 settmode(TMODE_RAW);
5473}
5474
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005475 static int
5476before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5477{
5478 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5479
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005480 // Bail out when autocommands closed the window.
5481 // Refuse to quit when the buffer in the last window is being closed (can
5482 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005483 if (!win_valid(wp)
5484 || curbuf_locked()
5485 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5486 return TRUE;
5487
5488 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5489 {
5490 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005491 // Refuse to quit when locked or when the window was closed or the
5492 // buffer in the last window is being closed (can only happen in
5493 // autocommands).
5494 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005495 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5496 return TRUE;
5497 }
5498
5499 return FALSE;
5500}
5501
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005503 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005504 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005505 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005507 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005508ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005510 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005511
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512#ifdef FEAT_CMDWIN
5513 if (cmdwin_type != 0)
5514 {
5515 cmdwin_result = Ctrl_C;
5516 return;
5517 }
5518#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005519 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005520 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005521 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005522 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005523 return;
5524 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005525 if (eap->addr_count > 0)
5526 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005527 int wnr = eap->line2;
5528
5529 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5530 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005531 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005532 }
5533 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005534 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005535
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005536 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005537 if (curbuf_locked())
5538 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005539
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005540 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005541 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005542 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543
5544#ifdef FEAT_NETBEANS_INTG
5545 netbeansForcedQuit = eap->forceit;
5546#endif
5547
5548 /*
5549 * If there are more files or windows we won't exit.
5550 */
5551 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5552 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005553 if ((!buf_hide(wp->w_buffer)
5554 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005555 | (eap->forceit ? CCGD_FORCEIT : 0)
5556 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005558 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 {
5560 not_exiting();
5561 }
5562 else
5563 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005564 // quit last window
5565 // Note: only_one_window() returns true, even so a help window is
5566 // still open. In that case only quit, if no address has been
5567 // specified. Example:
5568 // :h|wincmd w|1q - don't quit
5569 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005570 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005572 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005573#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005575#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005576 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005577 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 }
5579}
5580
5581/*
5582 * ":cquit".
5583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005585ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005587 // this does not always pass on the exit code to the Manx compiler. why?
5588 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589}
5590
5591/*
5592 * ":qall": try to quit all windows
5593 */
5594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005595ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596{
5597# ifdef FEAT_CMDWIN
5598 if (cmdwin_type != 0)
5599 {
5600 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005601 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 else
5603 cmdwin_result = K_XF2;
5604 return;
5605 }
5606# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005607
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005608 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005609 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005610 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005611 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005612 return;
5613 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005614
5615 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005616 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005617
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005619 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 getout(0);
5621 not_exiting();
5622}
5623
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624/*
5625 * ":close": close current window, unless it is the last one
5626 */
5627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005628ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005630 win_T *win;
5631 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005632#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005634 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005636#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005637 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005638 {
5639 if (eap->addr_count == 0)
5640 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005641 else
5642 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005643 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005644 {
5645 winnr++;
5646 if (winnr == eap->line2)
5647 break;
5648 }
5649 if (win == NULL)
5650 win = lastwin;
5651 ex_win_close(eap->forceit, win, NULL);
5652 }
5653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654}
5655
Bram Moolenaar4033c552017-09-16 20:54:51 +02005656#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005657/*
5658 * ":pclose": Close any preview window.
5659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005661ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005662{
5663 win_T *win;
5664
Bram Moolenaar79648732019-07-18 21:43:07 +02005665 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005666 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005667 if (win->w_p_pvw)
5668 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005669 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005670 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005671 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005672# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005673 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005674 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005675# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005676}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005677#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005678
Bram Moolenaarf740b292006-02-16 22:11:02 +00005679/*
5680 * Close window "win" and take care of handling closing the last window for a
5681 * modified buffer.
5682 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005683 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005684ex_win_close(
5685 int forceit,
5686 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005687 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688{
5689 int need_hide;
5690 buf_T *buf = win->w_buffer;
5691
Bram Moolenaarcf844172020-06-26 19:44:06 +02005692 // Never close the autocommand window.
5693 if (win == aucmd_win)
5694 {
5695 emsg(_(e_autocmd_close));
5696 return;
5697 }
5698
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005700 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005702#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005703 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005705 bufref_T bufref;
5706
5707 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005709 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 return;
5711 need_hide = FALSE;
5712 }
5713 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005716 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 return;
5718 }
5719 }
5720
Bram Moolenaar4033c552017-09-16 20:54:51 +02005721#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005723#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005724
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005725 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005726 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005727 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005728 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005729 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730}
5731
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005733 * Handle the argument for a tabpage related ex command.
5734 * Returns a tabpage number.
5735 * When an error is encountered then eap->errmsg is set.
5736 */
5737 static int
5738get_tabpage_arg(exarg_T *eap)
5739{
5740 int tab_number;
5741 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5742
5743 if (eap->arg && *eap->arg != NUL)
5744 {
5745 char_u *p = eap->arg;
5746 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005747 int relative = 0; // argument +N/-N means: go to N places to the
5748 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005749
5750 if (*p == '-')
5751 {
5752 relative = -1;
5753 p++;
5754 }
5755 else if (*p == '+')
5756 {
5757 relative = 1;
5758 p++;
5759 }
5760
5761 p_save = p;
5762 tab_number = getdigits(&p);
5763
5764 if (relative == 0)
5765 {
5766 if (STRCMP(p, "$") == 0)
5767 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005768 else if (STRCMP(p, "#") == 0)
5769 if (valid_tabpage(lastused_tabpage))
5770 tab_number = tabpage_index(lastused_tabpage);
5771 else
5772 {
5773 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5774 tab_number = 0;
5775 goto theend;
5776 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005777 else if (p == p_save || *p_save == '-' || *p != NUL
5778 || tab_number > LAST_TAB_NR)
5779 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005780 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005781 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005782 goto theend;
5783 }
5784 }
5785 else
5786 {
5787 if (*p_save == NUL)
5788 tab_number = 1;
5789 else if (p == p_save || *p_save == '-' || *p != NUL
5790 || tab_number == 0)
5791 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005792 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005793 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005794 goto theend;
5795 }
5796 tab_number = tab_number * relative + tabpage_index(curtab);
5797 if (!unaccept_arg0 && relative == -1)
5798 --tab_number;
5799 }
5800 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005801 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005802 }
5803 else if (eap->addr_count > 0)
5804 {
5805 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005806 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005807 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005808 tab_number = 0;
5809 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005810 else
5811 {
5812 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005813 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005814 {
5815 --tab_number;
5816 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005817 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005818 }
5819 }
5820 }
5821 else
5822 {
5823 switch (eap->cmdidx)
5824 {
5825 case CMD_tabnext:
5826 tab_number = tabpage_index(curtab) + 1;
5827 if (tab_number > LAST_TAB_NR)
5828 tab_number = 1;
5829 break;
5830 case CMD_tabmove:
5831 tab_number = LAST_TAB_NR;
5832 break;
5833 default:
5834 tab_number = tabpage_index(curtab);
5835 }
5836 }
5837
5838theend:
5839 return tab_number;
5840}
5841
5842/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005843 * ":tabclose": close current tab page, unless it is the last one.
5844 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 */
5846 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005847ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005849 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005850 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005851
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005852# ifdef FEAT_CMDWIN
5853 if (cmdwin_type != 0)
5854 cmdwin_result = K_IGNORE;
5855 else
5856# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005857 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005858 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005859 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005861 tab_number = get_tabpage_arg(eap);
5862 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005863 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005864 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005865 if (tp == NULL)
5866 {
5867 beep_flush();
5868 return;
5869 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005870 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005871 {
5872 tabpage_close_other(tp, eap->forceit);
5873 return;
5874 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005875 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005876 tabpage_close(eap->forceit);
5877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005879}
5880
5881/*
5882 * ":tabonly": close all tab pages except the current one
5883 */
5884 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005885ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005886{
5887 tabpage_T *tp;
5888 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005889 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005890
5891# ifdef FEAT_CMDWIN
5892 if (cmdwin_type != 0)
5893 cmdwin_result = K_IGNORE;
5894 else
5895# endif
5896 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005897 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005898 else
5899 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005900 tab_number = get_tabpage_arg(eap);
5901 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005902 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005903 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005904 // Repeat this up to a 1000 times, because autocommands may
5905 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005906 for (done = 0; done < 1000; ++done)
5907 {
5908 FOR_ALL_TABPAGES(tp)
5909 if (tp->tp_topframe != topframe)
5910 {
5911 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005912 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005913 if (valid_tabpage(tp))
5914 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005915 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005916 break;
5917 }
5918 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005919 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005920 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005921 }
5922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924
5925/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005926 * Close the current tab page.
5927 */
5928 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005929tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005930{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005931 // First close all the windows but the current one. If that worked then
5932 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005933 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005934 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005935 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005936 ex_win_close(forceit, curwin, NULL);
5937# ifdef FEAT_GUI
5938 need_mouse_correct = TRUE;
5939# endif
5940}
5941
5942/*
5943 * Close tab page "tp", which is not the current tab page.
5944 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005945 * Also takes care of the tab pages line disappearing when closing the
5946 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005947 */
5948 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005949tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005950{
5951 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005952 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005953 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005954
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005955 // Limit to 1000 windows, autocommands may add a window while we close
5956 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005957 while (++done < 1000)
5958 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005959 wp = tp->tp_firstwin;
5960 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005961
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005962 // Autocommands may delete the tab page under our fingers and we may
5963 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005964 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005965 break;
5966 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005967
Bram Moolenaar29323592016-07-24 22:04:11 +02005968 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005969
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005970 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005971 if (h != tabline_height())
5972 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005973}
5974
5975/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 * ":only".
5977 */
5978 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005979ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005981 win_T *wp;
5982 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983# ifdef FEAT_GUI
5984 need_mouse_correct = TRUE;
5985# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005986 if (eap->addr_count > 0)
5987 {
5988 wnr = eap->line2;
5989 for (wp = firstwin; --wnr > 0; )
5990 {
5991 if (wp->w_next == NULL)
5992 break;
5993 else
5994 wp = wp->w_next;
5995 }
5996 win_goto(wp);
5997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 close_others(TRUE, eap->forceit);
5999}
6000
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006002ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006004 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01006005 if (!eap->skip)
6006 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006007#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006008 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006009#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006010 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006011 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01006012 else
6013 {
6014 int winnr = 0;
6015 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006016
Bram Moolenaar2256c992016-11-15 21:17:07 +01006017 FOR_ALL_WINDOWS(win)
6018 {
6019 winnr++;
6020 if (winnr == eap->line2)
6021 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006022 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006023 if (win == NULL)
6024 win = lastwin;
6025 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 }
6028}
6029
6030/*
6031 * ":stop" and ":suspend": Suspend Vim.
6032 */
6033 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006034ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035{
6036 /*
6037 * Disallow suspending for "rvim".
6038 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006039 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 {
6041 if (!eap->forceit)
6042 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01006043 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 windgoto((int)Rows - 1, 0);
6045 out_char('\n');
6046 out_flush();
6047 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006048 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006050 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006052 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053#ifdef FEAT_TITLE
6054 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006055 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056#endif
6057 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006058 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006060 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006061 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 }
6063}
6064
6065/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006066 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 */
6068 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006069ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006071#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006072 if (not_in_vim9(eap) == FAIL)
6073 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075#ifdef FEAT_CMDWIN
6076 if (cmdwin_type != 0)
6077 {
6078 cmdwin_result = Ctrl_C;
6079 return;
6080 }
6081#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006082 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006083 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006084 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006085 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006086 return;
6087 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006088
6089 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006090 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091
6092 /*
6093 * if more files or windows we won't exit
6094 */
6095 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6096 exiting = TRUE;
6097 if ( ((eap->cmdidx == CMD_wq
6098 || curbufIsChanged())
6099 && do_write(eap) == FAIL)
6100 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006101 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 {
6103 not_exiting();
6104 }
6105 else
6106 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006107 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006109 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110# ifdef FEAT_GUI
6111 need_mouse_correct = TRUE;
6112# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006113 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006114 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 }
6116}
6117
6118/*
6119 * ":print", ":list", ":number".
6120 */
6121 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006122ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006124 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006125 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006126 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006128 for ( ;!got_int; ui_breakcheck())
6129 {
6130 print_line(eap->line1,
6131 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6132 || (eap->flags & EXFLAG_NR)),
6133 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6134 if (++eap->line1 > eap->line2)
6135 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006136 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006137 }
6138 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006139 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006140 curwin->w_cursor.lnum = eap->line2;
6141 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 }
6143
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145}
6146
6147#ifdef FEAT_BYTEOFF
6148 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006149ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150{
6151 goto_byte(eap->line2);
6152}
6153#endif
6154
6155/*
6156 * ":shell".
6157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006159ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160{
6161 do_shell(NULL, 0);
6162}
6163
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006164#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006166static int drop_busy = FALSE;
6167static int drop_filec;
6168static char_u **drop_filev = NULL;
6169static int drop_split;
6170static void (*drop_callback)(void *);
6171static void *drop_cookie;
6172
6173 static void
6174handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175{
6176 exarg_T ea;
6177 int save_msg_scroll = msg_scroll;
6178
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006179 // Setting the argument list may cause screen updates and being called
6180 // recursively. Avoid that by setting drop_busy.
6181 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006183 // Check whether the current buffer is changed. If so, we will need
6184 // to split the current window or data could be lost.
6185 // We don't need to check if the 'hidden' option is set, as in this
6186 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006187 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 {
6189 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006190 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 --emsg_off;
6192 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006193 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 if (win_split(0, 0) == FAIL)
6196 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006197 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006199 // When splitting the window, create a new alist. Otherwise the
6200 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 alist_unlink(curwin->w_alist);
6202 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 }
6204
6205 /*
6206 * Set up the new argument list.
6207 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006208 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209
6210 /*
6211 * Move to the first file.
6212 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006213 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006214 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 ea.cmd = (char_u *)"next";
6216 do_argfile(&ea, 0);
6217
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006218 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6219 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 need_start_insertmode = FALSE;
6221
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006222 // Restore msg_scroll, otherwise a following command may cause scrolling
6223 // unexpectedly. The screen will be redrawn by the caller, thus
6224 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006226
6227 if (drop_callback != NULL)
6228 drop_callback(drop_cookie);
6229
6230 drop_filev = NULL;
6231 drop_busy = FALSE;
6232}
6233
6234/*
6235 * Handle a file drop. The code is here because a drop is *nearly* like an
6236 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006237 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006238 * code is very similar to :args and hence needs access to a lot of the static
6239 * functions in this file.
6240 *
6241 * The "filev" list must have been allocated using alloc(), as should each item
6242 * in the list. This function takes over responsibility for freeing the "filev"
6243 * list.
6244 */
6245 void
6246handle_drop(
6247 int filec, // the number of files dropped
6248 char_u **filev, // the list of files dropped
6249 int split, // force splitting the window
6250 void (*callback)(void *), // to be called after setting the argument
6251 // list
6252 void *cookie) // argument for "callback" (allocated)
6253{
6254 // Cannot handle recursive drops, finish the pending one.
6255 if (drop_busy)
6256 {
6257 FreeWild(filec, filev);
6258 vim_free(cookie);
6259 return;
6260 }
6261
6262 // When calling handle_drop() more than once in a row we only use the last
6263 // one.
6264 if (drop_filev != NULL)
6265 {
6266 FreeWild(drop_filec, drop_filev);
6267 vim_free(drop_cookie);
6268 }
6269
6270 drop_filec = filec;
6271 drop_filev = filev;
6272 drop_split = split;
6273 drop_callback = callback;
6274 drop_cookie = cookie;
6275
6276 // Postpone this when:
6277 // - editing the command line
6278 // - not possible to change the current buffer
6279 // - updating the screen
6280 // As it may change buffers and window structures that are in use and cause
6281 // freed memory to be used.
6282 if (text_locked() || curbuf_locked() || updating_screen)
6283 return;
6284
6285 handle_drop_internal();
6286}
6287
6288/*
6289 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6290 * reset: Handle a postponed drop.
6291 */
6292 void
6293handle_any_postponed_drop(void)
6294{
6295 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006296 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006297 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298}
6299#endif
6300
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 * ":preserve".
6303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006305ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006307 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 ml_preserve(curbuf, TRUE);
6309}
6310
6311/*
6312 * ":recover".
6313 */
6314 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006315ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006317 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006319 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6320 | CCGD_MULTWIN
6321 | (eap->forceit ? CCGD_FORCEIT : 0)
6322 | CCGD_EXCMD)
6323
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 && (*eap->arg == NUL
6325 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006326 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 recoverymode = FALSE;
6328}
6329
6330/*
6331 * Command modifier used in a wrong way.
6332 */
6333 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006334ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006336 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337}
6338
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339/*
6340 * :sview [+command] file split window with new file, read-only
6341 * :split [[+command] file] split window with current or new file
6342 * :vsplit [[+command] file] split window vertically with current or new file
6343 * :new [[+command] file] split window with no or new file
6344 * :vnew [[+command] file] split vertically window with no or new file
6345 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006346 *
6347 * :tabedit open new Tab page with empty window
6348 * :tabedit [+command] file open new Tab page and edit "file"
6349 * :tabnew [[+command] file] just like :tabedit
6350 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 */
6352 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006353ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006355 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006356#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006358#endif
6359#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006360 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006361 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006362#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006363 int use_tab = eap->cmdidx == CMD_tabedit
6364 || eap->cmdidx == CMD_tabfind
6365 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006367 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006368 return;
6369
Bram Moolenaar4033c552017-09-16 20:54:51 +02006370#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373
Bram Moolenaar4033c552017-09-16 20:54:51 +02006374#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006375 // A ":split" in the quickfix window works like ":new". Don't want two
6376 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006377 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 {
6379 if (eap->cmdidx == CMD_split)
6380 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 if (eap->cmdidx == CMD_vsplit)
6382 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385
Bram Moolenaar4033c552017-09-16 20:54:51 +02006386#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006387 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 {
6389 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6390 FNAME_MESS, TRUE, curbuf->b_ffname);
6391 if (fname == NULL)
6392 goto theend;
6393 eap->arg = fname;
6394 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006395# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006396 else
6397# endif
6398#endif
6399#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006400 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 && eap->cmdidx != CMD_new)
6403 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006404 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006405# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006406 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006407# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006408 au_has_group((char_u *)"FileExplorer"))
6409 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006410 // No browsing supported but we do have the file explorer:
6411 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006412 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006413 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006414 }
6415 else
6416 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006417 fname = do_browse(0, (char_u *)(use_tab
6418 ? _("Edit File in new tab page")
6419 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006421 if (fname == NULL)
6422 goto theend;
6423 eap->arg = fname;
6424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006426 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006429 /*
6430 * Either open new tab page or split the window.
6431 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006432 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006433 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006434 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006435 : eap->addr_count == 0 ? 0
6436 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006437 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006438 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006439
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006440 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006441 if (curwin != old_curwin
6442 && win_valid(old_curwin)
6443 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006444 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006445 old_curwin->w_alt_fnum = curbuf->b_fnum;
6446 }
6447 }
6448 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6450 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006451 // Reset 'scrollbind' when editing another file, but keep it when
6452 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006453 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006454 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 else
6456 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 do_exedit(eap, old_curwin);
6458 }
6459
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006460# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006461 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006462# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006464# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465theend:
6466 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006467# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006469
6470/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006471 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006472 */
6473 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006474tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006475{
6476 exarg_T ea;
6477
Bram Moolenaara80faa82020-04-12 19:37:17 +02006478 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006479 ea.cmdidx = CMD_tabnew;
6480 ea.cmd = (char_u *)"tabn";
6481 ea.arg = (char_u *)"";
6482 ex_splitview(&ea);
6483}
6484
6485/*
6486 * :tabnext command
6487 */
6488 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006489ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006490{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006491 int tab_number;
6492
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006493 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006494 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006495 switch (eap->cmdidx)
6496 {
6497 case CMD_tabfirst:
6498 case CMD_tabrewind:
6499 goto_tabpage(1);
6500 break;
6501 case CMD_tablast:
6502 goto_tabpage(9999);
6503 break;
6504 case CMD_tabprevious:
6505 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006506 if (eap->arg && *eap->arg != NUL)
6507 {
6508 char_u *p = eap->arg;
6509 char_u *p_save = p;
6510
6511 tab_number = getdigits(&p);
6512 if (p == p_save || *p_save == '-' || *p != NUL
6513 || tab_number == 0)
6514 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006515 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006516 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006517 return;
6518 }
6519 }
6520 else
6521 {
6522 if (eap->addr_count == 0)
6523 tab_number = 1;
6524 else
6525 {
6526 tab_number = eap->line2;
6527 if (tab_number < 1)
6528 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006529 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006530 return;
6531 }
6532 }
6533 }
6534 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006535 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006536 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006537 tab_number = get_tabpage_arg(eap);
6538 if (eap->errmsg == NULL)
6539 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006540 break;
6541 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006542}
6543
6544/*
6545 * :tabmove command
6546 */
6547 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006548ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006549{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006550 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006551
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006552 tab_number = get_tabpage_arg(eap);
6553 if (eap->errmsg == NULL)
6554 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006555}
6556
6557/*
6558 * :tabs command: List tabs and their contents.
6559 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006560 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006561ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006562{
6563 tabpage_T *tp;
6564 win_T *wp;
6565 int tabcount = 1;
6566
6567 msg_start();
6568 msg_scroll = TRUE;
6569 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6570 {
6571 msg_putchar('\n');
6572 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006573 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006574 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006575 ui_breakcheck();
6576
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006577 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006578 wp = firstwin;
6579 else
6580 wp = tp->tp_firstwin;
6581 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6582 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006583 msg_putchar('\n');
6584 msg_putchar(wp == curwin ? '>' : ' ');
6585 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006586 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6587 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006588 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006589 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006590 else
6591 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6592 IObuff, IOSIZE, TRUE);
6593 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006594 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006595 ui_breakcheck();
6596 }
6597 }
6598}
6599
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600/*
6601 * ":mode": Set screen mode.
6602 * If no argument given, just get the screen size and redraw.
6603 */
6604 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006605ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606{
6607 if (*eap->arg == NUL)
6608 shell_resized();
6609 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006610 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611}
6612
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613/*
6614 * ":resize".
6615 * set, increment or decrement current window height
6616 */
6617 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006618ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619{
6620 int n;
6621 win_T *wp = curwin;
6622
6623 if (eap->addr_count > 0)
6624 {
6625 n = eap->line2;
6626 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6627 ;
6628 }
6629
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006630# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006632# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006634 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 {
6636 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006637 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006638 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 n = 9999;
6640 win_setwidth_win((int)n, wp);
6641 }
6642 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 {
6644 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006645 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006646 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 n = 9999;
6648 win_setheight_win((int)n, wp);
6649 }
6650}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651
6652/*
6653 * ":find [+command] <file>" command.
6654 */
6655 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006656ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657{
6658#ifdef FEAT_SEARCHPATH
6659 char_u *fname;
6660 int count;
6661
6662 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6663 TRUE, curbuf->b_ffname);
6664 if (eap->addr_count > 0)
6665 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006666 // Repeat finding the file "count" times. This matters when it
6667 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 count = eap->line2;
6669 while (fname != NULL && --count > 0)
6670 {
6671 vim_free(fname);
6672 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6673 FALSE, curbuf->b_ffname);
6674 }
6675 }
6676
6677 if (fname != NULL)
6678 {
6679 eap->arg = fname;
6680#endif
6681 do_exedit(eap, NULL);
6682#ifdef FEAT_SEARCHPATH
6683 vim_free(fname);
6684 }
6685#endif
6686}
6687
6688/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006689 * ":open" simulation: for now just work like ":visual".
6690 */
6691 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006692ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006693{
6694 regmatch_T regmatch;
6695 char_u *p;
6696
Bram Moolenaar65088802021-03-13 21:07:21 +01006697#ifdef FEAT_EVAL
6698 if (not_in_vim9(eap) == FAIL)
6699 return;
6700#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006701 curwin->w_cursor.lnum = eap->line2;
6702 beginline(BL_SOL | BL_FIX);
6703 if (*eap->arg == '/')
6704 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006705 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006706 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006707 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00006708 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006709 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006710 if (regmatch.regprog != NULL)
6711 {
6712 regmatch.rm_ic = p_ic;
6713 p = ml_get_curline();
6714 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006715 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006716 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006717 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006718 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006719 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006720 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006721 eap->arg += STRLEN(eap->arg);
6722 }
6723 check_cursor();
6724
6725 eap->cmdidx = CMD_visual;
6726 do_exedit(eap, NULL);
6727}
6728
6729/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006730 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 */
6732 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006733ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734{
6735 do_exedit(eap, NULL);
6736}
6737
6738/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006739 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006742do_exedit(
6743 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006744 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745{
6746 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006748 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006750 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6751 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006752 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 /*
6754 * ":vi" command ends Ex mode.
6755 */
6756 if (exmode_active && (eap->cmdidx == CMD_visual
6757 || eap->cmdidx == CMD_view))
6758 {
6759 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006760 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006762 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006763 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006764 if (global_busy)
6765 {
6766 int rd = RedrawingDisabled;
6767 int nwr = no_wait_return;
6768 int ms = msg_scroll;
6769#ifdef FEAT_GUI
6770 int he = hold_gui_events;
6771#endif
6772
6773 if (eap->nextcmd != NULL)
6774 {
6775 stuffReadbuff(eap->nextcmd);
6776 eap->nextcmd = NULL;
6777 }
6778
6779 if (exmode_was != EXMODE_VIM)
6780 settmode(TMODE_RAW);
6781 RedrawingDisabled = 0;
6782 no_wait_return = 0;
6783 need_wait_return = FALSE;
6784 msg_scroll = 0;
6785#ifdef FEAT_GUI
6786 hold_gui_events = 0;
6787#endif
6788 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006789 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006790
6791 main_loop(FALSE, TRUE);
6792
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006793 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006794 RedrawingDisabled = rd;
6795 no_wait_return = nwr;
6796 msg_scroll = ms;
6797#ifdef FEAT_GUI
6798 hold_gui_events = he;
6799#endif
6800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 }
6804
6805 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006806 || eap->cmdidx == CMD_tabnew
6807 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006808 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006810 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 setpcmark();
6812 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006813 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6814 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006816 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 || *eap->arg != NUL
6818#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006819 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820#endif
6821 )
6822 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006823 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6824 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006825 if (*eap->arg != NUL && curbuf_locked())
6826 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006827
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 n = readonlymode;
6829 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6830 readonlymode = TRUE;
6831 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006832 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6833 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01006834 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
6835 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6837 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006838 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6840 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6841 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006842 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006844 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006845 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006846 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
6847 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006848 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006850 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 if (old_curwin != NULL)
6852 {
6853 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006854 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006856#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006857 cleanup_T cs;
6858
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006859 // Reset the error/interrupt/exception state here so that
6860 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006861 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006862#endif
6863#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006865#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006866 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006867
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006868#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006869 // Restore the error/interrupt/exception state if not
6870 // discarded by a new aborting error, interrupt, or
6871 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006872 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 }
6875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 }
6877 else if (readonlymode && curbuf->b_nwindows == 1)
6878 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006879 // When editing an already visited buffer, 'readonly' won't be set
6880 // but the previous value is kept. With ":view" and ":sview" we
6881 // want the file to be readonly, except when another window is
6882 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 curbuf->b_p_ro = TRUE;
6884 }
6885 readonlymode = n;
6886 }
6887 else
6888 {
6889 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01006890 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891#ifdef FEAT_TITLE
6892 n = curwin->w_arg_idx_invalid;
6893#endif
6894 check_arg_idx(curwin);
6895#ifdef FEAT_TITLE
6896 if (n != curwin->w_arg_idx_invalid)
6897 maketitle();
6898#endif
6899 }
6900
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 /*
6902 * if ":split file" worked, set alternate file name in old window to new
6903 * file
6904 */
6905 if (old_curwin != NULL
6906 && *eap->arg != NUL
6907 && curwin != old_curwin
6908 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006909 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006910 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912
6913 ex_no_reprint = TRUE;
6914}
6915
6916#ifndef FEAT_GUI
6917/*
6918 * ":gui" and ":gvim" when there is no GUI.
6919 */
6920 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006921ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006923 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924}
6925#endif
6926
Bram Moolenaar4f974752019-02-17 17:44:42 +01006927#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006929ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930{
6931 gui_make_tearoff(eap->arg);
6932}
6933#endif
6934
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006935#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6936 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006938ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006940# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6941 if (gui.in_use)
6942 gui_make_popup(eap->arg, eap->forceit);
6943# ifdef FEAT_TERM_POPUP_MENU
6944 else
6945# endif
6946# endif
6947# ifdef FEAT_TERM_POPUP_MENU
6948 pum_make_popup(eap->arg, eap->forceit);
6949# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950}
6951#endif
6952
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006954ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955{
6956 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006957 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006959 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960}
6961
6962/*
6963 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6964 * offset.
6965 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006968ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006971 win_T *save_curwin = curwin;
6972 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 long topline;
6974 long y;
6975 linenr_T old_linenr = curwin->w_cursor.lnum;
6976
6977 setpcmark();
6978
6979 /*
6980 * determine max topline
6981 */
6982 if (curwin->w_p_scb)
6983 {
6984 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006985 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 {
6987 if (wp->w_p_scb && wp->w_buffer)
6988 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006989 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 if (topline > y)
6991 topline = y;
6992 }
6993 }
6994 if (topline < 1)
6995 topline = 1;
6996 }
6997 else
6998 {
6999 topline = 1;
7000 }
7001
7002
7003 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007004 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007006 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 {
7008 if (curwin->w_p_scb)
7009 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007010 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 y = topline - curwin->w_topline;
7012 if (y > 0)
7013 scrollup(y, TRUE);
7014 else
7015 scrolldown(-y, TRUE);
7016 curwin->w_scbind_pos = topline;
7017 redraw_later(VALID);
7018 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 }
7021 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007022 curwin = save_curwin;
7023 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 if (curwin->w_p_scb)
7025 {
7026 did_syncbind = TRUE;
7027 checkpcmark();
7028 if (old_linenr != curwin->w_cursor.lnum)
7029 {
7030 char_u ctrl_o[2];
7031
7032 ctrl_o[0] = Ctrl_O;
7033 ctrl_o[1] = 0;
7034 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7035 }
7036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037}
7038
7039
7040 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007041ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007043 int i;
7044 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7045 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007047 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 do_bang(1, eap, FALSE, FALSE, TRUE);
7049 else
7050 {
7051 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7052 return;
7053
7054#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007055 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 {
7057 char_u *browseFile;
7058
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007059 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 NULL, NULL, NULL, curbuf);
7061 if (browseFile != NULL)
7062 {
7063 i = readfile(browseFile, NULL,
7064 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7065 vim_free(browseFile);
7066 }
7067 else
7068 i = OK;
7069 }
7070 else
7071#endif
7072 if (*eap->arg == NUL)
7073 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007074 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 return;
7076 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7077 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7078 }
7079 else
7080 {
7081 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7082 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7083 i = readfile(eap->arg, NULL,
7084 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7085
7086 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007087 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007089#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 if (!aborting())
7091#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007092 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 }
7094 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007095 {
7096 if (empty && exmode_active)
7097 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007098 // Delete the empty line that remains. Historically ex does
7099 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007100 if (eap->line2 == 0)
7101 lnum = curbuf->b_ml.ml_line_count;
7102 else
7103 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007104 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007105 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007106 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007107 if (curwin->w_cursor.lnum > 1
7108 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007109 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007110 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007111 }
7112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 }
7116}
7117
Bram Moolenaarea408852005-06-25 22:49:46 +00007118static char_u *prev_dir = NULL;
7119
7120#if defined(EXITFREE) || defined(PROTO)
7121 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007122free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007123{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007124 VIM_CLEAR(prev_dir);
7125 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007126}
7127#endif
7128
Bram Moolenaarf4258302013-06-02 18:20:17 +02007129/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007130 * Get the previous directory for the given chdir scope.
7131 */
7132 static char_u *
7133get_prevdir(cdscope_T scope)
7134{
7135 if (scope == CDSCOPE_WINDOW)
7136 return curwin->w_prevdir;
7137 else if (scope == CDSCOPE_TABPAGE)
7138 return curtab->tp_prevdir;
7139 return prev_dir;
7140}
7141
7142/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007143 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007144 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7145 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007146 */
7147 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007148post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007149{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007150 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007151 // Clear tab local directory for both :cd and :tcd
7152 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007153 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007154 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007155 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007156 char_u *pdir = get_prevdir(scope);
7157
7158 // If still in the global directory, need to remember current
7159 // directory as the global directory.
7160 if (globaldir == NULL && pdir != NULL)
7161 globaldir = vim_strsave(pdir);
7162
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007163 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007164 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007165 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007166 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007167 curtab->tp_localdir = vim_strsave(NameBuff);
7168 else
7169 curwin->w_localdir = vim_strsave(NameBuff);
7170 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007171 }
7172 else
7173 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007174 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007175 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007176 }
7177
7178 shorten_fnames(TRUE);
7179}
7180
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007181/*
7182 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007183 * chdir() function.
7184 * scope == CDSCOPE_WINDOW: changes the window-local directory
7185 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7186 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007187 * Returns TRUE if the directory is successfully changed.
7188 */
7189 int
7190changedir_func(
7191 char_u *new_dir,
7192 int forceit,
7193 cdscope_T scope)
7194{
7195 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02007196 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007197 int dir_differs;
7198 int retval = FALSE;
7199
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007200 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007201 return FALSE;
7202
7203 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7204 {
7205 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7206 return FALSE;
7207 }
7208
7209 // ":cd -": Change to previous directory
7210 if (STRCMP(new_dir, "-") == 0)
7211 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007212 pdir = get_prevdir(scope);
7213 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007214 {
7215 emsg(_("E186: No previous directory"));
7216 return FALSE;
7217 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007218 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007219 }
7220
Bram Moolenaar002bc792020-06-05 22:33:42 +02007221 // Free the previous directory
7222 tofree = get_prevdir(scope);
7223
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007224 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007225 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007226 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007227 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007228 pdir = NULL;
7229 if (scope == CDSCOPE_WINDOW)
7230 curwin->w_prevdir = pdir;
7231 else if (scope == CDSCOPE_TABPAGE)
7232 curtab->tp_prevdir = pdir;
7233 else
7234 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007235
7236#if defined(UNIX) || defined(VMS)
7237 // for UNIX ":cd" means: go to home directory
7238 if (*new_dir == NUL)
7239 {
7240 // use NameBuff for home directory name
7241# ifdef VMS
7242 char_u *p;
7243
7244 p = mch_getenv((char_u *)"SYS$LOGIN");
7245 if (p == NULL || *p == NUL) // empty is the same as not set
7246 NameBuff[0] = NUL;
7247 else
7248 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7249# else
7250 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7251# endif
7252 new_dir = NameBuff;
7253 }
7254#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02007255 dir_differs = new_dir == NULL || pdir == NULL
7256 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007257 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7258 emsg(_(e_failed));
7259 else
7260 {
7261 char_u *acmd_fname;
7262
7263 post_chdir(scope);
7264
7265 if (dir_differs)
7266 {
7267 if (scope == CDSCOPE_WINDOW)
7268 acmd_fname = (char_u *)"window";
7269 else if (scope == CDSCOPE_TABPAGE)
7270 acmd_fname = (char_u *)"tabpage";
7271 else
7272 acmd_fname = (char_u *)"global";
7273 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7274 curbuf);
7275 }
7276 retval = TRUE;
7277 }
7278 vim_free(tofree);
7279
7280 return retval;
7281}
Bram Moolenaarea408852005-06-25 22:49:46 +00007282
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007284 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007286 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007287ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007289 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290
7291 new_dir = eap->arg;
7292#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007293 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 if (*new_dir == NUL)
7295 ex_pwd(NULL);
7296 else
7297#endif
7298 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007299 cdscope_T scope = CDSCOPE_GLOBAL;
7300
7301 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7302 scope = CDSCOPE_WINDOW;
7303 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7304 scope = CDSCOPE_TABPAGE;
7305
7306 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007307 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007308 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007309 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 ex_pwd(eap);
7311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 }
7313}
7314
7315/*
7316 * ":pwd".
7317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007319ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320{
7321 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7322 {
7323#ifdef BACKSLASH_IN_FILENAME
7324 slash_adjust(NameBuff);
7325#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007326 if (p_verbose > 0)
7327 {
7328 char *context = "global";
7329
7330 if (curwin->w_localdir != NULL)
7331 context = "window";
7332 else if (curtab->tp_localdir != NULL)
7333 context = "tabpage";
7334 smsg("[%s] %s", context, (char *)NameBuff);
7335 }
7336 else
7337 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 }
7339 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007340 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341}
7342
7343/*
7344 * ":=".
7345 */
7346 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007347ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007349 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007350 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351}
7352
7353 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007354ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007356 int n;
7357 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358
7359 if (cursor_valid())
7360 {
7361 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7362 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007363 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007365
7366 len = eap->line2;
7367 switch (*eap->arg)
7368 {
7369 case 'm': break;
7370 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007371 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007372 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007373
7374 // Hide the cursor if invoked with !
7375 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376}
7377
7378/*
7379 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007380 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 */
7382 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007383do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384{
Bram Moolenaar52953192019-08-16 10:27:13 +02007385 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007386 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007387# ifdef ELAPSED_FUNC
7388 elapsed_T start_tv;
7389
7390 // Remember at what time we started, so that we know how much longer we
7391 // should wait after waiting for a bit.
7392 ELAPSED_INIT(start_tv);
7393# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007395 if (hide_cursor)
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007396 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007397 else
7398 cursor_on();
7399
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007400 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007401 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007403 wait_now = msec - done > 1000L ? 1000L : msec - done;
7404#ifdef FEAT_TIMERS
7405 {
7406 long due_time = check_due_timer();
7407
7408 if (due_time > 0 && due_time < wait_now)
7409 wait_now = due_time;
7410 }
7411#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007412#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007413 if (has_any_channel() && wait_now > 20L)
7414 wait_now = 20L;
7415#endif
7416#ifdef FEAT_SOUND
7417 if (has_any_sound_callback() && wait_now > 20L)
7418 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007419#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007420 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007421
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007422#ifdef FEAT_JOB_CHANNEL
7423 if (has_any_channel())
7424 ui_breakcheck_force(TRUE);
7425 else
7426#endif
7427 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007428#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007429 // Process the netbeans and clientserver messages that may have been
7430 // received in the call to ui_breakcheck() when the GUI is in use. This
7431 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007432 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007433#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007434
7435# ifdef ELAPSED_FUNC
7436 // actual time passed
7437 done = ELAPSED_FUNC(start_tv);
7438# else
7439 // guestimate time passed (will actually be more)
7440 done += wait_now;
7441# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007443
7444 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7445 // input buffer, otherwise a following call to input() fails.
7446 if (got_int)
7447 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007448
7449 if (hide_cursor)
7450 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451}
7452
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453/*
7454 * ":winsize" command (obsolete).
7455 */
7456 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007457ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458{
7459 int w, h;
7460 char_u *arg = eap->arg;
7461 char_u *p;
7462
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007463 if (!isdigit(*arg))
7464 {
7465 semsg(_(e_invarg2), arg);
7466 return;
7467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 w = getdigits(&arg);
7469 arg = skipwhite(arg);
7470 p = arg;
7471 h = getdigits(&arg);
7472 if (*p != NUL && *arg == NUL)
7473 set_shellsize(w, h, TRUE);
7474 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007475 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476}
7477
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007479ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480{
7481 int xchar = NUL;
7482 char_u *p;
7483
7484 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7485 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007486 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 if (eap->arg[1] == NUL)
7488 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007489 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 return;
7491 }
7492 xchar = eap->arg[1];
7493 p = eap->arg + 2;
7494 }
7495 else
7496 p = eap->arg + 1;
7497
7498 eap->nextcmd = check_nextcmd(p);
7499 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007500 if (*p != NUL && *p != (
7501#ifdef FEAT_EVAL
7502 in_vim9script() ? '#' :
7503#endif
7504 '"')
7505 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007506 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007507 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007509 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007510 postponed_split_flags = cmdmod.cmod_split;
7511 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7513 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007514 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 }
7516}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517
Bram Moolenaar843ee412004-06-30 16:16:41 +00007518#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519/*
7520 * ":winpos".
7521 */
7522 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007523ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524{
7525 int x, y;
7526 char_u *arg = eap->arg;
7527 char_u *p;
7528
7529 if (*arg == NUL)
7530 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007531# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007532# ifdef VIMDLL
7533 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7534 mch_get_winpos(&x, &y) != FAIL)
7535# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007537# else
7538 if (mch_get_winpos(&x, &y) != FAIL)
7539# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 {
7541 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007542 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 }
7544 else
7545# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007546 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 }
7548 else
7549 {
7550 x = getdigits(&arg);
7551 arg = skipwhite(arg);
7552 p = arg;
7553 y = getdigits(&arg);
7554 if (*p == NUL || *arg != NUL)
7555 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007556 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 return;
7558 }
7559# ifdef FEAT_GUI
7560 if (gui.in_use)
7561 gui_mch_set_winpos(x, y);
7562 else if (gui.starting)
7563 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007564 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 gui_win_x = x;
7566 gui_win_y = y;
7567 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007568# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 else
7570# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007571# endif
7572# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007573 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574# endif
7575# ifdef HAVE_TGETENT
7576 if (*T_CWP)
7577 term_set_winpos(x, y);
7578# endif
7579 }
7580}
7581#endif
7582
7583/*
7584 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7585 */
7586 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007587ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588{
7589 oparg_T oa;
7590
7591 clear_oparg(&oa);
7592 oa.regname = eap->regname;
7593 oa.start.lnum = eap->line1;
7594 oa.end.lnum = eap->line2;
7595 oa.line_count = eap->line2 - eap->line1 + 1;
7596 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007598 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 {
7600 setpcmark();
7601 curwin->w_cursor.lnum = eap->line1;
7602 beginline(BL_SOL | BL_FIX);
7603 }
7604
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007605 if (VIsual_active)
7606 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007607
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 switch (eap->cmdidx)
7609 {
7610 case CMD_delete:
7611 oa.op_type = OP_DELETE;
7612 op_delete(&oa);
7613 break;
7614
7615 case CMD_yank:
7616 oa.op_type = OP_YANK;
7617 (void)op_yank(&oa, FALSE, TRUE);
7618 break;
7619
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007620 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007621 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007623 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7624#else
7625 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007627 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 oa.op_type = OP_RSHIFT;
7629 else
7630 oa.op_type = OP_LSHIFT;
7631 op_shift(&oa, FALSE, eap->amount);
7632 break;
7633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007635 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636}
7637
7638/*
7639 * ":put".
7640 */
7641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007642ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007644 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 if (eap->line2 == 0)
7646 {
7647 eap->line2 = 1;
7648 eap->forceit = TRUE;
7649 }
7650 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007651 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007652 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653}
7654
7655/*
7656 * Handle ":copy" and ":move".
7657 */
7658 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007659ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660{
7661 long n;
7662
Bram Moolenaar491799b2020-08-01 19:23:43 +02007663#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007664 if (not_in_vim9(eap) == FAIL)
7665 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007666#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007667 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007668 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 {
7670 eap->nextcmd = NULL;
7671 return;
7672 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007673 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674
7675 /*
7676 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7677 */
7678 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7679 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007680 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 return;
7682 }
7683
7684 if (eap->cmdidx == CMD_move)
7685 {
7686 if (do_move(eap->line1, eap->line2, n) == FAIL)
7687 return;
7688 }
7689 else
7690 ex_copy(eap->line1, eap->line2, n);
7691 u_clearline();
7692 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007693 ex_may_print(eap);
7694}
7695
7696/*
7697 * Print the current line if flags were given to the Ex command.
7698 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007699 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007700ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007701{
7702 if (eap->flags != 0)
7703 {
7704 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7705 (eap->flags & EXFLAG_LIST));
7706 ex_no_reprint = TRUE;
7707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708}
7709
7710/*
7711 * ":smagic" and ":snomagic".
7712 */
7713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007714ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007716 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007718 magic_overruled = eap->cmdidx == CMD_smagic
7719 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02007720 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007721 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722}
7723
7724/*
7725 * ":join".
7726 */
7727 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007728ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729{
7730 curwin->w_cursor.lnum = eap->line1;
7731 if (eap->line1 == eap->line2)
7732 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007733 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 return;
7735 if (eap->line2 == curbuf->b_ml.ml_line_count)
7736 {
7737 beep_flush();
7738 return;
7739 }
7740 ++eap->line2;
7741 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007742 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007744 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745}
7746
7747/*
7748 * ":[addr]@r" or ":[addr]*r": execute register
7749 */
7750 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007751ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752{
7753 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007754 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755
7756 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007757 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758
7759#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007760 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761#endif
7762
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007763 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 c = *eap->arg;
7765 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7766 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007767 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007768 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7769 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007770 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 else
7774 {
7775 int save_efr = exec_from_reg;
7776
7777 exec_from_reg = TRUE;
7778
7779 /*
7780 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007781 * Continue until the stuff buffer is empty and all added characters
7782 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007784 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7786
7787 exec_from_reg = save_efr;
7788 }
7789}
7790
7791/*
7792 * ":!".
7793 */
7794 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007795ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796{
7797 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7798}
7799
7800/*
7801 * ":undo".
7802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007804ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007806 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007807 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007808 else
7809 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810}
7811
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007812#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007813 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007814ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007815{
7816 char_u hash[UNDO_HASH_SIZE];
7817
7818 u_compute_hash(hash);
7819 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7820}
7821
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007822 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007823ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007824{
7825 char_u hash[UNDO_HASH_SIZE];
7826
7827 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007828 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007829}
7830#endif
7831
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832/*
7833 * ":redo".
7834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007836ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837{
7838 u_redo(1);
7839}
7840
7841/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007842 * ":earlier" and ":later".
7843 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007844 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007845ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007846{
7847 long count = 0;
7848 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007849 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007850 char_u *p = eap->arg;
7851
7852 if (*p == NUL)
7853 count = 1;
7854 else if (isdigit(*p))
7855 {
7856 count = getdigits(&p);
7857 switch (*p)
7858 {
7859 case 's': ++p; sec = TRUE; break;
7860 case 'm': ++p; sec = TRUE; count *= 60; break;
7861 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007862 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7863 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007864 }
7865 }
7866
7867 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007868 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007869 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007870 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7871 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007872}
7873
7874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 * ":redir": start/stop redirection.
7876 */
7877 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007878ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879{
7880 char *mode;
7881 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007882 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883
Bram Moolenaarba768492016-07-08 15:32:54 +02007884#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007885 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007886 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007887 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007888 return;
7889 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007890#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007891
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 if (STRICMP(eap->arg, "END") == 0)
7893 close_redir();
7894 else
7895 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007896 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007898 ++arg;
7899 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007901 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 mode = "a";
7903 }
7904 else
7905 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007906 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907
7908 close_redir();
7909
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007910 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007911 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 if (fname == NULL)
7913 return;
7914#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007915 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {
7917 char_u *browseFile;
7918
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007919 browseFile = do_browse(BROWSE_SAVE,
7920 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007921 fname, NULL, NULL,
7922 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007924 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 vim_free(fname);
7926 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007927 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 }
7929#endif
7930
7931 redir_fd = open_exfile(fname, eap->forceit, mode);
7932 vim_free(fname);
7933 }
7934#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007935 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007937 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007939 ++arg;
7940 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007942 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007943 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007945 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007947 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007948 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007949 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007950 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007952 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007953 if (*arg == '>')
7954 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007955 // Make register empty when not using @A-@Z and the
7956 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007957 if (*arg == NUL && !isupper(redir_reg))
7958 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007960 }
7961 if (*arg != NUL)
7962 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007963 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007964 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007965 }
7966 }
7967 else if (*arg == '=' && arg[1] == '>')
7968 {
7969 int append;
7970
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007971 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007972 close_redir();
7973 arg += 2;
7974
7975 if (*arg == '>')
7976 {
7977 ++arg;
7978 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 }
7980 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007981 append = FALSE;
7982
7983 if (var_redir_start(skipwhite(arg), append) == OK)
7984 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 }
7986#endif
7987
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007988 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007991 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007993
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007994 // Make sure redirection is not off. Can happen for cmdline completion
7995 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007996 if (redir_fd != NULL
7997#ifdef FEAT_EVAL
7998 || redir_reg || redir_vname
7999#endif
8000 )
8001 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002}
8003
8004/*
8005 * ":redraw": force redraw
8006 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008007 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008008ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009{
8010 int r = RedrawingDisabled;
8011 int p = p_lz;
8012
8013 RedrawingDisabled = 0;
8014 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008015 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008017 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018#ifdef FEAT_TITLE
8019 if (need_maketitle)
8020 maketitle();
8021#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008022#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8023# ifdef VIMDLL
8024 if (!gui.in_use)
8025# endif
8026 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 RedrawingDisabled = r;
8029 p_lz = p;
8030
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008031 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 msg_didout = FALSE;
8033 msg_col = 0;
8034
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008035 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008036 need_wait_return = FALSE;
8037
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 out_flush();
8039}
8040
8041/*
8042 * ":redrawstatus": force redraw of status line(s)
8043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008045ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 int r = RedrawingDisabled;
8048 int p = p_lz;
8049
8050 RedrawingDisabled = 0;
8051 p_lz = FALSE;
8052 if (eap->forceit)
8053 status_redraw_all();
8054 else
8055 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008056 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 RedrawingDisabled = r;
8058 p_lz = p;
8059 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060}
8061
Bram Moolenaare12bab32019-01-08 22:02:56 +01008062/*
8063 * ":redrawtabline": force redraw of the tabline
8064 */
8065 static void
8066ex_redrawtabline(exarg_T *eap UNUSED)
8067{
8068 int r = RedrawingDisabled;
8069 int p = p_lz;
8070
8071 RedrawingDisabled = 0;
8072 p_lz = FALSE;
8073
8074 draw_tabline();
8075
8076 RedrawingDisabled = r;
8077 p_lz = p;
8078 out_flush();
8079}
8080
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008082close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083{
8084 if (redir_fd != NULL)
8085 {
8086 fclose(redir_fd);
8087 redir_fd = NULL;
8088 }
8089#ifdef FEAT_EVAL
8090 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008091 if (redir_vname)
8092 {
8093 var_redir_stop();
8094 redir_vname = 0;
8095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096#endif
8097}
8098
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008099#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008100 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008101vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008102{
8103 if (vim_mkdir(name, prot) != 0)
8104 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008105 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008106 return FAIL;
8107 }
8108 return OK;
8109}
8110#endif
8111
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112/*
8113 * Open a file for writing for an Ex command, with some checks.
8114 * Return file descriptor, or NULL on failure.
8115 */
8116 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008117open_exfile(
8118 char_u *fname,
8119 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008120 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121{
8122 FILE *fd;
8123
8124#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008125 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 if (mch_isdir(fname))
8127 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008128 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 return NULL;
8130 }
8131#endif
8132 if (!forceit && *mode != 'a' && vim_fexists(fname))
8133 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008134 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 return NULL;
8136 }
8137
8138 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008139 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140
8141 return fd;
8142}
8143
8144/*
8145 * ":mark" and ":k".
8146 */
8147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008148ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149{
8150 pos_T pos;
8151
Bram Moolenaar10b94212021-02-19 21:42:57 +01008152#ifdef FEAT_EVAL
8153 if (not_in_vim9(eap) == FAIL)
8154 return;
8155#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008156 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008157 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008158 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02008159 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 else
8161 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008162 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 curwin->w_cursor.lnum = eap->line2;
8164 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008165 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008166 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008167 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 }
8169}
8170
8171/*
8172 * Update w_topline, w_leftcol and the cursor position.
8173 */
8174 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008175update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008177 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 update_topline();
8179 if (!curwin->w_p_wrap)
8180 validate_cursor();
8181 update_curswant();
8182}
8183
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008185 * Save the current State and go to Normal mode.
8186 * Return TRUE if the typeahead could be saved.
8187 */
8188 int
8189save_current_state(save_state_T *sst)
8190{
8191 sst->save_msg_scroll = msg_scroll;
8192 sst->save_restart_edit = restart_edit;
8193 sst->save_msg_didout = msg_didout;
8194 sst->save_State = State;
8195 sst->save_insertmode = p_im;
8196 sst->save_finish_op = finish_op;
8197 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008198 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008199
Bram Moolenaar4324d872020-12-01 20:12:24 +01008200 msg_scroll = FALSE; // no msg scrolling in Normal mode
8201 restart_edit = 0; // don't go to Insert mode
8202 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008203
Bram Moolenaara452b802020-12-01 21:47:59 +01008204 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008205 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008206
8207 /*
8208 * Save the current typeahead. This is required to allow using ":normal"
8209 * from an event handler and makes sure we don't hang when the argument
8210 * ends with half a command.
8211 */
8212 save_typeahead(&sst->tabuf);
8213 return sst->tabuf.typebuf_valid;
8214}
8215
8216 void
8217restore_current_state(save_state_T *sst)
8218{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008219 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008220 restore_typeahead(&sst->tabuf);
8221
8222 msg_scroll = sst->save_msg_scroll;
8223 restart_edit = sst->save_restart_edit;
8224 p_im = sst->save_insertmode;
8225 finish_op = sst->save_finish_op;
8226 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008227 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008228 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008229 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008230
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008231 // Restore the state (needed when called from a function executed for
8232 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008233 State = sst->save_State;
8234#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008235 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008236#endif
8237}
8238
8239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 * ":normal[!] {commands}": Execute normal mode commands.
8241 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008242 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008243ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008245 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 char_u *arg = NULL;
8247 int l;
8248 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008250 if (ex_normal_lock > 0)
8251 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008252 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008253 return;
8254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 if (ex_normal_busy >= p_mmd)
8256 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008257 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 return;
8259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 /*
8262 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8263 * this for the K_SPECIAL leading byte, otherwise special keys will not
8264 * work.
8265 */
8266 if (has_mbyte)
8267 {
8268 int len = 0;
8269
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008270 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 for (p = eap->arg; *p != NUL; ++p)
8272 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008273#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008274 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008276#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008277 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008278 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008279#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 )
8283 len += 2;
8284 }
8285 if (len > 0)
8286 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008287 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 if (arg != NULL)
8289 {
8290 len = 0;
8291 for (p = eap->arg; *p != NUL; ++p)
8292 {
8293 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008294#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 if (*p == CSI)
8296 {
8297 arg[len++] = KS_EXTRA;
8298 arg[len++] = (int)KE_CSI;
8299 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008300#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008301 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {
8303 arg[len++] = *++p;
8304 if (*p == K_SPECIAL)
8305 {
8306 arg[len++] = KS_SPECIAL;
8307 arg[len++] = KE_FILLER;
8308 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008309#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 else if (*p == CSI)
8311 {
8312 arg[len++] = KS_EXTRA;
8313 arg[len++] = (int)KE_CSI;
8314 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 }
8317 arg[len] = NUL;
8318 }
8319 }
8320 }
8321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008323 ++ex_normal_busy;
8324 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 {
8326 /*
8327 * Repeat the :normal command for each line in the range. When no
8328 * range given, execute it just once, without positioning the cursor
8329 * first.
8330 */
8331 do
8332 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 if (eap->addr_count != 0)
8334 {
8335 curwin->w_cursor.lnum = eap->line1++;
8336 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008337 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 }
8339
Bram Moolenaar13505972019-01-24 15:04:48 +01008340 exec_normal_cmd(arg != NULL
8341 ? arg
8342 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 }
8344 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8345 }
8346
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008347 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 update_topline_cursor();
8349
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008350 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008352 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008353#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008354 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008355#endif
8356
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358}
8359
8360/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008361 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 */
8363 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008364ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008366 if (eap->forceit)
8367 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008368 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008369 if (!curwin->w_cursor.lnum)
8370 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008371 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008372 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008373#ifdef FEAT_TERMINAL
8374 // Ignore this when running in an active terminal.
8375 if (term_job_running(curbuf->b_term))
8376 return;
8377#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008378
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008379 // Ignore the command when already in Insert mode. Inserting an
8380 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008381 if (State & INSERT)
8382 return;
8383
Bram Moolenaar64969662005-12-14 21:59:55 +00008384 if (eap->cmdidx == CMD_startinsert)
8385 restart_edit = 'a';
8386 else if (eap->cmdidx == CMD_startreplace)
8387 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008389 restart_edit = 'V';
8390
8391 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008393 if (eap->cmdidx == CMD_startinsert)
8394 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008395 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008397
8398 if (VIsual_active)
8399 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400}
8401
8402/*
8403 * ":stopinsert"
8404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008406ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407{
8408 restart_edit = 0;
8409 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008410 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008413/*
8414 * Execute normal mode command "cmd".
8415 * "remap" can be REMAP_NONE or REMAP_YES.
8416 */
8417 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008418exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008419{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008420 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008421 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008422 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008423}
Bram Moolenaar25281632016-01-21 23:32:32 +01008424
Bram Moolenaar25281632016-01-21 23:32:32 +01008425/*
8426 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008427 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008428 */
8429 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008430exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008431{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008432 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008433 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008434
Bram Moolenaar905dd902019-04-07 14:21:47 +02008435 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8436 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008437 clear_oparg(&oa);
8438 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008439 while ((!stuff_empty()
8440 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008441 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008442 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008443 {
8444 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008445#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008446 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008447 && oa.op_type == OP_NOP && oa.regname == NUL
8448 && !VIsual_active)
8449 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008450 // If terminal_loop() returns OK we got a key that is handled
8451 // in Normal model. With FAIL we first need to position the
8452 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008453 if (terminal_loop(TRUE) == OK)
8454 normal_cmd(&oa, TRUE);
8455 }
8456 else
8457#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008458 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008459 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008460 }
8461}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008462
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463#ifdef FEAT_FIND_ID
8464 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008465ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466{
8467 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8468 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8469 (linenr_T)1, (linenr_T)MAXLNUM);
8470}
8471
Bram Moolenaar4033c552017-09-16 20:54:51 +02008472#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473/*
8474 * ":psearch"
8475 */
8476 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008477ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478{
8479 g_do_tagpreview = p_pvh;
8480 ex_findpat(eap);
8481 g_do_tagpreview = 0;
8482}
8483#endif
8484
8485 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008486ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487{
8488 int whole = TRUE;
8489 long n;
8490 char_u *p;
8491 int action;
8492
8493 switch (cmdnames[eap->cmdidx].cmd_name[2])
8494 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008495 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8497 action = ACTION_GOTO;
8498 else
8499 action = ACTION_SHOW;
8500 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008501 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 action = ACTION_SHOW_ALL;
8503 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008504 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 action = ACTION_GOTO;
8506 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008507 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 action = ACTION_SPLIT;
8509 break;
8510 }
8511
8512 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008513 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 {
8515 n = getdigits(&eap->arg);
8516 eap->arg = skipwhite(eap->arg);
8517 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008518 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 {
8520 whole = FALSE;
8521 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008522 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 if (*p)
8524 {
8525 *p++ = NUL;
8526 p = skipwhite(p);
8527
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008528 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008529 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008530 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 else
8532 eap->nextcmd = check_nextcmd(p);
8533 }
8534 }
8535 if (!eap->skip)
8536 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8537 whole, !eap->forceit,
8538 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8539 n, action, eap->line1, eap->line2);
8540}
8541#endif
8542
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543
Bram Moolenaar4033c552017-09-16 20:54:51 +02008544#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545/*
8546 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8547 */
8548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008549ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008551 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8553}
8554
8555/*
8556 * ":pedit"
8557 */
8558 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008559ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560{
8561 win_T *curwin_save = curwin;
8562
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008563 if (ERROR_IF_ANY_POPUP_WINDOW)
8564 return;
8565
Bram Moolenaar026587b2019-08-17 15:08:00 +02008566 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008568 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008569
Bram Moolenaar026587b2019-08-17 15:08:00 +02008570 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008572
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 if (curwin != curwin_save && win_valid(curwin_save))
8574 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008575 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 validate_cursor();
8577 redraw_later(VALID);
8578 win_enter(curwin_save, TRUE);
8579 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008580# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008581 else if (WIN_IS_POPUP(curwin))
8582 {
8583 // can't keep focus in popup window
8584 win_enter(firstwin, TRUE);
8585 }
8586# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 g_do_tagpreview = 0;
8588}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590
8591/*
8592 * ":stag", ":stselect" and ":stjump".
8593 */
8594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008595ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596{
8597 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008598 postponed_split_flags = cmdmod.cmod_split;
8599 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8601 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008602 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604
8605/*
8606 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8607 */
8608 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008609ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610{
8611 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8612}
8613
8614 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008615ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616{
8617 int cmd;
8618
8619 switch (name[1])
8620 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008621 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008623 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008625 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008627 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008629 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008631 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008633 case 'f': // ":tfirst"
8634 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008636 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008638 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008640 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008642 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 return;
8644 }
8645#endif
8646 cmd = DT_TAG;
8647 break;
8648 }
8649
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008650 if (name[0] == 'l')
8651 {
8652#ifndef FEAT_QUICKFIX
8653 ex_ni(eap);
8654 return;
8655#else
8656 cmd = DT_LTAG;
8657#endif
8658 }
8659
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8661 eap->forceit, TRUE);
8662}
8663
8664/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008665 * Check "str" for starting with a special cmdline variable.
8666 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8667 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8668 */
8669 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008670find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008671{
8672 int len;
8673 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008674 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008675 "%",
8676#define SPEC_PERC 0
8677 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008678#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008679 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008680#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008681 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008682#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008683 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008684#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008685 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008686#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008687 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008688#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008689 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008690#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008691 "<stack>", // call stack
8692#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008693 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008694#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008695 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008696#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008697 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008698#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008699 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008700#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008701 "<SID>", // script ID: <SNR>123_
8702#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008703#ifdef FEAT_CLIENTSERVER
8704 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008705# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008706#endif
8707 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008708
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008709 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008710 {
8711 len = (int)STRLEN(spec_str[i]);
8712 if (STRNCMP(src, spec_str[i], len) == 0)
8713 {
8714 *usedlen = len;
8715 return i;
8716 }
8717 }
8718 return -1;
8719}
8720
8721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 * Evaluate cmdline variables.
8723 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008724 * change "%" to curbuf->b_ffname
8725 * "#" to curwin->w_alt_fnum
8726 * "%%" to curwin->w_alt_fnum in Vim9 script
8727 * "<cword>" to word under the cursor
8728 * "<cWORD>" to WORD under the cursor
8729 * "<cexpr>" to C-expression under the cursor
8730 * "<cfile>" to path name under the cursor
8731 * "<sfile>" to sourced file name
8732 * "<stack>" to call stack
8733 * "<slnum>" to sourced file line number
8734 * "<afile>" to file name for autocommand
8735 * "<abuf>" to buffer number for autocommand
8736 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 *
8738 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8739 * "" for error without a message) and NULL is returned.
8740 * Returns an allocated string if a valid match was found.
8741 * Returns NULL if no match was found. "usedlen" then still contains the
8742 * number of characters to skip.
8743 */
8744 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008745eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008746 char_u *src, // pointer into commandline
8747 char_u *srcstart, // beginning of valid memory for src
8748 int *usedlen, // characters after src that are used
8749 linenr_T *lnump, // line number for :e command, or NULL
8750 char **errormsg, // pointer to error message
8751 int *escaped) // return value has escaped white space (can
8752 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753{
8754 int i;
8755 char_u *s;
8756 char_u *result;
8757 char_u *resultbuf = NULL;
8758 int resultlen;
8759 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008760 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008762 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765
8766 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008767 if (escaped != NULL)
8768 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769
8770 /*
8771 * Check if there is something to do.
8772 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008773 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008774 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 {
8776 *usedlen = 1;
8777 return NULL;
8778 }
8779
8780 /*
8781 * Skip when preceded with a backslash "\%" and "\#".
8782 * Note: In "\\%" the % is also not recognized!
8783 */
8784 if (src > srcstart && src[-1] == '\\')
8785 {
8786 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008787 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 return NULL;
8789 }
8790
8791 /*
8792 * word or WORD under cursor
8793 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008794 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8795 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008797 resultlen = find_ident_under_cursor(&result,
8798 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8799 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8800 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 if (resultlen == 0)
8802 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008803 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 return NULL;
8805 }
8806 }
8807
8808 /*
8809 * '#': Alternate file name
8810 * '%': Current file name
8811 * File name under the cursor
8812 * File name for autocommand
8813 * and following modifiers
8814 */
8815 else
8816 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008817 int off = 0;
8818
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 switch (spec_idx)
8820 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008821 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008822#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008823 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008826 // '%': current file
8827 if (curbuf->b_fname == NULL)
8828 {
8829 result = (char_u *)"";
8830 valid = 0; // Must have ":p:h" to be valid
8831 }
8832 else
8833 {
8834 result = curbuf->b_fname;
8835 tilde_file = STRCMP(result, "~") == 0;
8836 }
8837 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008839#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008840 // "%%" alternate file
8841 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008842#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01008843 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008844 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008845 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008847 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 result = arg_all();
8849 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008850 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008851 if (escaped != NULL)
8852 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 break;
8855 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008856 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008857 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008858 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008860 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008861 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008862 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008863 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008865 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008867 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00008868 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008869 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008870 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008871 return NULL;
8872 }
8873#ifdef FEAT_EVAL
8874 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8875 (long)i);
8876 if (result == NULL)
8877 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008878 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008879 return NULL;
8880 }
8881#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008882 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 }
8886 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008887 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008888 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
8889 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008890 buf = buflist_findnr(i);
8891 if (buf == NULL)
8892 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008893 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008894 return NULL;
8895 }
8896 if (lnump != NULL)
8897 *lnump = ECMD_LAST;
8898 if (buf->b_fname == NULL)
8899 {
8900 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008901 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008902 }
8903 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008904 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008905 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008906 tilde_file = STRCMP(result, "~") == 0;
8907 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 break;
8910
8911#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008912 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008913 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 if (result == NULL)
8915 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008916 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 return NULL;
8918 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008919 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 break;
8921#endif
8922
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008923 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008925 if (result != NULL && !autocmd_fname_full)
8926 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008927 // Still need to turn the fname into a full path. It is
8928 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008929 autocmd_fname_full = TRUE;
8930 result = FullName_save(autocmd_fname, FALSE);
8931 vim_free(autocmd_fname);
8932 autocmd_fname = result;
8933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 if (result == NULL)
8935 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008936 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 return NULL;
8938 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008939 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 break;
8941
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008942 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 if (autocmd_bufnr <= 0)
8944 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008945 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 return NULL;
8947 }
8948 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8949 result = strbuf;
8950 break;
8951
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008952 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 result = autocmd_match;
8954 if (result == NULL)
8955 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008956 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 return NULL;
8958 }
8959 break;
8960
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008961 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008962 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008963 result = estack_sfile(spec_idx == SPEC_SFILE
8964 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 if (result == NULL)
8966 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008967 *errormsg = spec_idx == SPEC_SFILE
8968 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8969 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 return NULL;
8971 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008972 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008974
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008975 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008976 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008977 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008978 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008979 return NULL;
8980 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008981 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008982 result = strbuf;
8983 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008984
8985#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008986 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008987 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008988 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008989 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008990 return NULL;
8991 }
8992 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008993 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008994 result = strbuf;
8995 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008996
Bram Moolenaar90944302020-08-01 20:45:11 +02008997 case SPEC_SID:
8998 if (current_sctx.sc_sid <= 0)
8999 {
9000 *errormsg = _(e_usingsid);
9001 return NULL;
9002 }
9003 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9004 result = strbuf;
9005 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009006#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009007
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009008#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009009 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009010 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9011 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 result = strbuf;
9013 break;
9014#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009015
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009016 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009017 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009018 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 }
9020
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009021 resultlen = (int)STRLEN(result); // length of new string
9022 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 {
9024 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 resultlen = (int)(s - result);
9027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 else if (!skip_mod)
9029 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009030 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 &resultlen);
9032 if (result == NULL)
9033 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009034 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 return NULL;
9036 }
9037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 }
9039
9040 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9041 {
9042 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009043 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009044 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009046 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 result = NULL;
9048 }
9049 else
9050 result = vim_strnsave(result, resultlen);
9051 vim_free(resultbuf);
9052 return result;
9053}
9054
9055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 * Expand the <sfile> string in "arg".
9057 *
9058 * Returns an allocated string, or NULL for any error.
9059 */
9060 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009061expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009063 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 int len;
9065 char_u *result;
9066 char_u *newres;
9067 char_u *repl;
9068 int srclen;
9069 char_u *p;
9070
9071 result = vim_strsave(arg);
9072 if (result == NULL)
9073 return NULL;
9074
9075 for (p = result; *p; )
9076 {
9077 if (STRNCMP(p, "<sfile>", 7) != 0)
9078 ++p;
9079 else
9080 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009081 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00009082 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 if (errormsg != NULL)
9084 {
9085 if (*errormsg)
9086 emsg(errormsg);
9087 vim_free(result);
9088 return NULL;
9089 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009090 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 {
9092 p += srclen;
9093 continue;
9094 }
9095 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9096 newres = alloc(len);
9097 if (newres == NULL)
9098 {
9099 vim_free(repl);
9100 vim_free(result);
9101 return NULL;
9102 }
9103 mch_memmove(newres, result, (size_t)(p - result));
9104 STRCPY(newres + (p - result), repl);
9105 len = (int)STRLEN(newres);
9106 STRCAT(newres, p + srclen);
9107 vim_free(repl);
9108 vim_free(result);
9109 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009110 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 }
9112 }
9113
9114 return result;
9115}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009118/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009119 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009120 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009121 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009123dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 if (fname == NULL)
9126 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009127 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128}
9129#endif
9130
9131/*
9132 * ":behave {mswin,xterm}"
9133 */
9134 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009135ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136{
9137 if (STRCMP(eap->arg, "mswin") == 0)
9138 {
9139 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9140 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9141 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9142 set_option_value((char_u *)"keymodel", 0L,
9143 (char_u *)"startsel,stopsel", 0);
9144 }
9145 else if (STRCMP(eap->arg, "xterm") == 0)
9146 {
9147 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9148 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9149 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9150 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9151 }
9152 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009153 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154}
9155
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156static int filetype_detect = FALSE;
9157static int filetype_plugin = FALSE;
9158static int filetype_indent = FALSE;
9159
9160/*
9161 * ":filetype [plugin] [indent] {on,off,detect}"
9162 * on: Load the filetype.vim file to install autocommands for file types.
9163 * off: Load the ftoff.vim file to remove all autocommands for file types.
9164 * plugin on: load filetype.vim and ftplugin.vim
9165 * plugin off: load ftplugof.vim
9166 * indent on: load filetype.vim and indent.vim
9167 * indent off: load indoff.vim
9168 */
9169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009170ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171{
9172 char_u *arg = eap->arg;
9173 int plugin = FALSE;
9174 int indent = FALSE;
9175
9176 if (*eap->arg == NUL)
9177 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009178 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009179 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 filetype_detect ? "ON" : "OFF",
9181 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9182 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9183 return;
9184 }
9185
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009186 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 for (;;)
9188 {
9189 if (STRNCMP(arg, "plugin", 6) == 0)
9190 {
9191 plugin = TRUE;
9192 arg = skipwhite(arg + 6);
9193 continue;
9194 }
9195 if (STRNCMP(arg, "indent", 6) == 0)
9196 {
9197 indent = TRUE;
9198 arg = skipwhite(arg + 6);
9199 continue;
9200 }
9201 break;
9202 }
9203 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9204 {
9205 if (*arg == 'o' || !filetype_detect)
9206 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009207 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 filetype_detect = TRUE;
9209 if (plugin)
9210 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009211 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 filetype_plugin = TRUE;
9213 }
9214 if (indent)
9215 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009216 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 filetype_indent = TRUE;
9218 }
9219 }
9220 if (*arg == 'd')
9221 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009222 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009223 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 }
9225 }
9226 else if (STRCMP(arg, "off") == 0)
9227 {
9228 if (plugin || indent)
9229 {
9230 if (plugin)
9231 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009232 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 filetype_plugin = FALSE;
9234 }
9235 if (indent)
9236 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009237 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 filetype_indent = FALSE;
9239 }
9240 }
9241 else
9242 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009243 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 filetype_detect = FALSE;
9245 }
9246 }
9247 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009248 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249}
9250
9251/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009252 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 */
9254 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009255ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256{
9257 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009258 {
9259 char_u *arg = eap->arg;
9260
9261 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9262 arg += 9;
9263
9264 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9265 if (arg != eap->arg)
9266 did_filetype = FALSE;
9267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009271ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272{
9273#ifdef FEAT_DIGRAPHS
9274 if (*eap->arg != NUL)
9275 putdigraph(eap->arg);
9276 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009277 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009279 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280#endif
9281}
9282
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009283#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9284 void
9285set_no_hlsearch(int flag)
9286{
9287 no_hlsearch = flag;
9288# ifdef FEAT_EVAL
9289 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9290# endif
9291}
9292
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293/*
9294 * ":nohlsearch"
9295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009297ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009299 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009300 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302#endif
9303
9304#ifdef FEAT_CRYPT
9305/*
9306 * ":X": Get crypt key
9307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009309ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009311 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009312 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313}
9314#endif
9315
9316#ifdef FEAT_FOLDING
9317 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009318ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319{
9320 if (foldManualAllowed(TRUE))
9321 foldCreate(eap->line1, eap->line2);
9322}
9323
9324 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009325ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326{
9327 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9328 eap->forceit, FALSE);
9329}
9330
9331 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009332ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333{
9334 linenr_T lnum;
9335
Bram Moolenaar4facea32019-10-12 20:17:40 +02009336# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009337 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009338# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009339
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009340 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9342 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9343 ml_setmarked(lnum);
9344
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009345 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009347 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009348# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009349 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009350# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351}
9352#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009353
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009354#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009355/*
9356 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9357 * instead of a quickfix command.
9358 */
9359 int
9360is_loclist_cmd(int cmdidx)
9361{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009362 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009363 return FALSE;
9364 return cmdnames[cmdidx].cmd_name[0] == 'l';
9365}
9366#endif
9367
Bram Moolenaar4facea32019-10-12 20:17:40 +02009368#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009369 int
9370get_pressedreturn(void)
9371{
9372 return ex_pressedreturn;
9373}
9374
9375 void
9376set_pressedreturn(int val)
9377{
9378 ex_pressedreturn = val;
9379}
9380#endif