blob: 2d5752445b0e6675d966b1d3daf8a8ac51452c80 [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);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100112static void ex_exit(exarg_T *eap);
113static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100115static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#else
117# define ex_goto ex_ni
118#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100119static void ex_shell(exarg_T *eap);
120static void ex_preserve(exarg_T *eap);
121static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100122static void ex_mode(exarg_T *eap);
123static void ex_wrongmodifier(exarg_T *eap);
124static void ex_find(exarg_T *eap);
125static void ex_open(exarg_T *eap);
126static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#ifndef FEAT_GUI
128# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100129static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100131#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100132static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#else
134# define ex_tearoff ex_ni
135#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100136#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
137 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100138static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#else
140# define ex_popup ex_ni
141#endif
142#ifndef FEAT_GUI_MSWIN
143# define ex_simalt ex_ni
144#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000145#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146# define gui_mch_find_dialog ex_ni
147# define gui_mch_replace_dialog ex_ni
148#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000149#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150# define ex_helpfind ex_ni
151#endif
152#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100153# define ex_cscope ex_ni
154# define ex_scscope ex_ni
155# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#endif
157#ifndef FEAT_SYN_HL
158# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200159# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000160#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200161#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200162# define ex_syntime ex_ni
163#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000164#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000165# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000166# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000167# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000168# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000169# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000170#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200171#ifndef FEAT_PERSISTENT_UNDO
172# define ex_rundo ex_ni
173# define ex_wundo ex_ni
174#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200175#ifndef FEAT_LUA
176# define ex_lua ex_script_ni
177# define ex_luado ex_ni
178# define ex_luafile ex_ni
179#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000180#ifndef FEAT_MZSCHEME
181# define ex_mzscheme ex_script_ni
182# define ex_mzfile ex_ni
183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184#ifndef FEAT_PERL
185# define ex_perl ex_script_ni
186# define ex_perldo ex_ni
187#endif
188#ifndef FEAT_PYTHON
189# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200190# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191# define ex_pyfile ex_ni
192#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200193#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200194# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200195# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200196# define ex_py3file ex_ni
197#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100198#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
199# define ex_pyx ex_script_ni
200# define ex_pyxdo ex_ni
201# define ex_pyxfile ex_ni
202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203#ifndef FEAT_TCL
204# define ex_tcl ex_script_ni
205# define ex_tcldo ex_ni
206# define ex_tclfile ex_ni
207#endif
208#ifndef FEAT_RUBY
209# define ex_ruby ex_script_ni
210# define ex_rubydo ex_ni
211# define ex_rubyfile ex_ni
212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213#ifndef FEAT_KEYMAP
214# define ex_loadkeymap ex_ni
215#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100216static void ex_swapname(exarg_T *eap);
217static void ex_syncbind(exarg_T *eap);
218static void ex_read(exarg_T *eap);
219static void ex_pwd(exarg_T *eap);
220static void ex_equal(exarg_T *eap);
221static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100222static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000224#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#else
227# define ex_winpos ex_ni
228#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100229static void ex_operators(exarg_T *eap);
230static void ex_put(exarg_T *eap);
231static void ex_copymove(exarg_T *eap);
232static void ex_submagic(exarg_T *eap);
233static void ex_join(exarg_T *eap);
234static void ex_at(exarg_T *eap);
235static void ex_bang(exarg_T *eap);
236static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200237#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100238static void ex_wundo(exarg_T *eap);
239static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200240#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ex_redo(exarg_T *eap);
242static void ex_later(exarg_T *eap);
243static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100244static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100245static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100246static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ex_startinsert(exarg_T *eap);
249static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100251static void ex_checkpath(exarg_T *eap);
252static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253#else
254# define ex_findpat ex_ni
255# define ex_checkpath ex_ni
256#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200257#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100258static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259#else
260# define ex_psearch ex_ni
261#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100262static void ex_tag(exarg_T *eap);
263static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#ifndef FEAT_EVAL
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200265# define ex_block ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200266# define ex_break ex_ni
267# define ex_breakadd ex_ni
268# define ex_breakdel ex_ni
269# define ex_breaklist ex_ni
270# define ex_call ex_ni
271# define ex_catch ex_ni
272# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200273# define ex_continue ex_ni
274# define ex_debug ex_ni
275# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200276# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200277# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279# define ex_echo ex_ni
280# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281# define ex_else ex_ni
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200282# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200283# define ex_endfunction ex_ni
284# define ex_endif ex_ni
285# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200287# define ex_eval ex_ni
288# define ex_execute ex_ni
Bram Moolenaar340c59e2021-04-24 20:37:03 +0200289# define ex_incdec ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200290# 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 Moolenaare4ce8252019-08-04 15:30:16 +0200306# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100307# define ex_import ex_ni
308# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200310#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311# define ex_loadview ex_ni
312#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200313#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314# define ex_viminfo ex_ni
315#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100316static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_filetype(exarg_T *eap);
318static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000320# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321# define ex_diffpatch ex_ni
322# define ex_diffgetput ex_ni
323# define ex_diffsplit ex_ni
324# define ex_diffthis ex_ni
325# define ex_diffupdate ex_ni
326#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100329static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#else
331# define ex_nohlsearch ex_ni
332# define ex_match ex_ni
333#endif
334#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100335static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336#else
337# define ex_X ex_ni
338#endif
339#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100340static void ex_fold(exarg_T *eap);
341static void ex_foldopen(exarg_T *eap);
342static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#else
344# define ex_fold ex_ni
345# define ex_foldopen ex_ni
346# define ex_folddo ex_ni
347#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100348#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349# define ex_language ex_ni
350#endif
351#ifndef FEAT_SIGNS
352# define ex_sign ex_ni
353#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000354#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200355# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200357# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359
Bram Moolenaar05159a02005-02-26 23:04:13 +0000360#ifndef FEAT_PROFILE
361# define ex_profile ex_ni
362#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200363#ifndef FEAT_TERMINAL
364# define ex_terminal ex_ni
365#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200366#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
367# define ex_xrestore ex_ni
368#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100369#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200370# define ex_popupclear ex_ni
371#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000372
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373/*
374 * Declare cmdnames[].
375 */
376#define DO_DECLARE_EXCMD
377#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200378#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100379
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380static char_u dollar_command[2] = {'$', 0};
381
382
383#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100384// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385typedef struct
386{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100387 char_u *line; // command line
388 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389} wcmd_T;
390
391/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000392 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000394 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000396struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100398 garray_T *lines_gap; // growarray with line info
399 int current_line; // last read line from growarray
400 int repeating; // TRUE when looping a second time
401 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200402 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 void *cookie;
404};
405
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200406static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100407static int store_loop_line(garray_T *gap, char_u *line);
408static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000409
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100410// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000411struct dbg_stuff
412{
413 int trylevel;
414 int force_abort;
415 except_T *caught_stack;
416 char_u *vv_exception;
417 char_u *vv_throwpoint;
418 int did_emsg;
419 int got_int;
420 int did_throw;
421 int need_rethrow;
422 int check_cstack;
423 except_T *current_exception;
424};
425
Bram Moolenaared203462004-06-16 11:19:22 +0000426 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100427save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000428{
429 dsp->trylevel = trylevel; trylevel = 0;
430 dsp->force_abort = force_abort; force_abort = FALSE;
431 dsp->caught_stack = caught_stack; caught_stack = NULL;
432 dsp->vv_exception = v_exception(NULL);
433 dsp->vv_throwpoint = v_throwpoint(NULL);
434
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100435 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000436 dsp->did_emsg = did_emsg; did_emsg = FALSE;
437 dsp->got_int = got_int; got_int = FALSE;
438 dsp->did_throw = did_throw; did_throw = FALSE;
439 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
440 dsp->check_cstack = check_cstack; check_cstack = FALSE;
441 dsp->current_exception = current_exception; current_exception = NULL;
442}
443
444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100445restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000446{
447 suppress_errthrow = FALSE;
448 trylevel = dsp->trylevel;
449 force_abort = dsp->force_abort;
450 caught_stack = dsp->caught_stack;
451 (void)v_exception(dsp->vv_exception);
452 (void)v_throwpoint(dsp->vv_throwpoint);
453 did_emsg = dsp->did_emsg;
454 got_int = dsp->got_int;
455 did_throw = dsp->did_throw;
456 need_rethrow = dsp->need_rethrow;
457 check_cstack = dsp->check_cstack;
458 current_exception = dsp->current_exception;
459}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460#endif
461
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462/*
463 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
464 * command is given.
465 */
466 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100467do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100468 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469{
470 int save_msg_scroll;
471 int prev_msg_row;
472 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100473 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000474
475 if (improved)
476 exmode_active = EXMODE_VIM;
477 else
478 exmode_active = EXMODE_NORMAL;
479 State = NORMAL;
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +0200480 trigger_modechanged();
Bram Moolenaardf177f62005-02-22 08:39:57 +0000481
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100482 // When using ":global /pat/ visual" and then "Q" we return to continue
483 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000484 if (global_busy)
485 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486
487 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100488 ++RedrawingDisabled; // don't redisplay the window
489 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100491 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 ++hold_gui_events;
493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494
Bram Moolenaar32526b32019-01-19 17:43:09 +0100495 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 while (exmode_active)
497 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100498 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000499 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
500 {
501 exmode_active = FALSE;
502 break;
503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 msg_scroll = TRUE;
505 need_wait_return = FALSE;
506 ex_pressedreturn = FALSE;
507 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100508 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 prev_msg_row = msg_row;
510 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 if (improved)
512 {
513 cmdline_row = msg_row;
514 do_cmdline(NULL, getexline, NULL, 0);
515 }
516 else
517 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
518 lines_left = Rows - 1;
519
Bram Moolenaardf177f62005-02-22 08:39:57 +0000520 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100521 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000523 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000524 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000525 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000527 if (ex_pressedreturn)
528 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100529 // go up one line, to overwrite the ":<CR>" line, so the
530 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000531 msg_row = prev_msg_row;
532 if (prev_msg_row == Rows - 1)
533 msg_row--;
534 }
535 msg_col = 0;
536 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
537 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100540 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000541 {
542 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000543 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000544 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000545 emsg(_(e_at_end_of_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 }
548
549#ifdef FEAT_GUI
550 --hold_gui_events;
551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 --RedrawingDisabled;
553 --no_wait_return;
554 update_screen(CLEAR);
555 need_wait_return = FALSE;
556 msg_scroll = save_msg_scroll;
557}
558
559/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200560 * Print the executed command for when 'verbose' is set.
561 * When "lnum" is 0 only print the command.
562 */
563 static void
564msg_verbose_cmd(linenr_T lnum, char_u *cmd)
565{
566 ++no_wait_return;
567 verbose_enter_scroll();
568
569 if (lnum == 0)
570 smsg(_("Executing: %s"), cmd);
571 else
572 smsg(_("line %ld: %s"), (long)lnum, cmd);
573 if (msg_silent == 0)
574 msg_puts("\n"); // don't overwrite this
575
576 verbose_leave_scroll();
577 --no_wait_return;
578}
579
580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 * Execute a simple command line. Used for translated commands like "*".
582 */
583 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100584do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585{
586 return do_cmdline(cmd, NULL, NULL,
587 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
588}
589
590/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100591 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
592 * This allows for using a range without ":" in Vim9 script.
593 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200594 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100595do_cmd_argument(char_u *cmd)
596{
597 return do_cmdline(cmd, NULL, NULL,
598 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
599}
600
601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 * do_cmdline(): execute one Ex command line
603 *
604 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100605 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 * 2. Split up in parts separated with '|'.
607 *
608 * This function can be called recursively!
609 *
610 * flags:
611 * DOCMD_VERBOSE - The command will be included in the error message.
612 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100613 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 * DOCMD_KEYTYPED - Don't reset KeyTyped.
615 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
616 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
617 *
618 * return FAIL if cmdline could not be executed, OK otherwise
619 */
620 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100621do_cmdline(
622 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200623 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100624 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100625 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100627 char_u *next_cmdline; // next cmd to execute
628 char_u *cmdline_copy = NULL; // copy of cmd line
629 int used_getline = FALSE; // used "fgetline" to obtain command
630 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100632 int count = 0; // line number count
633 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 int retval = OK;
635#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100636 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100637 garray_T lines_ga; // keep lines for ":while"/":for"
638 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200639 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100640 char_u *fname = NULL; // function or script name
641 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
642 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
643 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200645 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200646 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100648 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200649 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000651 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000653 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100655# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656# define cmd_cookie cookie
657#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100658 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200659#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100660 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
661 // location for storing error messages to be converted to an exception.
662 // This ensures that the do_errthrow() call in do_one_cmd() does not
663 // combine the messages stored by an earlier invocation of do_one_cmd()
664 // with the command name of the later one. This would happen when
665 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 saved_msg_list = msg_list;
667 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668#endif
669
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100670 // It's possible to create an endless loop with ":execute", catch that
671 // here. The value of 200 allows nested function calls, ":source", etc.
672 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100673 if (call_depth >= 200
674#ifdef FEAT_EVAL
675 && call_depth >= p_mfd
676#endif
677 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000679 emsg(_(e_command_too_recursive));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100681 // When converting to an exception, we do not include the command name
682 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100683 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 msg_list = saved_msg_list;
685#endif
686 return FAIL;
687 }
688 ++call_depth;
689
690#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100691 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 cstack.cs_idx = -1;
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000693 ga_init2(&lines_ga, sizeof(wcmd_T), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100695 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100697 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100698 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000699 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 ++ex_nesting_level;
701
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100702 // Get the function or script name and the address where the next breakpoint
703 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000704 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {
706 fname = func_name(real_cookie);
707 breakpoint = func_breakpoint(real_cookie);
708 dbg_tick = func_dbg_tick(real_cookie);
709 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100710 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100712 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 breakpoint = source_breakpoint(real_cookie);
714 dbg_tick = source_dbg_tick(real_cookie);
715 }
716
717 /*
718 * Initialize "force_abort" and "suppress_errthrow" at the top level.
719 */
720 if (!recursive)
721 {
722 force_abort = FALSE;
723 suppress_errthrow = FALSE;
724 }
725
726 /*
727 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000728 * exception handling (used when debugging). Otherwise clear it to avoid
729 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000731 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000732 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000733 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200734 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735
736 initial_trylevel = trylevel;
737
738 /*
739 * "did_throw" will be set to TRUE when an exception is being thrown.
740 */
741 did_throw = FALSE;
742#endif
743 /*
744 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000745 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 * If force_abort is set, we cancel everything.
747 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100748#ifdef FEAT_EVAL
749 did_emsg_cumul += did_emsg;
750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 did_emsg = FALSE;
752
753 /*
754 * KeyTyped is only set when calling vgetc(). Reset it here when not
755 * calling vgetc() (sourced command lines).
756 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100757 if (!(flags & DOCMD_KEYTYPED)
758 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 KeyTyped = FALSE;
760
761 /*
762 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000763 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 * - for multiple commands on one line, separated with '|'
765 * - when repeating until there are no more lines (for ":source")
766 */
767 next_cmdline = cmdline;
768 do
769 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000770#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100771 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000772#endif
773
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100774 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 if (next_cmdline == NULL
776#ifdef FEAT_EVAL
777 && !force_abort
778 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000779 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780#endif
781 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100782 {
783#ifdef FEAT_EVAL
784 did_emsg_cumul += did_emsg;
785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
789 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000790 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100791 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 * 3. If a line is given: Make a copy, so we can mess with it.
793 */
794
795#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100796 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000797 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100799 // Each '|' separated command is stored separately in lines_ga, to
800 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100801 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100803 // Check if a function has returned or, unless it has an unclosed
804 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000805 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000808 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000809 func_line_end(real_cookie);
810# endif
811 if (func_has_ended(real_cookie))
812 {
813 retval = FAIL;
814 break;
815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000817#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000818 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100819 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000820 script_line_end();
821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100823 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100824 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 {
826 retval = FAIL;
827 break;
828 }
829
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100830 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 if (breakpoint != NULL && dbg_tick != NULL
832 && *dbg_tick != debug_tick)
833 {
834 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100835 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100836 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 *dbg_tick = debug_tick;
838 }
839
840 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100841 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100843 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100845 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100847 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100848 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100850 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100851 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 *dbg_tick = debug_tick;
853 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000854# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000855 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000856 {
857 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100858 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100859 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000860 script_line_start();
861 }
862# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864#endif
865
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100866 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 if (next_cmdline == NULL)
868 {
869 /*
870 * Need to set msg_didout for the first line after an ":if",
871 * otherwise the ":if" will be overwritten.
872 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100873 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100875 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876#ifdef FEAT_EVAL
877 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
878#else
879 0
880#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100881 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
882 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100884 // Don't call wait_return for aborted command line. The NULL
885 // returned for the end of a sourced file or executed function
886 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 if (KeyTyped && !(flags & DOCMD_REPEAT))
888 need_wait_return = FALSE;
889 retval = FAIL;
890 break;
891 }
892 used_getline = TRUE;
893
894 /*
895 * Keep the first typed line. Clear it when more lines are typed.
896 */
897 if (flags & DOCMD_KEEPLINE)
898 {
899 vim_free(repeat_cmdline);
900 if (count == 0)
901 repeat_cmdline = vim_strsave(next_cmdline);
902 else
903 repeat_cmdline = NULL;
904 }
905 }
906
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100907 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 else if (cmdline_copy == NULL)
909 {
910 next_cmdline = vim_strsave(next_cmdline);
911 if (next_cmdline == NULL)
912 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200913 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 retval = FAIL;
915 break;
916 }
917 }
918 cmdline_copy = next_cmdline;
919
920#ifdef FEAT_EVAL
921 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200922 * Inside a while/for loop, and when the command looks like a ":while"
923 * or ":for", the line is stored, because we may need it later when
924 * looping.
925 *
926 * When there is a '|' and another command, it is stored separately,
927 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000928 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200929 *
930 * Pass a different "fgetline" function to do_one_cmd() below,
931 * that it stores lines in or reads them from "lines_ga". Makes it
932 * possible to define a function inside a while/for loop and handles
933 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200935 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200937 cmd_getline = get_loop_line;
938 cmd_cookie = (void *)&cmd_loop_cookie;
939 cmd_loop_cookie.lines_gap = &lines_ga;
940 cmd_loop_cookie.current_line = current_line;
941 cmd_loop_cookie.getline = fgetline;
942 cmd_loop_cookie.cookie = cookie;
943 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
944
945 // Save the current line when encountering it the first time.
946 if (current_line == lines_ga.ga_len
947 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 {
949 retval = FAIL;
950 break;
951 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200952 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200954 else
955 {
956 cmd_getline = fgetline;
957 cmd_cookie = cookie;
958 }
959
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 did_endif = FALSE;
961#endif
962
963 if (count++ == 0)
964 {
965 /*
966 * All output from the commands is put below each other, without
967 * waiting for a return. Don't do this when executing commands
968 * from a script or when being called recursive (e.g. for ":e
969 * +command file").
970 */
971 if (!(flags & DOCMD_NOWAIT) && !recursive)
972 {
973 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100974 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100976 msg_scroll = TRUE; // put messages below each other
977 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 ++RedrawingDisabled;
979 did_inc = TRUE;
980 }
981 }
982
Bram Moolenaar823654b2020-05-29 23:03:09 +0200983 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100984 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985
986 /*
987 * 2. Execute one '|' separated command.
988 * do_one_cmd() will return NULL if there is no trailing '|'.
989 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
990 */
991 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100992 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#ifdef FEAT_EVAL
994 &cstack,
995#endif
996 cmd_getline, cmd_cookie);
997 --recursive;
998
999#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001000 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001001 // Use "current_line" from "cmd_loop_cookie", it may have been
1002 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001003 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004#endif
1005
1006 if (next_cmdline == NULL)
1007 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001008 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 /*
1011 * If the command was typed, remember it for the ':' register.
1012 * Do this AFTER executing the command to make :@: work.
1013 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001014 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 && new_last_cmdline != NULL)
1016 {
1017 vim_free(last_cmdline);
1018 last_cmdline = new_last_cmdline;
1019 new_last_cmdline = NULL;
1020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 }
1022 else
1023 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001024 // need to copy the command after the '|' to cmdline_copy, for the
1025 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001026 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 next_cmdline = cmdline_copy;
1028 }
1029
1030
1031#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001032 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001034 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001036 {
1037 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001041 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 {
1043 ++current_line;
1044
1045 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001046 * An ":endwhile", ":endfor" and ":continue" is handled here.
1047 * If we were executing commands, jump back to the ":while" or
1048 * ":for".
1049 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001051 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001053 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001055 // Jump back to the matching ":while" or ":for". Be careful
1056 // not to use a cs_line[] from an entry that isn't a ":while"
1057 // or ":for": It would make "current_line" invalid and can
1058 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 if (!did_emsg && !got_int && !did_throw
1060 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001061 && (cstack.cs_flags[cstack.cs_idx]
1062 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 && cstack.cs_line[cstack.cs_idx] >= 0
1064 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1065 {
1066 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001067 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001069 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001071 // Check for the next breakpoint at or after the ":while"
1072 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 if (breakpoint != NULL)
1074 {
1075 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001076 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 fname,
1078 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1079 *dbg_tick = debug_tick;
1080 }
1081 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001084 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001086 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1087 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 }
1089 }
1090
1091 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001092 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001096 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001097 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 }
1099 }
1100
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001101 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001102 if (breakpoint != NULL && has_watchexpr())
1103 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001104 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001105 *dbg_tick = debug_tick;
1106 }
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 /*
1109 * When not inside any ":while" loop, clear remembered lines.
1110 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001111 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 {
1113 if (lines_ga.ga_len > 0)
1114 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001115 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1117 free_cmdlines(&lines_ga);
1118 }
1119 current_line = 0;
1120 }
1121
1122 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001123 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1124 * being restored at the ":endtry". Reset them here and set the
1125 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1126 * This includes the case where a missing ":endif", ":endwhile" or
1127 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001129 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 cstack.cs_lflags &= ~CSL_HAD_FINA;
1132 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1133 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 did_throw ? (void *)current_exception : NULL);
1135 did_emsg = got_int = did_throw = FALSE;
1136 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1137 }
1138
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001139 // Update global "trylevel" for recursive calls to do_cmdline() from
1140 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 trylevel = initial_trylevel + cstack.cs_trylevel;
1142
1143 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001144 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 * files) is aborted because of an error, an interrupt, or an uncaught
1146 * exception, cancel everything. If it is left normally, reset
1147 * force_abort to get the non-EH compatible abortion behavior for
1148 * the rest of the script.
1149 */
1150 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1151 force_abort = FALSE;
1152
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001153 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001155#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156
1157 }
1158 /*
1159 * Continue executing command lines when:
1160 * - no CTRL-C typed, no aborting error, no exception thrown or try
1161 * conditionals need to be checked for executing finally clauses or
1162 * catching an interrupt exception
1163 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001164 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 * looping for ":source" command or function call.
1166 */
1167 while (!((got_int
1168#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001169 || (did_emsg && (force_abort || in_vim9script()))
1170 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171#endif
1172 )
1173#ifdef FEAT_EVAL
1174 && cstack.cs_trylevel == 0
1175#endif
1176 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001177 && !(did_emsg
1178#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001179 // Keep going when inside try/catch, so that the error can be
1180 // deal with, except when it is a syntax error, it may cause
1181 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001182 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1183#endif
1184 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001185 && (getline_equal(fgetline, cookie, getexmodeline)
1186 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 && (next_cmdline != NULL
1188#ifdef FEAT_EVAL
1189 || cstack.cs_idx >= 0
1190#endif
1191 || (flags & DOCMD_REPEAT)));
1192
1193 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001194 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195#ifdef FEAT_EVAL
1196 free_cmdlines(&lines_ga);
1197 ga_clear(&lines_ga);
1198
1199 if (cstack.cs_idx >= 0)
1200 {
1201 /*
1202 * If a sourced file or executed function ran to its end, report the
1203 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001204 * In Vim9 script do not give a second error, executing aborts after
1205 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 */
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001207 if (!got_int && !did_throw && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001208 && ((getline_equal(fgetline, cookie, getsourceline)
1209 && !source_finished(fgetline, cookie))
1210 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 && !func_has_ended(real_cookie))))
1212 {
1213 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001214 emsg(_(e_missing_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001216 emsg(_(e_missing_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001217 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001218 emsg(_(e_missing_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001220 emsg(_(e_missing_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 }
1222
1223 /*
1224 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1225 * ":endtry" in a sourced file or executed function. If the try
1226 * conditional is in its finally clause, ignore anything pending.
1227 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001228 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 */
1230 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001231 {
1232 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1233
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001234 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001235 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001236 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1237 &cstack.cs_looplevel);
1238 }
1239 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 trylevel = initial_trylevel;
1241 }
1242
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001243 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1244 // lack was reported above and the error message is to be converted to an
1245 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001246 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 ? (char_u *)"endfunction" : (char_u *)NULL);
1248
1249 if (trylevel == 0)
1250 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001251 // Just in case did_throw got set but current_exception wasn't.
1252 if (current_exception == NULL)
1253 did_throw = FALSE;
1254
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 /*
1256 * When an exception is being thrown out of the outermost try
1257 * conditional, discard the uncaught exception, disable the conversion
1258 * of interrupts or errors to exceptions, and ensure that no more
1259 * commands are executed.
1260 */
1261 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001262 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263
1264 /*
1265 * On an interrupt or an aborting error not converted to an exception,
1266 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001267 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 * when force_abort is set and did_emsg unset in case of an interrupt
1269 * from a finally clause after an error.
1270 */
1271 else if (got_int || (did_emsg && force_abort))
1272 suppress_errthrow = TRUE;
1273 }
1274
1275 /*
1276 * The current cstack will be freed when do_cmdline() returns. An uncaught
1277 * exception will have to be rethrown in the previous cstack. If a function
1278 * has just returned or a script file was just finished and the previous
1279 * cstack belongs to the same function or, respectively, script file, it
1280 * will have to be checked for finally clauses to be executed due to the
1281 * ":return" or ":finish". This is done in do_one_cmd().
1282 */
1283 if (did_throw)
1284 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001285 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001287 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 && ex_nesting_level > func_level(real_cookie) + 1))
1289 {
1290 if (!did_throw)
1291 check_cstack = TRUE;
1292 }
1293 else
1294 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001295 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001296 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 --ex_nesting_level;
1298 /*
1299 * Go to debug mode when returning from a function in which we are
1300 * single-stepping.
1301 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001302 if ((getline_equal(fgetline, cookie, getsourceline)
1303 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001305 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 ? (char_u *)_("End of sourced file")
1307 : (char_u *)_("End of function"));
1308 }
1309
1310 /*
1311 * Restore the exception environment (done after returning from the
1312 * debugger).
1313 */
1314 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001315 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316
1317 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001318
1319 // Cleanup if "cs_emsg_silent_list" remains.
1320 if (cstack.cs_emsg_silent_list != NULL)
1321 {
1322 eslist_T *elem, *temp;
1323
1324 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1325 {
1326 temp = elem->next;
1327 vim_free(elem);
1328 }
1329 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001330#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331
1332 /*
1333 * If there was too much output to fit on the command line, ask the user to
1334 * hit return before redrawing the screen. With the ":global" command we do
1335 * this only once after the command is finished.
1336 */
1337 if (did_inc)
1338 {
1339 --RedrawingDisabled;
1340 --no_wait_return;
1341 msg_scroll = FALSE;
1342
1343 /*
1344 * When just finished an ":if"-":else" which was typed, no need to
1345 * wait for hit-return. Also for an error situation.
1346 */
1347 if (retval == FAIL
1348#ifdef FEAT_EVAL
1349 || (did_endif && KeyTyped && !did_emsg)
1350#endif
1351 )
1352 {
1353 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001354 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356 else if (need_wait_return)
1357 {
1358 /*
1359 * The msg_start() above clears msg_didout. The wait_return we do
1360 * here should not overwrite the command that may be shown before
1361 * doing that.
1362 */
1363 msg_didout |= msg_didout_before_start;
1364 wait_return(FALSE);
1365 }
1366 }
1367
Bram Moolenaar2a942252012-11-28 23:03:07 +01001368#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001369 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001370#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 /*
1372 * Reset if_level, in case a sourced script file contains more ":if" than
1373 * ":endif" (could be ":if x | foo | endif").
1374 */
1375 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001376#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001377
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 --call_depth;
1379 return retval;
1380}
1381
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001382#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001383/*
1384 * Handle when "did_throw" is set after executing commands.
1385 */
1386 void
1387handle_did_throw()
1388{
1389 char *p = NULL;
1390 msglist_T *messages = NULL;
Bram Moolenaar9e0ee592021-07-31 22:17:28 +02001391 ESTACK_CHECK_DECLARATION
Bram Moolenaar620c9592021-07-31 21:32:31 +02001392
1393 /*
1394 * If the uncaught exception is a user exception, report it as an
1395 * error. If it is an error exception, display the saved error
1396 * message now. For an interrupt exception, do nothing; the
1397 * interrupt message is given elsewhere.
1398 */
1399 switch (current_exception->type)
1400 {
1401 case ET_USER:
1402 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001403 _(e_exception_not_caught_str),
Bram Moolenaar620c9592021-07-31 21:32:31 +02001404 current_exception->value);
1405 p = (char *)vim_strsave(IObuff);
1406 break;
1407 case ET_ERROR:
1408 messages = current_exception->messages;
1409 current_exception->messages = NULL;
1410 break;
1411 case ET_INTERRUPT:
1412 break;
1413 }
1414
1415 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1416 current_exception->throw_lnum);
1417 ESTACK_CHECK_SETUP
1418 current_exception->throw_name = NULL;
1419
1420 discard_current_exception(); // uses IObuff if 'verbose'
1421 suppress_errthrow = TRUE;
1422 force_abort = TRUE;
1423
1424 if (messages != NULL)
1425 {
1426 do
1427 {
1428 msglist_T *next = messages->next;
1429 int save_compiling = estack_compiling;
1430
1431 estack_compiling = messages->msg_compiling;
1432 emsg(messages->msg);
1433 vim_free(messages->msg);
1434 vim_free(messages->sfile);
1435 vim_free(messages);
1436 messages = next;
1437 estack_compiling = save_compiling;
1438 }
1439 while (messages != NULL);
1440 }
1441 else if (p != NULL)
1442 {
1443 emsg(p);
1444 vim_free(p);
1445 }
1446 vim_free(SOURCING_NAME);
1447 ESTACK_CHECK_NOW
1448 estack_pop();
1449}
1450
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001452 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 */
1454 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001455get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001457 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 wcmd_T *wp;
1459 char_u *line;
1460
1461 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1462 {
1463 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001464 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001466 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 if (cp->getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001468 line = getcmdline(c, 0L, indent, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001470 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001471 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 ++cp->current_line;
1473
1474 return line;
1475 }
1476
1477 KeyTyped = FALSE;
1478 ++cp->current_line;
1479 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001480 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 return vim_strsave(wp->line);
1482}
1483
1484/*
1485 * Store a line in "gap" so that a ":while" loop can execute it again.
1486 */
1487 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001488store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489{
1490 if (ga_grow(gap, 1) == FAIL)
1491 return FAIL;
1492 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001493 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 return OK;
1496}
1497
1498/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001499 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 */
1501 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001502free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
1504 while (gap->ga_len > 0)
1505 {
1506 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1507 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 }
1509}
1510#endif
1511
1512/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001513 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1514 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001517getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001518 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001519 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001520 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521{
1522#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001523 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001524 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001526 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1527 // function that's originally used to obtain the lines. This may be
1528 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001529 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001530 cp = (struct loop_cookie *)cookie;
1531 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 {
1533 gp = cp->getline;
1534 cp = cp->cookie;
1535 }
1536 return gp == func;
1537#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001538 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539#endif
1540}
1541
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001543 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 * getline function. Otherwise return "cookie".
1545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001547getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001548 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001549 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550{
Bram Moolenaar13505972019-01-24 15:04:48 +01001551#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001552 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001553 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001555 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1556 // cookie that's originally used to obtain the lines. This may be nested
1557 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001558 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001559 cp = (struct loop_cookie *)cookie;
1560 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 {
1562 gp = cp->getline;
1563 cp = cp->cookie;
1564 }
1565 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001566#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001569}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570
Bram Moolenaard5053d02020-06-28 15:51:16 +02001571#if defined(FEAT_EVAL) || defined(PROT)
1572/*
1573 * Get the next line source line without advancing.
1574 */
1575 char_u *
1576getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001577 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001578 void *cookie) // argument for fgetline()
1579{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001580 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001581 struct loop_cookie *cp;
1582 wcmd_T *wp;
1583
1584 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1585 // cookie that's originally used to obtain the lines. This may be nested
1586 // several levels.
1587 gp = fgetline;
1588 cp = (struct loop_cookie *)cookie;
1589 while (gp == get_loop_line)
1590 {
1591 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1592 {
1593 // executing lines a second time, use the stored copy
1594 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1595 return wp->line;
1596 }
1597 gp = cp->getline;
1598 cp = cp->cookie;
1599 }
1600 if (gp == getsourceline)
1601 return source_nextline(cp);
1602 return NULL;
1603}
1604#endif
1605
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001606
1607/*
1608 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1609 * ":bwipeout", etc.
1610 * Returns the buffer number.
1611 */
1612 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001613compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001614{
1615 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001616 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001617 int count = offset;
1618
1619 buf = firstbuf;
1620 while (buf->b_next != NULL && buf->b_fnum < lnum)
1621 buf = buf->b_next;
1622 while (count != 0)
1623 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001624 count += (offset < 0) ? 1 : -1;
1625 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1626 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001627 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001628 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001629 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001630 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001631 while (buf->b_ml.ml_mfp == NULL)
1632 {
1633 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1634 if (nextbuf == NULL)
1635 break;
1636 buf = nextbuf;
1637 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001638 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001639 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001640 if (addr_type == ADDR_LOADED_BUFFERS)
1641 while (buf->b_ml.ml_mfp == NULL)
1642 {
1643 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1644 if (nextbuf == NULL)
1645 break;
1646 buf = nextbuf;
1647 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001648 return buf->b_fnum;
1649}
1650
Bram Moolenaarb7316892019-05-01 18:08:42 +02001651/*
1652 * Return the window number of "win".
1653 * When "win" is NULL return the number of windows.
1654 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001655 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001656current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001657{
1658 win_T *wp;
1659 int nr = 0;
1660
Bram Moolenaar29323592016-07-24 22:04:11 +02001661 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001662 {
1663 ++nr;
1664 if (wp == win)
1665 break;
1666 }
1667 return nr;
1668}
1669
1670 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001671current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001672{
1673 tabpage_T *tp;
1674 int nr = 0;
1675
Bram Moolenaar29323592016-07-24 22:04:11 +02001676 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001677 {
1678 ++nr;
1679 if (tp == tab)
1680 break;
1681 }
1682 return nr;
1683}
1684
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001685 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001686comment_start(char_u *p, int starts_with_colon UNUSED)
1687{
1688#ifdef FEAT_EVAL
1689 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001690 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001691#endif
1692 return *p == '"';
1693}
1694
Bram Moolenaarf240e182014-11-27 18:33:02 +01001695# define CURRENT_WIN_NR current_win_nr(curwin)
1696# define LAST_WIN_NR current_win_nr(NULL)
1697# define CURRENT_TAB_NR current_tab_nr(curtab)
1698# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001699
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700/*
1701 * Execute one Ex command.
1702 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001703 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1704 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 *
1706 * 1. skip comment lines and leading space
1707 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001708 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001709 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001710 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001711 * 6. parse arguments
1712 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001714 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 *
1716 * This function may be called recursively!
1717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001719do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001720 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001721 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001723 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001725 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001726 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001728 char_u *p;
1729 linenr_T lnum;
1730 long n;
1731 char *errormsg = NULL; // error message
1732 char_u *after_modifier = NULL;
1733 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001734 cmdmod_T save_cmdmod;
1735 int save_reg_executing = reg_executing;
1736 int ni; // set when Not Implemented
1737 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001738 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001739#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001740 int may_have_range;
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001741 int vim9script;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001742 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001743#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001744 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745
Bram Moolenaara80faa82020-04-12 19:37:17 +02001746 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 ea.line1 = 1;
1748 ea.line2 = 1;
1749#ifdef FEAT_EVAL
1750 ++ex_nesting_level;
1751#endif
1752
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001753 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 if (quitmore
1755#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001756 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001757 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001758#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001759 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001760 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 --quitmore;
1762
1763 /*
1764 * Reset browse, confirm, etc.. They are restored when returning, for
1765 * recursive calls.
1766 */
1767 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001769 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001770 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1771 goto doend;
1772
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001773/*
1774 * 1. Skip comment lines and leading white space and colons.
1775 * 2. Handle command modifiers.
1776 */
1777 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001779 ea.cmdlinep = cmdlinep;
1780 ea.getline = fgetline;
1781 ea.cookie = cookie;
1782#ifdef FEAT_EVAL
1783 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001784 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001786 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001787 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001788 apply_cmdmod(&cmdmod);
Bram Moolenaar2379f872021-02-14 14:07:34 +01001789#ifdef FEAT_EVAL
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001790 vim9script = in_vim9script();
Bram Moolenaar2379f872021-02-14 14:07:34 +01001791#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001792 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793
1794#ifdef FEAT_EVAL
1795 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1796 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1797#else
1798 ea.skip = (if_level > 0);
1799#endif
1800
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001802 * 3. Skip over the range to find the command. Let "p" point to after it.
1803 *
1804 * We need the command to know what kind of range it uses.
1805 */
1806 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001807#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001808 // In Vim9 script a colon is required before the range. This may also be
1809 // after command modifiers.
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001810 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001811 {
1812 may_have_range = FALSE;
1813 for (p = ea.cmd; p >= *cmdlinep; --p)
1814 {
1815 if (*p == ':')
1816 may_have_range = TRUE;
1817 if (p < ea.cmd && !VIM_ISWHITE(*p))
1818 break;
1819 }
1820 }
1821 else
1822 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001823 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001824#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001825 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826
1827#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001828 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001829 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001830 if (ea.cmd == cmd + 1 && *cmd == '$')
1831 // should be "$VAR = val"
1832 --ea.cmd;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001833 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001834 if (ea.cmdidx == CMD_SIZE)
1835 {
1836 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1837
1838 // If a ':' before the range is missing, give a clearer error
1839 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001840 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001841 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001842 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001843 goto doend;
1844 }
1845 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001846 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847 else
1848#endif
1849 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001850
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001851#ifdef FEAT_EVAL
1852# ifdef FEAT_PROFILE
1853 // Count this line for profiling if skip is TRUE.
1854 if (do_profiling == PROF_YES
1855 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1856 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1857 {
1858 int skip = did_emsg || got_int || did_throw;
1859
1860 if (ea.cmdidx == CMD_catch)
1861 skip = !skip && !(cstack->cs_idx >= 0
1862 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1863 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1864 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1865 skip = skip || !(cstack->cs_idx >= 0
1866 && !(cstack->cs_flags[cstack->cs_idx]
1867 & (CSF_ACTIVE | CSF_TRUE)));
1868 else if (ea.cmdidx == CMD_finally)
1869 skip = FALSE;
1870 else if (ea.cmdidx != CMD_endif
1871 && ea.cmdidx != CMD_endfor
1872 && ea.cmdidx != CMD_endtry
1873 && ea.cmdidx != CMD_endwhile)
1874 skip = ea.skip;
1875
1876 if (!skip)
1877 {
1878 if (getline_equal(fgetline, cookie, get_func_line))
1879 func_line_exec(getline_cookie(fgetline, cookie));
1880 else if (getline_equal(fgetline, cookie, getsourceline))
1881 script_line_exec();
1882 }
1883 }
1884# endif
1885
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001886 // May go to debug mode. If this happens and the ">quit" debug command is
1887 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001888 dbg_check_breakpoint(&ea);
1889 if (!ea.skip && got_int)
1890 {
1891 ea.skip = TRUE;
1892 (void)do_intthrow(cstack);
1893 }
1894#endif
1895
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001896/*
1897 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 *
1899 * where 'addr' is:
1900 *
1901 * % (entire file)
1902 * $ [+-NUM]
1903 * 'x [+-NUM] (where x denotes a currently defined mark)
1904 * . [+-NUM]
1905 * [+-NUM]..
1906 * NUM
1907 *
1908 * The ea.cmd pointer is updated to point to the first character following the
1909 * range spec. If an initial address is found, but no second, the upper bound
1910 * is equal to the lower.
1911 */
1912
Bram Moolenaar25190db2019-05-04 15:05:28 +02001913 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001914 if (!IS_USER_CMDIDX(ea.cmdidx))
1915 {
1916 if (ea.cmdidx != CMD_SIZE)
1917 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1918 else
1919 ea.addr_type = ADDR_LINES;
1920
Bram Moolenaar25190db2019-05-04 15:05:28 +02001921 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001922 if (ea.cmdidx == CMD_wincmd && p != NULL)
1923 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001924#ifdef FEAT_QUICKFIX
1925 // :.cc in quickfix window uses line number
1926 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1927 ea.addr_type = ADDR_OTHER;
1928#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001929 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001930
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001931 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001932#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001933 if (!may_have_range)
1934 ea.line1 = ea.line2 = default_address(&ea);
1935 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001936#endif
1937 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1938 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001941 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 */
1943
1944 /*
1945 * Skip ':' and any white space
1946 */
1947 ea.cmd = skipwhite(ea.cmd);
1948 while (*ea.cmd == ':')
1949 ea.cmd = skipwhite(ea.cmd + 1);
1950
1951 /*
1952 * If we got a line, but no command, then go to the line.
1953 * If we find a '|' or '\n' we set ea.nextcmd.
1954 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001955 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1956 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 {
1958 /*
1959 * strange vi behaviour:
1960 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001961 * ":3|..." prints line 3 (not in Vim9 script)
1962 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001964 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001966 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 goto doend;
1968 }
1969
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001970 // If this looks like an undefined user command and there are CmdUndefined
1971 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001972 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1973 && ASCII_ISUPPER(*ea.cmd)
1974 && has_cmdundefined())
1975 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001976 int ret;
1977
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001978 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001979 while (ASCII_ISALNUM(*p))
1980 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001981 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001982 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1983 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001984 // If the autocommands did something and didn't cause an error, try
1985 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001986 p = (ret
1987#ifdef FEAT_EVAL
1988 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001989#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001991 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001992
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 if (p == NULL)
1994 {
1995 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01001996 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 goto doend;
1998 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001999 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002000 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2001 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 {
2003 errormsg = uc_fun_cmd();
2004 goto doend;
2005 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002006
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 if (ea.cmdidx == CMD_SIZE)
2008 {
2009 if (!ea.skip)
2010 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002011 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002013 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002014 // If the modifier was parsed OK the error must be in the
2015 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002016 if (after_modifier != NULL)
2017 append_command(after_modifier);
2018 else
2019 append_command(*cmdlinep);
2020 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002021 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002022 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 }
2024 goto doend;
2025 }
2026
Bram Moolenaar958636c2014-10-21 20:01:58 +02002027 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2028 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002029#ifdef HAVE_EX_SCRIPT_NI
2030 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2031#endif
2032 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033
2034#ifndef FEAT_EVAL
2035 /*
2036 * When the expression evaluation is disabled, recognize the ":if" and
2037 * ":endif" commands and ignore everything in between it.
2038 */
2039 if (ea.cmdidx == CMD_if)
2040 ++if_level;
2041 if (if_level)
2042 {
2043 if (ea.cmdidx == CMD_endif)
2044 --if_level;
2045 goto doend;
2046 }
2047
2048#endif
2049
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002050 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002051 if (*p == '!' && ea.cmdidx != CMD_substitute
2052 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 {
2054 ++p;
2055 ea.forceit = TRUE;
2056 }
2057 else
2058 ea.forceit = FALSE;
2059
2060/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002061 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002063 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002064 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
2066 if (!ea.skip)
2067 {
2068#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002069 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002071 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002072 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 goto doend;
2074 }
2075#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002076 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002077 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002078 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002079 goto doend;
2080 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002081 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002083 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002084 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 goto doend;
2086 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002087
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002088 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002090#ifdef FEAT_CMDWIN
2091 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2092 {
2093 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002094 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002095 goto doend;
2096 }
2097#endif
2098 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2099 {
2100 // Command not allowed when text is locked
2101 errormsg = _(get_text_locked_msg());
2102 goto doend;
2103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002105
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002106 // Disallow editing another buffer when "curbuf_lock" is set.
2107 // Do allow ":checktime" (it is postponed).
2108 // Do allow ":edit" (check for an argument later).
2109 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002110 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002111 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002112 && ea.cmdidx != CMD_edit
2113 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002114 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002115 && curbuf_locked())
2116 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002118 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002120 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 goto doend;
2122 }
2123 }
2124
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002125 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002127 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 goto doend;
2129 }
2130
2131 /*
2132 * Don't complain about the range if it is not used
2133 * (could happen if line_count is accidentally set to 0).
2134 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002135 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 {
2137 /*
2138 * If the range is backwards, ask for confirmation and, if given, swap
2139 * ea.line1 & ea.line2 so it's forwards again.
2140 * When global command is busy, don't ask, will fail below.
2141 */
2142 if (!global_busy && ea.line1 > ea.line2)
2143 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002144 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002146 if (sourcing || exmode_active)
2147 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002148 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002149 goto doend;
2150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 if (ask_yesno((char_u *)
2152 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002153 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 }
2155 lnum = ea.line1;
2156 ea.line1 = ea.line2;
2157 ea.line2 = lnum;
2158 }
2159 if ((errormsg = invalid_range(&ea)) != NULL)
2160 goto doend;
2161 }
2162
Bram Moolenaarb7316892019-05-01 18:08:42 +02002163 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2164 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 ea.line2 = 1;
2166
2167 correct_range(&ea);
2168
2169#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002170 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002171 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002173 // Put the first line at the start of a closed fold, put the last line
2174 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 (void)hasFolding(ea.line1, &ea.line1, NULL);
2176 (void)hasFolding(ea.line2, NULL, &ea.line2);
2177 }
2178#endif
2179
2180#ifdef FEAT_QUICKFIX
2181 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002182 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 * option here, so things like % get expanded.
2184 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002185 p = replace_makeprg(&ea, p, cmdlinep);
2186 if (p == NULL)
2187 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188#endif
2189
2190 /*
2191 * Skip to start of argument.
2192 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2193 */
2194 if (ea.cmdidx == CMD_bang)
2195 ea.arg = p;
2196 else
2197 ea.arg = skipwhite(p);
2198
Bram Moolenaar379fb762018-08-30 15:58:28 +02002199 // ":file" cannot be run with an argument when "curbuf_lock" is set
2200 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2201 goto doend;
2202
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 /*
2204 * Check for "++opt=val" argument.
2205 * Must be first, allow ":w ++enc=utf8 !cmd"
2206 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002207 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2209 if (getargopt(&ea) == FAIL && !ni)
2210 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002211 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 goto doend;
2213 }
2214
2215 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2216 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002217 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002219 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002221 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 goto doend;
2223 }
2224 ea.arg = skipwhite(ea.arg + 1);
2225 ea.append = TRUE;
2226 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002227 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 {
2229 ++ea.arg;
2230 ea.usefilter = TRUE;
2231 }
2232 }
2233
2234 if (ea.cmdidx == CMD_read)
2235 {
2236 if (ea.forceit)
2237 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002238 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 ea.forceit = FALSE;
2240 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002241 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 {
2243 ++ea.arg;
2244 ea.usefilter = TRUE;
2245 }
2246 }
2247
2248 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2249 {
2250 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002251 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 {
2253 ++ea.arg;
2254 ++ea.amount;
2255 }
2256 ea.arg = skipwhite(ea.arg);
2257 }
2258
2259 /*
2260 * Check for "+command" argument, before checking for next command.
2261 * Don't do this for ":read !cmd" and ":write !cmd".
2262 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002263 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2265
2266 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002267 * For commands that do not use '|' inside their argument: Check for '|' to
2268 * separate commands and '"' or '#' to start comments.
2269 *
2270 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002271 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002272 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002273 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002275 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2276 {
Bram Moolenaarac485062022-03-23 19:45:01 +00002277 separate_nextcmd(&ea, FALSE);
Bram Moolenaar63b91732021-08-05 20:40:03 +02002278 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002279 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002280 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002281 || ea.cmdidx == CMD_global
2282 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002283 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002284#ifdef FEAT_EVAL
2285 || inside_block(&ea)
2286#endif
2287 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
2289 for (p = ea.arg; *p; ++p)
2290 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002291 // Remove one backslash before a newline, so that it's possible to
2292 // pass a newline to the shell and also a newline that is preceded
2293 // with a backslash. This makes it impossible to end a shell
2294 // command in a backslash, but that doesn't appear useful.
2295 // Halving the number of backslashes is incompatible with previous
2296 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002298 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002299 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 {
2301 ea.nextcmd = p + 1;
2302 *p = NUL;
2303 break;
2304 }
2305 }
2306 }
2307
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002308 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002309 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002311 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002312 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002314 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002315 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002316 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002318#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002319 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002320 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002322 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002323 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 }
2325#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002326 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2327 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002328 {
2329 ea.regname = *ea.arg++;
2330#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002331 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002332 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2333 {
kuuote08d7b1c2021-10-04 22:17:36 +01002334 if (!ea.skip)
2335 {
2336 set_expr_line(vim_strsave(ea.arg), &ea);
2337 did_set_expr_line = TRUE;
2338 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002339 ea.arg += STRLEN(ea.arg);
2340 }
2341#endif
2342 ea.arg = skipwhite(ea.arg);
2343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 }
2345
2346 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002347 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 * count, it's a buffer name.
2349 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002350 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002351 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002352 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002354 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002356 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002358 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 goto doend;
2360 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002361 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 {
2363 ea.line2 = n;
2364 if (ea.addr_count == 0)
2365 ea.addr_count = 1;
2366 }
2367 else
2368 {
2369 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002370 if (ea.line2 >= LONG_MAX - (n - 1))
2371 ea.line2 = LONG_MAX; // avoid overflow
2372 else
2373 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 ++ea.addr_count;
2375 /*
2376 * Be vi compatible: no error message for out of range.
2377 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002378 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 ea.line2 = curbuf->b_ml.ml_line_count;
2380 }
2381 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002382
2383 /*
2384 * Check for flags: 'l', 'p' and '#'.
2385 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002386 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002387 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002388 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2389 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002391 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002392 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 goto doend;
2394 }
2395
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002396 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002398 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 goto doend;
2400 }
2401
2402#ifdef FEAT_EVAL
2403 /*
2404 * Skip the command when it's not going to be executed.
2405 * The commands like :if, :endif, etc. always need to be executed.
2406 * Also make an exception for commands that handle a trailing command
2407 * themselves.
2408 */
2409 if (ea.skip)
2410 {
2411 switch (ea.cmdidx)
2412 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002413 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 case CMD_while:
2415 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002416 case CMD_for:
2417 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 case CMD_if:
2419 case CMD_elseif:
2420 case CMD_else:
2421 case CMD_endif:
2422 case CMD_try:
2423 case CMD_catch:
2424 case CMD_finally:
2425 case CMD_endtry:
2426 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002427 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 break;
2429
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002430 // Commands that handle '|' themselves. Check: A command should
2431 // either have the EX_TRLBAR flag, appear in this list or appear in
2432 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 case CMD_aboveleft:
2434 case CMD_and:
2435 case CMD_belowright:
2436 case CMD_botright:
2437 case CMD_browse:
2438 case CMD_call:
2439 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002440 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 case CMD_delfunction:
2442 case CMD_djump:
2443 case CMD_dlist:
2444 case CMD_dsearch:
2445 case CMD_dsplit:
2446 case CMD_echo:
2447 case CMD_echoerr:
2448 case CMD_echomsg:
2449 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002450 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002452 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002453 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 case CMD_help:
2455 case CMD_hide:
2456 case CMD_ijump:
2457 case CMD_ilist:
2458 case CMD_isearch:
2459 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002460 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 case CMD_keepjumps:
2462 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002463 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 case CMD_leftabove:
2465 case CMD_let:
2466 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002467 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002468 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002470 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002471 case CMD_noautocmd:
2472 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 case CMD_perl:
2474 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002475 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002476 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002477 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 case CMD_return:
2479 case CMD_rightbelow:
2480 case CMD_ruby:
2481 case CMD_silent:
2482 case CMD_smagic:
2483 case CMD_snomagic:
2484 case CMD_substitute:
2485 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002486 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 case CMD_tcl:
2488 case CMD_throw:
2489 case CMD_tilde:
2490 case CMD_topleft:
2491 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002492 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002493 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 case CMD_verbose:
2495 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002496 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 break;
2498
2499 default: goto doend;
2500 }
2501 }
2502#endif
2503
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002504 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 {
2506 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2507 goto doend;
2508 }
2509
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 /*
2511 * Accept buffer name. Cannot be used at the same time with a buffer
2512 * number. Don't do this for a user command.
2513 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002514 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002515 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 {
2517 /*
2518 * :bdelete, :bwipeout and :bunload take several arguments, separated
2519 * by spaces: find next space (skipping over escaped characters).
2520 * The others take one argument: ignore trailing spaces.
2521 */
2522 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2523 || ea.cmdidx == CMD_bunload)
2524 p = skiptowhite_esc(ea.arg);
2525 else
2526 {
2527 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002528 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 --p;
2530 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002531 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002532 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002533 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 goto doend;
2535 ea.addr_count = 1;
2536 ea.arg = skipwhite(p);
2537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002539 // The :try command saves the emsg_silent flag, reset it here when
2540 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002541 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002542 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002543 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002544 if (emsg_silent < 0)
2545 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002546 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002547 }
2548
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002550 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552
Bram Moolenaar958636c2014-10-21 20:01:58 +02002553 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
2555 /*
2556 * Execute a user-defined command.
2557 */
2558 do_ucmd(&ea);
2559 }
2560 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 {
2562 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002563 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 */
2565 ea.errmsg = NULL;
2566 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2567 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002568 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 }
2570
2571#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002573 // Not if this was a command that wasn't executed or :endif.
Yegappan Lakshmanan85b43c62022-03-21 19:45:17 +00002574 if (sourcing_a_script(&ea)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002575 && current_sctx.sc_sid > 0
2576 && ea.cmdidx != CMD_endif
2577 && (cstack->cs_idx < 0
2578 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002579 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 /*
2582 * If the command just executed called do_cmdline(), any throw or ":return"
2583 * or ":finish" encountered there must also check the cstack of the still
2584 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2585 * exception, or reanimate a returned function or finished script file and
2586 * return or finish it again.
2587 */
2588 if (need_rethrow)
2589 do_throw(cstack);
2590 else if (check_cstack)
2591 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002592 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002594 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 && current_func_returned())
2596 do_return(&ea, TRUE, FALSE, NULL);
2597 }
2598 need_rethrow = check_cstack = FALSE;
2599#endif
2600
2601doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002602 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002603 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002605 curwin->w_cursor.col = 0;
2606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607
2608 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2609 {
Bram Moolenaarbed34f02022-01-19 20:48:37 +00002610 if (sourcing || !KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002612 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 {
2614 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002615 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002617 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 }
2619 emsg(errormsg);
2620 }
2621#ifdef FEAT_EVAL
2622 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002623 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2624 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002625
2626 if (did_set_expr_line)
2627 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628#endif
2629
Bram Moolenaare1004402020-10-24 20:49:43 +02002630 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002632 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002634 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 ea.nextcmd = NULL;
2636
2637#ifdef FEAT_EVAL
2638 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002639 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640#endif
2641
2642 return ea.nextcmd;
2643}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002645static char ex_error_buf[MSG_BUF_LEN];
2646
2647/*
2648 * Return an error message with argument included.
2649 * Uses a static buffer, only the last error will be kept.
2650 * "msg" will be translated, caller should use N_().
2651 */
2652 char *
2653ex_errmsg(char *msg, char_u *arg)
2654{
2655 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2656 return ex_error_buf;
2657}
2658
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002660 * Handle a range without a command.
2661 * Returns an error message on failure.
2662 */
2663 char *
2664ex_range_without_command(exarg_T *eap)
2665{
2666 char *errormsg = NULL;
2667
2668 if ((*eap->cmd == '|' || (exmode_active && eap->line1 != eap->line2))
2669#ifdef FEAT_EVAL
2670 && !in_vim9script()
2671#endif
2672 )
2673 {
2674 eap->cmdidx = CMD_print;
2675 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2676 if ((errormsg = invalid_range(eap)) == NULL)
2677 {
2678 correct_range(eap);
2679 ex_print(eap);
2680 }
2681 }
2682 else if (eap->addr_count != 0)
2683 {
2684 if (eap->line2 > curbuf->b_ml.ml_line_count)
2685 {
2686 // With '-' in 'cpoptions' a line number past the file is an
2687 // error, otherwise put it at the end of the file.
2688 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2689 eap->line2 = -1;
2690 else
2691 eap->line2 = curbuf->b_ml.ml_line_count;
2692 }
2693
2694 if (eap->line2 < 0)
2695 errormsg = _(e_invalid_range);
2696 else
2697 {
2698 if (eap->line2 == 0)
2699 curwin->w_cursor.lnum = 1;
2700 else
2701 curwin->w_cursor.lnum = eap->line2;
2702 beginline(BL_SOL | BL_FIX);
2703 }
2704 }
2705 return errormsg;
2706}
2707
2708/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002709 * Check for an Ex command with optional tail.
2710 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002711 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002712 */
2713 static int
2714checkforcmd_opt(
2715 char_u **pp, // start of command
2716 char *cmd, // name of command
2717 int len, // required length
2718 int noparen)
2719{
2720 int i;
2721
2722 for (i = 0; cmd[i] != NUL; ++i)
2723 if (((char_u *)cmd)[i] != (*pp)[i])
2724 break;
Bram Moolenaar68854a82022-01-31 18:59:13 +00002725 if (i >= len && !isalpha((*pp)[i]) && (*pp)[i] != '_'
2726 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002727 {
2728 *pp = skipwhite(*pp + i);
2729 return TRUE;
2730 }
2731 return FALSE;
2732}
2733
2734/*
2735 * Check for an Ex command with optional tail.
2736 * If there is a match advance "pp" to the argument and return TRUE.
2737 */
2738 int
2739checkforcmd(
2740 char_u **pp, // start of command
2741 char *cmd, // name of command
2742 int len) // required length
2743{
2744 return checkforcmd_opt(pp, cmd, len, FALSE);
2745}
2746
2747/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002748 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002749 * If there is a match advance "pp" to the argument and return TRUE.
2750 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002751 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002752checkforcmd_noparen(
2753 char_u **pp, // start of command
2754 char *cmd, // name of command
2755 int len) // required length
2756{
2757 return checkforcmd_opt(pp, cmd, len, TRUE);
2758}
2759
2760/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002761 * Parse and skip over command modifiers:
2762 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002763 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002764 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002765 * When "skip_only" is TRUE the global variables are not changed, except for
2766 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002767 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2768 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002769 * Call apply_cmdmod() to get the side effects of the modifiers:
2770 * - Increment "sandbox" for ":sandbox"
2771 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002772 * - set msg_silent for ":silent"
2773 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002774 * Return FAIL when the command is not to be executed.
2775 * May set "errormsg" to an error message.
2776 */
2777 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002778parse_command_modifiers(
2779 exarg_T *eap,
2780 char **errormsg,
2781 cmdmod_T *cmod,
2782 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002783{
Bram Moolenaar565d1272022-03-27 18:11:05 +01002784 char_u *cmd_start = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002785 char_u *p;
2786 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002787 int vim9script = in_vim9script();
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002788 int has_visual_range = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002789
Bram Moolenaare1004402020-10-24 20:49:43 +02002790 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002791 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002792
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002793 if (STRNCMP(eap->cmd, "'<,'>", 5) == 0)
2794 {
2795 // The automatically inserted Visual area range is skipped, so that
2796 // typing ":cmdmod cmd" in Visual mode works without having to move the
2797 // range to after the modififiers.
2798 eap->cmd += 5;
2799 cmd_start = eap->cmd;
2800 has_visual_range = TRUE;
2801 }
2802
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002803 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002804 for (;;)
2805 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002806 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002807 {
2808 if (*eap->cmd == ':')
2809 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002810 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002811 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002812
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002813 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002814 if (*eap->cmd == NUL && exmode_active
2815 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2816 || getline_equal(eap->getline, eap->cookie, getexline))
2817 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2818 {
2819 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002820 if (!skip_only)
2821 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002822 }
2823
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002824 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002825 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002826 {
2827 // a comment ends at a NL
2828 if (eap->nextcmd == NULL)
2829 {
2830 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2831 if (eap->nextcmd != NULL)
2832 ++eap->nextcmd;
2833 }
Bram Moolenaarbc510062022-02-14 21:19:04 +00002834 if (vim9script && has_cmdmod(cmod, FALSE))
2835 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaareffed932018-08-14 13:38:17 +02002836 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002837 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002838 if (*eap->cmd == NUL)
2839 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002840 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002841 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002842 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002843 if (vim9script && has_cmdmod(cmod, FALSE))
2844 *errormsg = _(e_command_modifier_without_command);
2845 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002846 return FAIL;
2847 }
2848
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002849 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002850
2851 // In Vim9 script a variable can shadow a command modifier:
2852 // verbose = 123
2853 // verbose += 123
2854 // silent! verbose = func()
2855 // verbose.member = 2
2856 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002857 // But not:
2858 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002859 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002860 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002861 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002862
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002863 for (s = eap->cmd; ASCII_ISALPHA(*s); ++s)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002864 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002865 n = skipwhite(s);
Bram Moolenaarc75bca32022-03-27 13:36:50 +01002866 if (*n == '.' || *n == '=' || (*n != NUL && n[1] == '=')
2867 || *s == '[')
Bram Moolenaar17126b12021-01-07 22:03:02 +01002868 break;
2869 }
2870
Bram Moolenaareffed932018-08-14 13:38:17 +02002871 switch (*p)
2872 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002873 // When adding an entry, also modify cmd_exists().
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002874 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002875 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002876 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002877 continue;
2878
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002879 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002880 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002881 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002882 continue;
2883 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002884 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002885 {
2886#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002887 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002888#endif
2889 continue;
2890 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002891 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002892 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002893 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002894 continue;
2895
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002896 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002897 break;
2898#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002899 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002900#endif
2901 continue;
2902
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002903 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002904 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002905 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002906 continue;
2907 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002908 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002909 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002910 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002911 continue;
2912 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002913 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002914 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002915 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002916 continue;
2917 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002918 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002919 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002920 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002921 continue;
2922
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002923 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002924 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02002925 char_u *reg_pat;
2926 char_u *nulp = NULL;
2927 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002928
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002929 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002930 || *p == NUL
2931 || (ends_excmd(*p)
2932#ifdef FEAT_EVAL
2933 // in ":filter #pat# cmd" # does not
2934 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00002935 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002936#endif
2937 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02002938 break;
2939 if (*p == '!')
2940 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002941 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002942 p = skipwhite(p + 1);
2943 if (*p == NUL || ends_excmd(*p))
2944 break;
2945 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002946#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002947 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00002948 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002949 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002950#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002951 if (skip_only)
2952 p = skip_vimgrep_pat(p, NULL, NULL);
2953 else
2954 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02002955 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
2956 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02002957 if (p == NULL || *p == NUL)
2958 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002959 if (!skip_only)
2960 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002961 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002962 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002963 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002964 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02002965 // restore the character overwritten by NUL
2966 if (nulp != NULL)
2967 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002968 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002969 eap->cmd = p;
2970 continue;
2971 }
2972
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002973 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002974 case 'h': if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02002975 || *p == NUL || ends_excmd(*p))
2976 break;
2977 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002978 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002979 continue;
2980
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002981 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002982 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002983 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002984 continue;
2985 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002986 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
2987 {
2988 if (ends_excmd2(p, eap->cmd))
2989 {
2990 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02002991 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002992 return FAIL;
2993 }
2994 cmod->cmod_flags |= CMOD_LEGACY;
2995 continue;
2996 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002997
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002998 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002999 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003000 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003001 continue;
3002
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003003 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003004 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003005 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02003006 continue;
3007 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003008 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003009 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003010 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003011 continue;
3012
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003013 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003014 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003015 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003016 continue;
3017
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003018 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003019 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003020 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003021 continue;
3022 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003023 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003024 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003025 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003026 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3027 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003028 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003029 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003030 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003031 }
3032 continue;
3033
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003034 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003035 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003036 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003037 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003038 long tabnr = get_address(eap, &eap->cmd,
3039 ADDR_TABS, eap->skip,
3040 skip_only, FALSE, 1);
3041 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003042 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003043 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003044 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003045 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3046 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003047 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003048 return FAIL;
3049 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003050 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003051 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003052 }
3053 eap->cmd = p;
3054 continue;
3055 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003056 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003057 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003058 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003059 continue;
3060
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003061 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003062 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003063 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003064 continue;
3065
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003066 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003067 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003068 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003069 continue;
3070 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003071 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003072 {
3073 if (ends_excmd2(p, eap->cmd))
3074 {
3075 *errormsg =
3076 _(e_vim9cmd_must_be_followed_by_command);
3077 return FAIL;
3078 }
3079 cmod->cmod_flags |= CMOD_VIM9CMD;
3080 continue;
3081 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003082 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003083 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003084 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02003085 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003086 else
Bram Moolenaare1004402020-10-24 20:49:43 +02003087 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003088 eap->cmd = p;
3089 continue;
3090 }
3091 break;
3092 }
3093
Bram Moolenaar1501b632022-03-27 16:56:21 +01003094 if (has_visual_range)
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003095 {
Bram Moolenaar1501b632022-03-27 16:56:21 +01003096 if (eap->cmd > cmd_start)
3097 {
3098 // Move the '<,'> range to after the modifiers and insert a colon.
3099 // Since the modifiers have been parsed put the colon on top of the
3100 // space: "'<,'>mod cmd" -> "mod:'<,'>cmd
3101 // Put eap->cmd after the colon.
3102 mch_memmove(cmd_start - 5, cmd_start, eap->cmd - cmd_start);
3103 eap->cmd -= 5;
3104 mch_memmove(eap->cmd - 1, ":'<,'>", 6);
3105 }
3106 else
3107 // no modifiers, move the pointer back
3108 eap->cmd -= 5;
Bram Moolenaarc75bca32022-03-27 13:36:50 +01003109 }
3110
Bram Moolenaareffed932018-08-14 13:38:17 +02003111 return OK;
3112}
3113
3114/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003115 * Return TRUE if "cmod" has anything set.
3116 */
3117 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003118has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003119{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003120 return (cmod->cmod_flags != 0 && (!ignore_silent
3121 || (cmod->cmod_flags
3122 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003123 || cmod->cmod_split != 0
3124 || cmod->cmod_verbose != 0
3125 || cmod->cmod_tab != 0
3126 || cmod->cmod_filter_regmatch.regprog != NULL;
3127}
3128
Bram Moolenaar8991be22022-02-14 21:51:46 +00003129#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003130/*
3131 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3132 */
3133 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003134cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003135{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003136 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003137 {
3138 emsg(_(e_misplaced_command_modifier));
3139 return TRUE;
3140 }
3141 return FALSE;
3142}
Dominique Pelle748b3082022-01-08 12:41:16 +00003143#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003144
3145/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003146 * Apply the command modifiers. Saves current state in "cmdmod", call
3147 * undo_cmdmod() later.
3148 */
3149 void
3150apply_cmdmod(cmdmod_T *cmod)
3151{
3152#ifdef HAVE_SANDBOX
3153 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3154 {
3155 ++sandbox;
3156 cmod->cmod_did_sandbox = TRUE;
3157 }
3158#endif
3159 if (cmod->cmod_verbose > 0)
3160 {
3161 if (cmod->cmod_verbose_save == 0)
3162 cmod->cmod_verbose_save = p_verbose + 1;
3163 p_verbose = cmod->cmod_verbose;
3164 }
3165
3166 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3167 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003168 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003169 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003170 cmod->cmod_save_msg_scroll = msg_scroll;
3171 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003172 if (cmod->cmod_flags & CMOD_SILENT)
3173 ++msg_silent;
3174 if (cmod->cmod_flags & CMOD_UNSILENT)
3175 msg_silent = 0;
3176
3177 if (cmod->cmod_flags & CMOD_ERRSILENT)
3178 {
3179 ++emsg_silent;
3180 ++cmod->cmod_did_esilent;
3181 }
3182
Bram Moolenaare1004402020-10-24 20:49:43 +02003183 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003184 {
3185 // Set 'eventignore' to "all".
3186 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003187 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003188 set_string_option_direct((char_u *)"ei", -1,
3189 (char_u *)"all", OPT_FREE, SID_NONE);
3190 }
3191}
3192
3193/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003194 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003195 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003196 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003197undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003198{
Bram Moolenaare1004402020-10-24 20:49:43 +02003199 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003200 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003201 p_verbose = cmod->cmod_verbose_save - 1;
3202 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003203 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003204
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003205#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003206 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003207 {
3208 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003209 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003210 }
3211#endif
3212
Bram Moolenaare1004402020-10-24 20:49:43 +02003213 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003214 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003215 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003216 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3217 OPT_FREE, SID_NONE);
3218 free_string_option(cmod->cmod_save_ei);
3219 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003220 }
3221
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003222 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003223
Bram Moolenaare1004402020-10-24 20:49:43 +02003224 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003225 {
3226 // messages could be enabled for a serious error, need to check if the
3227 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003228 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3229 msg_silent = cmod->cmod_save_msg_silent - 1;
3230 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003231 if (emsg_silent < 0)
3232 emsg_silent = 0;
3233 // Restore msg_scroll, it's set by file I/O commands, even when no
3234 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003235 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003236
3237 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3238 // somewhere in the line. Put it back in the first column.
3239 if (redirecting())
3240 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003241
Bram Moolenaare1004402020-10-24 20:49:43 +02003242 cmod->cmod_save_msg_silent = 0;
3243 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003244 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003245}
3246
3247/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003248 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003249 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003250 * Return FAIL and set "errormsg" or return OK.
3251 */
3252 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003253parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003254{
3255 int address_count = 1;
3256 linenr_T lnum;
3257
3258 // Repeat for all ',' or ';' separated addresses.
3259 for (;;)
3260 {
3261 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003262 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003263 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003264 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003265 eap->addr_count == 0, address_count++);
3266 if (eap->cmd == NULL) // error detected
3267 return FAIL;
3268 if (lnum == MAXLNUM)
3269 {
3270 if (*eap->cmd == '%') // '%' - all lines
3271 {
3272 ++eap->cmd;
3273 switch (eap->addr_type)
3274 {
3275 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003276 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003277 eap->line1 = 1;
3278 eap->line2 = curbuf->b_ml.ml_line_count;
3279 break;
3280 case ADDR_LOADED_BUFFERS:
3281 {
3282 buf_T *buf = firstbuf;
3283
3284 while (buf->b_next != NULL
3285 && buf->b_ml.ml_mfp == NULL)
3286 buf = buf->b_next;
3287 eap->line1 = buf->b_fnum;
3288 buf = lastbuf;
3289 while (buf->b_prev != NULL
3290 && buf->b_ml.ml_mfp == NULL)
3291 buf = buf->b_prev;
3292 eap->line2 = buf->b_fnum;
3293 break;
3294 }
3295 case ADDR_BUFFERS:
3296 eap->line1 = firstbuf->b_fnum;
3297 eap->line2 = lastbuf->b_fnum;
3298 break;
3299 case ADDR_WINDOWS:
3300 case ADDR_TABS:
3301 if (IS_USER_CMDIDX(eap->cmdidx))
3302 {
3303 eap->line1 = 1;
3304 eap->line2 = eap->addr_type == ADDR_WINDOWS
3305 ? LAST_WIN_NR : LAST_TAB_NR;
3306 }
3307 else
3308 {
3309 // there is no Vim command which uses '%' and
3310 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003311 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003312 return FAIL;
3313 }
3314 break;
3315 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003316 case ADDR_UNSIGNED:
3317 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003318 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003319 return FAIL;
3320 case ADDR_ARGUMENTS:
3321 if (ARGCOUNT == 0)
3322 eap->line1 = eap->line2 = 0;
3323 else
3324 {
3325 eap->line1 = 1;
3326 eap->line2 = ARGCOUNT;
3327 }
3328 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003329 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003330#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003331 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003332 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003333 if (eap->line2 == 0)
3334 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003335#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003336 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003337 case ADDR_NONE:
3338 // Will give an error later if a range is found.
3339 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003340 }
3341 ++eap->addr_count;
3342 }
3343 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3344 {
3345 pos_T *fp;
3346
3347 // '*' - visual area
3348 if (eap->addr_type != ADDR_LINES)
3349 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003350 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003351 return FAIL;
3352 }
3353
3354 ++eap->cmd;
3355 if (!eap->skip)
3356 {
3357 fp = getmark('<', FALSE);
3358 if (check_mark(fp) == FAIL)
3359 return FAIL;
3360 eap->line1 = fp->lnum;
3361 fp = getmark('>', FALSE);
3362 if (check_mark(fp) == FAIL)
3363 return FAIL;
3364 eap->line2 = fp->lnum;
3365 ++eap->addr_count;
3366 }
3367 }
3368 }
3369 else
3370 eap->line2 = lnum;
3371 eap->addr_count++;
3372
3373 if (*eap->cmd == ';')
3374 {
3375 if (!eap->skip)
3376 {
3377 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003378 // Don't leave the cursor on an illegal line or column, but do
3379 // accept zero as address, so 0;/PATTERN/ works correctly.
3380 if (eap->line2 > 0)
3381 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003382 }
3383 }
3384 else if (*eap->cmd != ',')
3385 break;
3386 ++eap->cmd;
3387 }
3388
3389 // One address given: set start and end lines.
3390 if (eap->addr_count == 1)
3391 {
3392 eap->line1 = eap->line2;
3393 // ... but only implicit: really no address given
3394 if (lnum == MAXLNUM)
3395 eap->addr_count = 0;
3396 }
3397 return OK;
3398}
3399
3400/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003401 * Append "cmd" to the error message in IObuff.
3402 * Takes care of limiting the length and handling 0xa0, which would be
3403 * invisible otherwise.
3404 */
3405 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003406append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003407{
3408 char_u *s = cmd;
3409 char_u *d;
3410
3411 STRCAT(IObuff, ": ");
3412 d = IObuff + STRLEN(IObuff);
3413 while (*s != NUL && d - IObuff < IOSIZE - 7)
3414 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003415 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003416 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003417 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003418 STRCPY(d, "<a0>");
3419 d += 4;
3420 }
3421 else
3422 MB_COPY_CHAR(s, d);
3423 }
3424 *d = NUL;
3425}
3426
Dominique Pelle748b3082022-01-08 12:41:16 +00003427#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003428/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003429 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3430 * the name. Otherwise just return "start".
3431 */
3432 char_u *
3433skip_option_env_lead(char_u *start)
3434{
3435 char_u *name = start;
3436
3437 if (*start == '&')
3438 {
3439 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3440 name += 3;
3441 else
3442 name += 1;
3443 }
3444 else if (*start == '$')
3445 name += 1;
3446 return name;
3447}
Dominique Pelle748b3082022-01-08 12:41:16 +00003448#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003449
3450/*
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003451 * Return TRUE and set "*idx" if "p" points to a one letter command.
3452 * If not in Vim9 script:
3453 * - The 'k' command can directly be followed by any character.
3454 * - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3455 * but :sre[wind] is another command, as are :scr[iptnames],
3456 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
3457 */
3458 static int
3459one_letter_cmd(char_u *p, cmdidx_T *idx)
3460{
Bram Moolenaar1e2c4172022-03-24 15:24:45 +00003461 if (in_vim9script())
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003462 return FALSE;
3463 if (*p == 'k')
3464 {
3465 *idx = CMD_k;
3466 return TRUE;
3467 }
3468 if (p[0] == 's'
3469 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3470 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
3471 || p[1] == 'g'
3472 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3473 || p[1] == 'I'
3474 || (p[1] == 'r' && p[2] != 'e')))
3475 {
3476 *idx = CMD_substitute;
3477 return TRUE;
3478 }
3479 return FALSE;
3480}
3481
3482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003484 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003485 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003486 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 *
3488 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3489 * assignment without "let". Sets eap->cmdidx to the command while returning
3490 * "eap->cmd".
3491 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 * Returns NULL for an ambiguous user command.
3493 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 char_u *
3495find_ex_command(
3496 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003497 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003498 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003499 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500{
3501 int len;
3502 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003503 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003504#ifndef FEAT_EVAL
3505 int vim9 = FALSE;
3506#else
3507 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 /*
3510 * Recognize a Vim9 script function/method call and assignment:
3511 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3512 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003513 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003514 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003516 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003518 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003519 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3520 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003522 int oplen;
3523 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003524 char_u *swp;
3525
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003526 if (*eap->cmd == '&'
3527 || *eap->cmd == '$'
3528 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003529 && (valid_yank_reg(eap->cmd[1], FALSE)
3530 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003531 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003532 if (*eap->cmd == '&')
3533 {
3534 p = eap->cmd + 1;
3535 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3536 p += 2;
3537 p = to_name_end(p, FALSE);
3538 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003539 else if (*eap->cmd == '$')
3540 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003541 else
3542 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003543 if (ends_excmd(*skipwhite(p)))
3544 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003545 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3546 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003547 eap->cmdidx = CMD_eval;
3548 return eap->cmd;
3549 }
3550 // "&option" can be followed by "->" or "=", check below
3551 }
3552
3553 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003555 if (
3556 // "(..." is an expression.
3557 // "funcname(" is always a function call.
3558 *p == '('
3559 || (p == eap->cmd
3560 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003561 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003562 *eap->cmd == '{'
3563 // "'string'->func()" is an expression.
3564 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003565 // '"string"->func()' is an expression.
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003566 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
3567 // '"string"->func()' is an expression.
Bram Moolenaar10409562020-07-29 20:00:38 +02003568 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003569 // "g:varname" is an expression.
3570 || eap->cmd[1] == ':'
3571 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003572 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003573 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003574 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003575 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3576 {
3577 // "{" by itself is the start of a block.
3578 eap->cmdidx = CMD_block;
3579 return eap->cmd + 1;
3580 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003581 eap->cmdidx = CMD_eval;
3582 return eap->cmd;
3583 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003584
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003585 if ((p != eap->cmd && (
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003586 // "varname[]" is an expression.
3587 *p == '['
3588 // "varname.key" is an expression.
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003589 || (*p == '.'
3590 && (ASCII_ISALPHA(p[1]) || p[1] == '_'))))
3591 // g:[key] is an expression
3592 || STRNCMP(eap->cmd, "g:[", 3) == 0)
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003593 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003594 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003595
3596 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003597 // Skip over the whole thing, it can be:
3598 // name.member = val
3599 // name[a : b] = val
3600 // name[idx] = val
3601 // name[idx].member = val
3602 // etc.
3603 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003604 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003605 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003606 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003607 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003608 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003609 || (after[0] == '.' && after[1] == '.'
3610 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003611 eap->cmdidx = CMD_var;
3612 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003613 --emsg_silent;
3614 return eap->cmd;
3615 }
3616
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003617 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3618 // an assignment.
3619 // If there is no line break inside the "[...]" then "p" is
3620 // advanced to after the "]" by to_name_const_end(): check if a "="
3621 // follows.
3622 // If "[...]" has a line break "p" still points at the "[" and it
3623 // can't be an assignment.
3624 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003625 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003626 char_u *eq;
3627
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003628 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003629 if (p == eap->cmd && *p == '[')
3630 {
3631 int count = 0;
3632 int semicolon = FALSE;
3633
3634 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3635 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003636 eq = p;
3637 if (eq != NULL)
3638 {
3639 eq = skipwhite(eq);
3640 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3641 ++eq;
3642 }
3643 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003644 {
3645 eap->cmdidx = CMD_eval;
3646 return eap->cmd;
3647 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003648 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003649 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003650 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003651 return eap->cmd;
3652 }
3653 }
3654
3655 // Recognize an assignment if we recognize the variable name:
3656 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003657 // "@r = expr"
3658 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003659 // "var = expr" where "var" is a variable name or we are skipping
3660 // (variable declaration might have been skipped).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003661 oplen = assignment_len(skipwhite(p), &heredoc);
3662 if (oplen > 0)
3663 {
3664 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3665 || *eap->cmd == '&'
3666 || *eap->cmd == '$'
3667 || *eap->cmd == '@'
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003668 || eap->skip
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003669 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003670 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003671 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003672 return eap->cmd;
3673 }
3674 }
3675
3676 // Recognize using a type for a w:, b:, t: or g: variable:
3677 // "w:varname: number = 123".
3678 if (eap->cmd[1] == ':' && *p == ':')
3679 {
3680 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003681 return eap->cmd;
3682 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003683 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003684
Bram Moolenaar7b829262021-10-13 15:04:34 +01003685 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3686 // an expression. A global/substitute/list command needs to use a
3687 // longer name.
3688 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3689 {
3690 eap->cmdidx = CMD_eval;
3691 return eap->cmd;
3692 }
3693
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003694 // If it is an ID it might be a variable with an operator on the next
3695 // line, if the variable exists it can't be an Ex command.
3696 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003697 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003698 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3699 {
3700 eap->cmdidx = CMD_eval;
3701 return eap->cmd;
3702 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003703
3704 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003705 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3706 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003707 {
3708 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3709 return eap->cmd + 2;
3710 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 }
3712#endif
3713
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 /*
3715 * Isolate the command and search for it in the command table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 */
3717 p = eap->cmd;
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003718 if (one_letter_cmd(p, &eap->cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 ++p;
3721 }
3722 else
3723 {
3724 while (ASCII_ISALPHA(*p))
3725 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003726 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003727 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003729 while (ASCII_ISALNUM(*p))
3730 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 }
3732 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3733 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003734 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 ++p;
3736 while (ASCII_ISALPHA(*p))
3737 ++p;
3738 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003739
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003740 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003741 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 ++p;
3743 len = (int)(p - eap->cmd);
Bram Moolenaarf4f05252022-03-24 13:08:36 +00003744 // The "d" command can directly be followed by 'l' or 'p' flag, when
3745 // not in Vim9 script.
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003746 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003747 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003748 // Check for ":dl", ":dell", etc. to ":deletel": that's
3749 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003750 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003751 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003752 break;
3753 if (i == len - 1)
3754 {
3755 --len;
3756 if (p[-1] == 'l')
3757 eap->flags |= EXFLAG_LIST;
3758 else
3759 eap->flags |= EXFLAG_PRINT;
3760 }
3761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003763 if (ASCII_ISLOWER(eap->cmd[0]))
3764 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003765 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003766 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003767
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003768 if (command_count != (int)CMD_SIZE)
3769 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003770 iemsg(_(e_command_table_needs_to_be_updated_run_make_cmdidxs));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003771 getout(1);
3772 }
3773
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003774 // Use a precomputed index for fast look-up in cmdnames[]
3775 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003776 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3777 if (ASCII_ISLOWER(c2))
3778 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3779 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003780 else if (ASCII_ISUPPER(eap->cmd[0]))
3781 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003783 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784
3785 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3786 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3787 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3788 (size_t)len) == 0)
3789 {
3790#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003791 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 *full = TRUE;
3793#endif
3794 break;
3795 }
3796
Bram Moolenaar204852a2022-03-05 12:56:44 +00003797 // :Print and :mode are not supported in Vim9 script.
3798 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003799 if (vim9 && eap->cmdidx != CMD_SIZE)
3800 {
3801 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3802 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003803 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003804 && len < (int)STRLEN(cmdnames[eap->cmdidx].cmd_name))
Bram Moolenaar204852a2022-03-05 12:56:44 +00003805 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003806 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003807 eap->cmdidx = CMD_SIZE;
3808 }
3809 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003810
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003811 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003812 // 'cpoptions'.
3813 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3814 p = eap->cmd;
3815
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003816 // Look for a user defined command as a last resort. Let ":Print" be
3817 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003818 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3819 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003821 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 while (ASCII_ISALNUM(*p))
3823 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003824 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003826 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 eap->cmdidx = CMD_SIZE;
3828 }
3829
Bram Moolenaar204852a2022-03-05 12:56:44 +00003830 // ":fina" means ":finally" in legacy script, for backwards compatibility.
3831 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02003832 eap->cmdidx = CMD_finally;
3833
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003834#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003835 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003836 && vim9
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +02003837 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003838 && (eap->cmdidx < 0 ||
3839 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003840 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003841 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3842
3843 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003844 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003845 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003846 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003847#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003848
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 return p;
3850}
3851
3852#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003853static struct cmdmod
3854{
3855 char *name;
3856 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003857 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003858} cmdmods[] = {
3859 {"aboveleft", 3, FALSE},
3860 {"belowright", 3, FALSE},
3861 {"botright", 2, FALSE},
3862 {"browse", 3, FALSE},
3863 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003864 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003865 {"hide", 3, FALSE},
3866 {"keepalt", 5, FALSE},
3867 {"keepjumps", 5, FALSE},
3868 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003869 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003870 {"leftabove", 5, FALSE},
3871 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003872 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003873 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003874 {"rightbelow", 6, FALSE},
3875 {"sandbox", 3, FALSE},
3876 {"silent", 3, FALSE},
3877 {"tab", 3, TRUE},
3878 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003879 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003880 {"verbose", 4, TRUE},
3881 {"vertical", 4, FALSE},
3882};
3883
3884/*
3885 * Return length of a command modifier (including optional count).
3886 * Return zero when it's not a modifier.
3887 */
3888 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003889modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003890{
3891 int i, j;
3892 char_u *p = cmd;
3893
3894 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02003895 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02003896 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003897 {
3898 for (j = 0; p[j] != NUL; ++j)
3899 if (p[j] != cmdmods[i].name[j])
3900 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003901 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003902 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003903 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003904 }
3905 return 0;
3906}
3907
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908/*
3909 * Return > 0 if an Ex command "name" exists.
3910 * Return 2 if there is an exact match.
3911 * Return 3 if there is an ambiguous match.
3912 */
3913 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003914cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915{
3916 exarg_T ea;
3917 int full = FALSE;
3918 int i;
3919 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003920 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003922 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02003923 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 {
3925 for (j = 0; name[j] != NUL; ++j)
3926 if (name[j] != cmdmods[i].name[j])
3927 break;
3928 if (name[j] == NUL && j >= cmdmods[i].minlen)
3929 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3930 }
3931
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003932 // Check built-in commands and user defined commands.
3933 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003934 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar03a297c2022-03-25 14:39:51 +00003936 ea.flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003938 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003940 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3941 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003942 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003943 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3945}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003946
3947/*
3948 * "fullcommand" function
3949 */
3950 void
3951f_fullcommand(typval_T *argvars, typval_T *rettv)
3952{
3953 exarg_T ea;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003954 char_u *name;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003955 char_u *p;
3956
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003957 rettv->v_type = VAR_STRING;
3958 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003959
3960 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
3961 return;
3962
3963 name = argvars[0].vval.v_string;
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003964 if (name == NULL)
3965 return;
3966
zeertzjqc024ed92022-01-04 16:22:52 +00003967 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003968 name++;
3969 name = skip_range(name, TRUE, NULL);
3970
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003971 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
3972 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003973 ea.addr_count = 0;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003974 p = find_ex_command(&ea, NULL, NULL, NULL);
3975 if (p == NULL || ea.cmdidx == CMD_SIZE)
3976 return;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003977 if (in_vim9script())
3978 {
3979 int res;
3980
3981 ++emsg_silent;
3982 res = not_in_vim9(&ea);
3983 --emsg_silent;
3984
3985 if (res == FAIL)
3986 return;
3987 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003988
3989 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02003990 ? get_user_command_name(ea.useridx, ea.cmdidx)
3991 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003992}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993#endif
3994
Bram Moolenaard0190392019-08-23 21:17:35 +02003995 cmdidx_T
3996excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997{
Bram Moolenaard0190392019-08-23 21:17:35 +02003998 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999
Bram Moolenaarf4f05252022-03-24 13:08:36 +00004000 if (!one_letter_cmd(cmd, &idx))
4001 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
4002 idx = (cmdidx_T)((int)idx + 1))
4003 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
4004 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005
Bram Moolenaard0190392019-08-23 21:17:35 +02004006 return idx;
4007}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
Bram Moolenaard0190392019-08-23 21:17:35 +02004009 long
4010excmd_get_argt(cmdidx_T idx)
4011{
4012 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013}
4014
4015/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004016 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 *
4018 * Backslashed delimiters after / or ? will be skipped, and commands will
4019 * not be expanded between /'s and ?'s or after "'".
4020 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02004021 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 * Returns the "cmd" pointer advanced to beyond the range.
4023 */
4024 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004025skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004026 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004027 int skip_star, // skip "*" used for Visual range
4028 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004030 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004031 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004033 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004035 if (*cmd == '\\')
4036 {
4037 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4038 ++cmd;
4039 else
4040 break;
4041 }
4042 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00004044 char_u *p = cmd;
4045
4046 // a quote is only valid at the start or after a separator
4047 while (p > cmd_start)
4048 {
4049 --p;
4050 if (!VIM_ISWHITE(*p))
4051 break;
4052 }
4053 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4054 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 if (*++cmd == NUL && ctx != NULL)
4056 *ctx = EXPAND_NOTHING;
4057 }
4058 else if (*cmd == '/' || *cmd == '?')
4059 {
4060 delim = *cmd++;
4061 while (*cmd != NUL && *cmd != delim)
4062 if (*cmd++ == '\\' && *cmd != NUL)
4063 ++cmd;
4064 if (*cmd == NUL && ctx != NULL)
4065 *ctx = EXPAND_NOTHING;
4066 }
4067 if (*cmd != NUL)
4068 ++cmd;
4069 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004070
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004071 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004072 while (*cmd == ':')
4073 cmd = skipwhite(cmd + 1);
4074
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004075 // Skip "*" used for Visual range.
4076 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4077 cmd = skipwhite(cmd + 1);
4078
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 return cmd;
4080}
4081
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004082 static void
4083addr_error(cmd_addr_T addr_type)
4084{
4085 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004086 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004087 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004088 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004089}
4090
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004092 * Return the default address for an address type.
4093 */
4094 static linenr_T
4095default_address(exarg_T *eap)
4096{
4097 linenr_T lnum = 0;
4098
4099 switch (eap->addr_type)
4100 {
4101 case ADDR_LINES:
4102 case ADDR_OTHER:
4103 // Default is the cursor line number. Avoid using an invalid
4104 // line number though.
4105 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4106 lnum = curbuf->b_ml.ml_line_count;
4107 else
4108 lnum = curwin->w_cursor.lnum;
4109 break;
4110 case ADDR_WINDOWS:
4111 lnum = CURRENT_WIN_NR;
4112 break;
4113 case ADDR_ARGUMENTS:
4114 lnum = curwin->w_arg_idx + 1;
4115 if (lnum > ARGCOUNT)
4116 lnum = ARGCOUNT;
4117 break;
4118 case ADDR_LOADED_BUFFERS:
4119 case ADDR_BUFFERS:
4120 lnum = curbuf->b_fnum;
4121 break;
4122 case ADDR_TABS:
4123 lnum = CURRENT_TAB_NR;
4124 break;
4125 case ADDR_TABS_RELATIVE:
4126 case ADDR_UNSIGNED:
4127 lnum = 1;
4128 break;
4129 case ADDR_QUICKFIX:
4130#ifdef FEAT_QUICKFIX
4131 lnum = qf_get_cur_idx(eap);
4132#endif
4133 break;
4134 case ADDR_QUICKFIX_VALID:
4135#ifdef FEAT_QUICKFIX
4136 lnum = qf_get_cur_valid_idx(eap);
4137#endif
4138 break;
4139 case ADDR_NONE:
4140 // Will give an error later if a range is found.
4141 break;
4142 }
4143 return lnum;
4144}
4145
4146/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004147 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 *
4149 * Set ptr to the next character after the part that was interpreted.
4150 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004151 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 *
4153 * Return MAXLNUM when no Ex address was found.
4154 */
4155 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004156get_address(
4157 exarg_T *eap UNUSED,
4158 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004159 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004160 int skip, // only skip the address, don't use it
4161 int silent, // no errors or side effects
4162 int to_other_file, // flag: may jump to other file
4163 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164{
4165 int c;
4166 int i;
4167 long n;
4168 char_u *cmd;
4169 pos_T pos;
4170 pos_T *fp;
4171 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004172 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173
4174 cmd = skipwhite(*ptr);
4175 lnum = MAXLNUM;
4176 do
4177 {
4178 switch (*cmd)
4179 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004180 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004181 ++cmd;
4182 switch (addr_type)
4183 {
4184 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004185 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 lnum = curwin->w_cursor.lnum;
4187 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004188 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004189 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004190 break;
4191 case ADDR_ARGUMENTS:
4192 lnum = curwin->w_arg_idx + 1;
4193 break;
4194 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004195 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004196 lnum = curbuf->b_fnum;
4197 break;
4198 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004199 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004200 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004201 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004202 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004203 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004204 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004205 cmd = NULL;
4206 goto error;
4207 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004208 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004209#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004210 lnum = qf_get_cur_idx(eap);
4211#endif
4212 break;
4213 case ADDR_QUICKFIX_VALID:
4214#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004215 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004216#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004217 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004218 }
4219 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004221 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004222 ++cmd;
4223 switch (addr_type)
4224 {
4225 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004226 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 lnum = curbuf->b_ml.ml_line_count;
4228 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004229 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004230 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004231 break;
4232 case ADDR_ARGUMENTS:
4233 lnum = ARGCOUNT;
4234 break;
4235 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004236 buf = lastbuf;
4237 while (buf->b_ml.ml_mfp == NULL)
4238 {
4239 if (buf->b_prev == NULL)
4240 break;
4241 buf = buf->b_prev;
4242 }
4243 lnum = buf->b_fnum;
4244 break;
4245 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004246 lnum = lastbuf->b_fnum;
4247 break;
4248 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004249 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004250 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004251 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004252 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004253 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004254 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004255 cmd = NULL;
4256 goto error;
4257 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004258 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004259#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004260 lnum = qf_get_size(eap);
4261 if (lnum == 0)
4262 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004263#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004264 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004265 case ADDR_QUICKFIX_VALID:
4266#ifdef FEAT_QUICKFIX
4267 lnum = qf_get_valid_size(eap);
4268 if (lnum == 0)
4269 lnum = 1;
4270#endif
4271 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004272 }
4273 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004275 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004276 if (*++cmd == NUL)
4277 {
4278 cmd = NULL;
4279 goto error;
4280 }
4281 if (addr_type != ADDR_LINES)
4282 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004283 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004284 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004285 goto error;
4286 }
4287 if (skip)
4288 ++cmd;
4289 else
4290 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004291 // Only accept a mark in another file when it is
4292 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004293 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4294 ++cmd;
4295 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004296 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004297 lnum = curwin->w_cursor.lnum;
4298 else
4299 {
4300 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 {
4302 cmd = NULL;
4303 goto error;
4304 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004305 lnum = fp->lnum;
4306 }
4307 }
4308 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
4310 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004311 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004312 c = *cmd++;
4313 if (addr_type != ADDR_LINES)
4314 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004315 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004316 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004317 goto error;
4318 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004319 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004320 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004321 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004322 if (*cmd == c)
4323 ++cmd;
4324 }
4325 else
4326 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004327 int flags;
4328
4329 pos = curwin->w_cursor; // save curwin->w_cursor
4330
4331 // When '/' or '?' follows another address, start from
4332 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004333 if (lnum > 0 && lnum != MAXLNUM)
4334 curwin->w_cursor.lnum =
4335 lnum > curbuf->b_ml.ml_line_count
4336 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004337
4338 // Start a forward search at the end of the line (unless
4339 // before the first line).
4340 // Start a backward search at the start of the line.
4341 // This makes sure we never match in the current
4342 // line, and can match anywhere in the
4343 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004344 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004345 curwin->w_cursor.col = MAXCOL;
4346 else
4347 curwin->w_cursor.col = 0;
4348 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004349 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004350 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004351 {
4352 curwin->w_cursor = pos;
4353 cmd = NULL;
4354 goto error;
4355 }
4356 lnum = curwin->w_cursor.lnum;
4357 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004358 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004359 cmd += searchcmdlen;
4360 }
4361 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004363 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004364 ++cmd;
4365 if (addr_type != ADDR_LINES)
4366 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004367 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004368 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004369 goto error;
4370 }
4371 if (*cmd == '&')
4372 i = RE_SUBST;
4373 else if (*cmd == '?' || *cmd == '/')
4374 i = RE_SEARCH;
4375 else
4376 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004377 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004378 cmd = NULL;
4379 goto error;
4380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004382 if (!skip)
4383 {
4384 /*
4385 * When search follows another address, start from
4386 * there.
4387 */
4388 if (lnum != MAXLNUM)
4389 pos.lnum = lnum;
4390 else
4391 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004393 /*
4394 * Start the search just like for the above
4395 * do_search().
4396 */
4397 if (*cmd != '?')
4398 pos.col = MAXCOL;
4399 else
4400 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004401 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004402 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004403 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004404 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004405 lnum = pos.lnum;
4406 else
4407 {
4408 cmd = NULL;
4409 goto error;
4410 }
4411 }
4412 ++cmd;
4413 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414
4415 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004416 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004417 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 }
4419
4420 for (;;)
4421 {
4422 cmd = skipwhite(cmd);
4423 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4424 break;
4425
4426 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004427 {
4428 switch (addr_type)
4429 {
4430 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004431 case ADDR_OTHER:
4432 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004433 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004434 break;
4435 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004436 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004437 break;
4438 case ADDR_ARGUMENTS:
4439 lnum = curwin->w_arg_idx + 1;
4440 break;
4441 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004442 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004443 lnum = curbuf->b_fnum;
4444 break;
4445 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004446 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004447 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004448 case ADDR_TABS_RELATIVE:
4449 lnum = 1;
4450 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004451 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004452#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004453 lnum = qf_get_cur_idx(eap);
4454#endif
4455 break;
4456 case ADDR_QUICKFIX_VALID:
4457#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004458 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004459#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004460 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004461 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004462 case ADDR_UNSIGNED:
4463 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004464 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004465 }
4466 }
4467
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004469 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 else
4471 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004472 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 n = 1;
4474 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004475 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004477 if (n == MAXLNUM)
4478 {
4479 emsg(_(e_line_number_out_of_range));
4480 goto error;
4481 }
4482 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004483
4484 if (addr_type == ADDR_TABS_RELATIVE)
4485 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004486 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004487 cmd = NULL;
4488 goto error;
4489 }
4490 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004491 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004492 lnum = compute_buffer_local_count(
4493 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004495 {
4496#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004497 // Relative line addressing, need to adjust for folded lines
4498 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004499 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
Bram Moolenaar03725c52021-11-24 12:17:53 +00004500 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004501 (void)hasFolding(lnum, NULL, &lnum);
4502#endif
4503 if (i == '-')
4504 lnum -= n;
4505 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004506 {
4507 if (n >= LONG_MAX - lnum)
4508 {
4509 emsg(_(e_line_number_out_of_range));
4510 goto error;
4511 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004512 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004513 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 }
4516 } while (*cmd == '/' || *cmd == '?');
4517
4518error:
4519 *ptr = cmd;
4520 return lnum;
4521}
4522
4523/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004524 * Set eap->line1 and eap->line2 to the whole range.
4525 * Used for commands with the EX_DFLALL flag and no range given.
4526 */
4527 static void
4528address_default_all(exarg_T *eap)
4529{
4530 eap->line1 = 1;
4531 switch (eap->addr_type)
4532 {
4533 case ADDR_LINES:
4534 case ADDR_OTHER:
4535 eap->line2 = curbuf->b_ml.ml_line_count;
4536 break;
4537 case ADDR_LOADED_BUFFERS:
4538 {
4539 buf_T *buf = firstbuf;
4540
4541 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4542 buf = buf->b_next;
4543 eap->line1 = buf->b_fnum;
4544 buf = lastbuf;
4545 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4546 buf = buf->b_prev;
4547 eap->line2 = buf->b_fnum;
4548 }
4549 break;
4550 case ADDR_BUFFERS:
4551 eap->line1 = firstbuf->b_fnum;
4552 eap->line2 = lastbuf->b_fnum;
4553 break;
4554 case ADDR_WINDOWS:
4555 eap->line2 = LAST_WIN_NR;
4556 break;
4557 case ADDR_TABS:
4558 eap->line2 = LAST_TAB_NR;
4559 break;
4560 case ADDR_TABS_RELATIVE:
4561 eap->line2 = 1;
4562 break;
4563 case ADDR_ARGUMENTS:
4564 if (ARGCOUNT == 0)
4565 eap->line1 = eap->line2 = 0;
4566 else
4567 eap->line2 = ARGCOUNT;
4568 break;
4569 case ADDR_QUICKFIX_VALID:
4570#ifdef FEAT_QUICKFIX
4571 eap->line2 = qf_get_valid_size(eap);
4572 if (eap->line2 == 0)
4573 eap->line2 = 1;
4574#endif
4575 break;
4576 case ADDR_NONE:
4577 case ADDR_UNSIGNED:
4578 case ADDR_QUICKFIX:
4579 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4580 break;
4581 }
4582}
4583
4584
4585/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004586 * Get flags from an Ex command argument.
4587 */
4588 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004589get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004590{
4591 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4592 {
4593 if (*eap->arg == 'l')
4594 eap->flags |= EXFLAG_LIST;
4595 else if (*eap->arg == 'p')
4596 eap->flags |= EXFLAG_PRINT;
4597 else
4598 eap->flags |= EXFLAG_NR;
4599 eap->arg = skipwhite(eap->arg + 1);
4600 }
4601}
4602
4603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 * Function called for command which is Not Implemented. NI!
4605 */
4606 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004607ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608{
4609 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004610 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004611 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612}
4613
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004614#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615/*
4616 * Function called for script command which is Not Implemented. NI!
4617 * Skips over ":perl <<EOF" constructs.
4618 */
4619 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004620ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621{
4622 if (!eap->skip)
4623 ex_ni(eap);
4624 else
4625 vim_free(script_get(eap, eap->arg));
4626}
4627#endif
4628
4629/*
4630 * Check range in Ex command for validity.
4631 * Return NULL when valid, error message when invalid.
4632 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004633 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004634invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004636 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004637
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 if ( eap->line1 < 0
4639 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004640 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004641 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004642
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004643 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004644 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004645 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004646 {
4647 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004648 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004649#ifdef FEAT_DIFF
4650 + (eap->cmdidx == CMD_diffget)
4651#endif
4652 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004653 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004654 break;
4655 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004656 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004657 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004658 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004659 break;
4660 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004661 // Only a boundary check, not whether the buffers actually
4662 // exist.
4663 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004664 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004665 break;
4666 case ADDR_LOADED_BUFFERS:
4667 buf = firstbuf;
4668 while (buf->b_ml.ml_mfp == NULL)
4669 {
4670 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004671 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004672 buf = buf->b_next;
4673 }
4674 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004675 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004676 buf = lastbuf;
4677 while (buf->b_ml.ml_mfp == NULL)
4678 {
4679 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004680 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004681 buf = buf->b_prev;
4682 }
4683 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004684 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004685 break;
4686 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004687 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004688 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004689 break;
4690 case ADDR_TABS:
4691 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004692 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004693 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004694 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004695 case ADDR_OTHER:
4696 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004697 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004698 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004699#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004700 // No error for value that is too big, will use the last entry.
4701 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004702 {
4703 if (eap->addr_count == 0)
4704 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004705 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004706 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004707#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004708 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004709 case ADDR_QUICKFIX_VALID:
4710#ifdef FEAT_QUICKFIX
4711 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4712 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004713 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004714#endif
4715 break;
4716 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004717 case ADDR_NONE:
4718 // Will give an error elsewhere.
4719 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004720 }
4721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 return NULL;
4723}
4724
4725/*
4726 * Correct the range for zero line number, if required.
4727 */
4728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004729correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004731 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 {
4733 if (eap->line1 == 0)
4734 eap->line1 = 1;
4735 if (eap->line2 == 0)
4736 eap->line2 = 1;
4737 }
4738}
4739
Bram Moolenaar748bf032005-02-02 23:04:36 +00004740#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004741/*
4742 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4743 * pattern. Otherwise return eap->arg.
4744 */
4745 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004746skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004747{
4748 char_u *p = eap->arg;
4749
Bram Moolenaara37420f2006-02-04 22:37:47 +00004750 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4751 || eap->cmdidx == CMD_vimgrepadd
4752 || eap->cmdidx == CMD_lvimgrepadd
4753 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004754 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004755 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004756 if (p == NULL)
4757 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004758 }
4759 return p;
4760}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004761
4762/*
4763 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4764 * in the command line, so that things like % get expanded.
4765 */
4766 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004767replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004768{
4769 char_u *new_cmdline;
4770 char_u *program;
4771 char_u *pos;
4772 char_u *ptr;
4773 int len;
4774 int i;
4775
4776 /*
4777 * Don't do it when ":vimgrep" is used for ":grep".
4778 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004779 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4780 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4781 || eap->cmdidx == CMD_grepadd
4782 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004783 && !grep_internal(eap->cmdidx))
4784 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004785 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4786 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004787 {
4788 if (*curbuf->b_p_gp == NUL)
4789 program = p_gp;
4790 else
4791 program = curbuf->b_p_gp;
4792 }
4793 else
4794 {
4795 if (*curbuf->b_p_mp == NUL)
4796 program = p_mp;
4797 else
4798 program = curbuf->b_p_mp;
4799 }
4800
4801 p = skipwhite(p);
4802
4803 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4804 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004805 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004806 i = 1;
4807 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4808 ++i;
4809 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004810 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004811 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004812 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004813 ptr = new_cmdline;
4814 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4815 {
4816 i = (int)(pos - program);
4817 STRNCPY(ptr, program, i);
4818 STRCPY(ptr += i, p);
4819 ptr += len;
4820 program = pos + 2;
4821 }
4822 STRCPY(ptr, program);
4823 }
4824 else
4825 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004826 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004827 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004828 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004829 STRCPY(new_cmdline, program);
4830 STRCAT(new_cmdline, " ");
4831 STRCAT(new_cmdline, p);
4832 }
4833 msg_make(p);
4834
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004835 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004836 vim_free(*cmdlinep);
4837 *cmdlinep = new_cmdline;
4838 p = new_cmdline;
4839 }
4840 return p;
4841}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004842#endif
4843
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844/*
4845 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004846 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 * Return FAIL for failure, OK otherwise.
4848 */
4849 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004850expand_filename(
4851 exarg_T *eap,
4852 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004853 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004855 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 char_u *repl;
4857 int srclen;
4858 char_u *p;
4859 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004860 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861
Bram Moolenaar748bf032005-02-02 23:04:36 +00004862#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004863 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004864 p = skip_grep_pat(eap);
4865#else
4866 p = eap->arg;
4867#endif
4868
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 /*
4870 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4871 * the file name contains a wildcard it should not cause expanding.
4872 * (it will be expanded anyway if there is a wildcard before replacing).
4873 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004874 has_wildcards = mch_has_wildcard(p);
4875 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004877#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004878 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004879 if (p[0] == '`' && p[1] == '=')
4880 {
4881 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004882 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004883 if (*p == '`')
4884 ++p;
4885 continue;
4886 }
4887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 /*
4889 * Quick check if this cannot be the start of a special string.
4890 * Also removes backslash before '%', '#' and '<'.
4891 */
4892 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4893 {
4894 ++p;
4895 continue;
4896 }
4897
4898 /*
4899 * Try to find a match at this position.
4900 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004901 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4902 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004903 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004905 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
4907 p += srclen;
4908 continue;
4909 }
4910
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004911 // Wildcards won't be expanded below, the replacement is taken
4912 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004913 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4914 {
4915 char_u *l = repl;
4916
4917 repl = expand_env_save(repl);
4918 vim_free(l);
4919 }
4920
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004921 // Need to escape white space et al. with a backslash.
4922 // Don't do this for:
4923 // - replacement that already has been escaped: "##"
4924 // - shell commands (may have to use quotes instead).
4925 // - non-unix systems when there is a single argument (spaces don't
4926 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004928 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 && eap->cmdidx != CMD_grep
4931 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004932 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004933 && eap->cmdidx != CMD_lgrep
4934 && eap->cmdidx != CMD_lgrepadd
4935 && eap->cmdidx != CMD_lmake
4936 && eap->cmdidx != CMD_make
4937 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004939 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940#endif
4941 )
4942 {
4943 char_u *l;
4944#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004945 // Don't escape a backslash here, because rem_backslash() doesn't
4946 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 static char_u *nobslash = (char_u *)" \t\"|";
4948# define ESCAPE_CHARS nobslash
4949#else
4950# define ESCAPE_CHARS escape_chars
4951#endif
4952
4953 for (l = repl; *l; ++l)
4954 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4955 {
4956 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4957 if (l != NULL)
4958 {
4959 vim_free(repl);
4960 repl = l;
4961 }
4962 break;
4963 }
4964 }
4965
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004966 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004967 if ((eap->usefilter || eap->cmdidx == CMD_bang
4968 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004969 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 {
4971 char_u *l;
4972
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004973 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 if (l != NULL)
4975 {
4976 vim_free(repl);
4977 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 }
4979 }
4980
4981 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4982 vim_free(repl);
4983 if (p == NULL)
4984 return FAIL;
4985 }
4986
4987 /*
4988 * One file argument: Expand wildcards.
4989 * Don't do this with ":r !command" or ":w !command".
4990 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004991 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 {
4993 /*
4994 * May do this twice:
4995 * 1. Replace environment variables.
4996 * 2. Replace any other wildcards, remove backslashes.
4997 */
4998 for (n = 1; n <= 2; ++n)
4999 {
5000 if (n == 2)
5001 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 /*
5003 * Halve the number of backslashes (this is Vi compatible).
5004 * For Unix and OS/2, when wildcards are expanded, this is
5005 * done by ExpandOne() below.
5006 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005007#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 if (!has_wildcards)
5009#endif
5010 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 }
5012
5013 if (has_wildcards)
5014 {
5015 if (n == 1)
5016 {
5017 /*
5018 * First loop: May expand environment variables. This
5019 * can be done much faster with expand_env() than with
5020 * something else (e.g., calling a shell).
5021 * After expanding environment variables, check again
5022 * if there are still wildcards present.
5023 */
5024 if (vim_strchr(eap->arg, '$') != NULL
5025 || vim_strchr(eap->arg, '~') != NULL)
5026 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005027 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005028 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 has_wildcards = mch_has_wildcard(NameBuff);
5030 p = NameBuff;
5031 }
5032 else
5033 p = NULL;
5034 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005035 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 {
5037 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02005038 int options = WILD_LIST_NOTFOUND
5039 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040
5041 ExpandInit(&xpc);
5042 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005043 if (p_wic)
5044 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005046 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 if (p == NULL)
5048 return FAIL;
5049 }
5050 if (p != NULL)
5051 {
5052 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5053 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005054 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 vim_free(p);
5056 }
5057 }
5058 }
5059 }
5060 return OK;
5061}
5062
5063/*
5064 * Replace part of the command line, keeping eap->cmd, eap->arg and
5065 * eap->nextcmd correct.
5066 * "src" points to the part that is to be replaced, of length "srclen".
5067 * "repl" is the replacement string.
5068 * Returns a pointer to the character after the replaced string.
5069 * Returns NULL for failure.
5070 */
5071 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005072repl_cmdline(
5073 exarg_T *eap,
5074 char_u *src,
5075 int srclen,
5076 char_u *repl,
5077 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078{
5079 int len;
5080 int i;
5081 char_u *new_cmdline;
5082
5083 /*
5084 * The new command line is build in new_cmdline[].
5085 * First allocate it.
5086 * Careful: a "+cmd" argument may have been NUL terminated.
5087 */
5088 len = (int)STRLEN(repl);
5089 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005090 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005091 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005092 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005093 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094
5095 /*
5096 * Copy the stuff before the expanded part.
5097 * Copy the expanded stuff.
5098 * Copy what came after the expanded part.
5099 * Copy the next commands, if there are any.
5100 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005101 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005103
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005105 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005107 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005109 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
5111 i = (int)STRLEN(new_cmdline) + 1;
5112 STRCPY(new_cmdline + i, eap->nextcmd);
5113 eap->nextcmd = new_cmdline + i;
5114 }
5115 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5116 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5117 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5118 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5119 vim_free(*cmdlinep);
5120 *cmdlinep = new_cmdline;
5121
5122 return src;
5123}
5124
5125/*
5126 * Check for '|' to separate commands and '"' to start comments.
Bram Moolenaarac485062022-03-23 19:45:01 +00005127 * If "keep_backslash" is TRUE do not remove any backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 */
5129 void
Bram Moolenaarac485062022-03-23 19:45:01 +00005130separate_nextcmd(exarg_T *eap, int keep_backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131{
5132 char_u *p;
5133
Bram Moolenaar86b68352004-12-27 21:59:20 +00005134#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005135 p = skip_grep_pat(eap);
5136#else
5137 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005138#endif
5139
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005140 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 {
5142 if (*p == Ctrl_V)
5143 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005144 if ((eap->argt & (EX_CTRLV | EX_XFILE)) || keep_backslash)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005145 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005147 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005148 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005149 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 break;
5151 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005152
5153#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005154 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005155 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005156 {
5157 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005158 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005159 if (*p == NUL) // stop at NUL after CTRL-V
5160 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005161 }
5162#endif
5163
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005164 // Check for '"': start of comment or '|': next command
5165 // :@" and :*" do not start a comment!
5166 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005167 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02005168#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005169 && !in_vim9script()
5170#endif
5171 && !(eap->argt & EX_NOTRLCOM)
5172 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5173 || p != eap->arg)
5174 && (eap->cmdidx != CMD_redir
5175 || p != eap->arg + 1 || p[-1] != '@'))
5176#ifdef FEAT_EVAL
5177 || (*p == '#'
5178 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005179 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005180 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02005181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 || *p == '|' || *p == '\n')
5183 {
5184 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005185 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 * AND 'b' is present in 'cpoptions'.
5187 */
5188 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005189 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 {
Bram Moolenaarac485062022-03-23 19:45:01 +00005191 if (!keep_backslash)
5192 {
5193 STRMOVE(p - 1, p); // remove the '\'
5194 --p;
5195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 }
5197 else
5198 {
5199 eap->nextcmd = check_nextcmd(p);
5200 *p = NUL;
5201 break;
5202 }
5203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005205
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005206 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 del_trailing_spaces(eap->arg);
5208}
5209
5210/*
5211 * get + command from ex argument
5212 */
5213 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005214getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215{
5216 char_u *arg = *argp;
5217 char_u *command = NULL;
5218
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005219 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 {
5221 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005222 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 command = dollar_command;
5224 else
5225 {
5226 command = arg;
5227 arg = skip_cmd_arg(command, TRUE);
5228 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005229 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 }
5231
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005232 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 *argp = arg;
5234 }
5235 return command;
5236}
5237
5238/*
5239 * Find end of "+command" argument. Skip over "\ " and "\\".
5240 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005241 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005242skip_cmd_arg(
5243 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005244 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245{
5246 while (*p && !vim_isspace(*p))
5247 {
5248 if (*p == '\\' && p[1] != NUL)
5249 {
5250 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005251 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 else
5253 ++p;
5254 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005255 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 }
5257 return p;
5258}
5259
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005260 int
5261get_bad_opt(char_u *p, exarg_T *eap)
5262{
5263 if (STRICMP(p, "keep") == 0)
5264 eap->bad_char = BAD_KEEP;
5265 else if (STRICMP(p, "drop") == 0)
5266 eap->bad_char = BAD_DROP;
5267 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5268 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005269 else
5270 return FAIL;
5271 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005272}
5273
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274/*
5275 * Get "++opt=arg" argument.
5276 * Return FAIL or OK.
5277 */
5278 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005279getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280{
5281 char_u *arg = eap->arg + 2;
5282 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005283 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005286 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5288 {
5289 if (*arg == 'n')
5290 {
5291 arg += 2;
5292 eap->force_bin = FORCE_NOBIN;
5293 }
5294 else
5295 eap->force_bin = FORCE_BIN;
5296 if (!checkforcmd(&arg, "binary", 3))
5297 return FAIL;
5298 eap->arg = skipwhite(arg);
5299 return OK;
5300 }
5301
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005302 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005303 if (STRNCMP(arg, "edit", 4) == 0)
5304 {
5305 eap->read_edit = TRUE;
5306 eap->arg = skipwhite(arg + 4);
5307 return OK;
5308 }
5309
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 if (STRNCMP(arg, "ff", 2) == 0)
5311 {
5312 arg += 2;
5313 pp = &eap->force_ff;
5314 }
5315 else if (STRNCMP(arg, "fileformat", 10) == 0)
5316 {
5317 arg += 10;
5318 pp = &eap->force_ff;
5319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 else if (STRNCMP(arg, "enc", 3) == 0)
5321 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005322 if (STRNCMP(arg, "encoding", 8) == 0)
5323 arg += 8;
5324 else
5325 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 pp = &eap->force_enc;
5327 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005328 else if (STRNCMP(arg, "bad", 3) == 0)
5329 {
5330 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005331 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333
5334 if (pp == NULL || *arg != '=')
5335 return FAIL;
5336
5337 ++arg;
5338 *pp = (int)(arg - eap->cmd);
5339 arg = skip_cmd_arg(arg, FALSE);
5340 eap->arg = skipwhite(arg);
5341 *arg = NUL;
5342
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 if (pp == &eap->force_ff)
5344 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5346 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005347 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005349 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005351 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5353 *p = TOLOWER_ASC(*p);
5354 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005355 else
5356 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005357 // Check ++bad= argument. Must be a single-byte character, "keep" or
5358 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005359 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005360 return FAIL;
5361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362
5363 return OK;
5364}
5365
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005367ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368{
5369 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005370 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 * directory for security reasons.
5372 */
5373 if (secure)
5374 {
5375 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005376 eap->errmsg =
5377 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 }
5379 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005380 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 else
5382 do_augroup(eap->arg, eap->forceit);
5383}
5384
5385/*
5386 * ":doautocmd": Apply the automatic commands to the current buffer.
5387 */
5388 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005389ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005391 char_u *arg = eap->arg;
5392 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005393 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005394
Bram Moolenaar1610d052016-06-09 22:53:01 +02005395 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005396 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005397 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005398 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401/*
5402 * :[N]bunload[!] [N] [bufname] unload buffer
5403 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5404 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5405 */
5406 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005407ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005409 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005410 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 eap->errmsg = do_bufdel(
5412 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5413 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5414 : DOBUF_UNLOAD, eap->arg,
5415 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5416}
5417
5418/*
5419 * :[N]buffer [N] to buffer N
5420 * :[N]sbuffer [N] to buffer N
5421 */
5422 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005423ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005425 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005426 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005428 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 else
5430 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005431 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5433 else
5434 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005435 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005436 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 }
5438}
5439
5440/*
5441 * :[N]bmodified [N] to next mod. buffer
5442 * :[N]sbmodified [N] to next mod. buffer
5443 */
5444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005445ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446{
5447 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005448 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005449 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450}
5451
5452/*
5453 * :[N]bnext [N] to next buffer
5454 * :[N]sbnext [N] split and to next buffer
5455 */
5456 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005457ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005459 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005460 return;
5461
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005463 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005464 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465}
5466
5467/*
5468 * :[N]bNext [N] to previous buffer
5469 * :[N]bprevious [N] to previous buffer
5470 * :[N]sbNext [N] split and to previous buffer
5471 * :[N]sbprevious [N] split and to previous buffer
5472 */
5473 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005474ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005476 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005477 return;
5478
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005480 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005481 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482}
5483
5484/*
5485 * :brewind to first buffer
5486 * :bfirst to first buffer
5487 * :sbrewind split and to first buffer
5488 * :sbfirst split and to first buffer
5489 */
5490 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005491ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005493 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005494 return;
5495
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005497 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005498 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499}
5500
5501/*
5502 * :blast to last buffer
5503 * :sblast split and to last buffer
5504 */
5505 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005506ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005508 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005509 return;
5510
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005512 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005513 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515
Bram Moolenaar7a092242020-04-16 22:10:49 +02005516/*
5517 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005518 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005521ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005523 int comment_char = '"';
5524
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005525#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005526 if (in_vim9script())
5527 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005528#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005529 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005530}
5531
5532/*
5533 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5534 * to "cmd_start" or has a white space character before it.
5535 */
5536 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005537ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005538{
5539 int c = *cmd;
5540
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005541 if (c == NUL || c == '|' || c == '\n')
5542 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005543#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005544 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005545 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5546 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005547 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005548#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005549 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550}
5551
5552#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5553 || defined(PROTO)
5554/*
5555 * Return the next command, after the first '|' or '\n'.
5556 * Return NULL if not found.
5557 */
5558 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005559find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560{
5561 while (*p != '|' && *p != '\n')
5562 {
5563 if (*p == NUL)
5564 return NULL;
5565 ++p;
5566 }
5567 return (p + 1);
5568}
5569#endif
5570
5571/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005572 * Check if *p is a separator between Ex commands, skipping over white space.
5573 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 */
5575 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005576check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005578 char_u *s = skipwhite(p);
5579
5580 if (*s == '|' || *s == '\n')
5581 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 else
5583 return NULL;
5584}
5585
5586/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005587 * If "eap->nextcmd" is not set, check for a next command at "p".
5588 */
5589 void
5590set_nextcmd(exarg_T *eap, char_u *arg)
5591{
5592 char_u *p = check_nextcmd(arg);
5593
5594 if (eap->nextcmd == NULL)
5595 eap->nextcmd = p;
5596 else if (p != NULL)
5597 // cannot use "| command" inside a {} block
5598 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5599}
5600
5601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 * - if there are more files to edit
5603 * - and this is the last window
5604 * - and forceit not used
5605 * - and not repeated twice on a row
5606 * return FAIL and give error message if 'message' TRUE
5607 * return OK otherwise
5608 */
5609 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005610check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005611 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005612 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613{
5614 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5615
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005616 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005617 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 {
5619 if (message)
5620 {
5621#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005622 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5623 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005625 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005627 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5628 NGETTEXT("%d more file to edit. Quit anyway?",
5629 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5631 return OK;
5632 return FAIL;
5633 }
5634#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005635 semsg(NGETTEXT(e_nr_more_file_to_edit,
5636 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005637 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 }
5639 return FAIL;
5640 }
5641 return OK;
5642}
5643
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644/*
5645 * Function given to ExpandGeneric() to obtain the list of command names.
5646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005648get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649{
5650 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005651 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 return cmdnames[idx].cmd_name;
5653}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005656ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657{
Bram Moolenaare6850792010-05-14 15:28:44 +02005658 if (*eap->arg == NUL)
5659 {
5660#ifdef FEAT_EVAL
5661 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5662 char_u *p = NULL;
5663
5664 if (expr != NULL)
5665 {
5666 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005667 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005668 --emsg_off;
5669 vim_free(expr);
5670 }
5671 if (p != NULL)
5672 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005673 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005674 vim_free(p);
5675 }
5676 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005677 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005678#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005679 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005680#endif
5681 }
5682 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005683 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005684
5685#ifdef FEAT_VTP
5686 else if (has_vtp_working())
5687 {
5688 // background color change requires clear + redraw
5689 update_screen(CLEAR);
5690 redrawcmd();
5691 }
5692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693}
5694
5695 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005696ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697{
5698 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005699 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 do_highlight(eap->arg, eap->forceit, FALSE);
5701}
5702
5703
5704/*
5705 * Call this function if we thought we were going to exit, but we won't
5706 * (because of an error). May need to restore the terminal mode.
5707 */
5708 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005709not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710{
5711 exiting = FALSE;
5712 settmode(TMODE_RAW);
5713}
5714
Bram Moolenaar3552e742021-05-29 12:21:58 +02005715 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005716before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5717{
5718 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5719
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005720 // Bail out when autocommands closed the window.
5721 // Refuse to quit when the buffer in the last window is being closed (can
5722 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005723 if (!win_valid(wp)
5724 || curbuf_locked()
5725 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5726 return TRUE;
5727
5728 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5729 {
5730 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005731 // Refuse to quit when locked or when the window was closed or the
5732 // buffer in the last window is being closed (can only happen in
5733 // autocommands).
5734 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005735 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5736 return TRUE;
5737 }
5738
5739 return FALSE;
5740}
5741
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005743 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005744 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005745 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005747 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005748ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005750 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005751
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752#ifdef FEAT_CMDWIN
5753 if (cmdwin_type != 0)
5754 {
5755 cmdwin_result = Ctrl_C;
5756 return;
5757 }
5758#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005759 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005760 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005761 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005762 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005763 return;
5764 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005765 if (eap->addr_count > 0)
5766 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005767 int wnr = eap->line2;
5768
5769 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5770 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005771 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005772 }
5773 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005774 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005775
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005776 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005777 if (curbuf_locked())
5778 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005779
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005780 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005781 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005782 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783
5784#ifdef FEAT_NETBEANS_INTG
5785 netbeansForcedQuit = eap->forceit;
5786#endif
5787
5788 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02005789 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 */
5791 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5792 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005793 if ((!buf_hide(wp->w_buffer)
5794 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005795 | (eap->forceit ? CCGD_FORCEIT : 0)
5796 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005798 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 {
5800 not_exiting();
5801 }
5802 else
5803 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005804 // quit last window
5805 // Note: only_one_window() returns true, even so a help window is
5806 // still open. In that case only quit, if no address has been
5807 // specified. Example:
5808 // :h|wincmd w|1q - don't quit
5809 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005810 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005812 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005813#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005815#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005816 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005817 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 }
5819}
5820
5821/*
5822 * ":cquit".
5823 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005825ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005827 // this does not always pass on the exit code to the Manx compiler. why?
5828 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829}
5830
5831/*
5832 * ":qall": try to quit all windows
5833 */
5834 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005835ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836{
5837# ifdef FEAT_CMDWIN
5838 if (cmdwin_type != 0)
5839 {
5840 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005841 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 else
5843 cmdwin_result = K_XF2;
5844 return;
5845 }
5846# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005847
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005848 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005849 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005850 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005851 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005852 return;
5853 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005854
5855 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005856 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005857
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005859 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 getout(0);
5861 not_exiting();
5862}
5863
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864/*
5865 * ":close": close current window, unless it is the last one
5866 */
5867 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005868ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005870 win_T *win;
5871 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005872#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005874 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005876#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005877 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005878 {
5879 if (eap->addr_count == 0)
5880 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005881 else
5882 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005883 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005884 {
5885 winnr++;
5886 if (winnr == eap->line2)
5887 break;
5888 }
5889 if (win == NULL)
5890 win = lastwin;
5891 ex_win_close(eap->forceit, win, NULL);
5892 }
5893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894}
5895
Bram Moolenaar4033c552017-09-16 20:54:51 +02005896#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005897/*
5898 * ":pclose": Close any preview window.
5899 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005901ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005902{
5903 win_T *win;
5904
Bram Moolenaar79648732019-07-18 21:43:07 +02005905 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005906 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005907 if (win->w_p_pvw)
5908 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005909 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005910 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005911 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005912# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005913 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005914 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005915# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005916}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005917#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005918
Bram Moolenaarf740b292006-02-16 22:11:02 +00005919/*
5920 * Close window "win" and take care of handling closing the last window for a
5921 * modified buffer.
5922 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005923 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005924ex_win_close(
5925 int forceit,
5926 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005927 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928{
5929 int need_hide;
5930 buf_T *buf = win->w_buffer;
5931
Bram Moolenaarcf844172020-06-26 19:44:06 +02005932 // Never close the autocommand window.
5933 if (win == aucmd_win)
5934 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00005935 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02005936 return;
5937 }
5938
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005940 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005942#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005943 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005945 bufref_T bufref;
5946
5947 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005949 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 return;
5951 need_hide = FALSE;
5952 }
5953 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005956 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 return;
5958 }
5959 }
5960
Bram Moolenaar4033c552017-09-16 20:54:51 +02005961#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005963#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005964
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005965 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005966 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005967 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005968 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005969 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970}
5971
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005973 * Handle the argument for a tabpage related ex command.
5974 * Returns a tabpage number.
5975 * When an error is encountered then eap->errmsg is set.
5976 */
5977 static int
5978get_tabpage_arg(exarg_T *eap)
5979{
5980 int tab_number;
5981 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5982
5983 if (eap->arg && *eap->arg != NUL)
5984 {
5985 char_u *p = eap->arg;
5986 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005987 int relative = 0; // argument +N/-N means: go to N places to the
5988 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005989
5990 if (*p == '-')
5991 {
5992 relative = -1;
5993 p++;
5994 }
5995 else if (*p == '+')
5996 {
5997 relative = 1;
5998 p++;
5999 }
6000
6001 p_save = p;
6002 tab_number = getdigits(&p);
6003
6004 if (relative == 0)
6005 {
6006 if (STRCMP(p, "$") == 0)
6007 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006008 else if (STRCMP(p, "#") == 0)
6009 if (valid_tabpage(lastused_tabpage))
6010 tab_number = tabpage_index(lastused_tabpage);
6011 else
6012 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006013 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str, eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02006014 tab_number = 0;
6015 goto theend;
6016 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006017 else if (p == p_save || *p_save == '-' || *p != NUL
6018 || tab_number > LAST_TAB_NR)
6019 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006020 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006021 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006022 goto theend;
6023 }
6024 }
6025 else
6026 {
6027 if (*p_save == NUL)
6028 tab_number = 1;
6029 else if (p == p_save || *p_save == '-' || *p != NUL
6030 || tab_number == 0)
6031 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006032 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006033 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006034 goto theend;
6035 }
6036 tab_number = tab_number * relative + tabpage_index(curtab);
6037 if (!unaccept_arg0 && relative == -1)
6038 --tab_number;
6039 }
6040 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006041 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01006042 }
6043 else if (eap->addr_count > 0)
6044 {
6045 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006046 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006047 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01006048 tab_number = 0;
6049 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006050 else
6051 {
6052 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006053 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006054 {
6055 --tab_number;
6056 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006057 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01006058 }
6059 }
6060 }
6061 else
6062 {
6063 switch (eap->cmdidx)
6064 {
6065 case CMD_tabnext:
6066 tab_number = tabpage_index(curtab) + 1;
6067 if (tab_number > LAST_TAB_NR)
6068 tab_number = 1;
6069 break;
6070 case CMD_tabmove:
6071 tab_number = LAST_TAB_NR;
6072 break;
6073 default:
6074 tab_number = tabpage_index(curtab);
6075 }
6076 }
6077
6078theend:
6079 return tab_number;
6080}
6081
6082/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006083 * ":tabclose": close current tab page, unless it is the last one.
6084 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 */
6086 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006087ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006089 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006090 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006091
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006092# ifdef FEAT_CMDWIN
6093 if (cmdwin_type != 0)
6094 cmdwin_result = K_IGNORE;
6095 else
6096# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006097 if (first_tabpage->tp_next == NULL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00006098 emsg(_(e_cannot_close_last_tab_page));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006099 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006101 tab_number = get_tabpage_arg(eap);
6102 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006103 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006104 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006105 if (tp == NULL)
6106 {
6107 beep_flush();
6108 return;
6109 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006110 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006111 {
6112 tabpage_close_other(tp, eap->forceit);
6113 return;
6114 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006115 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006116 tabpage_close(eap->forceit);
6117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006119}
6120
6121/*
6122 * ":tabonly": close all tab pages except the current one
6123 */
6124 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006125ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006126{
6127 tabpage_T *tp;
6128 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006129 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006130
6131# ifdef FEAT_CMDWIN
6132 if (cmdwin_type != 0)
6133 cmdwin_result = K_IGNORE;
6134 else
6135# endif
6136 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006137 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006138 else
6139 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006140 tab_number = get_tabpage_arg(eap);
6141 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006142 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006143 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006144 // Repeat this up to a 1000 times, because autocommands may
6145 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006146 for (done = 0; done < 1000; ++done)
6147 {
6148 FOR_ALL_TABPAGES(tp)
6149 if (tp->tp_topframe != topframe)
6150 {
6151 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006152 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006153 if (valid_tabpage(tp))
6154 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006155 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006156 break;
6157 }
6158 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006159 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006160 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006161 }
6162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164
6165/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006166 * Close the current tab page.
6167 */
6168 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006169tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006170{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006171 // First close all the windows but the current one. If that worked then
6172 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006173 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006174 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006175 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006176 ex_win_close(forceit, curwin, NULL);
6177# ifdef FEAT_GUI
6178 need_mouse_correct = TRUE;
6179# endif
6180}
6181
6182/*
6183 * Close tab page "tp", which is not the current tab page.
6184 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006185 * Also takes care of the tab pages line disappearing when closing the
6186 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006187 */
6188 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006189tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006190{
6191 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006192 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006193 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006194
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006195 // Limit to 1000 windows, autocommands may add a window while we close
6196 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006197 while (++done < 1000)
6198 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006199 wp = tp->tp_firstwin;
6200 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006201
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006202 // Autocommands may delete the tab page under our fingers and we may
6203 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006204 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006205 break;
6206 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006207
Bram Moolenaar29323592016-07-24 22:04:11 +02006208 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006209
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006210 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006211 if (h != tabline_height())
6212 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006213}
6214
6215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 * ":only".
6217 */
6218 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006219ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006221 win_T *wp;
6222 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223# ifdef FEAT_GUI
6224 need_mouse_correct = TRUE;
6225# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006226 if (eap->addr_count > 0)
6227 {
6228 wnr = eap->line2;
6229 for (wp = firstwin; --wnr > 0; )
6230 {
6231 if (wp->w_next == NULL)
6232 break;
6233 else
6234 wp = wp->w_next;
6235 }
6236 win_goto(wp);
6237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 close_others(TRUE, eap->forceit);
6239}
6240
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006242ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006244 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01006245 if (!eap->skip)
6246 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006247#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006248 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006249#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006250 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006251 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01006252 else
6253 {
6254 int winnr = 0;
6255 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006256
Bram Moolenaar2256c992016-11-15 21:17:07 +01006257 FOR_ALL_WINDOWS(win)
6258 {
6259 winnr++;
6260 if (winnr == eap->line2)
6261 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006262 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006263 if (win == NULL)
6264 win = lastwin;
6265 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 }
6268}
6269
6270/*
6271 * ":stop" and ":suspend": Suspend Vim.
6272 */
dbivolaruab16ad32021-12-29 19:41:47 +00006273 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006274ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275{
6276 /*
6277 * Disallow suspending for "rvim".
6278 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006279 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 {
6281 if (!eap->forceit)
6282 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01006283 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 windgoto((int)Rows - 1, 0);
6285 out_char('\n');
6286 out_flush();
6287 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006288 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006289 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006290 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006292 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006294 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006296 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006297 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 }
6299}
6300
6301/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006302 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 */
6304 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006305ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006307#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006308 if (not_in_vim9(eap) == FAIL)
6309 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311#ifdef FEAT_CMDWIN
6312 if (cmdwin_type != 0)
6313 {
6314 cmdwin_result = Ctrl_C;
6315 return;
6316 }
6317#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006318 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006319 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006320 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006321 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006322 return;
6323 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006324
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006326 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 */
6328 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6329 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006330
6331 // Write the buffer for ":wq" or when it was changed.
6332 // Trigger QuitPre and ExitPre.
6333 // Check if we can exit now, after autocommands have changed things.
6334 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6335 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006337 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 {
6339 not_exiting();
6340 }
6341 else
6342 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006343 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006345 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346# ifdef FEAT_GUI
6347 need_mouse_correct = TRUE;
6348# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006349 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006350 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 }
6352}
6353
6354/*
6355 * ":print", ":list", ":number".
6356 */
6357 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006358ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006360 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006361 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006362 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006364 for ( ;!got_int; ui_breakcheck())
6365 {
6366 print_line(eap->line1,
6367 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6368 || (eap->flags & EXFLAG_NR)),
6369 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6370 if (++eap->line1 > eap->line2)
6371 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006372 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006373 }
6374 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006375 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006376 curwin->w_cursor.lnum = eap->line2;
6377 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 }
6379
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381}
6382
6383#ifdef FEAT_BYTEOFF
6384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006385ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386{
6387 goto_byte(eap->line2);
6388}
6389#endif
6390
6391/*
6392 * ":shell".
6393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006395ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396{
6397 do_shell(NULL, 0);
6398}
6399
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006400#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006402static int drop_busy = FALSE;
6403static int drop_filec;
6404static char_u **drop_filev = NULL;
6405static int drop_split;
6406static void (*drop_callback)(void *);
6407static void *drop_cookie;
6408
6409 static void
6410handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411{
6412 exarg_T ea;
6413 int save_msg_scroll = msg_scroll;
6414
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006415 // Setting the argument list may cause screen updates and being called
6416 // recursively. Avoid that by setting drop_busy.
6417 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006419 // Check whether the current buffer is changed. If so, we will need
6420 // to split the current window or data could be lost.
6421 // We don't need to check if the 'hidden' option is set, as in this
6422 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006423 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 {
6425 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006426 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 --emsg_off;
6428 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006429 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 if (win_split(0, 0) == FAIL)
6432 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006433 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006435 // When splitting the window, create a new alist. Otherwise the
6436 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 alist_unlink(curwin->w_alist);
6438 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 }
6440
6441 /*
6442 * Set up the new argument list.
6443 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006444 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445
6446 /*
6447 * Move to the first file.
6448 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006449 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006450 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 ea.cmd = (char_u *)"next";
6452 do_argfile(&ea, 0);
6453
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006454 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6455 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 need_start_insertmode = FALSE;
6457
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006458 // Restore msg_scroll, otherwise a following command may cause scrolling
6459 // unexpectedly. The screen will be redrawn by the caller, thus
6460 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006462
6463 if (drop_callback != NULL)
6464 drop_callback(drop_cookie);
6465
6466 drop_filev = NULL;
6467 drop_busy = FALSE;
6468}
6469
6470/*
6471 * Handle a file drop. The code is here because a drop is *nearly* like an
6472 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006473 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006474 * code is very similar to :args and hence needs access to a lot of the static
6475 * functions in this file.
6476 *
6477 * The "filev" list must have been allocated using alloc(), as should each item
6478 * in the list. This function takes over responsibility for freeing the "filev"
6479 * list.
6480 */
6481 void
6482handle_drop(
6483 int filec, // the number of files dropped
6484 char_u **filev, // the list of files dropped
6485 int split, // force splitting the window
6486 void (*callback)(void *), // to be called after setting the argument
6487 // list
6488 void *cookie) // argument for "callback" (allocated)
6489{
6490 // Cannot handle recursive drops, finish the pending one.
6491 if (drop_busy)
6492 {
6493 FreeWild(filec, filev);
6494 vim_free(cookie);
6495 return;
6496 }
6497
6498 // When calling handle_drop() more than once in a row we only use the last
6499 // one.
6500 if (drop_filev != NULL)
6501 {
6502 FreeWild(drop_filec, drop_filev);
6503 vim_free(drop_cookie);
6504 }
6505
6506 drop_filec = filec;
6507 drop_filev = filev;
6508 drop_split = split;
6509 drop_callback = callback;
6510 drop_cookie = cookie;
6511
6512 // Postpone this when:
6513 // - editing the command line
6514 // - not possible to change the current buffer
6515 // - updating the screen
6516 // As it may change buffers and window structures that are in use and cause
6517 // freed memory to be used.
6518 if (text_locked() || curbuf_locked() || updating_screen)
6519 return;
6520
6521 handle_drop_internal();
6522}
6523
6524/*
6525 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6526 * reset: Handle a postponed drop.
6527 */
6528 void
6529handle_any_postponed_drop(void)
6530{
6531 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006532 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006533 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534}
6535#endif
6536
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 * ":preserve".
6539 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006541ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006543 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 ml_preserve(curbuf, TRUE);
6545}
6546
6547/*
6548 * ":recover".
6549 */
6550 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006551ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006553 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006555 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6556 | CCGD_MULTWIN
6557 | (eap->forceit ? CCGD_FORCEIT : 0)
6558 | CCGD_EXCMD)
6559
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 && (*eap->arg == NUL
6561 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006562 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 recoverymode = FALSE;
6564}
6565
6566/*
6567 * Command modifier used in a wrong way.
6568 */
6569 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006570ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006572 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573}
6574
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575/*
6576 * :sview [+command] file split window with new file, read-only
6577 * :split [[+command] file] split window with current or new file
6578 * :vsplit [[+command] file] split window vertically with current or new file
6579 * :new [[+command] file] split window with no or new file
6580 * :vnew [[+command] file] split vertically window with no or new file
6581 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006582 *
6583 * :tabedit open new Tab page with empty window
6584 * :tabedit [+command] file open new Tab page and edit "file"
6585 * :tabnew [[+command] file] just like :tabedit
6586 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 */
6588 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006589ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006591 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006592#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006594#endif
6595#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006596 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006597 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006598#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006599 int use_tab = eap->cmdidx == CMD_tabedit
6600 || eap->cmdidx == CMD_tabfind
6601 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006603 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006604 return;
6605
Bram Moolenaar4033c552017-09-16 20:54:51 +02006606#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609
Bram Moolenaar4033c552017-09-16 20:54:51 +02006610#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006611 // A ":split" in the quickfix window works like ":new". Don't want two
6612 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006613 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 {
6615 if (eap->cmdidx == CMD_split)
6616 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 if (eap->cmdidx == CMD_vsplit)
6618 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621
Bram Moolenaar4033c552017-09-16 20:54:51 +02006622#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006623 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 {
6625 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6626 FNAME_MESS, TRUE, curbuf->b_ffname);
6627 if (fname == NULL)
6628 goto theend;
6629 eap->arg = fname;
6630 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006631# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006632 else
6633# endif
6634#endif
6635#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006636 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 && eap->cmdidx != CMD_new)
6639 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006640 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006641# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006642 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006643# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006644 au_has_group((char_u *)"FileExplorer"))
6645 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006646 // No browsing supported but we do have the file explorer:
6647 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006648 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006649 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006650 }
6651 else
6652 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006653 fname = do_browse(0, (char_u *)(use_tab
6654 ? _("Edit File in new tab page")
6655 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006657 if (fname == NULL)
6658 goto theend;
6659 eap->arg = fname;
6660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006662 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006665 /*
6666 * Either open new tab page or split the window.
6667 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006668 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006669 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006670 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006671 : eap->addr_count == 0 ? 0
6672 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006673 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006674 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006675
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006676 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006677 if (curwin != old_curwin
6678 && win_valid(old_curwin)
6679 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006680 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006681 old_curwin->w_alt_fnum = curbuf->b_fnum;
6682 }
6683 }
6684 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6686 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006687 // Reset 'scrollbind' when editing another file, but keep it when
6688 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006689 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006690 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 else
6692 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 do_exedit(eap, old_curwin);
6694 }
6695
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006696# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006697 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006698# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006700# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701theend:
6702 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006703# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006705
6706/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006707 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006708 */
6709 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006710tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006711{
6712 exarg_T ea;
6713
Bram Moolenaara80faa82020-04-12 19:37:17 +02006714 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006715 ea.cmdidx = CMD_tabnew;
6716 ea.cmd = (char_u *)"tabn";
6717 ea.arg = (char_u *)"";
6718 ex_splitview(&ea);
6719}
6720
6721/*
6722 * :tabnext command
6723 */
6724 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006725ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006726{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006727 int tab_number;
6728
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006729 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006730 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006731 switch (eap->cmdidx)
6732 {
6733 case CMD_tabfirst:
6734 case CMD_tabrewind:
6735 goto_tabpage(1);
6736 break;
6737 case CMD_tablast:
6738 goto_tabpage(9999);
6739 break;
6740 case CMD_tabprevious:
6741 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006742 if (eap->arg && *eap->arg != NUL)
6743 {
6744 char_u *p = eap->arg;
6745 char_u *p_save = p;
6746
6747 tab_number = getdigits(&p);
6748 if (p == p_save || *p_save == '-' || *p != NUL
6749 || tab_number == 0)
6750 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006751 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006752 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006753 return;
6754 }
6755 }
6756 else
6757 {
6758 if (eap->addr_count == 0)
6759 tab_number = 1;
6760 else
6761 {
6762 tab_number = eap->line2;
6763 if (tab_number < 1)
6764 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006765 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006766 return;
6767 }
6768 }
6769 }
6770 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006771 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006772 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006773 tab_number = get_tabpage_arg(eap);
6774 if (eap->errmsg == NULL)
6775 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006776 break;
6777 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006778}
6779
6780/*
6781 * :tabmove command
6782 */
6783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006784ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006785{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006786 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006787
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006788 tab_number = get_tabpage_arg(eap);
6789 if (eap->errmsg == NULL)
6790 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006791}
6792
6793/*
6794 * :tabs command: List tabs and their contents.
6795 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006796 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006797ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006798{
6799 tabpage_T *tp;
6800 win_T *wp;
6801 int tabcount = 1;
6802
6803 msg_start();
6804 msg_scroll = TRUE;
6805 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6806 {
6807 msg_putchar('\n');
6808 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006809 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006810 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006811 ui_breakcheck();
6812
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006813 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006814 wp = firstwin;
6815 else
6816 wp = tp->tp_firstwin;
6817 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6818 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006819 msg_putchar('\n');
6820 msg_putchar(wp == curwin ? '>' : ' ');
6821 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006822 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6823 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006824 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006825 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006826 else
6827 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6828 IObuff, IOSIZE, TRUE);
6829 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006830 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006831 ui_breakcheck();
6832 }
6833 }
6834}
6835
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836/*
6837 * ":mode": Set screen mode.
6838 * If no argument given, just get the screen size and redraw.
6839 */
6840 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006841ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842{
6843 if (*eap->arg == NUL)
6844 shell_resized();
6845 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00006846 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847}
6848
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849/*
6850 * ":resize".
6851 * set, increment or decrement current window height
6852 */
6853 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006854ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855{
6856 int n;
6857 win_T *wp = curwin;
6858
6859 if (eap->addr_count > 0)
6860 {
6861 n = eap->line2;
6862 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6863 ;
6864 }
6865
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006866# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006868# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006870 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 {
6872 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006873 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006874 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00006876 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 }
6878 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 {
6880 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006881 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006882 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00006884 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 }
6886}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887
6888/*
6889 * ":find [+command] <file>" command.
6890 */
6891 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006892ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893{
6894#ifdef FEAT_SEARCHPATH
6895 char_u *fname;
6896 int count;
6897
6898 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6899 TRUE, curbuf->b_ffname);
6900 if (eap->addr_count > 0)
6901 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006902 // Repeat finding the file "count" times. This matters when it
6903 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 count = eap->line2;
6905 while (fname != NULL && --count > 0)
6906 {
6907 vim_free(fname);
6908 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6909 FALSE, curbuf->b_ffname);
6910 }
6911 }
6912
6913 if (fname != NULL)
6914 {
6915 eap->arg = fname;
6916#endif
6917 do_exedit(eap, NULL);
6918#ifdef FEAT_SEARCHPATH
6919 vim_free(fname);
6920 }
6921#endif
6922}
6923
6924/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006925 * ":open" simulation: for now just work like ":visual".
6926 */
6927 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006928ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006929{
6930 regmatch_T regmatch;
6931 char_u *p;
6932
Bram Moolenaar65088802021-03-13 21:07:21 +01006933#ifdef FEAT_EVAL
6934 if (not_in_vim9(eap) == FAIL)
6935 return;
6936#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006937 curwin->w_cursor.lnum = eap->line2;
6938 beginline(BL_SOL | BL_FIX);
6939 if (*eap->arg == '/')
6940 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006941 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006942 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006943 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00006944 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006945 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006946 if (regmatch.regprog != NULL)
6947 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00006948 // make a copy of the line, when searching for a mark it might be
6949 // flushed
6950 char_u *line = vim_strsave(ml_get_curline());
6951
Bram Moolenaardf177f62005-02-22 08:39:57 +00006952 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00006953 if (vim_regexec(&regmatch, line, (colnr_T)0))
6954 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006955 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006956 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02006957 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00006958 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006959 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006960 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006961 eap->arg += STRLEN(eap->arg);
6962 }
6963 check_cursor();
6964
6965 eap->cmdidx = CMD_visual;
6966 do_exedit(eap, NULL);
6967}
6968
6969/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006970 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 */
6972 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006973ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974{
6975 do_exedit(eap, NULL);
6976}
6977
6978/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006979 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006982do_exedit(
6983 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006984 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985{
6986 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006988 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006990 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6991 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006992 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 /*
6994 * ":vi" command ends Ex mode.
6995 */
6996 if (exmode_active && (eap->cmdidx == CMD_visual
6997 || eap->cmdidx == CMD_view))
6998 {
6999 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02007000 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007002 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007003 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00007004 if (global_busy)
7005 {
7006 int rd = RedrawingDisabled;
7007 int nwr = no_wait_return;
7008 int ms = msg_scroll;
7009#ifdef FEAT_GUI
7010 int he = hold_gui_events;
7011#endif
7012
7013 if (eap->nextcmd != NULL)
7014 {
7015 stuffReadbuff(eap->nextcmd);
7016 eap->nextcmd = NULL;
7017 }
7018
7019 if (exmode_was != EXMODE_VIM)
7020 settmode(TMODE_RAW);
7021 RedrawingDisabled = 0;
7022 no_wait_return = 0;
7023 need_wait_return = FALSE;
7024 msg_scroll = 0;
7025#ifdef FEAT_GUI
7026 hold_gui_events = 0;
7027#endif
7028 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007029 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007030
7031 main_loop(FALSE, TRUE);
7032
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01007033 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007034 RedrawingDisabled = rd;
7035 no_wait_return = nwr;
7036 msg_scroll = ms;
7037#ifdef FEAT_GUI
7038 hold_gui_events = he;
7039#endif
7040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 }
7044
7045 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007046 || eap->cmdidx == CMD_tabnew
7047 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007048 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007050 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 setpcmark();
7052 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007053 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7054 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007056 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 || *eap->arg != NUL
7058#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007059 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060#endif
7061 )
7062 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007063 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
7064 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007065 if (*eap->arg != NUL && curbuf_locked())
7066 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007067
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 n = readonlymode;
7069 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7070 readonlymode = TRUE;
7071 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007072 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7073 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007074 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7075 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7077 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007078 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7080 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7081 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007082 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007084 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007085 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007086 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7087 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007088 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007090 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 if (old_curwin != NULL)
7092 {
7093 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007094 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007096#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007097 cleanup_T cs;
7098
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007099 // Reset the error/interrupt/exception state here so that
7100 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007101 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007102#endif
7103#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007105#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007106 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007107
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007108#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007109 // Restore the error/interrupt/exception state if not
7110 // discarded by a new aborting error, interrupt, or
7111 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007112 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 }
7115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 }
7117 else if (readonlymode && curbuf->b_nwindows == 1)
7118 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007119 // When editing an already visited buffer, 'readonly' won't be set
7120 // but the previous value is kept. With ":view" and ":sview" we
7121 // want the file to be readonly, except when another window is
7122 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 curbuf->b_p_ro = TRUE;
7124 }
7125 readonlymode = n;
7126 }
7127 else
7128 {
7129 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007130 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 if (n != curwin->w_arg_idx_invalid)
7134 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 }
7136
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 /*
7138 * if ":split file" worked, set alternate file name in old window to new
7139 * file
7140 */
7141 if (old_curwin != NULL
7142 && *eap->arg != NUL
7143 && curwin != old_curwin
7144 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007145 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007146 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148
7149 ex_no_reprint = TRUE;
7150}
7151
7152#ifndef FEAT_GUI
7153/*
7154 * ":gui" and ":gvim" when there is no GUI.
7155 */
7156 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007157ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007159 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160}
7161#endif
7162
Bram Moolenaar4f974752019-02-17 17:44:42 +01007163#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007165ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166{
7167 gui_make_tearoff(eap->arg);
7168}
7169#endif
7170
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007171#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7172 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007174ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007176# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7177 if (gui.in_use)
7178 gui_make_popup(eap->arg, eap->forceit);
7179# ifdef FEAT_TERM_POPUP_MENU
7180 else
7181# endif
7182# endif
7183# ifdef FEAT_TERM_POPUP_MENU
7184 pum_make_popup(eap->arg, eap->forceit);
7185# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186}
7187#endif
7188
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007190ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191{
7192 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007193 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007195 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196}
7197
7198/*
7199 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7200 * offset.
7201 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007204ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007207 win_T *save_curwin = curwin;
7208 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 long topline;
7210 long y;
7211 linenr_T old_linenr = curwin->w_cursor.lnum;
7212
7213 setpcmark();
7214
7215 /*
7216 * determine max topline
7217 */
7218 if (curwin->w_p_scb)
7219 {
7220 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007221 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 {
7223 if (wp->w_p_scb && wp->w_buffer)
7224 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007225 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 if (topline > y)
7227 topline = y;
7228 }
7229 }
7230 if (topline < 1)
7231 topline = 1;
7232 }
7233 else
7234 {
7235 topline = 1;
7236 }
7237
7238
7239 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007240 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007242 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 {
7244 if (curwin->w_p_scb)
7245 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007246 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 y = topline - curwin->w_topline;
7248 if (y > 0)
7249 scrollup(y, TRUE);
7250 else
7251 scrolldown(-y, TRUE);
7252 curwin->w_scbind_pos = topline;
7253 redraw_later(VALID);
7254 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 }
7257 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007258 curwin = save_curwin;
7259 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 if (curwin->w_p_scb)
7261 {
7262 did_syncbind = TRUE;
7263 checkpcmark();
7264 if (old_linenr != curwin->w_cursor.lnum)
7265 {
7266 char_u ctrl_o[2];
7267
7268 ctrl_o[0] = Ctrl_O;
7269 ctrl_o[1] = 0;
7270 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7271 }
7272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273}
7274
7275
7276 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007277ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007279 int i;
7280 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7281 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007283 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 do_bang(1, eap, FALSE, FALSE, TRUE);
7285 else
7286 {
7287 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7288 return;
7289
7290#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007291 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 {
7293 char_u *browseFile;
7294
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007295 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 NULL, NULL, NULL, curbuf);
7297 if (browseFile != NULL)
7298 {
7299 i = readfile(browseFile, NULL,
7300 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7301 vim_free(browseFile);
7302 }
7303 else
7304 i = OK;
7305 }
7306 else
7307#endif
7308 if (*eap->arg == NUL)
7309 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007310 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 return;
7312 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7313 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7314 }
7315 else
7316 {
7317 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7318 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7319 i = readfile(eap->arg, NULL,
7320 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7321
7322 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007323 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007325#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 if (!aborting())
7327#endif
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007328 semsg(_(e_cant_open_file_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 }
7330 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007331 {
7332 if (empty && exmode_active)
7333 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007334 // Delete the empty line that remains. Historically ex does
7335 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007336 if (eap->line2 == 0)
7337 lnum = curbuf->b_ml.ml_line_count;
7338 else
7339 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007340 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007341 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007342 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007343 if (curwin->w_cursor.lnum > 1
7344 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007345 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007346 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007347 }
7348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 }
7352}
7353
Bram Moolenaarea408852005-06-25 22:49:46 +00007354static char_u *prev_dir = NULL;
7355
7356#if defined(EXITFREE) || defined(PROTO)
7357 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007358free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007359{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007360 VIM_CLEAR(prev_dir);
7361 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007362}
7363#endif
7364
Bram Moolenaarf4258302013-06-02 18:20:17 +02007365/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007366 * Get the previous directory for the given chdir scope.
7367 */
7368 static char_u *
7369get_prevdir(cdscope_T scope)
7370{
7371 if (scope == CDSCOPE_WINDOW)
7372 return curwin->w_prevdir;
7373 else if (scope == CDSCOPE_TABPAGE)
7374 return curtab->tp_prevdir;
7375 return prev_dir;
7376}
7377
7378/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007379 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007380 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7381 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007382 */
7383 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007384post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007385{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007386 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007387 // Clear tab local directory for both :cd and :tcd
7388 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007389 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007390 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007391 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007392 char_u *pdir = get_prevdir(scope);
7393
7394 // If still in the global directory, need to remember current
7395 // directory as the global directory.
7396 if (globaldir == NULL && pdir != NULL)
7397 globaldir = vim_strsave(pdir);
7398
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007399 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007400 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007401 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007402 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007403 curtab->tp_localdir = vim_strsave(NameBuff);
7404 else
7405 curwin->w_localdir = vim_strsave(NameBuff);
7406 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007407 }
7408 else
7409 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007410 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007411 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007412 }
7413
zeertzjq64be6aa2021-11-19 11:59:08 +00007414 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007415 shorten_fnames(TRUE);
7416}
7417
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007418/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007419 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
7420 */
7421 void
7422trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
7423{
7424#ifdef FEAT_EVAL
7425 dict_T *v_event;
7426 save_v_event_T save_v_event;
7427
7428 v_event = get_v_event(&save_v_event);
7429 (void)dict_add_string(v_event, "directory", new_dir);
7430 dict_set_items_ro(v_event);
7431#endif
7432 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
7433#ifdef FEAT_EVAL
7434 restore_v_event(v_event, &save_v_event);
7435#endif
7436}
7437
7438/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007439 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007440 * chdir() function.
7441 * scope == CDSCOPE_WINDOW: changes the window-local directory
7442 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7443 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007444 * Returns TRUE if the directory is successfully changed.
7445 */
7446 int
7447changedir_func(
7448 char_u *new_dir,
7449 int forceit,
7450 cdscope_T scope)
7451{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007452 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007453 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007454 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00007455 char_u **pp;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007456
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007457 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007458 return FALSE;
7459
7460 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7461 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007462 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007463 return FALSE;
7464 }
7465
7466 // ":cd -": Change to previous directory
7467 if (STRCMP(new_dir, "-") == 0)
7468 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007469 pdir = get_prevdir(scope);
7470 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007471 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007472 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007473 return FALSE;
7474 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007475 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007476 }
7477
7478 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007479 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007480 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007481 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007482 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007483
Bakudankun29f3a452021-12-11 12:28:08 +00007484 // For UNIX ":cd" means: go to home directory.
7485 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007486#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007487 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007488#else
7489 if (*new_dir == NUL && p_cdh)
7490#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007491 {
7492 // use NameBuff for home directory name
7493# ifdef VMS
7494 char_u *p;
7495
7496 p = mch_getenv((char_u *)"SYS$LOGIN");
7497 if (p == NULL || *p == NUL) // empty is the same as not set
7498 NameBuff[0] = NUL;
7499 else
7500 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7501# else
7502 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7503# endif
7504 new_dir = NameBuff;
7505 }
zeertzjqf38aad82021-12-30 13:45:57 +00007506 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007507 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
7508 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00007509 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007510 if (scope == CDSCOPE_WINDOW)
7511 acmd_fname = (char_u *)"window";
7512 else if (scope == CDSCOPE_TABPAGE)
7513 acmd_fname = (char_u *)"tabpage";
7514 else
7515 acmd_fname = (char_u *)"global";
7516 trigger_DirChangedPre(acmd_fname, new_dir);
7517
7518 if (vim_chdir(new_dir))
7519 {
7520 emsg(_(e_command_failed));
7521 vim_free(pdir);
7522 return FALSE;
7523 }
Richard Doty3d0abad2021-12-29 14:39:08 +00007524 }
zeertzjq73257142022-02-02 13:16:37 +00007525
7526 if (scope == CDSCOPE_WINDOW)
7527 pp = &curwin->w_prevdir;
7528 else if (scope == CDSCOPE_TABPAGE)
7529 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007530 else
zeertzjq73257142022-02-02 13:16:37 +00007531 pp = &prev_dir;
7532 vim_free(*pp);
7533 *pp = pdir;
7534
7535 post_chdir(scope);
7536
7537 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007538 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
zeertzjq73257142022-02-02 13:16:37 +00007539 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007540}
Bram Moolenaarea408852005-06-25 22:49:46 +00007541
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007543 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007545 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007546ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007548 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549
7550 new_dir = eap->arg;
7551#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007552 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7553 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 ex_pwd(NULL);
7555 else
7556#endif
7557 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007558 cdscope_T scope = CDSCOPE_GLOBAL;
7559
7560 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7561 scope = CDSCOPE_WINDOW;
7562 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7563 scope = CDSCOPE_TABPAGE;
7564
7565 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007566 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007567 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007568 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 ex_pwd(eap);
7570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 }
7572}
7573
7574/*
7575 * ":pwd".
7576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007578ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579{
7580 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7581 {
7582#ifdef BACKSLASH_IN_FILENAME
7583 slash_adjust(NameBuff);
7584#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007585 if (p_verbose > 0)
7586 {
7587 char *context = "global";
7588
Bram Moolenaar05268152021-11-18 18:53:45 +00007589 if (last_chdir_reason != NULL)
7590 context = last_chdir_reason;
7591 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007592 context = "window";
7593 else if (curtab->tp_localdir != NULL)
7594 context = "tabpage";
7595 smsg("[%s] %s", context, (char *)NameBuff);
7596 }
7597 else
7598 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 }
7600 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00007601 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602}
7603
7604/*
7605 * ":=".
7606 */
7607 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007608ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007610 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007611 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612}
7613
7614 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007615ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007617 int n;
7618 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619
7620 if (cursor_valid())
7621 {
7622 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7623 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007624 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007626
7627 len = eap->line2;
7628 switch (*eap->arg)
7629 {
7630 case 'm': break;
7631 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007632 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007633 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007634
7635 // Hide the cursor if invoked with !
7636 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637}
7638
7639/*
7640 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007641 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 */
7643 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007644do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645{
Bram Moolenaar52953192019-08-16 10:27:13 +02007646 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007647 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007648# ifdef ELAPSED_FUNC
7649 elapsed_T start_tv;
7650
7651 // Remember at what time we started, so that we know how much longer we
7652 // should wait after waiting for a bit.
7653 ELAPSED_INIT(start_tv);
7654# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007656 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007657 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007658 else
Richard Doty3d0abad2021-12-29 14:39:08 +00007659 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007660
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007661 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007662 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007664 wait_now = msec - done > 1000L ? 1000L : msec - done;
7665#ifdef FEAT_TIMERS
7666 {
7667 long due_time = check_due_timer();
7668
7669 if (due_time > 0 && due_time < wait_now)
7670 wait_now = due_time;
7671 }
7672#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007673#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007674 if (has_any_channel() && wait_now > 20L)
7675 wait_now = 20L;
7676#endif
7677#ifdef FEAT_SOUND
7678 if (has_any_sound_callback() && wait_now > 20L)
7679 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007680#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007681 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007682
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007683#ifdef FEAT_JOB_CHANNEL
7684 if (has_any_channel())
7685 ui_breakcheck_force(TRUE);
7686 else
7687#endif
7688 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007689#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007690 // Process the netbeans and clientserver messages that may have been
7691 // received in the call to ui_breakcheck() when the GUI is in use. This
7692 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007693 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007694#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007695
7696# ifdef ELAPSED_FUNC
7697 // actual time passed
7698 done = ELAPSED_FUNC(start_tv);
7699# else
7700 // guestimate time passed (will actually be more)
7701 done += wait_now;
7702# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007704
7705 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7706 // input buffer, otherwise a following call to input() fails.
7707 if (got_int)
7708 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007709
7710 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007711 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712}
7713
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714/*
7715 * ":winsize" command (obsolete).
7716 */
7717 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007718ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719{
7720 int w, h;
7721 char_u *arg = eap->arg;
7722 char_u *p;
7723
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007724 if (!isdigit(*arg))
7725 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007726 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007727 return;
7728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 w = getdigits(&arg);
7730 arg = skipwhite(arg);
7731 p = arg;
7732 h = getdigits(&arg);
7733 if (*p != NUL && *arg == NUL)
7734 set_shellsize(w, h, TRUE);
7735 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007736 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737}
7738
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007740ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741{
7742 int xchar = NUL;
7743 char_u *p;
7744
7745 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7746 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007747 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 if (eap->arg[1] == NUL)
7749 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007750 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 return;
7752 }
7753 xchar = eap->arg[1];
7754 p = eap->arg + 2;
7755 }
7756 else
7757 p = eap->arg + 1;
7758
Bram Moolenaar63b91732021-08-05 20:40:03 +02007759 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007761 if (*p != NUL && *p != (
7762#ifdef FEAT_EVAL
7763 in_vim9script() ? '#' :
7764#endif
7765 '"')
7766 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007767 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007768 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007770 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007771 postponed_split_flags = cmdmod.cmod_split;
7772 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7774 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007775 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 }
7777}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778
Bram Moolenaar843ee412004-06-30 16:16:41 +00007779#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780/*
7781 * ":winpos".
7782 */
7783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007784ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785{
7786 int x, y;
7787 char_u *arg = eap->arg;
7788 char_u *p;
7789
7790 if (*arg == NUL)
7791 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007792# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007793# ifdef VIMDLL
7794 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7795 mch_get_winpos(&x, &y) != FAIL)
7796# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007798# else
7799 if (mch_get_winpos(&x, &y) != FAIL)
7800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 {
7802 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007803 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 }
7805 else
7806# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00007807 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 }
7809 else
7810 {
7811 x = getdigits(&arg);
7812 arg = skipwhite(arg);
7813 p = arg;
7814 y = getdigits(&arg);
7815 if (*p == NUL || *arg != NUL)
7816 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007817 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 return;
7819 }
7820# ifdef FEAT_GUI
7821 if (gui.in_use)
7822 gui_mch_set_winpos(x, y);
7823 else if (gui.starting)
7824 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007825 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 gui_win_x = x;
7827 gui_win_y = y;
7828 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007829# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 else
7831# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007832# endif
7833# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007834 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835# endif
7836# ifdef HAVE_TGETENT
7837 if (*T_CWP)
7838 term_set_winpos(x, y);
7839# endif
7840 }
7841}
7842#endif
7843
7844/*
7845 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7846 */
7847 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007848ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849{
7850 oparg_T oa;
7851
7852 clear_oparg(&oa);
7853 oa.regname = eap->regname;
7854 oa.start.lnum = eap->line1;
7855 oa.end.lnum = eap->line2;
7856 oa.line_count = eap->line2 - eap->line1 + 1;
7857 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007859 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 {
7861 setpcmark();
7862 curwin->w_cursor.lnum = eap->line1;
7863 beginline(BL_SOL | BL_FIX);
7864 }
7865
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007866 if (VIsual_active)
7867 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007868
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 switch (eap->cmdidx)
7870 {
7871 case CMD_delete:
7872 oa.op_type = OP_DELETE;
7873 op_delete(&oa);
7874 break;
7875
7876 case CMD_yank:
7877 oa.op_type = OP_YANK;
7878 (void)op_yank(&oa, FALSE, TRUE);
7879 break;
7880
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007881 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007882 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007884 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7885#else
7886 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007888 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 oa.op_type = OP_RSHIFT;
7890 else
7891 oa.op_type = OP_LSHIFT;
7892 op_shift(&oa, FALSE, eap->amount);
7893 break;
7894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007896 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897}
7898
7899/*
7900 * ":put".
7901 */
7902 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007903ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007905 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 if (eap->line2 == 0)
7907 {
7908 eap->line2 = 1;
7909 eap->forceit = TRUE;
7910 }
7911 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00007912 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007913 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007914 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915}
7916
7917/*
7918 * Handle ":copy" and ":move".
7919 */
7920 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007921ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922{
7923 long n;
7924
Bram Moolenaar491799b2020-08-01 19:23:43 +02007925#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007926 if (not_in_vim9(eap) == FAIL)
7927 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007928#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007929 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007930 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 {
7932 eap->nextcmd = NULL;
7933 return;
7934 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007935 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936
7937 /*
7938 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7939 */
7940 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7941 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007942 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 return;
7944 }
7945
7946 if (eap->cmdidx == CMD_move)
7947 {
7948 if (do_move(eap->line1, eap->line2, n) == FAIL)
7949 return;
7950 }
7951 else
7952 ex_copy(eap->line1, eap->line2, n);
7953 u_clearline();
7954 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007955 ex_may_print(eap);
7956}
7957
7958/*
7959 * Print the current line if flags were given to the Ex command.
7960 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007961 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007962ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007963{
7964 if (eap->flags != 0)
7965 {
7966 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7967 (eap->flags & EXFLAG_LIST));
7968 ex_no_reprint = TRUE;
7969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970}
7971
7972/*
7973 * ":smagic" and ":snomagic".
7974 */
7975 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007976ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007978 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007980 magic_overruled = eap->cmdidx == CMD_smagic
7981 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02007982 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007983 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984}
7985
7986/*
7987 * ":join".
7988 */
7989 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007990ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991{
7992 curwin->w_cursor.lnum = eap->line1;
7993 if (eap->line1 == eap->line2)
7994 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007995 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 return;
7997 if (eap->line2 == curbuf->b_ml.ml_line_count)
7998 {
7999 beep_flush();
8000 return;
8001 }
8002 ++eap->line2;
8003 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008004 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008006 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007}
8008
8009/*
8010 * ":[addr]@r" or ":[addr]*r": execute register
8011 */
8012 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008013ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014{
8015 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008016 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017
8018 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008019 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020
8021#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008022 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023#endif
8024
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008025 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 c = *eap->arg;
8027 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8028 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008029 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008030 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8031 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008032 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 else
8036 {
8037 int save_efr = exec_from_reg;
8038
8039 exec_from_reg = TRUE;
8040
8041 /*
8042 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008043 * Continue until the stuff buffer is empty and all added characters
8044 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008046 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8048
8049 exec_from_reg = save_efr;
8050 }
8051}
8052
8053/*
8054 * ":!".
8055 */
8056 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008057ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058{
8059 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8060}
8061
8062/*
8063 * ":undo".
8064 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008066ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008068 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008069 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008070 else
8071 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072}
8073
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008074#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008075 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008076ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008077{
8078 char_u hash[UNDO_HASH_SIZE];
8079
8080 u_compute_hash(hash);
8081 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8082}
8083
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008084 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008085ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008086{
8087 char_u hash[UNDO_HASH_SIZE];
8088
8089 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008090 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008091}
8092#endif
8093
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094/*
8095 * ":redo".
8096 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008098ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099{
8100 u_redo(1);
8101}
8102
8103/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008104 * ":earlier" and ":later".
8105 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008106 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008107ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008108{
8109 long count = 0;
8110 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008111 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008112 char_u *p = eap->arg;
8113
8114 if (*p == NUL)
8115 count = 1;
8116 else if (isdigit(*p))
8117 {
8118 count = getdigits(&p);
8119 switch (*p)
8120 {
8121 case 's': ++p; sec = TRUE; break;
8122 case 'm': ++p; sec = TRUE; count *= 60; break;
8123 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008124 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8125 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008126 }
8127 }
8128
8129 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008130 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008131 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008132 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8133 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008134}
8135
8136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 * ":redir": start/stop redirection.
8138 */
8139 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008140ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141{
8142 char *mode;
8143 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008144 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145
Bram Moolenaarba768492016-07-08 15:32:54 +02008146#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008147 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008148 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008149 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008150 return;
8151 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008152#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008153
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 if (STRICMP(eap->arg, "END") == 0)
8155 close_redir();
8156 else
8157 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008158 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008160 ++arg;
8161 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008163 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 mode = "a";
8165 }
8166 else
8167 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008168 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169
8170 close_redir();
8171
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008172 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008173 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 if (fname == NULL)
8175 return;
8176#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008177 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {
8179 char_u *browseFile;
8180
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008181 browseFile = do_browse(BROWSE_SAVE,
8182 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008183 fname, NULL, NULL,
8184 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008186 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 vim_free(fname);
8188 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008189 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 }
8191#endif
8192
8193 redir_fd = open_exfile(fname, eap->forceit, mode);
8194 vim_free(fname);
8195 }
8196#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008197 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008199 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008201 ++arg;
8202 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008204 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008205 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008207 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008209 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008210 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008211 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008212 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008214 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008215 if (*arg == '>')
8216 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008217 // Make register empty when not using @A-@Z and the
8218 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00008219 if (*arg == NUL && !isupper(redir_reg))
8220 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008222 }
8223 if (*arg != NUL)
8224 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008225 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008226 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008227 }
8228 }
8229 else if (*arg == '=' && arg[1] == '>')
8230 {
8231 int append;
8232
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008233 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008234 close_redir();
8235 arg += 2;
8236
8237 if (*arg == '>')
8238 {
8239 ++arg;
8240 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 }
8242 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008243 append = FALSE;
8244
8245 if (var_redir_start(skipwhite(arg), append) == OK)
8246 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 }
8248#endif
8249
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008250 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008253 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008255
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008256 // Make sure redirection is not off. Can happen for cmdline completion
8257 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008258 if (redir_fd != NULL
8259#ifdef FEAT_EVAL
8260 || redir_reg || redir_vname
8261#endif
8262 )
8263 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264}
8265
8266/*
8267 * ":redraw": force redraw
8268 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008269 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008270ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271{
8272 int r = RedrawingDisabled;
8273 int p = p_lz;
8274
8275 RedrawingDisabled = 0;
8276 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008277 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008279 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 if (need_maketitle)
8281 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008282#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8283# ifdef VIMDLL
8284 if (!gui.in_use)
8285# endif
8286 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 RedrawingDisabled = r;
8289 p_lz = p;
8290
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008291 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 msg_didout = FALSE;
8293 msg_col = 0;
8294
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008295 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008296 need_wait_return = FALSE;
8297
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 out_flush();
8299}
8300
8301/*
8302 * ":redrawstatus": force redraw of status line(s)
8303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008305ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 int r = RedrawingDisabled;
8308 int p = p_lz;
8309
8310 RedrawingDisabled = 0;
8311 p_lz = FALSE;
8312 if (eap->forceit)
8313 status_redraw_all();
8314 else
8315 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008316 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 RedrawingDisabled = r;
8318 p_lz = p;
8319 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320}
8321
Bram Moolenaare12bab32019-01-08 22:02:56 +01008322/*
8323 * ":redrawtabline": force redraw of the tabline
8324 */
8325 static void
8326ex_redrawtabline(exarg_T *eap UNUSED)
8327{
8328 int r = RedrawingDisabled;
8329 int p = p_lz;
8330
8331 RedrawingDisabled = 0;
8332 p_lz = FALSE;
8333
8334 draw_tabline();
8335
8336 RedrawingDisabled = r;
8337 p_lz = p;
8338 out_flush();
8339}
8340
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008342close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343{
8344 if (redir_fd != NULL)
8345 {
8346 fclose(redir_fd);
8347 redir_fd = NULL;
8348 }
8349#ifdef FEAT_EVAL
8350 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008351 if (redir_vname)
8352 {
8353 var_redir_stop();
8354 redir_vname = 0;
8355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356#endif
8357}
8358
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008359#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008360 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008361vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008362{
8363 if (vim_mkdir(name, prot) != 0)
8364 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008365 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008366 return FAIL;
8367 }
8368 return OK;
8369}
8370#endif
8371
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372/*
8373 * Open a file for writing for an Ex command, with some checks.
8374 * Return file descriptor, or NULL on failure.
8375 */
8376 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008377open_exfile(
8378 char_u *fname,
8379 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008380 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381{
8382 FILE *fd;
8383
8384#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008385 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 if (mch_isdir(fname))
8387 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01008388 semsg(_(e_str_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 return NULL;
8390 }
8391#endif
8392 if (!forceit && *mode != 'a' && vim_fexists(fname))
8393 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008394 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 return NULL;
8396 }
8397
8398 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008399 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400
8401 return fd;
8402}
8403
8404/*
8405 * ":mark" and ":k".
8406 */
8407 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008408ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409{
8410 pos_T pos;
8411
Bram Moolenaar10b94212021-02-19 21:42:57 +01008412#ifdef FEAT_EVAL
8413 if (not_in_vim9(eap) == FAIL)
8414 return;
8415#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008416 if (*eap->arg == NUL) // No argument?
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008417 emsg(_(e_argument_required));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008418 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar74409f62022-01-01 15:58:22 +00008419 semsg(_(e_trailing_characters_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 else
8421 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008422 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 curwin->w_cursor.lnum = eap->line2;
8424 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008425 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaar6d057012021-12-31 18:49:43 +00008426 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008427 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 }
8429}
8430
8431/*
8432 * Update w_topline, w_leftcol and the cursor position.
8433 */
8434 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008435update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008437 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 update_topline();
8439 if (!curwin->w_p_wrap)
8440 validate_cursor();
8441 update_curswant();
8442}
8443
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008445 * Save the current State and go to Normal mode.
8446 * Return TRUE if the typeahead could be saved.
8447 */
8448 int
8449save_current_state(save_state_T *sst)
8450{
8451 sst->save_msg_scroll = msg_scroll;
8452 sst->save_restart_edit = restart_edit;
8453 sst->save_msg_didout = msg_didout;
8454 sst->save_State = State;
8455 sst->save_insertmode = p_im;
8456 sst->save_finish_op = finish_op;
8457 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008458 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008459
Bram Moolenaar4324d872020-12-01 20:12:24 +01008460 msg_scroll = FALSE; // no msg scrolling in Normal mode
8461 restart_edit = 0; // don't go to Insert mode
8462 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008463
Bram Moolenaara452b802020-12-01 21:47:59 +01008464 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008465 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008466
8467 /*
8468 * Save the current typeahead. This is required to allow using ":normal"
8469 * from an event handler and makes sure we don't hang when the argument
8470 * ends with half a command.
8471 */
8472 save_typeahead(&sst->tabuf);
8473 return sst->tabuf.typebuf_valid;
8474}
8475
8476 void
8477restore_current_state(save_state_T *sst)
8478{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008479 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008480 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008481
8482 msg_scroll = sst->save_msg_scroll;
8483 restart_edit = sst->save_restart_edit;
8484 p_im = sst->save_insertmode;
8485 finish_op = sst->save_finish_op;
8486 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008487 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008488 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008489 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008490
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008491 // Restore the state (needed when called from a function executed for
8492 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008493 State = sst->save_State;
8494#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008495 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008496#endif
8497}
8498
8499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 * ":normal[!] {commands}": Execute normal mode commands.
8501 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008502 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008503ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008505 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 char_u *arg = NULL;
8507 int l;
8508 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008510 if (ex_normal_lock > 0)
8511 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00008512 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008513 return;
8514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 if (ex_normal_busy >= p_mmd)
8516 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00008517 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 return;
8519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 /*
8522 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8523 * this for the K_SPECIAL leading byte, otherwise special keys will not
8524 * work.
8525 */
8526 if (has_mbyte)
8527 {
8528 int len = 0;
8529
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008530 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 for (p = eap->arg; *p != NUL; ++p)
8532 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008533#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008534 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008536#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008537 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008538 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008539#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 )
8543 len += 2;
8544 }
8545 if (len > 0)
8546 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008547 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 if (arg != NULL)
8549 {
8550 len = 0;
8551 for (p = eap->arg; *p != NUL; ++p)
8552 {
8553 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008554#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 if (*p == CSI)
8556 {
8557 arg[len++] = KS_EXTRA;
8558 arg[len++] = (int)KE_CSI;
8559 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008560#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008561 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 {
8563 arg[len++] = *++p;
8564 if (*p == K_SPECIAL)
8565 {
8566 arg[len++] = KS_SPECIAL;
8567 arg[len++] = KE_FILLER;
8568 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008569#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 else if (*p == CSI)
8571 {
8572 arg[len++] = KS_EXTRA;
8573 arg[len++] = (int)KE_CSI;
8574 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 }
8577 arg[len] = NUL;
8578 }
8579 }
8580 }
8581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008583 ++ex_normal_busy;
8584 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 {
8586 /*
8587 * Repeat the :normal command for each line in the range. When no
8588 * range given, execute it just once, without positioning the cursor
8589 * first.
8590 */
8591 do
8592 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 if (eap->addr_count != 0)
8594 {
8595 curwin->w_cursor.lnum = eap->line1++;
8596 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008597 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 }
8599
Bram Moolenaar13505972019-01-24 15:04:48 +01008600 exec_normal_cmd(arg != NULL
8601 ? arg
8602 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 }
8604 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8605 }
8606
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008607 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 update_topline_cursor();
8609
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008610 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008612 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008613#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008614 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008615#endif
8616
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618}
8619
8620/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008621 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 */
8623 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008624ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008626 if (eap->forceit)
8627 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008628 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008629 if (!curwin->w_cursor.lnum)
8630 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008631 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008632 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008633#ifdef FEAT_TERMINAL
8634 // Ignore this when running in an active terminal.
8635 if (term_job_running(curbuf->b_term))
8636 return;
8637#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008638
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008639 // Ignore the command when already in Insert mode. Inserting an
8640 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008641 if (State & INSERT)
8642 return;
8643
Bram Moolenaar64969662005-12-14 21:59:55 +00008644 if (eap->cmdidx == CMD_startinsert)
8645 restart_edit = 'a';
8646 else if (eap->cmdidx == CMD_startreplace)
8647 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008649 restart_edit = 'V';
8650
8651 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008653 if (eap->cmdidx == CMD_startinsert)
8654 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008655 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008657
8658 if (VIsual_active)
8659 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660}
8661
8662/*
8663 * ":stopinsert"
8664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008666ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667{
8668 restart_edit = 0;
8669 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008670 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008673/*
8674 * Execute normal mode command "cmd".
8675 * "remap" can be REMAP_NONE or REMAP_YES.
8676 */
8677 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008678exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008679{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008680 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008681 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008682 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008683}
Bram Moolenaar25281632016-01-21 23:32:32 +01008684
Bram Moolenaar25281632016-01-21 23:32:32 +01008685/*
8686 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008687 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008688 */
8689 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008690exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008691{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008692 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008693 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008694
Bram Moolenaar905dd902019-04-07 14:21:47 +02008695 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8696 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008697 clear_oparg(&oa);
8698 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008699 while ((!stuff_empty()
8700 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008701 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008702 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008703 {
8704 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008705#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008706 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008707 && oa.op_type == OP_NOP && oa.regname == NUL
8708 && !VIsual_active)
8709 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008710 // If terminal_loop() returns OK we got a key that is handled
8711 // in Normal model. With FAIL we first need to position the
8712 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008713 if (terminal_loop(TRUE) == OK)
8714 normal_cmd(&oa, TRUE);
8715 }
8716 else
8717#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008718 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008719 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008720 }
8721}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008722
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723#ifdef FEAT_FIND_ID
8724 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008725ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726{
8727 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8728 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8729 (linenr_T)1, (linenr_T)MAXLNUM);
8730}
8731
Bram Moolenaar4033c552017-09-16 20:54:51 +02008732#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733/*
8734 * ":psearch"
8735 */
8736 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008737ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738{
8739 g_do_tagpreview = p_pvh;
8740 ex_findpat(eap);
8741 g_do_tagpreview = 0;
8742}
8743#endif
8744
8745 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008746ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747{
8748 int whole = TRUE;
8749 long n;
8750 char_u *p;
8751 int action;
8752
8753 switch (cmdnames[eap->cmdidx].cmd_name[2])
8754 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008755 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8757 action = ACTION_GOTO;
8758 else
8759 action = ACTION_SHOW;
8760 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008761 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 action = ACTION_SHOW_ALL;
8763 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008764 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 action = ACTION_GOTO;
8766 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008767 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 action = ACTION_SPLIT;
8769 break;
8770 }
8771
8772 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008773 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 {
8775 n = getdigits(&eap->arg);
8776 eap->arg = skipwhite(eap->arg);
8777 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008778 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 {
8780 whole = FALSE;
8781 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008782 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 if (*p)
8784 {
8785 *p++ = NUL;
8786 p = skipwhite(p);
8787
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008788 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008789 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00008790 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02008792 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 }
8794 }
8795 if (!eap->skip)
8796 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8797 whole, !eap->forceit,
8798 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8799 n, action, eap->line1, eap->line2);
8800}
8801#endif
8802
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803
Bram Moolenaar4033c552017-09-16 20:54:51 +02008804#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805/*
8806 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8807 */
8808 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008809ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008811 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8813}
8814
8815/*
8816 * ":pedit"
8817 */
8818 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008819ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820{
8821 win_T *curwin_save = curwin;
8822
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008823 if (ERROR_IF_ANY_POPUP_WINDOW)
8824 return;
8825
Bram Moolenaar026587b2019-08-17 15:08:00 +02008826 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008828 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008829
Bram Moolenaar026587b2019-08-17 15:08:00 +02008830 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008832
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 if (curwin != curwin_save && win_valid(curwin_save))
8834 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008835 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 validate_cursor();
8837 redraw_later(VALID);
8838 win_enter(curwin_save, TRUE);
8839 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008840# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008841 else if (WIN_IS_POPUP(curwin))
8842 {
8843 // can't keep focus in popup window
8844 win_enter(firstwin, TRUE);
8845 }
8846# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 g_do_tagpreview = 0;
8848}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850
8851/*
8852 * ":stag", ":stselect" and ":stjump".
8853 */
8854 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008855ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856{
8857 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008858 postponed_split_flags = cmdmod.cmod_split;
8859 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8861 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008862 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864
8865/*
8866 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8867 */
8868 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008869ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870{
8871 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8872}
8873
8874 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008875ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876{
8877 int cmd;
8878
8879 switch (name[1])
8880 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008881 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008883 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008885 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008887 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008889 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008891 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008893 case 'f': // ":tfirst"
8894 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008896 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008898 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008900 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008902 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 return;
8904 }
8905#endif
8906 cmd = DT_TAG;
8907 break;
8908 }
8909
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008910 if (name[0] == 'l')
8911 {
8912#ifndef FEAT_QUICKFIX
8913 ex_ni(eap);
8914 return;
8915#else
8916 cmd = DT_LTAG;
8917#endif
8918 }
8919
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8921 eap->forceit, TRUE);
8922}
8923
8924/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008925 * Check "str" for starting with a special cmdline variable.
8926 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8927 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8928 */
8929 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008930find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008931{
8932 int len;
8933 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008934 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008935 "%",
8936#define SPEC_PERC 0
8937 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008938#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008939 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008940#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008941 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008942#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008943 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008944#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008945 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008946#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008947 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008948#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008949 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008950#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008951 "<stack>", // call stack
8952#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008953 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008954#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008955 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008956#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008957 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008958#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008959 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008960#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008961 "<SID>", // script ID: <SNR>123_
8962#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008963#ifdef FEAT_CLIENTSERVER
8964 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008965# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008966#endif
8967 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008968
K.Takataeeec2542021-06-02 13:28:16 +02008969 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008970 {
8971 len = (int)STRLEN(spec_str[i]);
8972 if (STRNCMP(src, spec_str[i], len) == 0)
8973 {
8974 *usedlen = len;
8975 return i;
8976 }
8977 }
8978 return -1;
8979}
8980
8981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 * Evaluate cmdline variables.
8983 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008984 * change "%" to curbuf->b_ffname
8985 * "#" to curwin->w_alt_fnum
8986 * "%%" to curwin->w_alt_fnum in Vim9 script
8987 * "<cword>" to word under the cursor
8988 * "<cWORD>" to WORD under the cursor
8989 * "<cexpr>" to C-expression under the cursor
8990 * "<cfile>" to path name under the cursor
8991 * "<sfile>" to sourced file name
8992 * "<stack>" to call stack
8993 * "<slnum>" to sourced file line number
8994 * "<afile>" to file name for autocommand
8995 * "<abuf>" to buffer number for autocommand
8996 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 *
8998 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8999 * "" for error without a message) and NULL is returned.
9000 * Returns an allocated string if a valid match was found.
9001 * Returns NULL if no match was found. "usedlen" then still contains the
9002 * number of characters to skip.
9003 */
9004 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009005eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009006 char_u *src, // pointer into commandline
9007 char_u *srcstart, // beginning of valid memory for src
9008 int *usedlen, // characters after src that are used
9009 linenr_T *lnump, // line number for :e command, or NULL
9010 char **errormsg, // pointer to error message
9011 int *escaped) // return value has escaped white space (can
9012 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013{
9014 int i;
9015 char_u *s;
9016 char_u *result;
9017 char_u *resultbuf = NULL;
9018 int resultlen;
9019 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009020 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009022 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025
9026 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009027 if (escaped != NULL)
9028 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029
9030 /*
9031 * Check if there is something to do.
9032 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009033 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009034 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 {
9036 *usedlen = 1;
9037 return NULL;
9038 }
9039
9040 /*
9041 * Skip when preceded with a backslash "\%" and "\#".
9042 * Note: In "\\%" the % is also not recognized!
9043 */
9044 if (src > srcstart && src[-1] == '\\')
9045 {
9046 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009047 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 return NULL;
9049 }
9050
9051 /*
9052 * word or WORD under cursor
9053 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009054 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9055 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009057 resultlen = find_ident_under_cursor(&result,
9058 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9059 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9060 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 if (resultlen == 0)
9062 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009063 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 return NULL;
9065 }
9066 }
9067
9068 /*
9069 * '#': Alternate file name
9070 * '%': Current file name
9071 * File name under the cursor
9072 * File name for autocommand
9073 * and following modifiers
9074 */
9075 else
9076 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009077 int off = 0;
9078
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 switch (spec_idx)
9080 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009081 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009082#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009083 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009086 // '%': current file
9087 if (curbuf->b_fname == NULL)
9088 {
9089 result = (char_u *)"";
9090 valid = 0; // Must have ":p:h" to be valid
9091 }
9092 else
9093 {
9094 result = curbuf->b_fname;
9095 tilde_file = STRCMP(result, "~") == 0;
9096 }
9097 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009099#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009100 // "%%" alternate file
9101 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009102#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009103 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009104 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009105 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009107 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 result = arg_all();
9109 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009110 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009111 if (escaped != NULL)
9112 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 break;
9115 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009116 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009117 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009118 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009120 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009121 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009122 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009123 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009125 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009127 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009128 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009129 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009130 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009131 return NULL;
9132 }
9133#ifdef FEAT_EVAL
9134 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9135 (long)i);
9136 if (result == NULL)
9137 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009138 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009139 return NULL;
9140 }
9141#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009142 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 }
9146 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009147 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009148 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9149 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009150 buf = buflist_findnr(i);
9151 if (buf == NULL)
9152 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009153 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009154 return NULL;
9155 }
9156 if (lnump != NULL)
9157 *lnump = ECMD_LAST;
9158 if (buf->b_fname == NULL)
9159 {
9160 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009161 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009162 }
9163 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009164 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009165 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009166 tilde_file = STRCMP(result, "~") == 0;
9167 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 break;
9170
9171#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009172 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009173 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 if (result == NULL)
9175 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009176 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 return NULL;
9178 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009179 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 break;
9181#endif
9182
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009183 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009185 if (result != NULL && !autocmd_fname_full)
9186 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009187 // Still need to turn the fname into a full path. It is
9188 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009189 autocmd_fname_full = TRUE;
9190 result = FullName_save(autocmd_fname, FALSE);
9191 vim_free(autocmd_fname);
9192 autocmd_fname = result;
9193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 if (result == NULL)
9195 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009196 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 return NULL;
9198 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009199 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 break;
9201
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009202 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 if (autocmd_bufnr <= 0)
9204 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009205 *errormsg = _(e_no_autocommand_buffer_name_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 return NULL;
9207 }
9208 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9209 result = strbuf;
9210 break;
9211
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009212 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 result = autocmd_match;
9214 if (result == NULL)
9215 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009216 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 return NULL;
9218 }
9219 break;
9220
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009221 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02009222 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02009223 result = estack_sfile(spec_idx == SPEC_SFILE
9224 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 if (result == NULL)
9226 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02009227 *errormsg = spec_idx == SPEC_SFILE
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009228 ? _(e_no_source_file_name_to_substitute_for_sfile)
9229 : _(e_no_call_stack_to_substitute_for_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 return NULL;
9231 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009232 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009234
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009235 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009236 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009237 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009238 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009239 return NULL;
9240 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009241 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009242 result = strbuf;
9243 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009244
9245#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009246 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009247 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009248 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009249 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009250 return NULL;
9251 }
9252 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009253 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009254 result = strbuf;
9255 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009256
Bram Moolenaar90944302020-08-01 20:45:11 +02009257 case SPEC_SID:
9258 if (current_sctx.sc_sid <= 0)
9259 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009260 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009261 return NULL;
9262 }
9263 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9264 result = strbuf;
9265 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009266#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009267
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009268#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009269 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009270 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9271 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 result = strbuf;
9273 break;
9274#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009275
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009276 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009277 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009278 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 }
9280
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009281 resultlen = (int)STRLEN(result); // length of new string
9282 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 {
9284 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 resultlen = (int)(s - result);
9287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 else if (!skip_mod)
9289 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009290 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 &resultlen);
9292 if (result == NULL)
9293 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009294 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 return NULL;
9296 }
9297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 }
9299
9300 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9301 {
9302 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009303 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009305 *errormsg = _(e_evaluates_to_an_empty_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 result = NULL;
9307 }
9308 else
9309 result = vim_strnsave(result, resultlen);
9310 vim_free(resultbuf);
9311 return result;
9312}
9313
9314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 * Expand the <sfile> string in "arg".
9316 *
9317 * Returns an allocated string, or NULL for any error.
9318 */
9319 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009320expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009322 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 int len;
9324 char_u *result;
9325 char_u *newres;
9326 char_u *repl;
9327 int srclen;
9328 char_u *p;
9329
9330 result = vim_strsave(arg);
9331 if (result == NULL)
9332 return NULL;
9333
9334 for (p = result; *p; )
9335 {
9336 if (STRNCMP(p, "<sfile>", 7) != 0)
9337 ++p;
9338 else
9339 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009340 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00009341 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 if (errormsg != NULL)
9343 {
9344 if (*errormsg)
9345 emsg(errormsg);
9346 vim_free(result);
9347 return NULL;
9348 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009349 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 {
9351 p += srclen;
9352 continue;
9353 }
9354 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9355 newres = alloc(len);
9356 if (newres == NULL)
9357 {
9358 vim_free(repl);
9359 vim_free(result);
9360 return NULL;
9361 }
9362 mch_memmove(newres, result, (size_t)(p - result));
9363 STRCPY(newres + (p - result), repl);
9364 len = (int)STRLEN(newres);
9365 STRCAT(newres, p + srclen);
9366 vim_free(repl);
9367 vim_free(result);
9368 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009369 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 }
9371 }
9372
9373 return result;
9374}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009377/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009378 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009379 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009382dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 if (fname == NULL)
9385 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009386 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387}
9388#endif
9389
9390/*
9391 * ":behave {mswin,xterm}"
9392 */
9393 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009394ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395{
9396 if (STRCMP(eap->arg, "mswin") == 0)
9397 {
9398 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9399 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9400 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9401 set_option_value((char_u *)"keymodel", 0L,
9402 (char_u *)"startsel,stopsel", 0);
9403 }
9404 else if (STRCMP(eap->arg, "xterm") == 0)
9405 {
9406 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9407 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9408 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9409 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9410 }
9411 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009412 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413}
9414
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415static int filetype_detect = FALSE;
9416static int filetype_plugin = FALSE;
9417static int filetype_indent = FALSE;
9418
9419/*
9420 * ":filetype [plugin] [indent] {on,off,detect}"
9421 * on: Load the filetype.vim file to install autocommands for file types.
9422 * off: Load the ftoff.vim file to remove all autocommands for file types.
9423 * plugin on: load filetype.vim and ftplugin.vim
9424 * plugin off: load ftplugof.vim
9425 * indent on: load filetype.vim and indent.vim
9426 * indent off: load indoff.vim
9427 */
9428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009429ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430{
9431 char_u *arg = eap->arg;
9432 int plugin = FALSE;
9433 int indent = FALSE;
9434
9435 if (*eap->arg == NUL)
9436 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009437 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009438 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 filetype_detect ? "ON" : "OFF",
9440 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9441 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9442 return;
9443 }
9444
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009445 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 for (;;)
9447 {
9448 if (STRNCMP(arg, "plugin", 6) == 0)
9449 {
9450 plugin = TRUE;
9451 arg = skipwhite(arg + 6);
9452 continue;
9453 }
9454 if (STRNCMP(arg, "indent", 6) == 0)
9455 {
9456 indent = TRUE;
9457 arg = skipwhite(arg + 6);
9458 continue;
9459 }
9460 break;
9461 }
9462 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9463 {
9464 if (*arg == 'o' || !filetype_detect)
9465 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009466 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 filetype_detect = TRUE;
9468 if (plugin)
9469 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009470 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 filetype_plugin = TRUE;
9472 }
9473 if (indent)
9474 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009475 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 filetype_indent = TRUE;
9477 }
9478 }
9479 if (*arg == 'd')
9480 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009481 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009482 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 }
9484 }
9485 else if (STRCMP(arg, "off") == 0)
9486 {
9487 if (plugin || indent)
9488 {
9489 if (plugin)
9490 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009491 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 filetype_plugin = FALSE;
9493 }
9494 if (indent)
9495 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009496 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 filetype_indent = FALSE;
9498 }
9499 }
9500 else
9501 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009502 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 filetype_detect = FALSE;
9504 }
9505 }
9506 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009507 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508}
9509
9510/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009511 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 */
9513 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009514ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515{
9516 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009517 {
9518 char_u *arg = eap->arg;
9519
9520 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9521 arg += 9;
9522
9523 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9524 if (arg != eap->arg)
9525 did_filetype = FALSE;
9526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009530ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531{
9532#ifdef FEAT_DIGRAPHS
9533 if (*eap->arg != NUL)
9534 putdigraph(eap->arg);
9535 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009536 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537#else
mityu61065042021-07-19 20:07:21 +02009538 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539#endif
9540}
9541
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009542#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9543 void
9544set_no_hlsearch(int flag)
9545{
9546 no_hlsearch = flag;
9547# ifdef FEAT_EVAL
9548 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9549# endif
9550}
9551
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552/*
9553 * ":nohlsearch"
9554 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009556ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009558 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009559 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561#endif
9562
9563#ifdef FEAT_CRYPT
9564/*
9565 * ":X": Get crypt key
9566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009568ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009570 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009571 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572}
9573#endif
9574
9575#ifdef FEAT_FOLDING
9576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009577ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578{
9579 if (foldManualAllowed(TRUE))
9580 foldCreate(eap->line1, eap->line2);
9581}
9582
9583 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009584ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585{
9586 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9587 eap->forceit, FALSE);
9588}
9589
9590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009591ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592{
9593 linenr_T lnum;
9594
Bram Moolenaar4facea32019-10-12 20:17:40 +02009595# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009596 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009597# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009598
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009599 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9601 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9602 ml_setmarked(lnum);
9603
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009604 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009606 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009607# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009608 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009609# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610}
9611#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009612
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009613#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009614/*
9615 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9616 * instead of a quickfix command.
9617 */
9618 int
9619is_loclist_cmd(int cmdidx)
9620{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009621 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009622 return FALSE;
9623 return cmdnames[cmdidx].cmd_name[0] == 'l';
9624}
9625#endif
9626
Bram Moolenaar4facea32019-10-12 20:17:40 +02009627#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009628 int
9629get_pressedreturn(void)
9630{
9631 return ex_pressedreturn;
9632}
9633
9634 void
9635set_pressedreturn(int val)
9636{
9637 ex_pressedreturn = val;
9638}
9639#endif