blob: 1df6e236f8a1d70adbfc6ee960eb4e973a09e6aa [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 Moolenaar65c1b012005-01-31 19:02:28 +0000306# define ex_unlockvar ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200307# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100308# define ex_import ex_ni
309# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200311#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_loadview ex_ni
313#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200314#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315# define ex_viminfo ex_ni
316#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_filetype(exarg_T *eap);
319static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000321# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322# define ex_diffpatch ex_ni
323# define ex_diffgetput ex_ni
324# define ex_diffsplit ex_ni
325# define ex_diffthis ex_ni
326# define ex_diffupdate ex_ni
327#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100328static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100330static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#else
332# define ex_nohlsearch ex_ni
333# define ex_match ex_ni
334#endif
335#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100336static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337#else
338# define ex_X ex_ni
339#endif
340#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100341static void ex_fold(exarg_T *eap);
342static void ex_foldopen(exarg_T *eap);
343static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344#else
345# define ex_fold ex_ni
346# define ex_foldopen ex_ni
347# define ex_folddo ex_ni
348#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100349#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350# define ex_language ex_ni
351#endif
352#ifndef FEAT_SIGNS
353# define ex_sign ex_ni
354#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000355#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200356# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000357# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200358# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
Bram Moolenaar05159a02005-02-26 23:04:13 +0000361#ifndef FEAT_PROFILE
362# define ex_profile ex_ni
363#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200364#ifndef FEAT_TERMINAL
365# define ex_terminal ex_ni
366#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200367#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
368# define ex_xrestore ex_ni
369#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100370#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200371# define ex_popupclear ex_ni
372#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000373
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374/*
375 * Declare cmdnames[].
376 */
377#define DO_DECLARE_EXCMD
378#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200379#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100380
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381static char_u dollar_command[2] = {'$', 0};
382
383
384#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100385// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386typedef struct
387{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100388 char_u *line; // command line
389 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390} wcmd_T;
391
392/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000393 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000395 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000397struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100399 garray_T *lines_gap; // growarray with line info
400 int current_line; // last read line from growarray
401 int repeating; // TRUE when looping a second time
402 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200403 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 void *cookie;
405};
406
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200407static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100408static int store_loop_line(garray_T *gap, char_u *line);
409static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000410
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100411// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000412struct dbg_stuff
413{
414 int trylevel;
415 int force_abort;
416 except_T *caught_stack;
417 char_u *vv_exception;
418 char_u *vv_throwpoint;
419 int did_emsg;
420 int got_int;
421 int did_throw;
422 int need_rethrow;
423 int check_cstack;
424 except_T *current_exception;
425};
426
Bram Moolenaared203462004-06-16 11:19:22 +0000427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100428save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000429{
430 dsp->trylevel = trylevel; trylevel = 0;
431 dsp->force_abort = force_abort; force_abort = FALSE;
432 dsp->caught_stack = caught_stack; caught_stack = NULL;
433 dsp->vv_exception = v_exception(NULL);
434 dsp->vv_throwpoint = v_throwpoint(NULL);
435
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100436 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000437 dsp->did_emsg = did_emsg; did_emsg = FALSE;
438 dsp->got_int = got_int; got_int = FALSE;
439 dsp->did_throw = did_throw; did_throw = FALSE;
440 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
441 dsp->check_cstack = check_cstack; check_cstack = FALSE;
442 dsp->current_exception = current_exception; current_exception = NULL;
443}
444
445 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100446restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000447{
448 suppress_errthrow = FALSE;
449 trylevel = dsp->trylevel;
450 force_abort = dsp->force_abort;
451 caught_stack = dsp->caught_stack;
452 (void)v_exception(dsp->vv_exception);
453 (void)v_throwpoint(dsp->vv_throwpoint);
454 did_emsg = dsp->did_emsg;
455 got_int = dsp->got_int;
456 did_throw = dsp->did_throw;
457 need_rethrow = dsp->need_rethrow;
458 check_cstack = dsp->check_cstack;
459 current_exception = dsp->current_exception;
460}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461#endif
462
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463/*
464 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
465 * command is given.
466 */
467 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100468do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100469 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470{
471 int save_msg_scroll;
472 int prev_msg_row;
473 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100474 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000475
476 if (improved)
477 exmode_active = EXMODE_VIM;
478 else
479 exmode_active = EXMODE_NORMAL;
480 State = NORMAL;
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +0200481 trigger_modechanged();
Bram Moolenaardf177f62005-02-22 08:39:57 +0000482
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100483 // When using ":global /pat/ visual" and then "Q" we return to continue
484 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000485 if (global_busy)
486 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487
488 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100489 ++RedrawingDisabled; // don't redisplay the window
490 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100492 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 ++hold_gui_events;
494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495
Bram Moolenaar32526b32019-01-19 17:43:09 +0100496 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 while (exmode_active)
498 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100499 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000500 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
501 {
502 exmode_active = FALSE;
503 break;
504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 msg_scroll = TRUE;
506 need_wait_return = FALSE;
507 ex_pressedreturn = FALSE;
508 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100509 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 prev_msg_row = msg_row;
511 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 if (improved)
513 {
514 cmdline_row = msg_row;
515 do_cmdline(NULL, getexline, NULL, 0);
516 }
517 else
518 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
519 lines_left = Rows - 1;
520
Bram Moolenaardf177f62005-02-22 08:39:57 +0000521 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100522 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000524 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000525 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000526 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 if (ex_pressedreturn)
529 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100530 // go up one line, to overwrite the ":<CR>" line, so the
531 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000532 msg_row = prev_msg_row;
533 if (prev_msg_row == Rows - 1)
534 msg_row--;
535 }
536 msg_col = 0;
537 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
538 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100541 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000542 {
543 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000544 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000545 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000546 emsg(_(e_at_end_of_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 }
549
550#ifdef FEAT_GUI
551 --hold_gui_events;
552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 --RedrawingDisabled;
554 --no_wait_return;
555 update_screen(CLEAR);
556 need_wait_return = FALSE;
557 msg_scroll = save_msg_scroll;
558}
559
560/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200561 * Print the executed command for when 'verbose' is set.
562 * When "lnum" is 0 only print the command.
563 */
564 static void
565msg_verbose_cmd(linenr_T lnum, char_u *cmd)
566{
567 ++no_wait_return;
568 verbose_enter_scroll();
569
570 if (lnum == 0)
571 smsg(_("Executing: %s"), cmd);
572 else
573 smsg(_("line %ld: %s"), (long)lnum, cmd);
574 if (msg_silent == 0)
575 msg_puts("\n"); // don't overwrite this
576
577 verbose_leave_scroll();
578 --no_wait_return;
579}
580
581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 * Execute a simple command line. Used for translated commands like "*".
583 */
584 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100585do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586{
587 return do_cmdline(cmd, NULL, NULL,
588 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
589}
590
591/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100592 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
593 * This allows for using a range without ":" in Vim9 script.
594 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200595 static int
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100596do_cmd_argument(char_u *cmd)
597{
598 return do_cmdline(cmd, NULL, NULL,
599 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
600}
601
602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 * do_cmdline(): execute one Ex command line
604 *
605 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100606 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 * 2. Split up in parts separated with '|'.
608 *
609 * This function can be called recursively!
610 *
611 * flags:
612 * DOCMD_VERBOSE - The command will be included in the error message.
613 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100614 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 * DOCMD_KEYTYPED - Don't reset KeyTyped.
616 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
617 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
618 *
619 * return FAIL if cmdline could not be executed, OK otherwise
620 */
621 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100622do_cmdline(
623 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200624 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100625 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100626 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100628 char_u *next_cmdline; // next cmd to execute
629 char_u *cmdline_copy = NULL; // copy of cmd line
630 int used_getline = FALSE; // used "fgetline" to obtain command
631 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100633 int count = 0; // line number count
634 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 int retval = OK;
636#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100637 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100638 garray_T lines_ga; // keep lines for ":while"/":for"
639 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200640 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100641 char_u *fname = NULL; // function or script name
642 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
643 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
644 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200646 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200647 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100649 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200650 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000652 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000654 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100656# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657# define cmd_cookie cookie
658#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100659 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200660#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100661 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
662 // location for storing error messages to be converted to an exception.
663 // This ensures that the do_errthrow() call in do_one_cmd() does not
664 // combine the messages stored by an earlier invocation of do_one_cmd()
665 // with the command name of the later one. This would happen when
666 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 saved_msg_list = msg_list;
668 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669#endif
670
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100671 // It's possible to create an endless loop with ":execute", catch that
672 // here. The value of 200 allows nested function calls, ":source", etc.
673 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100674 if (call_depth >= 200
675#ifdef FEAT_EVAL
676 && call_depth >= p_mfd
677#endif
678 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000680 emsg(_(e_command_too_recursive));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100682 // When converting to an exception, we do not include the command name
683 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100684 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 msg_list = saved_msg_list;
686#endif
687 return FAIL;
688 }
689 ++call_depth;
690
691#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100692 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 cstack.cs_idx = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
695
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100696 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100698 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100699 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000700 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 ++ex_nesting_level;
702
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100703 // Get the function or script name and the address where the next breakpoint
704 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000705 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 {
707 fname = func_name(real_cookie);
708 breakpoint = func_breakpoint(real_cookie);
709 dbg_tick = func_dbg_tick(real_cookie);
710 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100711 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100713 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 breakpoint = source_breakpoint(real_cookie);
715 dbg_tick = source_dbg_tick(real_cookie);
716 }
717
718 /*
719 * Initialize "force_abort" and "suppress_errthrow" at the top level.
720 */
721 if (!recursive)
722 {
723 force_abort = FALSE;
724 suppress_errthrow = FALSE;
725 }
726
727 /*
728 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000729 * exception handling (used when debugging). Otherwise clear it to avoid
730 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000732 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000733 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000734 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200735 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736
737 initial_trylevel = trylevel;
738
739 /*
740 * "did_throw" will be set to TRUE when an exception is being thrown.
741 */
742 did_throw = FALSE;
743#endif
744 /*
745 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000746 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 * If force_abort is set, we cancel everything.
748 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100749#ifdef FEAT_EVAL
750 did_emsg_cumul += did_emsg;
751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 did_emsg = FALSE;
753
754 /*
755 * KeyTyped is only set when calling vgetc(). Reset it here when not
756 * calling vgetc() (sourced command lines).
757 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100758 if (!(flags & DOCMD_KEYTYPED)
759 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 KeyTyped = FALSE;
761
762 /*
763 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000764 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 * - for multiple commands on one line, separated with '|'
766 * - when repeating until there are no more lines (for ":source")
767 */
768 next_cmdline = cmdline;
769 do
770 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000771#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100772 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000773#endif
774
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100775 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 if (next_cmdline == NULL
777#ifdef FEAT_EVAL
778 && !force_abort
779 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000780 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781#endif
782 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100783 {
784#ifdef FEAT_EVAL
785 did_emsg_cumul += did_emsg;
786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789
790 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000791 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100792 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 * 3. If a line is given: Make a copy, so we can mess with it.
794 */
795
796#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100797 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000798 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100800 // Each '|' separated command is stored separately in lines_ga, to
801 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100802 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100804 // Check if a function has returned or, unless it has an unclosed
805 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000806 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000809 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810 func_line_end(real_cookie);
811# endif
812 if (func_has_ended(real_cookie))
813 {
814 retval = FAIL;
815 break;
816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000819 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100820 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000821 script_line_end();
822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100824 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100825 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 {
827 retval = FAIL;
828 break;
829 }
830
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100831 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 if (breakpoint != NULL && dbg_tick != NULL
833 && *dbg_tick != debug_tick)
834 {
835 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100836 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100837 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 *dbg_tick = debug_tick;
839 }
840
841 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100842 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100844 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100846 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100848 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100849 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100851 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100852 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 *dbg_tick = debug_tick;
854 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000855# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000856 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000857 {
858 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100859 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100860 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000861 script_line_start();
862 }
863# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865#endif
866
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100867 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 if (next_cmdline == NULL)
869 {
870 /*
871 * Need to set msg_didout for the first line after an ":if",
872 * otherwise the ":if" will be overwritten.
873 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100874 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100876 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877#ifdef FEAT_EVAL
878 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
879#else
880 0
881#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100882 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
883 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100885 // Don't call wait_return for aborted command line. The NULL
886 // returned for the end of a sourced file or executed function
887 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 if (KeyTyped && !(flags & DOCMD_REPEAT))
889 need_wait_return = FALSE;
890 retval = FAIL;
891 break;
892 }
893 used_getline = TRUE;
894
895 /*
896 * Keep the first typed line. Clear it when more lines are typed.
897 */
898 if (flags & DOCMD_KEEPLINE)
899 {
900 vim_free(repeat_cmdline);
901 if (count == 0)
902 repeat_cmdline = vim_strsave(next_cmdline);
903 else
904 repeat_cmdline = NULL;
905 }
906 }
907
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100908 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 else if (cmdline_copy == NULL)
910 {
911 next_cmdline = vim_strsave(next_cmdline);
912 if (next_cmdline == NULL)
913 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200914 emsg(_(e_out_of_memory));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 retval = FAIL;
916 break;
917 }
918 }
919 cmdline_copy = next_cmdline;
920
921#ifdef FEAT_EVAL
922 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200923 * Inside a while/for loop, and when the command looks like a ":while"
924 * or ":for", the line is stored, because we may need it later when
925 * looping.
926 *
927 * When there is a '|' and another command, it is stored separately,
928 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000929 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200930 *
931 * Pass a different "fgetline" function to do_one_cmd() below,
932 * that it stores lines in or reads them from "lines_ga". Makes it
933 * possible to define a function inside a while/for loop and handles
934 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200936 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200938 cmd_getline = get_loop_line;
939 cmd_cookie = (void *)&cmd_loop_cookie;
940 cmd_loop_cookie.lines_gap = &lines_ga;
941 cmd_loop_cookie.current_line = current_line;
942 cmd_loop_cookie.getline = fgetline;
943 cmd_loop_cookie.cookie = cookie;
944 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
945
946 // Save the current line when encountering it the first time.
947 if (current_line == lines_ga.ga_len
948 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 {
950 retval = FAIL;
951 break;
952 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200953 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200955 else
956 {
957 cmd_getline = fgetline;
958 cmd_cookie = cookie;
959 }
960
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 did_endif = FALSE;
962#endif
963
964 if (count++ == 0)
965 {
966 /*
967 * All output from the commands is put below each other, without
968 * waiting for a return. Don't do this when executing commands
969 * from a script or when being called recursive (e.g. for ":e
970 * +command file").
971 */
972 if (!(flags & DOCMD_NOWAIT) && !recursive)
973 {
974 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100975 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100977 msg_scroll = TRUE; // put messages below each other
978 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 ++RedrawingDisabled;
980 did_inc = TRUE;
981 }
982 }
983
Bram Moolenaar823654b2020-05-29 23:03:09 +0200984 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100985 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
987 /*
988 * 2. Execute one '|' separated command.
989 * do_one_cmd() will return NULL if there is no trailing '|'.
990 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
991 */
992 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100993 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994#ifdef FEAT_EVAL
995 &cstack,
996#endif
997 cmd_getline, cmd_cookie);
998 --recursive;
999
1000#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001001 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001002 // Use "current_line" from "cmd_loop_cookie", it may have been
1003 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001004 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005#endif
1006
1007 if (next_cmdline == NULL)
1008 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001009 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001010
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 /*
1012 * If the command was typed, remember it for the ':' register.
1013 * Do this AFTER executing the command to make :@: work.
1014 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001015 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 && new_last_cmdline != NULL)
1017 {
1018 vim_free(last_cmdline);
1019 last_cmdline = new_last_cmdline;
1020 new_last_cmdline = NULL;
1021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 }
1023 else
1024 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001025 // need to copy the command after the '|' to cmdline_copy, for the
1026 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001027 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 next_cmdline = cmdline_copy;
1029 }
1030
1031
1032#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001033 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001035 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001037 {
1038 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001042 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
1044 ++current_line;
1045
1046 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001047 * An ":endwhile", ":endfor" and ":continue" is handled here.
1048 * If we were executing commands, jump back to the ":while" or
1049 * ":for".
1050 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001052 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001054 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001056 // Jump back to the matching ":while" or ":for". Be careful
1057 // not to use a cs_line[] from an entry that isn't a ":while"
1058 // or ":for": It would make "current_line" invalid and can
1059 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 if (!did_emsg && !got_int && !did_throw
1061 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001062 && (cstack.cs_flags[cstack.cs_idx]
1063 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 && cstack.cs_line[cstack.cs_idx] >= 0
1065 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1066 {
1067 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001068 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001069 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001070 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001072 // Check for the next breakpoint at or after the ":while"
1073 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 if (breakpoint != NULL)
1075 {
1076 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001077 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 fname,
1079 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1080 *dbg_tick = debug_tick;
1081 }
1082 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001083 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001085 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001087 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1088 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
1090 }
1091
1092 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001093 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001095 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001098 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 }
1100 }
1101
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001102 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001103 if (breakpoint != NULL && has_watchexpr())
1104 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001105 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001106 *dbg_tick = debug_tick;
1107 }
1108
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 /*
1110 * When not inside any ":while" loop, clear remembered lines.
1111 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001112 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 {
1114 if (lines_ga.ga_len > 0)
1115 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001116 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1118 free_cmdlines(&lines_ga);
1119 }
1120 current_line = 0;
1121 }
1122
1123 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001124 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1125 * being restored at the ":endtry". Reset them here and set the
1126 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1127 * This includes the case where a missing ":endif", ":endwhile" or
1128 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001130 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001132 cstack.cs_lflags &= ~CSL_HAD_FINA;
1133 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1134 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 did_throw ? (void *)current_exception : NULL);
1136 did_emsg = got_int = did_throw = FALSE;
1137 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1138 }
1139
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001140 // Update global "trylevel" for recursive calls to do_cmdline() from
1141 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 trylevel = initial_trylevel + cstack.cs_trylevel;
1143
1144 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001145 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 * files) is aborted because of an error, an interrupt, or an uncaught
1147 * exception, cancel everything. If it is left normally, reset
1148 * force_abort to get the non-EH compatible abortion behavior for
1149 * the rest of the script.
1150 */
1151 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1152 force_abort = FALSE;
1153
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001154 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001156#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
1158 }
1159 /*
1160 * Continue executing command lines when:
1161 * - no CTRL-C typed, no aborting error, no exception thrown or try
1162 * conditionals need to be checked for executing finally clauses or
1163 * catching an interrupt exception
1164 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001165 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 * looping for ":source" command or function call.
1167 */
1168 while (!((got_int
1169#ifdef FEAT_EVAL
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001170 || (did_emsg && (force_abort || in_vim9script()))
1171 || did_throw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172#endif
1173 )
1174#ifdef FEAT_EVAL
1175 && cstack.cs_trylevel == 0
1176#endif
1177 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001178 && !(did_emsg
1179#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001180 // Keep going when inside try/catch, so that the error can be
1181 // deal with, except when it is a syntax error, it may cause
1182 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001183 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1184#endif
1185 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001186 && (getline_equal(fgetline, cookie, getexmodeline)
1187 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 && (next_cmdline != NULL
1189#ifdef FEAT_EVAL
1190 || cstack.cs_idx >= 0
1191#endif
1192 || (flags & DOCMD_REPEAT)));
1193
1194 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001195 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196#ifdef FEAT_EVAL
1197 free_cmdlines(&lines_ga);
1198 ga_clear(&lines_ga);
1199
1200 if (cstack.cs_idx >= 0)
1201 {
1202 /*
1203 * If a sourced file or executed function ran to its end, report the
1204 * unclosed conditional.
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001205 * In Vim9 script do not give a second error, executing aborts after
1206 * the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 */
Bram Moolenaar227c58a2021-04-28 20:40:44 +02001208 if (!got_int && !did_throw && !(did_emsg && in_vim9script())
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001209 && ((getline_equal(fgetline, cookie, getsourceline)
1210 && !source_finished(fgetline, cookie))
1211 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 && !func_has_ended(real_cookie))))
1213 {
1214 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001215 emsg(_(e_missing_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001217 emsg(_(e_missing_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001218 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaar1a992222021-12-31 17:25:48 +00001219 emsg(_(e_missing_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00001221 emsg(_(e_missing_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 }
1223
1224 /*
1225 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1226 * ":endtry" in a sourced file or executed function. If the try
1227 * conditional is in its finally clause, ignore anything pending.
1228 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001229 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 */
1231 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001232 {
1233 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1234
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001235 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001236 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001237 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1238 &cstack.cs_looplevel);
1239 }
1240 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 trylevel = initial_trylevel;
1242 }
1243
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001244 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1245 // lack was reported above and the error message is to be converted to an
1246 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001247 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 ? (char_u *)"endfunction" : (char_u *)NULL);
1249
1250 if (trylevel == 0)
1251 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001252 // Just in case did_throw got set but current_exception wasn't.
1253 if (current_exception == NULL)
1254 did_throw = FALSE;
1255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 /*
1257 * When an exception is being thrown out of the outermost try
1258 * conditional, discard the uncaught exception, disable the conversion
1259 * of interrupts or errors to exceptions, and ensure that no more
1260 * commands are executed.
1261 */
1262 if (did_throw)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001263 handle_did_throw();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264
1265 /*
1266 * On an interrupt or an aborting error not converted to an exception,
1267 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001268 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 * when force_abort is set and did_emsg unset in case of an interrupt
1270 * from a finally clause after an error.
1271 */
1272 else if (got_int || (did_emsg && force_abort))
1273 suppress_errthrow = TRUE;
1274 }
1275
1276 /*
1277 * The current cstack will be freed when do_cmdline() returns. An uncaught
1278 * exception will have to be rethrown in the previous cstack. If a function
1279 * has just returned or a script file was just finished and the previous
1280 * cstack belongs to the same function or, respectively, script file, it
1281 * will have to be checked for finally clauses to be executed due to the
1282 * ":return" or ":finish". This is done in do_one_cmd().
1283 */
1284 if (did_throw)
1285 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001286 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001288 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 && ex_nesting_level > func_level(real_cookie) + 1))
1290 {
1291 if (!did_throw)
1292 check_cstack = TRUE;
1293 }
1294 else
1295 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001296 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001297 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 --ex_nesting_level;
1299 /*
1300 * Go to debug mode when returning from a function in which we are
1301 * single-stepping.
1302 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001303 if ((getline_equal(fgetline, cookie, getsourceline)
1304 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001306 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 ? (char_u *)_("End of sourced file")
1308 : (char_u *)_("End of function"));
1309 }
1310
1311 /*
1312 * Restore the exception environment (done after returning from the
1313 * debugger).
1314 */
1315 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001316 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317
1318 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001319
1320 // Cleanup if "cs_emsg_silent_list" remains.
1321 if (cstack.cs_emsg_silent_list != NULL)
1322 {
1323 eslist_T *elem, *temp;
1324
1325 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1326 {
1327 temp = elem->next;
1328 vim_free(elem);
1329 }
1330 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001331#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332
1333 /*
1334 * If there was too much output to fit on the command line, ask the user to
1335 * hit return before redrawing the screen. With the ":global" command we do
1336 * this only once after the command is finished.
1337 */
1338 if (did_inc)
1339 {
1340 --RedrawingDisabled;
1341 --no_wait_return;
1342 msg_scroll = FALSE;
1343
1344 /*
1345 * When just finished an ":if"-":else" which was typed, no need to
1346 * wait for hit-return. Also for an error situation.
1347 */
1348 if (retval == FAIL
1349#ifdef FEAT_EVAL
1350 || (did_endif && KeyTyped && !did_emsg)
1351#endif
1352 )
1353 {
1354 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001355 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 }
1357 else if (need_wait_return)
1358 {
1359 /*
1360 * The msg_start() above clears msg_didout. The wait_return we do
1361 * here should not overwrite the command that may be shown before
1362 * doing that.
1363 */
1364 msg_didout |= msg_didout_before_start;
1365 wait_return(FALSE);
1366 }
1367 }
1368
Bram Moolenaar2a942252012-11-28 23:03:07 +01001369#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001370 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001371#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 /*
1373 * Reset if_level, in case a sourced script file contains more ":if" than
1374 * ":endif" (could be ":if x | foo | endif").
1375 */
1376 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001377#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001378
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 --call_depth;
1380 return retval;
1381}
1382
Bram Moolenaar335c8c72021-07-31 21:44:35 +02001383#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar620c9592021-07-31 21:32:31 +02001384/*
1385 * Handle when "did_throw" is set after executing commands.
1386 */
1387 void
1388handle_did_throw()
1389{
1390 char *p = NULL;
1391 msglist_T *messages = NULL;
Bram Moolenaar9e0ee592021-07-31 22:17:28 +02001392 ESTACK_CHECK_DECLARATION
Bram Moolenaar620c9592021-07-31 21:32:31 +02001393
1394 /*
1395 * If the uncaught exception is a user exception, report it as an
1396 * error. If it is an error exception, display the saved error
1397 * message now. For an interrupt exception, do nothing; the
1398 * interrupt message is given elsewhere.
1399 */
1400 switch (current_exception->type)
1401 {
1402 case ET_USER:
1403 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001404 _(e_exception_not_caught_str),
Bram Moolenaar620c9592021-07-31 21:32:31 +02001405 current_exception->value);
1406 p = (char *)vim_strsave(IObuff);
1407 break;
1408 case ET_ERROR:
1409 messages = current_exception->messages;
1410 current_exception->messages = NULL;
1411 break;
1412 case ET_INTERRUPT:
1413 break;
1414 }
1415
1416 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1417 current_exception->throw_lnum);
1418 ESTACK_CHECK_SETUP
1419 current_exception->throw_name = NULL;
1420
1421 discard_current_exception(); // uses IObuff if 'verbose'
1422 suppress_errthrow = TRUE;
1423 force_abort = TRUE;
1424
1425 if (messages != NULL)
1426 {
1427 do
1428 {
1429 msglist_T *next = messages->next;
1430 int save_compiling = estack_compiling;
1431
1432 estack_compiling = messages->msg_compiling;
1433 emsg(messages->msg);
1434 vim_free(messages->msg);
1435 vim_free(messages->sfile);
1436 vim_free(messages);
1437 messages = next;
1438 estack_compiling = save_compiling;
1439 }
1440 while (messages != NULL);
1441 }
1442 else if (p != NULL)
1443 {
1444 emsg(p);
1445 vim_free(p);
1446 }
1447 vim_free(SOURCING_NAME);
1448 ESTACK_CHECK_NOW
1449 estack_pop();
1450}
1451
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001453 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 */
1455 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001456get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001458 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 wcmd_T *wp;
1460 char_u *line;
1461
1462 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1463 {
1464 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001465 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001467 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 if (cp->getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001469 line = getcmdline(c, 0L, indent, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001471 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001472 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 ++cp->current_line;
1474
1475 return line;
1476 }
1477
1478 KeyTyped = FALSE;
1479 ++cp->current_line;
1480 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001481 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 return vim_strsave(wp->line);
1483}
1484
1485/*
1486 * Store a line in "gap" so that a ":while" loop can execute it again.
1487 */
1488 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001489store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
1491 if (ga_grow(gap, 1) == FAIL)
1492 return FAIL;
1493 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001494 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 return OK;
1497}
1498
1499/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001500 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 */
1502 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001503free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505 while (gap->ga_len > 0)
1506 {
1507 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1508 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 }
1510}
1511#endif
1512
1513/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001514 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1515 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001518getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001519 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001520 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001521 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
1523#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001524 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001525 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001527 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1528 // function that's originally used to obtain the lines. This may be
1529 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001530 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001531 cp = (struct loop_cookie *)cookie;
1532 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 {
1534 gp = cp->getline;
1535 cp = cp->cookie;
1536 }
1537 return gp == func;
1538#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001539 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540#endif
1541}
1542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001544 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 * getline function. Otherwise return "cookie".
1546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001548getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001549 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001550 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551{
Bram Moolenaar13505972019-01-24 15:04:48 +01001552#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001553 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001554 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001556 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1557 // cookie that's originally used to obtain the lines. This may be nested
1558 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001559 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001560 cp = (struct loop_cookie *)cookie;
1561 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 {
1563 gp = cp->getline;
1564 cp = cp->cookie;
1565 }
1566 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001567#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001570}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
Bram Moolenaard5053d02020-06-28 15:51:16 +02001572#if defined(FEAT_EVAL) || defined(PROT)
1573/*
1574 * Get the next line source line without advancing.
1575 */
1576 char_u *
1577getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001578 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001579 void *cookie) // argument for fgetline()
1580{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001581 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001582 struct loop_cookie *cp;
1583 wcmd_T *wp;
1584
1585 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1586 // cookie that's originally used to obtain the lines. This may be nested
1587 // several levels.
1588 gp = fgetline;
1589 cp = (struct loop_cookie *)cookie;
1590 while (gp == get_loop_line)
1591 {
1592 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1593 {
1594 // executing lines a second time, use the stored copy
1595 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1596 return wp->line;
1597 }
1598 gp = cp->getline;
1599 cp = cp->cookie;
1600 }
1601 if (gp == getsourceline)
1602 return source_nextline(cp);
1603 return NULL;
1604}
1605#endif
1606
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001607
1608/*
1609 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1610 * ":bwipeout", etc.
1611 * Returns the buffer number.
1612 */
1613 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001614compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001615{
1616 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001617 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001618 int count = offset;
1619
1620 buf = firstbuf;
1621 while (buf->b_next != NULL && buf->b_fnum < lnum)
1622 buf = buf->b_next;
1623 while (count != 0)
1624 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001625 count += (offset < 0) ? 1 : -1;
1626 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1627 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001628 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001629 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001630 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001631 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001632 while (buf->b_ml.ml_mfp == NULL)
1633 {
1634 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1635 if (nextbuf == NULL)
1636 break;
1637 buf = nextbuf;
1638 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001639 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001640 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001641 if (addr_type == ADDR_LOADED_BUFFERS)
1642 while (buf->b_ml.ml_mfp == NULL)
1643 {
1644 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1645 if (nextbuf == NULL)
1646 break;
1647 buf = nextbuf;
1648 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001649 return buf->b_fnum;
1650}
1651
Bram Moolenaarb7316892019-05-01 18:08:42 +02001652/*
1653 * Return the window number of "win".
1654 * When "win" is NULL return the number of windows.
1655 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001656 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001657current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001658{
1659 win_T *wp;
1660 int nr = 0;
1661
Bram Moolenaar29323592016-07-24 22:04:11 +02001662 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001663 {
1664 ++nr;
1665 if (wp == win)
1666 break;
1667 }
1668 return nr;
1669}
1670
1671 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001672current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001673{
1674 tabpage_T *tp;
1675 int nr = 0;
1676
Bram Moolenaar29323592016-07-24 22:04:11 +02001677 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001678 {
1679 ++nr;
1680 if (tp == tab)
1681 break;
1682 }
1683 return nr;
1684}
1685
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001686 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001687comment_start(char_u *p, int starts_with_colon UNUSED)
1688{
1689#ifdef FEAT_EVAL
1690 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001691 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001692#endif
1693 return *p == '"';
1694}
1695
Bram Moolenaarf240e182014-11-27 18:33:02 +01001696# define CURRENT_WIN_NR current_win_nr(curwin)
1697# define LAST_WIN_NR current_win_nr(NULL)
1698# define CURRENT_TAB_NR current_tab_nr(curtab)
1699# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001700
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701/*
1702 * Execute one Ex command.
1703 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001704 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1705 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 *
1707 * 1. skip comment lines and leading space
1708 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001709 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001710 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001711 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001712 * 6. parse arguments
1713 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001715 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 *
1717 * This function may be called recursively!
1718 */
1719#if (_MSC_VER == 1200)
1720/*
Bram Moolenaared203462004-06-16 11:19:22 +00001721 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001723 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724#endif
1725 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001726do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001727 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001728 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001730 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001732 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001733 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001735 char_u *p;
1736 linenr_T lnum;
1737 long n;
1738 char *errormsg = NULL; // error message
1739 char_u *after_modifier = NULL;
1740 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001741 cmdmod_T save_cmdmod;
1742 int save_reg_executing = reg_executing;
1743 int ni; // set when Not Implemented
1744 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001745 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001746#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001747 int may_have_range;
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001748 int vim9script;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001749 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001750#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001751 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752
Bram Moolenaara80faa82020-04-12 19:37:17 +02001753 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 ea.line1 = 1;
1755 ea.line2 = 1;
1756#ifdef FEAT_EVAL
1757 ++ex_nesting_level;
1758#endif
1759
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001760 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 if (quitmore
1762#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001763 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001764 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001765#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001766 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001767 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 --quitmore;
1769
1770 /*
1771 * Reset browse, confirm, etc.. They are restored when returning, for
1772 * recursive calls.
1773 */
1774 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001776 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001777 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1778 goto doend;
1779
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001780/*
1781 * 1. Skip comment lines and leading white space and colons.
1782 * 2. Handle command modifiers.
1783 */
1784 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001786 ea.cmdlinep = cmdlinep;
1787 ea.getline = fgetline;
1788 ea.cookie = cookie;
1789#ifdef FEAT_EVAL
1790 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001791 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001793 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001794 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001795 apply_cmdmod(&cmdmod);
Bram Moolenaar2379f872021-02-14 14:07:34 +01001796#ifdef FEAT_EVAL
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001797 vim9script = in_vim9script();
Bram Moolenaar2379f872021-02-14 14:07:34 +01001798#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001799 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800
1801#ifdef FEAT_EVAL
1802 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1803 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1804#else
1805 ea.skip = (if_level > 0);
1806#endif
1807
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001809 * 3. Skip over the range to find the command. Let "p" point to after it.
1810 *
1811 * We need the command to know what kind of range it uses.
1812 */
1813 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001814#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001815 // In Vim9 script a colon is required before the range. This may also be
1816 // after command modifiers.
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001817 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001818 {
1819 may_have_range = FALSE;
1820 for (p = ea.cmd; p >= *cmdlinep; --p)
1821 {
1822 if (*p == ':')
1823 may_have_range = TRUE;
1824 if (p < ea.cmd && !VIM_ISWHITE(*p))
1825 break;
1826 }
1827 }
1828 else
1829 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001830 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001831#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001832 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833
1834#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001835 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001836 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001837 if (ea.cmd == cmd + 1 && *cmd == '$')
1838 // should be "$VAR = val"
1839 --ea.cmd;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001840 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001841 if (ea.cmdidx == CMD_SIZE)
1842 {
1843 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1844
1845 // If a ':' before the range is missing, give a clearer error
1846 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001847 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001848 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001849 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001850 goto doend;
1851 }
1852 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001853 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854 else
1855#endif
1856 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001857
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001858#ifdef FEAT_EVAL
1859# ifdef FEAT_PROFILE
1860 // Count this line for profiling if skip is TRUE.
1861 if (do_profiling == PROF_YES
1862 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1863 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1864 {
1865 int skip = did_emsg || got_int || did_throw;
1866
1867 if (ea.cmdidx == CMD_catch)
1868 skip = !skip && !(cstack->cs_idx >= 0
1869 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1870 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1871 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1872 skip = skip || !(cstack->cs_idx >= 0
1873 && !(cstack->cs_flags[cstack->cs_idx]
1874 & (CSF_ACTIVE | CSF_TRUE)));
1875 else if (ea.cmdidx == CMD_finally)
1876 skip = FALSE;
1877 else if (ea.cmdidx != CMD_endif
1878 && ea.cmdidx != CMD_endfor
1879 && ea.cmdidx != CMD_endtry
1880 && ea.cmdidx != CMD_endwhile)
1881 skip = ea.skip;
1882
1883 if (!skip)
1884 {
1885 if (getline_equal(fgetline, cookie, get_func_line))
1886 func_line_exec(getline_cookie(fgetline, cookie));
1887 else if (getline_equal(fgetline, cookie, getsourceline))
1888 script_line_exec();
1889 }
1890 }
1891# endif
1892
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001893 // May go to debug mode. If this happens and the ">quit" debug command is
1894 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001895 dbg_check_breakpoint(&ea);
1896 if (!ea.skip && got_int)
1897 {
1898 ea.skip = TRUE;
1899 (void)do_intthrow(cstack);
1900 }
1901#endif
1902
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001903/*
1904 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 *
1906 * where 'addr' is:
1907 *
1908 * % (entire file)
1909 * $ [+-NUM]
1910 * 'x [+-NUM] (where x denotes a currently defined mark)
1911 * . [+-NUM]
1912 * [+-NUM]..
1913 * NUM
1914 *
1915 * The ea.cmd pointer is updated to point to the first character following the
1916 * range spec. If an initial address is found, but no second, the upper bound
1917 * is equal to the lower.
1918 */
1919
Bram Moolenaar25190db2019-05-04 15:05:28 +02001920 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001921 if (!IS_USER_CMDIDX(ea.cmdidx))
1922 {
1923 if (ea.cmdidx != CMD_SIZE)
1924 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1925 else
1926 ea.addr_type = ADDR_LINES;
1927
Bram Moolenaar25190db2019-05-04 15:05:28 +02001928 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001929 if (ea.cmdidx == CMD_wincmd && p != NULL)
1930 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001931#ifdef FEAT_QUICKFIX
1932 // :.cc in quickfix window uses line number
1933 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1934 ea.addr_type = ADDR_OTHER;
1935#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001936 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001937
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001938 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001939#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001940 if (!may_have_range)
1941 ea.line1 = ea.line2 = default_address(&ea);
1942 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001943#endif
1944 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1945 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001948 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 */
1950
1951 /*
1952 * Skip ':' and any white space
1953 */
1954 ea.cmd = skipwhite(ea.cmd);
1955 while (*ea.cmd == ':')
1956 ea.cmd = skipwhite(ea.cmd + 1);
1957
1958 /*
1959 * If we got a line, but no command, then go to the line.
1960 * If we find a '|' or '\n' we set ea.nextcmd.
1961 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001962 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1963 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
1965 /*
1966 * strange vi behaviour:
1967 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001968 * ":3|..." prints line 3 (not in Vim9 script)
1969 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001971 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001973 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 goto doend;
1975 }
1976
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001977 // If this looks like an undefined user command and there are CmdUndefined
1978 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001979 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1980 && ASCII_ISUPPER(*ea.cmd)
1981 && has_cmdundefined())
1982 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001983 int ret;
1984
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001985 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001986 while (ASCII_ISALNUM(*p))
1987 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001988 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001989 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1990 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001991 // If the autocommands did something and didn't cause an error, try
1992 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001993 p = (ret
1994#ifdef FEAT_EVAL
1995 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001996#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001998 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001999
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 if (p == NULL)
2001 {
2002 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002003 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 goto doend;
2005 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002006 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002007 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2008 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 {
2010 errormsg = uc_fun_cmd();
2011 goto doend;
2012 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002013
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 if (ea.cmdidx == CMD_SIZE)
2015 {
2016 if (!ea.skip)
2017 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002018 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002020 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002021 // If the modifier was parsed OK the error must be in the
2022 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002023 if (after_modifier != NULL)
2024 append_command(after_modifier);
2025 else
2026 append_command(*cmdlinep);
2027 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002028 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002029 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 }
2031 goto doend;
2032 }
2033
Bram Moolenaar958636c2014-10-21 20:01:58 +02002034 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2035 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002036#ifdef HAVE_EX_SCRIPT_NI
2037 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2038#endif
2039 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040
2041#ifndef FEAT_EVAL
2042 /*
2043 * When the expression evaluation is disabled, recognize the ":if" and
2044 * ":endif" commands and ignore everything in between it.
2045 */
2046 if (ea.cmdidx == CMD_if)
2047 ++if_level;
2048 if (if_level)
2049 {
2050 if (ea.cmdidx == CMD_endif)
2051 --if_level;
2052 goto doend;
2053 }
2054
2055#endif
2056
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002057 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002058 if (*p == '!' && ea.cmdidx != CMD_substitute
2059 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 {
2061 ++p;
2062 ea.forceit = TRUE;
2063 }
2064 else
2065 ea.forceit = FALSE;
2066
2067/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002068 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002070 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002071 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072
2073 if (!ea.skip)
2074 {
2075#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002076 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002078 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002079 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 goto doend;
2081 }
2082#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002083 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002084 {
2085 errormsg = _("E981: Command not allowed in rvim");
2086 goto doend;
2087 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002088 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002090 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002091 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 goto doend;
2093 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002094
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002095 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002097#ifdef FEAT_CMDWIN
2098 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2099 {
2100 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002101 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002102 goto doend;
2103 }
2104#endif
2105 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2106 {
2107 // Command not allowed when text is locked
2108 errormsg = _(get_text_locked_msg());
2109 goto doend;
2110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002112
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002113 // Disallow editing another buffer when "curbuf_lock" is set.
2114 // Do allow ":checktime" (it is postponed).
2115 // Do allow ":edit" (check for an argument later).
2116 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002117 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002118 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002119 && ea.cmdidx != CMD_edit
2120 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002121 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002122 && curbuf_locked())
2123 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002125 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002127 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 goto doend;
2129 }
2130 }
2131
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002132 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002134 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 goto doend;
2136 }
2137
2138 /*
2139 * Don't complain about the range if it is not used
2140 * (could happen if line_count is accidentally set to 0).
2141 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002142 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 {
2144 /*
2145 * If the range is backwards, ask for confirmation and, if given, swap
2146 * ea.line1 & ea.line2 so it's forwards again.
2147 * When global command is busy, don't ask, will fail below.
2148 */
2149 if (!global_busy && ea.line1 > ea.line2)
2150 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002151 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002153 if (sourcing || exmode_active)
2154 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002155 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002156 goto doend;
2157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 if (ask_yesno((char_u *)
2159 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002160 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 }
2162 lnum = ea.line1;
2163 ea.line1 = ea.line2;
2164 ea.line2 = lnum;
2165 }
2166 if ((errormsg = invalid_range(&ea)) != NULL)
2167 goto doend;
2168 }
2169
Bram Moolenaarb7316892019-05-01 18:08:42 +02002170 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2171 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 ea.line2 = 1;
2173
2174 correct_range(&ea);
2175
2176#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002177 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002178 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002180 // Put the first line at the start of a closed fold, put the last line
2181 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 (void)hasFolding(ea.line1, &ea.line1, NULL);
2183 (void)hasFolding(ea.line2, NULL, &ea.line2);
2184 }
2185#endif
2186
2187#ifdef FEAT_QUICKFIX
2188 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002189 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 * option here, so things like % get expanded.
2191 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002192 p = replace_makeprg(&ea, p, cmdlinep);
2193 if (p == NULL)
2194 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195#endif
2196
2197 /*
2198 * Skip to start of argument.
2199 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2200 */
2201 if (ea.cmdidx == CMD_bang)
2202 ea.arg = p;
2203 else
2204 ea.arg = skipwhite(p);
2205
Bram Moolenaar379fb762018-08-30 15:58:28 +02002206 // ":file" cannot be run with an argument when "curbuf_lock" is set
2207 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2208 goto doend;
2209
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 /*
2211 * Check for "++opt=val" argument.
2212 * Must be first, allow ":w ++enc=utf8 !cmd"
2213 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002214 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2216 if (getargopt(&ea) == FAIL && !ni)
2217 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002218 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 goto doend;
2220 }
2221
2222 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2223 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002224 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002226 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002228 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 goto doend;
2230 }
2231 ea.arg = skipwhite(ea.arg + 1);
2232 ea.append = TRUE;
2233 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002234 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 {
2236 ++ea.arg;
2237 ea.usefilter = TRUE;
2238 }
2239 }
2240
2241 if (ea.cmdidx == CMD_read)
2242 {
2243 if (ea.forceit)
2244 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002245 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 ea.forceit = FALSE;
2247 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002248 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 {
2250 ++ea.arg;
2251 ea.usefilter = TRUE;
2252 }
2253 }
2254
2255 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2256 {
2257 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002258 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 {
2260 ++ea.arg;
2261 ++ea.amount;
2262 }
2263 ea.arg = skipwhite(ea.arg);
2264 }
2265
2266 /*
2267 * Check for "+command" argument, before checking for next command.
2268 * Don't do this for ":read !cmd" and ":write !cmd".
2269 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002270 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2272
2273 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002274 * For commands that do not use '|' inside their argument: Check for '|' to
2275 * separate commands and '"' or '#' to start comments.
2276 *
2277 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002278 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002279 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002280 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002282 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2283 {
2284 separate_nextcmd(&ea);
2285 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002286 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002287 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002288 || ea.cmdidx == CMD_global
2289 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002290 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002291#ifdef FEAT_EVAL
2292 || inside_block(&ea)
2293#endif
2294 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
2296 for (p = ea.arg; *p; ++p)
2297 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002298 // Remove one backslash before a newline, so that it's possible to
2299 // pass a newline to the shell and also a newline that is preceded
2300 // with a backslash. This makes it impossible to end a shell
2301 // command in a backslash, but that doesn't appear useful.
2302 // Halving the number of backslashes is incompatible with previous
2303 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002305 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002306 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 {
2308 ea.nextcmd = p + 1;
2309 *p = NUL;
2310 break;
2311 }
2312 }
2313 }
2314
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002315 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002316 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002318 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002319 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002321 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002322 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002323 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002325#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002326 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002327 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002329 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002330 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 }
2332#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002333 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2334 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002335 {
2336 ea.regname = *ea.arg++;
2337#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002338 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002339 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2340 {
kuuote08d7b1c2021-10-04 22:17:36 +01002341 if (!ea.skip)
2342 {
2343 set_expr_line(vim_strsave(ea.arg), &ea);
2344 did_set_expr_line = TRUE;
2345 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002346 ea.arg += STRLEN(ea.arg);
2347 }
2348#endif
2349 ea.arg = skipwhite(ea.arg);
2350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 }
2352
2353 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002354 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 * count, it's a buffer name.
2356 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002357 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002358 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002359 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002361 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002363 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002365 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 goto doend;
2367 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002368 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 {
2370 ea.line2 = n;
2371 if (ea.addr_count == 0)
2372 ea.addr_count = 1;
2373 }
2374 else
2375 {
2376 ea.line1 = ea.line2;
2377 ea.line2 += n - 1;
2378 ++ea.addr_count;
2379 /*
2380 * Be vi compatible: no error message for out of range.
2381 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002382 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 ea.line2 = curbuf->b_ml.ml_line_count;
2384 }
2385 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002386
2387 /*
2388 * Check for flags: 'l', 'p' and '#'.
2389 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002390 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002391 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002392 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2393 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002395 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002396 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 goto doend;
2398 }
2399
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002400 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002402 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 goto doend;
2404 }
2405
2406#ifdef FEAT_EVAL
2407 /*
2408 * Skip the command when it's not going to be executed.
2409 * The commands like :if, :endif, etc. always need to be executed.
2410 * Also make an exception for commands that handle a trailing command
2411 * themselves.
2412 */
2413 if (ea.skip)
2414 {
2415 switch (ea.cmdidx)
2416 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002417 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 case CMD_while:
2419 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002420 case CMD_for:
2421 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 case CMD_if:
2423 case CMD_elseif:
2424 case CMD_else:
2425 case CMD_endif:
2426 case CMD_try:
2427 case CMD_catch:
2428 case CMD_finally:
2429 case CMD_endtry:
2430 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002431 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 break;
2433
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002434 // Commands that handle '|' themselves. Check: A command should
2435 // either have the EX_TRLBAR flag, appear in this list or appear in
2436 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 case CMD_aboveleft:
2438 case CMD_and:
2439 case CMD_belowright:
2440 case CMD_botright:
2441 case CMD_browse:
2442 case CMD_call:
2443 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002444 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 case CMD_delfunction:
2446 case CMD_djump:
2447 case CMD_dlist:
2448 case CMD_dsearch:
2449 case CMD_dsplit:
2450 case CMD_echo:
2451 case CMD_echoerr:
2452 case CMD_echomsg:
2453 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002454 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002456 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002457 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 case CMD_help:
2459 case CMD_hide:
2460 case CMD_ijump:
2461 case CMD_ilist:
2462 case CMD_isearch:
2463 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002464 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 case CMD_keepjumps:
2466 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002467 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 case CMD_leftabove:
2469 case CMD_let:
2470 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002471 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002472 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002474 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002475 case CMD_noautocmd:
2476 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 case CMD_perl:
2478 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002479 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002480 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002481 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 case CMD_return:
2483 case CMD_rightbelow:
2484 case CMD_ruby:
2485 case CMD_silent:
2486 case CMD_smagic:
2487 case CMD_snomagic:
2488 case CMD_substitute:
2489 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002490 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 case CMD_tcl:
2492 case CMD_throw:
2493 case CMD_tilde:
2494 case CMD_topleft:
2495 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002496 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002497 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 case CMD_verbose:
2499 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002500 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 break;
2502
2503 default: goto doend;
2504 }
2505 }
2506#endif
2507
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002508 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 {
2510 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2511 goto doend;
2512 }
2513
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 /*
2515 * Accept buffer name. Cannot be used at the same time with a buffer
2516 * number. Don't do this for a user command.
2517 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002518 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002519 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
2521 /*
2522 * :bdelete, :bwipeout and :bunload take several arguments, separated
2523 * by spaces: find next space (skipping over escaped characters).
2524 * The others take one argument: ignore trailing spaces.
2525 */
2526 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2527 || ea.cmdidx == CMD_bunload)
2528 p = skiptowhite_esc(ea.arg);
2529 else
2530 {
2531 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002532 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 --p;
2534 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002535 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002536 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002537 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 goto doend;
2539 ea.addr_count = 1;
2540 ea.arg = skipwhite(p);
2541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002543 // The :try command saves the emsg_silent flag, reset it here when
2544 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002545 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002546 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002547 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002548 if (emsg_silent < 0)
2549 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002550 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002551 }
2552
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002554 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556
Bram Moolenaar958636c2014-10-21 20:01:58 +02002557 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 {
2559 /*
2560 * Execute a user-defined command.
2561 */
2562 do_ucmd(&ea);
2563 }
2564 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 {
2566 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002567 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 */
2569 ea.errmsg = NULL;
2570 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2571 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002572 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 }
2574
2575#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002577 // Not if this was a command that wasn't executed or :endif.
Bram Moolenaar16531552020-02-08 16:00:46 +01002578 if (getline_equal(ea.getline, ea.cookie, getsourceline)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002579 && current_sctx.sc_sid > 0
2580 && ea.cmdidx != CMD_endif
2581 && (cstack->cs_idx < 0
2582 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002583 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 /*
2586 * If the command just executed called do_cmdline(), any throw or ":return"
2587 * or ":finish" encountered there must also check the cstack of the still
2588 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2589 * exception, or reanimate a returned function or finished script file and
2590 * return or finish it again.
2591 */
2592 if (need_rethrow)
2593 do_throw(cstack);
2594 else if (check_cstack)
2595 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002596 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002598 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 && current_func_returned())
2600 do_return(&ea, TRUE, FALSE, NULL);
2601 }
2602 need_rethrow = check_cstack = FALSE;
2603#endif
2604
2605doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002606 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002607 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002609 curwin->w_cursor.col = 0;
2610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611
2612 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2613 {
2614 if (sourcing)
2615 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002616 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 {
2618 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002619 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002621 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 }
2623 emsg(errormsg);
2624 }
2625#ifdef FEAT_EVAL
2626 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002627 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2628 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002629
2630 if (did_set_expr_line)
2631 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632#endif
2633
Bram Moolenaare1004402020-10-24 20:49:43 +02002634 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002636 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002638 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 ea.nextcmd = NULL;
2640
2641#ifdef FEAT_EVAL
2642 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002643 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644#endif
2645
2646 return ea.nextcmd;
2647}
2648#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002649 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650#endif
2651
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002652static char ex_error_buf[MSG_BUF_LEN];
2653
2654/*
2655 * Return an error message with argument included.
2656 * Uses a static buffer, only the last error will be kept.
2657 * "msg" will be translated, caller should use N_().
2658 */
2659 char *
2660ex_errmsg(char *msg, char_u *arg)
2661{
2662 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2663 return ex_error_buf;
2664}
2665
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002667 * Handle a range without a command.
2668 * Returns an error message on failure.
2669 */
2670 char *
2671ex_range_without_command(exarg_T *eap)
2672{
2673 char *errormsg = NULL;
2674
2675 if ((*eap->cmd == '|' || (exmode_active && eap->line1 != eap->line2))
2676#ifdef FEAT_EVAL
2677 && !in_vim9script()
2678#endif
2679 )
2680 {
2681 eap->cmdidx = CMD_print;
2682 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2683 if ((errormsg = invalid_range(eap)) == NULL)
2684 {
2685 correct_range(eap);
2686 ex_print(eap);
2687 }
2688 }
2689 else if (eap->addr_count != 0)
2690 {
2691 if (eap->line2 > curbuf->b_ml.ml_line_count)
2692 {
2693 // With '-' in 'cpoptions' a line number past the file is an
2694 // error, otherwise put it at the end of the file.
2695 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2696 eap->line2 = -1;
2697 else
2698 eap->line2 = curbuf->b_ml.ml_line_count;
2699 }
2700
2701 if (eap->line2 < 0)
2702 errormsg = _(e_invalid_range);
2703 else
2704 {
2705 if (eap->line2 == 0)
2706 curwin->w_cursor.lnum = 1;
2707 else
2708 curwin->w_cursor.lnum = eap->line2;
2709 beginline(BL_SOL | BL_FIX);
2710 }
2711 }
2712 return errormsg;
2713}
2714
2715/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002716 * Check for an Ex command with optional tail.
2717 * If there is a match advance "pp" to the argument and return TRUE.
2718 * If "noparen" is TRUE do not recognize the command followed by "(".
2719 */
2720 static int
2721checkforcmd_opt(
2722 char_u **pp, // start of command
2723 char *cmd, // name of command
2724 int len, // required length
2725 int noparen)
2726{
2727 int i;
2728
2729 for (i = 0; cmd[i] != NUL; ++i)
2730 if (((char_u *)cmd)[i] != (*pp)[i])
2731 break;
2732 if (i >= len && !isalpha((*pp)[i])
2733 && (*pp)[i] != '_' && (!noparen || (*pp)[i] != '('))
2734 {
2735 *pp = skipwhite(*pp + i);
2736 return TRUE;
2737 }
2738 return FALSE;
2739}
2740
2741/*
2742 * Check for an Ex command with optional tail.
2743 * If there is a match advance "pp" to the argument and return TRUE.
2744 */
2745 int
2746checkforcmd(
2747 char_u **pp, // start of command
2748 char *cmd, // name of command
2749 int len) // required length
2750{
2751 return checkforcmd_opt(pp, cmd, len, FALSE);
2752}
2753
2754/*
2755 * Check for an Ex command with optional tail, not followed by "(".
2756 * If there is a match advance "pp" to the argument and return TRUE.
2757 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002758 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002759checkforcmd_noparen(
2760 char_u **pp, // start of command
2761 char *cmd, // name of command
2762 int len) // required length
2763{
2764 return checkforcmd_opt(pp, cmd, len, TRUE);
2765}
2766
2767/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002768 * Parse and skip over command modifiers:
2769 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002770 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002771 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002772 * When "skip_only" is TRUE the global variables are not changed, except for
2773 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002774 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2775 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002776 * Call apply_cmdmod() to get the side effects of the modifiers:
2777 * - Increment "sandbox" for ":sandbox"
2778 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002779 * - set msg_silent for ":silent"
2780 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002781 * Return FAIL when the command is not to be executed.
2782 * May set "errormsg" to an error message.
2783 */
2784 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002785parse_command_modifiers(
2786 exarg_T *eap,
2787 char **errormsg,
2788 cmdmod_T *cmod,
2789 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002790{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002791 char_u *p;
2792 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002793
Bram Moolenaare1004402020-10-24 20:49:43 +02002794 CLEAR_POINTER(cmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002795
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002796 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002797 for (;;)
2798 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002799 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002800 {
2801 if (*eap->cmd == ':')
2802 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002803 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002804 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002805
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002806 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002807 if (*eap->cmd == NUL && exmode_active
2808 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2809 || getline_equal(eap->getline, eap->cookie, getexline))
2810 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2811 {
2812 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002813 if (!skip_only)
2814 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002815 }
2816
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002817 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002818 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002819 {
2820 // a comment ends at a NL
2821 if (eap->nextcmd == NULL)
2822 {
2823 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2824 if (eap->nextcmd != NULL)
2825 ++eap->nextcmd;
2826 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002827 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002828 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002829 if (*eap->cmd == NUL)
2830 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002831 if (!skip_only)
2832 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002833 return FAIL;
2834 }
2835
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002836 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002837
2838 // In Vim9 script a variable can shadow a command modifier:
2839 // verbose = 123
2840 // verbose += 123
2841 // silent! verbose = func()
2842 // verbose.member = 2
2843 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002844 // But not:
2845 // verbose [a, b] = list
Bram Moolenaar17126b12021-01-07 22:03:02 +01002846 if (in_vim9script())
2847 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002848 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002849
2850 for (s = p; ASCII_ISALPHA(*s); ++s)
2851 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002852 n = skipwhite(s);
2853 if (vim_strchr((char_u *)".=", *n) != NULL
2854 || *s == '['
2855 || (*n != NUL && n[1] == '='))
Bram Moolenaar17126b12021-01-07 22:03:02 +01002856 break;
2857 }
2858
Bram Moolenaareffed932018-08-14 13:38:17 +02002859 switch (*p)
2860 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002861 // When adding an entry, also modify cmd_exists().
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002862 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002863 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002864 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002865 continue;
2866
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002867 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002868 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002869 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002870 continue;
2871 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002872 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002873 {
2874#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002875 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002876#endif
2877 continue;
2878 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002879 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002880 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002881 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002882 continue;
2883
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002884 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002885 break;
2886#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002887 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002888#endif
2889 continue;
2890
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002891 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002892 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002893 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002894 continue;
2895 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002896 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002897 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002898 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002899 continue;
2900 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002901 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002902 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002903 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002904 continue;
2905 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002906 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002907 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002908 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002909 continue;
2910
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002911 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002912 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02002913 char_u *reg_pat;
2914 char_u *nulp = NULL;
2915 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002916
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002917 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002918 || *p == NUL
2919 || (ends_excmd(*p)
2920#ifdef FEAT_EVAL
2921 // in ":filter #pat# cmd" # does not
2922 // start a comment
2923 && (!in_vim9script() || VIM_ISWHITE(p[1]))
2924#endif
2925 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02002926 break;
2927 if (*p == '!')
2928 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002929 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002930 p = skipwhite(p + 1);
2931 if (*p == NUL || ends_excmd(*p))
2932 break;
2933 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002934#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002935 // Avoid that "filter(arg)" is recognized.
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +01002936 if (in_vim9script() && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002937 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002938#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002939 if (skip_only)
2940 p = skip_vimgrep_pat(p, NULL, NULL);
2941 else
2942 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02002943 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
2944 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02002945 if (p == NULL || *p == NUL)
2946 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002947 if (!skip_only)
2948 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002949 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002950 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002951 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002952 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02002953 // restore the character overwritten by NUL
2954 if (nulp != NULL)
2955 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002956 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002957 eap->cmd = p;
2958 continue;
2959 }
2960
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002961 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002962 case 'h': if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02002963 || *p == NUL || ends_excmd(*p))
2964 break;
2965 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002966 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002967 continue;
2968
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002969 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002970 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002971 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002972 continue;
2973 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002974 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
2975 {
2976 if (ends_excmd2(p, eap->cmd))
2977 {
2978 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02002979 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002980 return FAIL;
2981 }
2982 cmod->cmod_flags |= CMOD_LEGACY;
2983 continue;
2984 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002985
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002986 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002987 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002988 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002989 continue;
2990
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002991 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002992 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002993 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002994 continue;
2995 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002996 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002997 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002998 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002999 continue;
3000
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003001 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003002 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003003 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003004 continue;
3005
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003006 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003007 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003008 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003009 continue;
3010 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003011 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003012 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003013 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003014 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3015 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003016 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003017 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003018 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003019 }
3020 continue;
3021
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003022 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003023 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003024 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003025 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003026 long tabnr = get_address(eap, &eap->cmd,
3027 ADDR_TABS, eap->skip,
3028 skip_only, FALSE, 1);
3029 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003030 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003031 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003032 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003033 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3034 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003035 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003036 return FAIL;
3037 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003038 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003039 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003040 }
3041 eap->cmd = p;
3042 continue;
3043 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003044 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003045 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003046 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003047 continue;
3048
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003049 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003050 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003051 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003052 continue;
3053
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003054 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003055 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003056 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003057 continue;
3058 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003059 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003060 {
3061 if (ends_excmd2(p, eap->cmd))
3062 {
3063 *errormsg =
3064 _(e_vim9cmd_must_be_followed_by_command);
3065 return FAIL;
3066 }
3067 cmod->cmod_flags |= CMOD_VIM9CMD;
3068 continue;
3069 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003070 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003071 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003072 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02003073 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003074 else
Bram Moolenaare1004402020-10-24 20:49:43 +02003075 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003076 eap->cmd = p;
3077 continue;
3078 }
3079 break;
3080 }
3081
3082 return OK;
3083}
3084
3085/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003086 * Return TRUE if "cmod" has anything set.
3087 */
3088 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003089has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003090{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003091 return (cmod->cmod_flags != 0 && (!ignore_silent
3092 || (cmod->cmod_flags
3093 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003094 || cmod->cmod_split != 0
3095 || cmod->cmod_verbose != 0
3096 || cmod->cmod_tab != 0
3097 || cmod->cmod_filter_regmatch.regprog != NULL;
3098}
3099
3100/*
3101 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3102 */
3103 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003104cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003105{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003106 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003107 {
3108 emsg(_(e_misplaced_command_modifier));
3109 return TRUE;
3110 }
3111 return FALSE;
3112}
3113
3114/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003115 * Apply the command modifiers. Saves current state in "cmdmod", call
3116 * undo_cmdmod() later.
3117 */
3118 void
3119apply_cmdmod(cmdmod_T *cmod)
3120{
3121#ifdef HAVE_SANDBOX
3122 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3123 {
3124 ++sandbox;
3125 cmod->cmod_did_sandbox = TRUE;
3126 }
3127#endif
3128 if (cmod->cmod_verbose > 0)
3129 {
3130 if (cmod->cmod_verbose_save == 0)
3131 cmod->cmod_verbose_save = p_verbose + 1;
3132 p_verbose = cmod->cmod_verbose;
3133 }
3134
3135 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3136 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003137 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003138 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003139 cmod->cmod_save_msg_scroll = msg_scroll;
3140 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003141 if (cmod->cmod_flags & CMOD_SILENT)
3142 ++msg_silent;
3143 if (cmod->cmod_flags & CMOD_UNSILENT)
3144 msg_silent = 0;
3145
3146 if (cmod->cmod_flags & CMOD_ERRSILENT)
3147 {
3148 ++emsg_silent;
3149 ++cmod->cmod_did_esilent;
3150 }
3151
Bram Moolenaare1004402020-10-24 20:49:43 +02003152 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003153 {
3154 // Set 'eventignore' to "all".
3155 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003156 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003157 set_string_option_direct((char_u *)"ei", -1,
3158 (char_u *)"all", OPT_FREE, SID_NONE);
3159 }
3160}
3161
3162/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003163 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003164 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003165 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003166undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003167{
Bram Moolenaare1004402020-10-24 20:49:43 +02003168 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003169 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003170 p_verbose = cmod->cmod_verbose_save - 1;
3171 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003172 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003173
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003174#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003175 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003176 {
3177 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003178 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003179 }
3180#endif
3181
Bram Moolenaare1004402020-10-24 20:49:43 +02003182 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003183 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003184 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003185 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3186 OPT_FREE, SID_NONE);
3187 free_string_option(cmod->cmod_save_ei);
3188 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003189 }
3190
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003191 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003192
Bram Moolenaare1004402020-10-24 20:49:43 +02003193 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003194 {
3195 // messages could be enabled for a serious error, need to check if the
3196 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003197 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3198 msg_silent = cmod->cmod_save_msg_silent - 1;
3199 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003200 if (emsg_silent < 0)
3201 emsg_silent = 0;
3202 // Restore msg_scroll, it's set by file I/O commands, even when no
3203 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003204 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003205
3206 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3207 // somewhere in the line. Put it back in the first column.
3208 if (redirecting())
3209 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003210
Bram Moolenaare1004402020-10-24 20:49:43 +02003211 cmod->cmod_save_msg_silent = 0;
3212 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003213 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003214}
3215
3216/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003217 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003218 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003219 * Return FAIL and set "errormsg" or return OK.
3220 */
3221 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003222parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003223{
3224 int address_count = 1;
3225 linenr_T lnum;
3226
3227 // Repeat for all ',' or ';' separated addresses.
3228 for (;;)
3229 {
3230 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003231 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003232 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003233 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003234 eap->addr_count == 0, address_count++);
3235 if (eap->cmd == NULL) // error detected
3236 return FAIL;
3237 if (lnum == MAXLNUM)
3238 {
3239 if (*eap->cmd == '%') // '%' - all lines
3240 {
3241 ++eap->cmd;
3242 switch (eap->addr_type)
3243 {
3244 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003245 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003246 eap->line1 = 1;
3247 eap->line2 = curbuf->b_ml.ml_line_count;
3248 break;
3249 case ADDR_LOADED_BUFFERS:
3250 {
3251 buf_T *buf = firstbuf;
3252
3253 while (buf->b_next != NULL
3254 && buf->b_ml.ml_mfp == NULL)
3255 buf = buf->b_next;
3256 eap->line1 = buf->b_fnum;
3257 buf = lastbuf;
3258 while (buf->b_prev != NULL
3259 && buf->b_ml.ml_mfp == NULL)
3260 buf = buf->b_prev;
3261 eap->line2 = buf->b_fnum;
3262 break;
3263 }
3264 case ADDR_BUFFERS:
3265 eap->line1 = firstbuf->b_fnum;
3266 eap->line2 = lastbuf->b_fnum;
3267 break;
3268 case ADDR_WINDOWS:
3269 case ADDR_TABS:
3270 if (IS_USER_CMDIDX(eap->cmdidx))
3271 {
3272 eap->line1 = 1;
3273 eap->line2 = eap->addr_type == ADDR_WINDOWS
3274 ? LAST_WIN_NR : LAST_TAB_NR;
3275 }
3276 else
3277 {
3278 // there is no Vim command which uses '%' and
3279 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003280 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003281 return FAIL;
3282 }
3283 break;
3284 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003285 case ADDR_UNSIGNED:
3286 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003287 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003288 return FAIL;
3289 case ADDR_ARGUMENTS:
3290 if (ARGCOUNT == 0)
3291 eap->line1 = eap->line2 = 0;
3292 else
3293 {
3294 eap->line1 = 1;
3295 eap->line2 = ARGCOUNT;
3296 }
3297 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003298 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003299#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003300 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003301 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003302 if (eap->line2 == 0)
3303 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003304#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003305 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003306 case ADDR_NONE:
3307 // Will give an error later if a range is found.
3308 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003309 }
3310 ++eap->addr_count;
3311 }
3312 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3313 {
3314 pos_T *fp;
3315
3316 // '*' - visual area
3317 if (eap->addr_type != ADDR_LINES)
3318 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003319 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003320 return FAIL;
3321 }
3322
3323 ++eap->cmd;
3324 if (!eap->skip)
3325 {
3326 fp = getmark('<', FALSE);
3327 if (check_mark(fp) == FAIL)
3328 return FAIL;
3329 eap->line1 = fp->lnum;
3330 fp = getmark('>', FALSE);
3331 if (check_mark(fp) == FAIL)
3332 return FAIL;
3333 eap->line2 = fp->lnum;
3334 ++eap->addr_count;
3335 }
3336 }
3337 }
3338 else
3339 eap->line2 = lnum;
3340 eap->addr_count++;
3341
3342 if (*eap->cmd == ';')
3343 {
3344 if (!eap->skip)
3345 {
3346 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003347 // Don't leave the cursor on an illegal line or column, but do
3348 // accept zero as address, so 0;/PATTERN/ works correctly.
3349 if (eap->line2 > 0)
3350 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003351 }
3352 }
3353 else if (*eap->cmd != ',')
3354 break;
3355 ++eap->cmd;
3356 }
3357
3358 // One address given: set start and end lines.
3359 if (eap->addr_count == 1)
3360 {
3361 eap->line1 = eap->line2;
3362 // ... but only implicit: really no address given
3363 if (lnum == MAXLNUM)
3364 eap->addr_count = 0;
3365 }
3366 return OK;
3367}
3368
3369/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003370 * Append "cmd" to the error message in IObuff.
3371 * Takes care of limiting the length and handling 0xa0, which would be
3372 * invisible otherwise.
3373 */
3374 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003375append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003376{
3377 char_u *s = cmd;
3378 char_u *d;
3379
3380 STRCAT(IObuff, ": ");
3381 d = IObuff + STRLEN(IObuff);
3382 while (*s != NUL && d - IObuff < IOSIZE - 7)
3383 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003384 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003385 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003386 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003387 STRCPY(d, "<a0>");
3388 d += 4;
3389 }
3390 else
3391 MB_COPY_CHAR(s, d);
3392 }
3393 *d = NUL;
3394}
3395
3396/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003397 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3398 * the name. Otherwise just return "start".
3399 */
3400 char_u *
3401skip_option_env_lead(char_u *start)
3402{
3403 char_u *name = start;
3404
3405 if (*start == '&')
3406 {
3407 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3408 name += 3;
3409 else
3410 name += 1;
3411 }
3412 else if (*start == '$')
3413 name += 1;
3414 return name;
3415}
3416
3417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003419 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003420 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003421 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 *
3423 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3424 * assignment without "let". Sets eap->cmdidx to the command while returning
3425 * "eap->cmd".
3426 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 * Returns NULL for an ambiguous user command.
3428 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 char_u *
3430find_ex_command(
3431 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003432 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003433 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003434 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435{
3436 int len;
3437 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003438 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003439#ifndef FEAT_EVAL
3440 int vim9 = FALSE;
3441#else
3442 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444 /*
3445 * Recognize a Vim9 script function/method call and assignment:
3446 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3447 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003448 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003449 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003451 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003453 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003454 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003455 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003456 int oplen;
3457 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003458 char_u *swp;
3459
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003460 if (*eap->cmd == '&'
3461 || *eap->cmd == '$'
3462 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003463 && (valid_yank_reg(eap->cmd[1], FALSE)
3464 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003465 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003466 if (*eap->cmd == '&')
3467 {
3468 p = eap->cmd + 1;
3469 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3470 p += 2;
3471 p = to_name_end(p, FALSE);
3472 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003473 else if (*eap->cmd == '$')
3474 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003475 else
3476 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003477 if (ends_excmd(*skipwhite(p)))
3478 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003479 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3480 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003481 eap->cmdidx = CMD_eval;
3482 return eap->cmd;
3483 }
3484 // "&option" can be followed by "->" or "=", check below
3485 }
3486
3487 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003489 if (
3490 // "(..." is an expression.
3491 // "funcname(" is always a function call.
3492 *p == '('
3493 || (p == eap->cmd
3494 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003495 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003496 *eap->cmd == '{'
3497 // "'string'->func()" is an expression.
3498 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003499 // '"string"->func()' is an expression.
3500 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003501 // "g:varname" is an expression.
3502 || eap->cmd[1] == ':'
3503 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003504 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003505 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003506 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003507 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3508 {
3509 // "{" by itself is the start of a block.
3510 eap->cmdidx = CMD_block;
3511 return eap->cmd + 1;
3512 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003513 eap->cmdidx = CMD_eval;
3514 return eap->cmd;
3515 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003516
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003517 if (p != eap->cmd && (
3518 // "varname[]" is an expression.
3519 *p == '['
3520 // "varname.key" is an expression.
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02003521 || (*p == '.' && (ASCII_ISALPHA(p[1])
3522 || p[1] == '_'))))
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003523 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003524 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003525
3526 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003527 // Skip over the whole thing, it can be:
3528 // name.member = val
3529 // name[a : b] = val
3530 // name[idx] = val
3531 // name[idx].member = val
3532 // etc.
3533 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003534 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003535 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003536 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003537 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003538 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003539 || (after[0] == '.' && after[1] == '.'
3540 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003541 eap->cmdidx = CMD_var;
3542 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003543 --emsg_silent;
3544 return eap->cmd;
3545 }
3546
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003547 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3548 // an assignment.
3549 // If there is no line break inside the "[...]" then "p" is
3550 // advanced to after the "]" by to_name_const_end(): check if a "="
3551 // follows.
3552 // If "[...]" has a line break "p" still points at the "[" and it
3553 // can't be an assignment.
3554 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003555 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003556 char_u *eq;
3557
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003558 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003559 if (p == eap->cmd && *p == '[')
3560 {
3561 int count = 0;
3562 int semicolon = FALSE;
3563
3564 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3565 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003566 eq = p;
3567 if (eq != NULL)
3568 {
3569 eq = skipwhite(eq);
3570 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3571 ++eq;
3572 }
3573 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003574 {
3575 eap->cmdidx = CMD_eval;
3576 return eap->cmd;
3577 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003578 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003579 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003580 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003581 return eap->cmd;
3582 }
3583 }
3584
3585 // Recognize an assignment if we recognize the variable name:
3586 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003587 // "@r = expr"
3588 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003589 // "var = expr" where "var" is a variable name or we are skipping
3590 // (variable declaration might have been skipped).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003591 oplen = assignment_len(skipwhite(p), &heredoc);
3592 if (oplen > 0)
3593 {
3594 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3595 || *eap->cmd == '&'
3596 || *eap->cmd == '$'
3597 || *eap->cmd == '@'
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003598 || eap->skip
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003599 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003600 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003601 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003602 return eap->cmd;
3603 }
3604 }
3605
3606 // Recognize using a type for a w:, b:, t: or g: variable:
3607 // "w:varname: number = 123".
3608 if (eap->cmd[1] == ':' && *p == ':')
3609 {
3610 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003611 return eap->cmd;
3612 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003613 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003614
Bram Moolenaar7b829262021-10-13 15:04:34 +01003615 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3616 // an expression. A global/substitute/list command needs to use a
3617 // longer name.
3618 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3619 {
3620 eap->cmdidx = CMD_eval;
3621 return eap->cmd;
3622 }
3623
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003624 // If it is an ID it might be a variable with an operator on the next
3625 // line, if the variable exists it can't be an Ex command.
3626 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003627 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003628 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3629 {
3630 eap->cmdidx = CMD_eval;
3631 return eap->cmd;
3632 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003633
3634 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003635 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3636 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003637 {
3638 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3639 return eap->cmd + 2;
3640 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 }
3642#endif
3643
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 /*
3645 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003646 * Exceptions:
Bram Moolenaar10b94212021-02-19 21:42:57 +01003647 * - The 'k' command can directly be followed by any character.
3648 * But it is not used in Vim9 script.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003650 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003652 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 */
3654 p = eap->cmd;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003655 if (!vim9 && *p == 'k')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 {
3657 eap->cmdidx = CMD_k;
3658 ++p;
3659 }
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003660 else if (!vim9
3661 && p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003662 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3663 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 || p[1] == 'g'
3665 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3666 || p[1] == 'I'
3667 || (p[1] == 'r' && p[2] != 'e')))
3668 {
3669 eap->cmdidx = CMD_substitute;
3670 ++p;
3671 }
3672 else
3673 {
3674 while (ASCII_ISALPHA(*p))
3675 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003676 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003677 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003679 while (ASCII_ISALNUM(*p))
3680 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 }
3682 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3683 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003684 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 ++p;
3686 while (ASCII_ISALPHA(*p))
3687 ++p;
3688 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003689
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003690 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003691 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 ++p;
3693 len = (int)(p - eap->cmd);
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003694 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003695 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003696 // Check for ":dl", ":dell", etc. to ":deletel": that's
3697 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003698 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003699 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003700 break;
3701 if (i == len - 1)
3702 {
3703 --len;
3704 if (p[-1] == 'l')
3705 eap->flags |= EXFLAG_LIST;
3706 else
3707 eap->flags |= EXFLAG_PRINT;
3708 }
3709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003711 if (ASCII_ISLOWER(eap->cmd[0]))
3712 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003713 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003714 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003715
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003716 if (command_count != (int)CMD_SIZE)
3717 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003718 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003719 getout(1);
3720 }
3721
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003722 // Use a precomputed index for fast look-up in cmdnames[]
3723 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003724 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3725 if (ASCII_ISLOWER(c2))
3726 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3727 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003728 else if (ASCII_ISUPPER(eap->cmd[0]))
3729 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003731 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732
3733 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3734 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3735 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3736 (size_t)len) == 0)
3737 {
3738#ifdef FEAT_EVAL
3739 if (full != NULL
3740 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3741 *full = TRUE;
3742#endif
3743 break;
3744 }
3745
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003746 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003747 // 'cpoptions'.
3748 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3749 p = eap->cmd;
3750
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003751 // Look for a user defined command as a last resort. Let ":Print" be
3752 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003753 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3754 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003756 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 while (ASCII_ISALNUM(*p))
3758 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003759 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003761 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 eap->cmdidx = CMD_SIZE;
3763 }
3764
Bram Moolenaar373863e2020-09-26 17:20:53 +02003765 // ":fina" means ":finally" for backwards compatibility.
3766 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3767 eap->cmdidx = CMD_finally;
3768
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003769#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003770 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003771 && vim9
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +02003772 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003773 && (eap->cmdidx < 0 ||
3774 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003775 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003776 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3777
3778 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003779 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003780 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003781 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003782#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003783
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 return p;
3785}
3786
3787#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003788static struct cmdmod
3789{
3790 char *name;
3791 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003792 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003793} cmdmods[] = {
3794 {"aboveleft", 3, FALSE},
3795 {"belowright", 3, FALSE},
3796 {"botright", 2, FALSE},
3797 {"browse", 3, FALSE},
3798 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003799 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003800 {"hide", 3, FALSE},
3801 {"keepalt", 5, FALSE},
3802 {"keepjumps", 5, FALSE},
3803 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003804 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003805 {"leftabove", 5, FALSE},
3806 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003807 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003808 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003809 {"rightbelow", 6, FALSE},
3810 {"sandbox", 3, FALSE},
3811 {"silent", 3, FALSE},
3812 {"tab", 3, TRUE},
3813 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003814 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003815 {"verbose", 4, TRUE},
3816 {"vertical", 4, FALSE},
3817};
3818
3819/*
3820 * Return length of a command modifier (including optional count).
3821 * Return zero when it's not a modifier.
3822 */
3823 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003824modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003825{
3826 int i, j;
3827 char_u *p = cmd;
3828
3829 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02003830 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02003831 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003832 {
3833 for (j = 0; p[j] != NUL; ++j)
3834 if (p[j] != cmdmods[i].name[j])
3835 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003836 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003837 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003838 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003839 }
3840 return 0;
3841}
3842
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843/*
3844 * Return > 0 if an Ex command "name" exists.
3845 * Return 2 if there is an exact match.
3846 * Return 3 if there is an ambiguous match.
3847 */
3848 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003849cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850{
3851 exarg_T ea;
3852 int full = FALSE;
3853 int i;
3854 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003855 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003857 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02003858 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
3860 for (j = 0; name[j] != NUL; ++j)
3861 if (name[j] != cmdmods[i].name[j])
3862 break;
3863 if (name[j] == NUL && j >= cmdmods[i].minlen)
3864 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3865 }
3866
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003867 // Check built-in commands and user defined commands.
3868 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003869 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003872 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003874 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3875 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003876 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003877 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3879}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003880
3881/*
3882 * "fullcommand" function
3883 */
3884 void
3885f_fullcommand(typval_T *argvars, typval_T *rettv)
3886{
3887 exarg_T ea;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003888 char_u *name;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003889 char_u *p;
3890
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003891 rettv->v_type = VAR_STRING;
3892 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003893
3894 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
3895 return;
3896
3897 name = argvars[0].vval.v_string;
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003898 if (name == NULL)
3899 return;
3900
zeertzjqc024ed92022-01-04 16:22:52 +00003901 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003902 name++;
3903 name = skip_range(name, TRUE, NULL);
3904
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003905 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
3906 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003907 ea.addr_count = 0;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003908 p = find_ex_command(&ea, NULL, NULL, NULL);
3909 if (p == NULL || ea.cmdidx == CMD_SIZE)
3910 return;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003911 if (in_vim9script())
3912 {
3913 int res;
3914
3915 ++emsg_silent;
3916 res = not_in_vim9(&ea);
3917 --emsg_silent;
3918
3919 if (res == FAIL)
3920 return;
3921 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003922
3923 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02003924 ? get_user_command_name(ea.useridx, ea.cmdidx)
3925 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003926}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927#endif
3928
Bram Moolenaard0190392019-08-23 21:17:35 +02003929 cmdidx_T
3930excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931{
Bram Moolenaard0190392019-08-23 21:17:35 +02003932 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933
Bram Moolenaard0190392019-08-23 21:17:35 +02003934 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3935 idx = (cmdidx_T)((int)idx + 1))
3936 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 break;
3938
Bram Moolenaard0190392019-08-23 21:17:35 +02003939 return idx;
3940}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941
Bram Moolenaard0190392019-08-23 21:17:35 +02003942 long
3943excmd_get_argt(cmdidx_T idx)
3944{
3945 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946}
3947
3948/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003949 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 *
3951 * Backslashed delimiters after / or ? will be skipped, and commands will
3952 * not be expanded between /'s and ?'s or after "'".
3953 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003954 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 * Returns the "cmd" pointer advanced to beyond the range.
3956 */
3957 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003958skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00003959 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003960 int skip_star, // skip "*" used for Visual range
3961 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00003963 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003964 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003966 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003968 if (*cmd == '\\')
3969 {
3970 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3971 ++cmd;
3972 else
3973 break;
3974 }
3975 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00003977 char_u *p = cmd;
3978
3979 // a quote is only valid at the start or after a separator
3980 while (p > cmd_start)
3981 {
3982 --p;
3983 if (!VIM_ISWHITE(*p))
3984 break;
3985 }
3986 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
3987 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 if (*++cmd == NUL && ctx != NULL)
3989 *ctx = EXPAND_NOTHING;
3990 }
3991 else if (*cmd == '/' || *cmd == '?')
3992 {
3993 delim = *cmd++;
3994 while (*cmd != NUL && *cmd != delim)
3995 if (*cmd++ == '\\' && *cmd != NUL)
3996 ++cmd;
3997 if (*cmd == NUL && ctx != NULL)
3998 *ctx = EXPAND_NOTHING;
3999 }
4000 if (*cmd != NUL)
4001 ++cmd;
4002 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004003
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004004 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004005 while (*cmd == ':')
4006 cmd = skipwhite(cmd + 1);
4007
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004008 // Skip "*" used for Visual range.
4009 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4010 cmd = skipwhite(cmd + 1);
4011
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 return cmd;
4013}
4014
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004015 static void
4016addr_error(cmd_addr_T addr_type)
4017{
4018 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004019 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004020 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004021 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004022}
4023
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004025 * Return the default address for an address type.
4026 */
4027 static linenr_T
4028default_address(exarg_T *eap)
4029{
4030 linenr_T lnum = 0;
4031
4032 switch (eap->addr_type)
4033 {
4034 case ADDR_LINES:
4035 case ADDR_OTHER:
4036 // Default is the cursor line number. Avoid using an invalid
4037 // line number though.
4038 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4039 lnum = curbuf->b_ml.ml_line_count;
4040 else
4041 lnum = curwin->w_cursor.lnum;
4042 break;
4043 case ADDR_WINDOWS:
4044 lnum = CURRENT_WIN_NR;
4045 break;
4046 case ADDR_ARGUMENTS:
4047 lnum = curwin->w_arg_idx + 1;
4048 if (lnum > ARGCOUNT)
4049 lnum = ARGCOUNT;
4050 break;
4051 case ADDR_LOADED_BUFFERS:
4052 case ADDR_BUFFERS:
4053 lnum = curbuf->b_fnum;
4054 break;
4055 case ADDR_TABS:
4056 lnum = CURRENT_TAB_NR;
4057 break;
4058 case ADDR_TABS_RELATIVE:
4059 case ADDR_UNSIGNED:
4060 lnum = 1;
4061 break;
4062 case ADDR_QUICKFIX:
4063#ifdef FEAT_QUICKFIX
4064 lnum = qf_get_cur_idx(eap);
4065#endif
4066 break;
4067 case ADDR_QUICKFIX_VALID:
4068#ifdef FEAT_QUICKFIX
4069 lnum = qf_get_cur_valid_idx(eap);
4070#endif
4071 break;
4072 case ADDR_NONE:
4073 // Will give an error later if a range is found.
4074 break;
4075 }
4076 return lnum;
4077}
4078
4079/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004080 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 *
4082 * Set ptr to the next character after the part that was interpreted.
4083 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004084 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 *
4086 * Return MAXLNUM when no Ex address was found.
4087 */
4088 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004089get_address(
4090 exarg_T *eap UNUSED,
4091 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004092 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004093 int skip, // only skip the address, don't use it
4094 int silent, // no errors or side effects
4095 int to_other_file, // flag: may jump to other file
4096 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097{
4098 int c;
4099 int i;
4100 long n;
4101 char_u *cmd;
4102 pos_T pos;
4103 pos_T *fp;
4104 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004105 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106
4107 cmd = skipwhite(*ptr);
4108 lnum = MAXLNUM;
4109 do
4110 {
4111 switch (*cmd)
4112 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004113 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004114 ++cmd;
4115 switch (addr_type)
4116 {
4117 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004118 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 lnum = curwin->w_cursor.lnum;
4120 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004121 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004122 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004123 break;
4124 case ADDR_ARGUMENTS:
4125 lnum = curwin->w_arg_idx + 1;
4126 break;
4127 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004128 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004129 lnum = curbuf->b_fnum;
4130 break;
4131 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004132 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004133 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004134 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004135 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004136 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004137 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004138 cmd = NULL;
4139 goto error;
4140 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004141 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004142#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004143 lnum = qf_get_cur_idx(eap);
4144#endif
4145 break;
4146 case ADDR_QUICKFIX_VALID:
4147#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004148 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004149#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004150 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004151 }
4152 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004154 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004155 ++cmd;
4156 switch (addr_type)
4157 {
4158 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004159 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 lnum = curbuf->b_ml.ml_line_count;
4161 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004162 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004163 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004164 break;
4165 case ADDR_ARGUMENTS:
4166 lnum = ARGCOUNT;
4167 break;
4168 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004169 buf = lastbuf;
4170 while (buf->b_ml.ml_mfp == NULL)
4171 {
4172 if (buf->b_prev == NULL)
4173 break;
4174 buf = buf->b_prev;
4175 }
4176 lnum = buf->b_fnum;
4177 break;
4178 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004179 lnum = lastbuf->b_fnum;
4180 break;
4181 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004182 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004183 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004184 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004185 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004186 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004187 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004188 cmd = NULL;
4189 goto error;
4190 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004191 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004192#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004193 lnum = qf_get_size(eap);
4194 if (lnum == 0)
4195 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004196#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004197 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004198 case ADDR_QUICKFIX_VALID:
4199#ifdef FEAT_QUICKFIX
4200 lnum = qf_get_valid_size(eap);
4201 if (lnum == 0)
4202 lnum = 1;
4203#endif
4204 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004205 }
4206 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004208 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004209 if (*++cmd == NUL)
4210 {
4211 cmd = NULL;
4212 goto error;
4213 }
4214 if (addr_type != ADDR_LINES)
4215 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004216 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004217 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004218 goto error;
4219 }
4220 if (skip)
4221 ++cmd;
4222 else
4223 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004224 // Only accept a mark in another file when it is
4225 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004226 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4227 ++cmd;
4228 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004229 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004230 lnum = curwin->w_cursor.lnum;
4231 else
4232 {
4233 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 {
4235 cmd = NULL;
4236 goto error;
4237 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004238 lnum = fp->lnum;
4239 }
4240 }
4241 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
4243 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004244 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004245 c = *cmd++;
4246 if (addr_type != ADDR_LINES)
4247 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004248 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004249 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004250 goto error;
4251 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004252 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004253 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004254 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004255 if (*cmd == c)
4256 ++cmd;
4257 }
4258 else
4259 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004260 int flags;
4261
4262 pos = curwin->w_cursor; // save curwin->w_cursor
4263
4264 // When '/' or '?' follows another address, start from
4265 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004266 if (lnum > 0 && lnum != MAXLNUM)
4267 curwin->w_cursor.lnum =
4268 lnum > curbuf->b_ml.ml_line_count
4269 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004270
4271 // Start a forward search at the end of the line (unless
4272 // before the first line).
4273 // Start a backward search at the start of the line.
4274 // This makes sure we never match in the current
4275 // line, and can match anywhere in the
4276 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004277 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004278 curwin->w_cursor.col = MAXCOL;
4279 else
4280 curwin->w_cursor.col = 0;
4281 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004282 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004283 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004284 {
4285 curwin->w_cursor = pos;
4286 cmd = NULL;
4287 goto error;
4288 }
4289 lnum = curwin->w_cursor.lnum;
4290 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004291 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004292 cmd += searchcmdlen;
4293 }
4294 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004296 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004297 ++cmd;
4298 if (addr_type != ADDR_LINES)
4299 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004300 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004301 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004302 goto error;
4303 }
4304 if (*cmd == '&')
4305 i = RE_SUBST;
4306 else if (*cmd == '?' || *cmd == '/')
4307 i = RE_SEARCH;
4308 else
4309 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004310 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004311 cmd = NULL;
4312 goto error;
4313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004315 if (!skip)
4316 {
4317 /*
4318 * When search follows another address, start from
4319 * there.
4320 */
4321 if (lnum != MAXLNUM)
4322 pos.lnum = lnum;
4323 else
4324 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004326 /*
4327 * Start the search just like for the above
4328 * do_search().
4329 */
4330 if (*cmd != '?')
4331 pos.col = MAXCOL;
4332 else
4333 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004334 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004335 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004336 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004337 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004338 lnum = pos.lnum;
4339 else
4340 {
4341 cmd = NULL;
4342 goto error;
4343 }
4344 }
4345 ++cmd;
4346 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347
4348 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004349 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004350 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352
4353 for (;;)
4354 {
4355 cmd = skipwhite(cmd);
4356 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4357 break;
4358
4359 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004360 {
4361 switch (addr_type)
4362 {
4363 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004364 case ADDR_OTHER:
4365 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004366 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004367 break;
4368 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004369 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004370 break;
4371 case ADDR_ARGUMENTS:
4372 lnum = curwin->w_arg_idx + 1;
4373 break;
4374 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004375 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004376 lnum = curbuf->b_fnum;
4377 break;
4378 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004379 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004380 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004381 case ADDR_TABS_RELATIVE:
4382 lnum = 1;
4383 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004384 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004385#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004386 lnum = qf_get_cur_idx(eap);
4387#endif
4388 break;
4389 case ADDR_QUICKFIX_VALID:
4390#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004391 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004392#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004393 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004394 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004395 case ADDR_UNSIGNED:
4396 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004397 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004398 }
4399 }
4400
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004402 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 else
4404 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004405 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 n = 1;
4407 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004408 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004410 if (n == MAXLNUM)
4411 {
4412 emsg(_(e_line_number_out_of_range));
4413 goto error;
4414 }
4415 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004416
4417 if (addr_type == ADDR_TABS_RELATIVE)
4418 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004419 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004420 cmd = NULL;
4421 goto error;
4422 }
4423 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004424 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004425 lnum = compute_buffer_local_count(
4426 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004428 {
4429#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004430 // Relative line addressing, need to adjust for folded lines
4431 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004432 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
Bram Moolenaar03725c52021-11-24 12:17:53 +00004433 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004434 (void)hasFolding(lnum, NULL, &lnum);
4435#endif
4436 if (i == '-')
4437 lnum -= n;
4438 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004439 {
4440 if (n >= LONG_MAX - lnum)
4441 {
4442 emsg(_(e_line_number_out_of_range));
4443 goto error;
4444 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004445 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004446 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
4449 } while (*cmd == '/' || *cmd == '?');
4450
4451error:
4452 *ptr = cmd;
4453 return lnum;
4454}
4455
4456/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004457 * Set eap->line1 and eap->line2 to the whole range.
4458 * Used for commands with the EX_DFLALL flag and no range given.
4459 */
4460 static void
4461address_default_all(exarg_T *eap)
4462{
4463 eap->line1 = 1;
4464 switch (eap->addr_type)
4465 {
4466 case ADDR_LINES:
4467 case ADDR_OTHER:
4468 eap->line2 = curbuf->b_ml.ml_line_count;
4469 break;
4470 case ADDR_LOADED_BUFFERS:
4471 {
4472 buf_T *buf = firstbuf;
4473
4474 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4475 buf = buf->b_next;
4476 eap->line1 = buf->b_fnum;
4477 buf = lastbuf;
4478 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4479 buf = buf->b_prev;
4480 eap->line2 = buf->b_fnum;
4481 }
4482 break;
4483 case ADDR_BUFFERS:
4484 eap->line1 = firstbuf->b_fnum;
4485 eap->line2 = lastbuf->b_fnum;
4486 break;
4487 case ADDR_WINDOWS:
4488 eap->line2 = LAST_WIN_NR;
4489 break;
4490 case ADDR_TABS:
4491 eap->line2 = LAST_TAB_NR;
4492 break;
4493 case ADDR_TABS_RELATIVE:
4494 eap->line2 = 1;
4495 break;
4496 case ADDR_ARGUMENTS:
4497 if (ARGCOUNT == 0)
4498 eap->line1 = eap->line2 = 0;
4499 else
4500 eap->line2 = ARGCOUNT;
4501 break;
4502 case ADDR_QUICKFIX_VALID:
4503#ifdef FEAT_QUICKFIX
4504 eap->line2 = qf_get_valid_size(eap);
4505 if (eap->line2 == 0)
4506 eap->line2 = 1;
4507#endif
4508 break;
4509 case ADDR_NONE:
4510 case ADDR_UNSIGNED:
4511 case ADDR_QUICKFIX:
4512 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4513 break;
4514 }
4515}
4516
4517
4518/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004519 * Get flags from an Ex command argument.
4520 */
4521 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004522get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004523{
4524 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4525 {
4526 if (*eap->arg == 'l')
4527 eap->flags |= EXFLAG_LIST;
4528 else if (*eap->arg == 'p')
4529 eap->flags |= EXFLAG_PRINT;
4530 else
4531 eap->flags |= EXFLAG_NR;
4532 eap->arg = skipwhite(eap->arg + 1);
4533 }
4534}
4535
4536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 * Function called for command which is Not Implemented. NI!
4538 */
4539 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004540ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541{
4542 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004543 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004544 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545}
4546
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004547#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548/*
4549 * Function called for script command which is Not Implemented. NI!
4550 * Skips over ":perl <<EOF" constructs.
4551 */
4552 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004553ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554{
4555 if (!eap->skip)
4556 ex_ni(eap);
4557 else
4558 vim_free(script_get(eap, eap->arg));
4559}
4560#endif
4561
4562/*
4563 * Check range in Ex command for validity.
4564 * Return NULL when valid, error message when invalid.
4565 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004566 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004567invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004569 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004570
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 if ( eap->line1 < 0
4572 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004573 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004574 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004575
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004576 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004577 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004578 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004579 {
4580 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004581 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004582#ifdef FEAT_DIFF
4583 + (eap->cmdidx == CMD_diffget)
4584#endif
4585 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004586 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004587 break;
4588 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004589 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004590 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004591 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004592 break;
4593 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004594 // Only a boundary check, not whether the buffers actually
4595 // exist.
4596 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004597 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004598 break;
4599 case ADDR_LOADED_BUFFERS:
4600 buf = firstbuf;
4601 while (buf->b_ml.ml_mfp == NULL)
4602 {
4603 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004604 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004605 buf = buf->b_next;
4606 }
4607 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004608 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004609 buf = lastbuf;
4610 while (buf->b_ml.ml_mfp == NULL)
4611 {
4612 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004613 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004614 buf = buf->b_prev;
4615 }
4616 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004617 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004618 break;
4619 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004620 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004621 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004622 break;
4623 case ADDR_TABS:
4624 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004625 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004626 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004627 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004628 case ADDR_OTHER:
4629 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004630 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004631 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004632#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004633 // No error for value that is too big, will use the last entry.
4634 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004635 {
4636 if (eap->addr_count == 0)
4637 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004638 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004639 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004640#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004641 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004642 case ADDR_QUICKFIX_VALID:
4643#ifdef FEAT_QUICKFIX
4644 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4645 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004646 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004647#endif
4648 break;
4649 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004650 case ADDR_NONE:
4651 // Will give an error elsewhere.
4652 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004653 }
4654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 return NULL;
4656}
4657
4658/*
4659 * Correct the range for zero line number, if required.
4660 */
4661 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004662correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004664 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 {
4666 if (eap->line1 == 0)
4667 eap->line1 = 1;
4668 if (eap->line2 == 0)
4669 eap->line2 = 1;
4670 }
4671}
4672
Bram Moolenaar748bf032005-02-02 23:04:36 +00004673#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004674/*
4675 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4676 * pattern. Otherwise return eap->arg.
4677 */
4678 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004679skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004680{
4681 char_u *p = eap->arg;
4682
Bram Moolenaara37420f2006-02-04 22:37:47 +00004683 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4684 || eap->cmdidx == CMD_vimgrepadd
4685 || eap->cmdidx == CMD_lvimgrepadd
4686 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004687 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004688 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004689 if (p == NULL)
4690 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004691 }
4692 return p;
4693}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004694
4695/*
4696 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4697 * in the command line, so that things like % get expanded.
4698 */
4699 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004700replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004701{
4702 char_u *new_cmdline;
4703 char_u *program;
4704 char_u *pos;
4705 char_u *ptr;
4706 int len;
4707 int i;
4708
4709 /*
4710 * Don't do it when ":vimgrep" is used for ":grep".
4711 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004712 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4713 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4714 || eap->cmdidx == CMD_grepadd
4715 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004716 && !grep_internal(eap->cmdidx))
4717 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004718 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4719 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004720 {
4721 if (*curbuf->b_p_gp == NUL)
4722 program = p_gp;
4723 else
4724 program = curbuf->b_p_gp;
4725 }
4726 else
4727 {
4728 if (*curbuf->b_p_mp == NUL)
4729 program = p_mp;
4730 else
4731 program = curbuf->b_p_mp;
4732 }
4733
4734 p = skipwhite(p);
4735
4736 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4737 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004738 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004739 i = 1;
4740 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4741 ++i;
4742 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004743 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004744 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004745 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004746 ptr = new_cmdline;
4747 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4748 {
4749 i = (int)(pos - program);
4750 STRNCPY(ptr, program, i);
4751 STRCPY(ptr += i, p);
4752 ptr += len;
4753 program = pos + 2;
4754 }
4755 STRCPY(ptr, program);
4756 }
4757 else
4758 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004759 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004760 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004761 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004762 STRCPY(new_cmdline, program);
4763 STRCAT(new_cmdline, " ");
4764 STRCAT(new_cmdline, p);
4765 }
4766 msg_make(p);
4767
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004768 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004769 vim_free(*cmdlinep);
4770 *cmdlinep = new_cmdline;
4771 p = new_cmdline;
4772 }
4773 return p;
4774}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004775#endif
4776
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777/*
4778 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004779 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 * Return FAIL for failure, OK otherwise.
4781 */
4782 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004783expand_filename(
4784 exarg_T *eap,
4785 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004786 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004788 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 char_u *repl;
4790 int srclen;
4791 char_u *p;
4792 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004793 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794
Bram Moolenaar748bf032005-02-02 23:04:36 +00004795#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004796 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004797 p = skip_grep_pat(eap);
4798#else
4799 p = eap->arg;
4800#endif
4801
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 /*
4803 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4804 * the file name contains a wildcard it should not cause expanding.
4805 * (it will be expanded anyway if there is a wildcard before replacing).
4806 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004807 has_wildcards = mch_has_wildcard(p);
4808 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004810#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004811 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004812 if (p[0] == '`' && p[1] == '=')
4813 {
4814 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004815 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004816 if (*p == '`')
4817 ++p;
4818 continue;
4819 }
4820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 /*
4822 * Quick check if this cannot be the start of a special string.
4823 * Also removes backslash before '%', '#' and '<'.
4824 */
4825 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4826 {
4827 ++p;
4828 continue;
4829 }
4830
4831 /*
4832 * Try to find a match at this position.
4833 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004834 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4835 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004836 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004838 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 {
4840 p += srclen;
4841 continue;
4842 }
4843
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004844 // Wildcards won't be expanded below, the replacement is taken
4845 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004846 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4847 {
4848 char_u *l = repl;
4849
4850 repl = expand_env_save(repl);
4851 vim_free(l);
4852 }
4853
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004854 // Need to escape white space et al. with a backslash.
4855 // Don't do this for:
4856 // - replacement that already has been escaped: "##"
4857 // - shell commands (may have to use quotes instead).
4858 // - non-unix systems when there is a single argument (spaces don't
4859 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004861 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 && eap->cmdidx != CMD_grep
4864 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004865 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004866 && eap->cmdidx != CMD_lgrep
4867 && eap->cmdidx != CMD_lgrepadd
4868 && eap->cmdidx != CMD_lmake
4869 && eap->cmdidx != CMD_make
4870 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004872 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873#endif
4874 )
4875 {
4876 char_u *l;
4877#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004878 // Don't escape a backslash here, because rem_backslash() doesn't
4879 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 static char_u *nobslash = (char_u *)" \t\"|";
4881# define ESCAPE_CHARS nobslash
4882#else
4883# define ESCAPE_CHARS escape_chars
4884#endif
4885
4886 for (l = repl; *l; ++l)
4887 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4888 {
4889 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4890 if (l != NULL)
4891 {
4892 vim_free(repl);
4893 repl = l;
4894 }
4895 break;
4896 }
4897 }
4898
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004899 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004900 if ((eap->usefilter || eap->cmdidx == CMD_bang
4901 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004902 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 {
4904 char_u *l;
4905
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004906 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 if (l != NULL)
4908 {
4909 vim_free(repl);
4910 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
4912 }
4913
4914 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4915 vim_free(repl);
4916 if (p == NULL)
4917 return FAIL;
4918 }
4919
4920 /*
4921 * One file argument: Expand wildcards.
4922 * Don't do this with ":r !command" or ":w !command".
4923 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004924 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 {
4926 /*
4927 * May do this twice:
4928 * 1. Replace environment variables.
4929 * 2. Replace any other wildcards, remove backslashes.
4930 */
4931 for (n = 1; n <= 2; ++n)
4932 {
4933 if (n == 2)
4934 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 /*
4936 * Halve the number of backslashes (this is Vi compatible).
4937 * For Unix and OS/2, when wildcards are expanded, this is
4938 * done by ExpandOne() below.
4939 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004940#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 if (!has_wildcards)
4942#endif
4943 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 }
4945
4946 if (has_wildcards)
4947 {
4948 if (n == 1)
4949 {
4950 /*
4951 * First loop: May expand environment variables. This
4952 * can be done much faster with expand_env() than with
4953 * something else (e.g., calling a shell).
4954 * After expanding environment variables, check again
4955 * if there are still wildcards present.
4956 */
4957 if (vim_strchr(eap->arg, '$') != NULL
4958 || vim_strchr(eap->arg, '~') != NULL)
4959 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004960 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004961 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 has_wildcards = mch_has_wildcard(NameBuff);
4963 p = NameBuff;
4964 }
4965 else
4966 p = NULL;
4967 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004968 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
4970 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004971 int options = WILD_LIST_NOTFOUND
4972 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973
4974 ExpandInit(&xpc);
4975 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004976 if (p_wic)
4977 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004979 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 if (p == NULL)
4981 return FAIL;
4982 }
4983 if (p != NULL)
4984 {
4985 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4986 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004987 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 vim_free(p);
4989 }
4990 }
4991 }
4992 }
4993 return OK;
4994}
4995
4996/*
4997 * Replace part of the command line, keeping eap->cmd, eap->arg and
4998 * eap->nextcmd correct.
4999 * "src" points to the part that is to be replaced, of length "srclen".
5000 * "repl" is the replacement string.
5001 * Returns a pointer to the character after the replaced string.
5002 * Returns NULL for failure.
5003 */
5004 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005005repl_cmdline(
5006 exarg_T *eap,
5007 char_u *src,
5008 int srclen,
5009 char_u *repl,
5010 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011{
5012 int len;
5013 int i;
5014 char_u *new_cmdline;
5015
5016 /*
5017 * The new command line is build in new_cmdline[].
5018 * First allocate it.
5019 * Careful: a "+cmd" argument may have been NUL terminated.
5020 */
5021 len = (int)STRLEN(repl);
5022 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005023 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005024 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005025 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005026 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027
5028 /*
5029 * Copy the stuff before the expanded part.
5030 * Copy the expanded stuff.
5031 * Copy what came after the expanded part.
5032 * Copy the next commands, if there are any.
5033 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005034 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005036
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005038 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005040 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005042 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 {
5044 i = (int)STRLEN(new_cmdline) + 1;
5045 STRCPY(new_cmdline + i, eap->nextcmd);
5046 eap->nextcmd = new_cmdline + i;
5047 }
5048 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5049 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5050 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5051 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5052 vim_free(*cmdlinep);
5053 *cmdlinep = new_cmdline;
5054
5055 return src;
5056}
5057
5058/*
5059 * Check for '|' to separate commands and '"' to start comments.
5060 */
5061 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005062separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063{
5064 char_u *p;
5065
Bram Moolenaar86b68352004-12-27 21:59:20 +00005066#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005067 p = skip_grep_pat(eap);
5068#else
5069 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005070#endif
5071
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005072 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 {
5074 if (*p == Ctrl_V)
5075 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005076 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005077 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005079 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005080 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005081 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 break;
5083 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005084
5085#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005086 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005087 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005088 {
5089 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005090 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005091 if (*p == NUL) // stop at NUL after CTRL-V
5092 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005093 }
5094#endif
5095
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005096 // Check for '"': start of comment or '|': next command
5097 // :@" and :*" do not start a comment!
5098 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005099 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02005100#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005101 && !in_vim9script()
5102#endif
5103 && !(eap->argt & EX_NOTRLCOM)
5104 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5105 || p != eap->arg)
5106 && (eap->cmdidx != CMD_redir
5107 || p != eap->arg + 1 || p[-1] != '@'))
5108#ifdef FEAT_EVAL
5109 || (*p == '#'
5110 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005111 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005112 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02005113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 || *p == '|' || *p == '\n')
5115 {
5116 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005117 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 * AND 'b' is present in 'cpoptions'.
5119 */
5120 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005121 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005123 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 --p;
5125 }
5126 else
5127 {
5128 eap->nextcmd = check_nextcmd(p);
5129 *p = NUL;
5130 break;
5131 }
5132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005134
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005135 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 del_trailing_spaces(eap->arg);
5137}
5138
5139/*
5140 * get + command from ex argument
5141 */
5142 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005143getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144{
5145 char_u *arg = *argp;
5146 char_u *command = NULL;
5147
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005148 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 {
5150 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005151 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 command = dollar_command;
5153 else
5154 {
5155 command = arg;
5156 arg = skip_cmd_arg(command, TRUE);
5157 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005158 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 }
5160
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005161 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 *argp = arg;
5163 }
5164 return command;
5165}
5166
5167/*
5168 * Find end of "+command" argument. Skip over "\ " and "\\".
5169 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005170 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005171skip_cmd_arg(
5172 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005173 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174{
5175 while (*p && !vim_isspace(*p))
5176 {
5177 if (*p == '\\' && p[1] != NUL)
5178 {
5179 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005180 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 else
5182 ++p;
5183 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005184 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
5186 return p;
5187}
5188
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005189 int
5190get_bad_opt(char_u *p, exarg_T *eap)
5191{
5192 if (STRICMP(p, "keep") == 0)
5193 eap->bad_char = BAD_KEEP;
5194 else if (STRICMP(p, "drop") == 0)
5195 eap->bad_char = BAD_DROP;
5196 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5197 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005198 else
5199 return FAIL;
5200 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005201}
5202
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203/*
5204 * Get "++opt=arg" argument.
5205 * Return FAIL or OK.
5206 */
5207 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005208getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209{
5210 char_u *arg = eap->arg + 2;
5211 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005212 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005215 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5217 {
5218 if (*arg == 'n')
5219 {
5220 arg += 2;
5221 eap->force_bin = FORCE_NOBIN;
5222 }
5223 else
5224 eap->force_bin = FORCE_BIN;
5225 if (!checkforcmd(&arg, "binary", 3))
5226 return FAIL;
5227 eap->arg = skipwhite(arg);
5228 return OK;
5229 }
5230
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005231 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005232 if (STRNCMP(arg, "edit", 4) == 0)
5233 {
5234 eap->read_edit = TRUE;
5235 eap->arg = skipwhite(arg + 4);
5236 return OK;
5237 }
5238
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 if (STRNCMP(arg, "ff", 2) == 0)
5240 {
5241 arg += 2;
5242 pp = &eap->force_ff;
5243 }
5244 else if (STRNCMP(arg, "fileformat", 10) == 0)
5245 {
5246 arg += 10;
5247 pp = &eap->force_ff;
5248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 else if (STRNCMP(arg, "enc", 3) == 0)
5250 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005251 if (STRNCMP(arg, "encoding", 8) == 0)
5252 arg += 8;
5253 else
5254 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 pp = &eap->force_enc;
5256 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005257 else if (STRNCMP(arg, "bad", 3) == 0)
5258 {
5259 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005260 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262
5263 if (pp == NULL || *arg != '=')
5264 return FAIL;
5265
5266 ++arg;
5267 *pp = (int)(arg - eap->cmd);
5268 arg = skip_cmd_arg(arg, FALSE);
5269 eap->arg = skipwhite(arg);
5270 *arg = NUL;
5271
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 if (pp == &eap->force_ff)
5273 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5275 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005276 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005278 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005280 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5282 *p = TOLOWER_ASC(*p);
5283 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005284 else
5285 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005286 // Check ++bad= argument. Must be a single-byte character, "keep" or
5287 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005288 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005289 return FAIL;
5290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291
5292 return OK;
5293}
5294
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005296ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297{
5298 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005299 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 * directory for security reasons.
5301 */
5302 if (secure)
5303 {
5304 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005305 eap->errmsg =
5306 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 }
5308 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005309 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 else
5311 do_augroup(eap->arg, eap->forceit);
5312}
5313
5314/*
5315 * ":doautocmd": Apply the automatic commands to the current buffer.
5316 */
5317 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005318ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005320 char_u *arg = eap->arg;
5321 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005322 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005323
Bram Moolenaar1610d052016-06-09 22:53:01 +02005324 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005325 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005326 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005327 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330/*
5331 * :[N]bunload[!] [N] [bufname] unload buffer
5332 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5333 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5334 */
5335 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005336ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005338 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005339 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 eap->errmsg = do_bufdel(
5341 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5342 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5343 : DOBUF_UNLOAD, eap->arg,
5344 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5345}
5346
5347/*
5348 * :[N]buffer [N] to buffer N
5349 * :[N]sbuffer [N] to buffer N
5350 */
5351 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005352ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005354 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005355 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005357 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 else
5359 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005360 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5362 else
5363 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005364 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005365 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 }
5367}
5368
5369/*
5370 * :[N]bmodified [N] to next mod. buffer
5371 * :[N]sbmodified [N] to next mod. buffer
5372 */
5373 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005374ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375{
5376 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005377 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005378 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379}
5380
5381/*
5382 * :[N]bnext [N] to next buffer
5383 * :[N]sbnext [N] split and to next buffer
5384 */
5385 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005386ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005388 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005389 return;
5390
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005392 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005393 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394}
5395
5396/*
5397 * :[N]bNext [N] to previous buffer
5398 * :[N]bprevious [N] to previous buffer
5399 * :[N]sbNext [N] split and to previous buffer
5400 * :[N]sbprevious [N] split and to previous buffer
5401 */
5402 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005403ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005405 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005406 return;
5407
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005409 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005410 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411}
5412
5413/*
5414 * :brewind to first buffer
5415 * :bfirst to first buffer
5416 * :sbrewind split and to first buffer
5417 * :sbfirst split and to first buffer
5418 */
5419 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005420ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005422 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005423 return;
5424
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005426 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005427 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428}
5429
5430/*
5431 * :blast to last buffer
5432 * :sblast split and to last buffer
5433 */
5434 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005435ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005437 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005438 return;
5439
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005441 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005442 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444
Bram Moolenaar7a092242020-04-16 22:10:49 +02005445/*
5446 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005447 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005450ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005452 int comment_char = '"';
5453
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005454#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005455 if (in_vim9script())
5456 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005457#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005458 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005459}
5460
5461/*
5462 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5463 * to "cmd_start" or has a white space character before it.
5464 */
5465 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005466ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005467{
5468 int c = *cmd;
5469
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005470 if (c == NUL || c == '|' || c == '\n')
5471 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005472#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005473 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005474 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5475 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005476 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005477#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005478 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479}
5480
5481#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5482 || defined(PROTO)
5483/*
5484 * Return the next command, after the first '|' or '\n'.
5485 * Return NULL if not found.
5486 */
5487 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005488find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489{
5490 while (*p != '|' && *p != '\n')
5491 {
5492 if (*p == NUL)
5493 return NULL;
5494 ++p;
5495 }
5496 return (p + 1);
5497}
5498#endif
5499
5500/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005501 * Check if *p is a separator between Ex commands, skipping over white space.
5502 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 */
5504 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005505check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005507 char_u *s = skipwhite(p);
5508
5509 if (*s == '|' || *s == '\n')
5510 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 else
5512 return NULL;
5513}
5514
5515/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005516 * If "eap->nextcmd" is not set, check for a next command at "p".
5517 */
5518 void
5519set_nextcmd(exarg_T *eap, char_u *arg)
5520{
5521 char_u *p = check_nextcmd(arg);
5522
5523 if (eap->nextcmd == NULL)
5524 eap->nextcmd = p;
5525 else if (p != NULL)
5526 // cannot use "| command" inside a {} block
5527 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5528}
5529
5530/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 * - if there are more files to edit
5532 * - and this is the last window
5533 * - and forceit not used
5534 * - and not repeated twice on a row
5535 * return FAIL and give error message if 'message' TRUE
5536 * return OK otherwise
5537 */
5538 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005539check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005540 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005541 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542{
5543 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5544
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005545 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005546 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 {
5548 if (message)
5549 {
5550#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005551 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5552 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005554 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005556 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5557 NGETTEXT("%d more file to edit. Quit anyway?",
5558 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5560 return OK;
5561 return FAIL;
5562 }
5563#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005564 semsg(NGETTEXT(e_nr_more_file_to_edit,
5565 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005566 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 }
5568 return FAIL;
5569 }
5570 return OK;
5571}
5572
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573/*
5574 * Function given to ExpandGeneric() to obtain the list of command names.
5575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005577get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
5579 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005580 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 return cmdnames[idx].cmd_name;
5582}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005585ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586{
Bram Moolenaare6850792010-05-14 15:28:44 +02005587 if (*eap->arg == NUL)
5588 {
5589#ifdef FEAT_EVAL
5590 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5591 char_u *p = NULL;
5592
5593 if (expr != NULL)
5594 {
5595 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005596 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005597 --emsg_off;
5598 vim_free(expr);
5599 }
5600 if (p != NULL)
5601 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005602 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005603 vim_free(p);
5604 }
5605 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005606 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005607#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005608 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005609#endif
5610 }
5611 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005612 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005613
5614#ifdef FEAT_VTP
5615 else if (has_vtp_working())
5616 {
5617 // background color change requires clear + redraw
5618 update_screen(CLEAR);
5619 redrawcmd();
5620 }
5621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622}
5623
5624 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005625ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626{
5627 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005628 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 do_highlight(eap->arg, eap->forceit, FALSE);
5630}
5631
5632
5633/*
5634 * Call this function if we thought we were going to exit, but we won't
5635 * (because of an error). May need to restore the terminal mode.
5636 */
5637 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005638not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639{
5640 exiting = FALSE;
5641 settmode(TMODE_RAW);
5642}
5643
Bram Moolenaar3552e742021-05-29 12:21:58 +02005644 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005645before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5646{
5647 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5648
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005649 // Bail out when autocommands closed the window.
5650 // Refuse to quit when the buffer in the last window is being closed (can
5651 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005652 if (!win_valid(wp)
5653 || curbuf_locked()
5654 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5655 return TRUE;
5656
5657 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5658 {
5659 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005660 // Refuse to quit when locked or when the window was closed or the
5661 // buffer in the last window is being closed (can only happen in
5662 // autocommands).
5663 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005664 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5665 return TRUE;
5666 }
5667
5668 return FALSE;
5669}
5670
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005672 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005673 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005674 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005676 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005677ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005679 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005680
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681#ifdef FEAT_CMDWIN
5682 if (cmdwin_type != 0)
5683 {
5684 cmdwin_result = Ctrl_C;
5685 return;
5686 }
5687#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005688 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005689 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005690 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005691 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005692 return;
5693 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005694 if (eap->addr_count > 0)
5695 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005696 int wnr = eap->line2;
5697
5698 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5699 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005700 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005701 }
5702 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005703 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005704
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005705 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005706 if (curbuf_locked())
5707 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005708
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005709 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005710 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005711 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712
5713#ifdef FEAT_NETBEANS_INTG
5714 netbeansForcedQuit = eap->forceit;
5715#endif
5716
5717 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02005718 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 */
5720 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5721 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005722 if ((!buf_hide(wp->w_buffer)
5723 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005724 | (eap->forceit ? CCGD_FORCEIT : 0)
5725 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005727 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 {
5729 not_exiting();
5730 }
5731 else
5732 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005733 // quit last window
5734 // Note: only_one_window() returns true, even so a help window is
5735 // still open. In that case only quit, if no address has been
5736 // specified. Example:
5737 // :h|wincmd w|1q - don't quit
5738 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005739 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005741 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005742#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005744#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005745 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005746 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
5748}
5749
5750/*
5751 * ":cquit".
5752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005754ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005756 // this does not always pass on the exit code to the Manx compiler. why?
5757 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758}
5759
5760/*
5761 * ":qall": try to quit all windows
5762 */
5763 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005764ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765{
5766# ifdef FEAT_CMDWIN
5767 if (cmdwin_type != 0)
5768 {
5769 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005770 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 else
5772 cmdwin_result = K_XF2;
5773 return;
5774 }
5775# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005776
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005777 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005778 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005779 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005780 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005781 return;
5782 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005783
5784 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005785 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005786
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005788 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 getout(0);
5790 not_exiting();
5791}
5792
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793/*
5794 * ":close": close current window, unless it is the last one
5795 */
5796 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005797ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005799 win_T *win;
5800 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005801#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005803 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005805#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005806 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005807 {
5808 if (eap->addr_count == 0)
5809 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005810 else
5811 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005812 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005813 {
5814 winnr++;
5815 if (winnr == eap->line2)
5816 break;
5817 }
5818 if (win == NULL)
5819 win = lastwin;
5820 ex_win_close(eap->forceit, win, NULL);
5821 }
5822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823}
5824
Bram Moolenaar4033c552017-09-16 20:54:51 +02005825#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005826/*
5827 * ":pclose": Close any preview window.
5828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005830ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005831{
5832 win_T *win;
5833
Bram Moolenaar79648732019-07-18 21:43:07 +02005834 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005835 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005836 if (win->w_p_pvw)
5837 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005838 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005839 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005840 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005841# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005842 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005843 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005844# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005845}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005846#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005847
Bram Moolenaarf740b292006-02-16 22:11:02 +00005848/*
5849 * Close window "win" and take care of handling closing the last window for a
5850 * modified buffer.
5851 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005852 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005853ex_win_close(
5854 int forceit,
5855 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005856 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857{
5858 int need_hide;
5859 buf_T *buf = win->w_buffer;
5860
Bram Moolenaarcf844172020-06-26 19:44:06 +02005861 // Never close the autocommand window.
5862 if (win == aucmd_win)
5863 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00005864 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02005865 return;
5866 }
5867
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005869 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005871#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005872 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005874 bufref_T bufref;
5875
5876 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005878 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 return;
5880 need_hide = FALSE;
5881 }
5882 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005885 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 return;
5887 }
5888 }
5889
Bram Moolenaar4033c552017-09-16 20:54:51 +02005890#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005892#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005893
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005894 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005895 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005896 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005897 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005898 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899}
5900
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005902 * Handle the argument for a tabpage related ex command.
5903 * Returns a tabpage number.
5904 * When an error is encountered then eap->errmsg is set.
5905 */
5906 static int
5907get_tabpage_arg(exarg_T *eap)
5908{
5909 int tab_number;
5910 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5911
5912 if (eap->arg && *eap->arg != NUL)
5913 {
5914 char_u *p = eap->arg;
5915 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005916 int relative = 0; // argument +N/-N means: go to N places to the
5917 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005918
5919 if (*p == '-')
5920 {
5921 relative = -1;
5922 p++;
5923 }
5924 else if (*p == '+')
5925 {
5926 relative = 1;
5927 p++;
5928 }
5929
5930 p_save = p;
5931 tab_number = getdigits(&p);
5932
5933 if (relative == 0)
5934 {
5935 if (STRCMP(p, "$") == 0)
5936 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005937 else if (STRCMP(p, "#") == 0)
5938 if (valid_tabpage(lastused_tabpage))
5939 tab_number = tabpage_index(lastused_tabpage);
5940 else
5941 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005942 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str, eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005943 tab_number = 0;
5944 goto theend;
5945 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005946 else if (p == p_save || *p_save == '-' || *p != NUL
5947 || tab_number > LAST_TAB_NR)
5948 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005949 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005950 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005951 goto theend;
5952 }
5953 }
5954 else
5955 {
5956 if (*p_save == NUL)
5957 tab_number = 1;
5958 else if (p == p_save || *p_save == '-' || *p != NUL
5959 || tab_number == 0)
5960 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005961 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005962 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005963 goto theend;
5964 }
5965 tab_number = tab_number * relative + tabpage_index(curtab);
5966 if (!unaccept_arg0 && relative == -1)
5967 --tab_number;
5968 }
5969 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005970 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005971 }
5972 else if (eap->addr_count > 0)
5973 {
5974 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005975 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005976 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005977 tab_number = 0;
5978 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005979 else
5980 {
5981 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005982 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005983 {
5984 --tab_number;
5985 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005986 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01005987 }
5988 }
5989 }
5990 else
5991 {
5992 switch (eap->cmdidx)
5993 {
5994 case CMD_tabnext:
5995 tab_number = tabpage_index(curtab) + 1;
5996 if (tab_number > LAST_TAB_NR)
5997 tab_number = 1;
5998 break;
5999 case CMD_tabmove:
6000 tab_number = LAST_TAB_NR;
6001 break;
6002 default:
6003 tab_number = tabpage_index(curtab);
6004 }
6005 }
6006
6007theend:
6008 return tab_number;
6009}
6010
6011/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006012 * ":tabclose": close current tab page, unless it is the last one.
6013 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 */
6015 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006016ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006018 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006019 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006020
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006021# ifdef FEAT_CMDWIN
6022 if (cmdwin_type != 0)
6023 cmdwin_result = K_IGNORE;
6024 else
6025# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006026 if (first_tabpage->tp_next == NULL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00006027 emsg(_(e_cannot_close_last_tab_page));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006028 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006030 tab_number = get_tabpage_arg(eap);
6031 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006032 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006033 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006034 if (tp == NULL)
6035 {
6036 beep_flush();
6037 return;
6038 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006039 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006040 {
6041 tabpage_close_other(tp, eap->forceit);
6042 return;
6043 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006044 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006045 tabpage_close(eap->forceit);
6046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006048}
6049
6050/*
6051 * ":tabonly": close all tab pages except the current one
6052 */
6053 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006054ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006055{
6056 tabpage_T *tp;
6057 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006058 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006059
6060# ifdef FEAT_CMDWIN
6061 if (cmdwin_type != 0)
6062 cmdwin_result = K_IGNORE;
6063 else
6064# endif
6065 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006066 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006067 else
6068 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006069 tab_number = get_tabpage_arg(eap);
6070 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006071 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006072 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006073 // Repeat this up to a 1000 times, because autocommands may
6074 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006075 for (done = 0; done < 1000; ++done)
6076 {
6077 FOR_ALL_TABPAGES(tp)
6078 if (tp->tp_topframe != topframe)
6079 {
6080 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006081 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006082 if (valid_tabpage(tp))
6083 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006084 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006085 break;
6086 }
6087 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006088 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006089 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006090 }
6091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093
6094/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006095 * Close the current tab page.
6096 */
6097 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006098tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006099{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006100 // First close all the windows but the current one. If that worked then
6101 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006102 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006103 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006104 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006105 ex_win_close(forceit, curwin, NULL);
6106# ifdef FEAT_GUI
6107 need_mouse_correct = TRUE;
6108# endif
6109}
6110
6111/*
6112 * Close tab page "tp", which is not the current tab page.
6113 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006114 * Also takes care of the tab pages line disappearing when closing the
6115 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006116 */
6117 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006118tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006119{
6120 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006121 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006122 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006123
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006124 // Limit to 1000 windows, autocommands may add a window while we close
6125 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006126 while (++done < 1000)
6127 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006128 wp = tp->tp_firstwin;
6129 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006130
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006131 // Autocommands may delete the tab page under our fingers and we may
6132 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006133 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006134 break;
6135 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006136
Bram Moolenaar29323592016-07-24 22:04:11 +02006137 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006138
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006139 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006140 if (h != tabline_height())
6141 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006142}
6143
6144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 * ":only".
6146 */
6147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006148ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006150 win_T *wp;
6151 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152# ifdef FEAT_GUI
6153 need_mouse_correct = TRUE;
6154# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006155 if (eap->addr_count > 0)
6156 {
6157 wnr = eap->line2;
6158 for (wp = firstwin; --wnr > 0; )
6159 {
6160 if (wp->w_next == NULL)
6161 break;
6162 else
6163 wp = wp->w_next;
6164 }
6165 win_goto(wp);
6166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 close_others(TRUE, eap->forceit);
6168}
6169
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006171ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006173 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01006174 if (!eap->skip)
6175 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006176#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006177 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006178#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006179 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006180 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01006181 else
6182 {
6183 int winnr = 0;
6184 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006185
Bram Moolenaar2256c992016-11-15 21:17:07 +01006186 FOR_ALL_WINDOWS(win)
6187 {
6188 winnr++;
6189 if (winnr == eap->line2)
6190 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006191 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006192 if (win == NULL)
6193 win = lastwin;
6194 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 }
6197}
6198
6199/*
6200 * ":stop" and ":suspend": Suspend Vim.
6201 */
dbivolaruab16ad32021-12-29 19:41:47 +00006202 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006203ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204{
6205 /*
6206 * Disallow suspending for "rvim".
6207 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006208 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 {
6210 if (!eap->forceit)
6211 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01006212 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 windgoto((int)Rows - 1, 0);
6214 out_char('\n');
6215 out_flush();
6216 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006217 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006218 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006219 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006221 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006223 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006225 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006226 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 }
6228}
6229
6230/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006231 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 */
6233 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006234ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006236#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006237 if (not_in_vim9(eap) == FAIL)
6238 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240#ifdef FEAT_CMDWIN
6241 if (cmdwin_type != 0)
6242 {
6243 cmdwin_result = Ctrl_C;
6244 return;
6245 }
6246#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006247 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006248 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006249 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006250 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006251 return;
6252 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006253
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006255 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 */
6257 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6258 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006259
6260 // Write the buffer for ":wq" or when it was changed.
6261 // Trigger QuitPre and ExitPre.
6262 // Check if we can exit now, after autocommands have changed things.
6263 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6264 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006266 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 {
6268 not_exiting();
6269 }
6270 else
6271 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006272 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006274 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275# ifdef FEAT_GUI
6276 need_mouse_correct = TRUE;
6277# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006278 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006279 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 }
6281}
6282
6283/*
6284 * ":print", ":list", ":number".
6285 */
6286 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006287ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006289 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006290 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006291 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006293 for ( ;!got_int; ui_breakcheck())
6294 {
6295 print_line(eap->line1,
6296 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6297 || (eap->flags & EXFLAG_NR)),
6298 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6299 if (++eap->line1 > eap->line2)
6300 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006301 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006302 }
6303 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006304 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006305 curwin->w_cursor.lnum = eap->line2;
6306 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 }
6308
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310}
6311
6312#ifdef FEAT_BYTEOFF
6313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006314ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315{
6316 goto_byte(eap->line2);
6317}
6318#endif
6319
6320/*
6321 * ":shell".
6322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006324ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325{
6326 do_shell(NULL, 0);
6327}
6328
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006329#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006331static int drop_busy = FALSE;
6332static int drop_filec;
6333static char_u **drop_filev = NULL;
6334static int drop_split;
6335static void (*drop_callback)(void *);
6336static void *drop_cookie;
6337
6338 static void
6339handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340{
6341 exarg_T ea;
6342 int save_msg_scroll = msg_scroll;
6343
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006344 // Setting the argument list may cause screen updates and being called
6345 // recursively. Avoid that by setting drop_busy.
6346 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006348 // Check whether the current buffer is changed. If so, we will need
6349 // to split the current window or data could be lost.
6350 // We don't need to check if the 'hidden' option is set, as in this
6351 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006352 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 {
6354 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006355 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 --emsg_off;
6357 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006358 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 if (win_split(0, 0) == FAIL)
6361 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006362 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006364 // When splitting the window, create a new alist. Otherwise the
6365 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 alist_unlink(curwin->w_alist);
6367 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 }
6369
6370 /*
6371 * Set up the new argument list.
6372 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006373 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374
6375 /*
6376 * Move to the first file.
6377 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006378 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006379 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 ea.cmd = (char_u *)"next";
6381 do_argfile(&ea, 0);
6382
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006383 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6384 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 need_start_insertmode = FALSE;
6386
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006387 // Restore msg_scroll, otherwise a following command may cause scrolling
6388 // unexpectedly. The screen will be redrawn by the caller, thus
6389 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006391
6392 if (drop_callback != NULL)
6393 drop_callback(drop_cookie);
6394
6395 drop_filev = NULL;
6396 drop_busy = FALSE;
6397}
6398
6399/*
6400 * Handle a file drop. The code is here because a drop is *nearly* like an
6401 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006402 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006403 * code is very similar to :args and hence needs access to a lot of the static
6404 * functions in this file.
6405 *
6406 * The "filev" list must have been allocated using alloc(), as should each item
6407 * in the list. This function takes over responsibility for freeing the "filev"
6408 * list.
6409 */
6410 void
6411handle_drop(
6412 int filec, // the number of files dropped
6413 char_u **filev, // the list of files dropped
6414 int split, // force splitting the window
6415 void (*callback)(void *), // to be called after setting the argument
6416 // list
6417 void *cookie) // argument for "callback" (allocated)
6418{
6419 // Cannot handle recursive drops, finish the pending one.
6420 if (drop_busy)
6421 {
6422 FreeWild(filec, filev);
6423 vim_free(cookie);
6424 return;
6425 }
6426
6427 // When calling handle_drop() more than once in a row we only use the last
6428 // one.
6429 if (drop_filev != NULL)
6430 {
6431 FreeWild(drop_filec, drop_filev);
6432 vim_free(drop_cookie);
6433 }
6434
6435 drop_filec = filec;
6436 drop_filev = filev;
6437 drop_split = split;
6438 drop_callback = callback;
6439 drop_cookie = cookie;
6440
6441 // Postpone this when:
6442 // - editing the command line
6443 // - not possible to change the current buffer
6444 // - updating the screen
6445 // As it may change buffers and window structures that are in use and cause
6446 // freed memory to be used.
6447 if (text_locked() || curbuf_locked() || updating_screen)
6448 return;
6449
6450 handle_drop_internal();
6451}
6452
6453/*
6454 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6455 * reset: Handle a postponed drop.
6456 */
6457 void
6458handle_any_postponed_drop(void)
6459{
6460 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006461 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006462 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463}
6464#endif
6465
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 * ":preserve".
6468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006470ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006472 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 ml_preserve(curbuf, TRUE);
6474}
6475
6476/*
6477 * ":recover".
6478 */
6479 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006480ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006482 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006484 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6485 | CCGD_MULTWIN
6486 | (eap->forceit ? CCGD_FORCEIT : 0)
6487 | CCGD_EXCMD)
6488
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 && (*eap->arg == NUL
6490 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006491 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 recoverymode = FALSE;
6493}
6494
6495/*
6496 * Command modifier used in a wrong way.
6497 */
6498 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006499ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006501 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502}
6503
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504/*
6505 * :sview [+command] file split window with new file, read-only
6506 * :split [[+command] file] split window with current or new file
6507 * :vsplit [[+command] file] split window vertically with current or new file
6508 * :new [[+command] file] split window with no or new file
6509 * :vnew [[+command] file] split vertically window with no or new file
6510 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006511 *
6512 * :tabedit open new Tab page with empty window
6513 * :tabedit [+command] file open new Tab page and edit "file"
6514 * :tabnew [[+command] file] just like :tabedit
6515 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 */
6517 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006518ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006520 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006521#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006523#endif
6524#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006525 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006526 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006527#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006528 int use_tab = eap->cmdidx == CMD_tabedit
6529 || eap->cmdidx == CMD_tabfind
6530 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006532 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006533 return;
6534
Bram Moolenaar4033c552017-09-16 20:54:51 +02006535#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538
Bram Moolenaar4033c552017-09-16 20:54:51 +02006539#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006540 // A ":split" in the quickfix window works like ":new". Don't want two
6541 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006542 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 {
6544 if (eap->cmdidx == CMD_split)
6545 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 if (eap->cmdidx == CMD_vsplit)
6547 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550
Bram Moolenaar4033c552017-09-16 20:54:51 +02006551#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006552 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 {
6554 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6555 FNAME_MESS, TRUE, curbuf->b_ffname);
6556 if (fname == NULL)
6557 goto theend;
6558 eap->arg = fname;
6559 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006560# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006561 else
6562# endif
6563#endif
6564#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006565 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 && eap->cmdidx != CMD_new)
6568 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006569 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006570# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006571 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006572# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006573 au_has_group((char_u *)"FileExplorer"))
6574 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006575 // No browsing supported but we do have the file explorer:
6576 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006577 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006578 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006579 }
6580 else
6581 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006582 fname = do_browse(0, (char_u *)(use_tab
6583 ? _("Edit File in new tab page")
6584 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006586 if (fname == NULL)
6587 goto theend;
6588 eap->arg = fname;
6589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006591 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006594 /*
6595 * Either open new tab page or split the window.
6596 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006597 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006598 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006599 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006600 : eap->addr_count == 0 ? 0
6601 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006602 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006603 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006604
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006605 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006606 if (curwin != old_curwin
6607 && win_valid(old_curwin)
6608 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006609 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006610 old_curwin->w_alt_fnum = curbuf->b_fnum;
6611 }
6612 }
6613 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6615 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006616 // Reset 'scrollbind' when editing another file, but keep it when
6617 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006618 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006619 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 else
6621 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 do_exedit(eap, old_curwin);
6623 }
6624
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006625# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006626 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006627# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006629# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630theend:
6631 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006632# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006634
6635/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006636 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006637 */
6638 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006639tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006640{
6641 exarg_T ea;
6642
Bram Moolenaara80faa82020-04-12 19:37:17 +02006643 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006644 ea.cmdidx = CMD_tabnew;
6645 ea.cmd = (char_u *)"tabn";
6646 ea.arg = (char_u *)"";
6647 ex_splitview(&ea);
6648}
6649
6650/*
6651 * :tabnext command
6652 */
6653 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006654ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006655{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006656 int tab_number;
6657
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006658 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006659 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006660 switch (eap->cmdidx)
6661 {
6662 case CMD_tabfirst:
6663 case CMD_tabrewind:
6664 goto_tabpage(1);
6665 break;
6666 case CMD_tablast:
6667 goto_tabpage(9999);
6668 break;
6669 case CMD_tabprevious:
6670 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006671 if (eap->arg && *eap->arg != NUL)
6672 {
6673 char_u *p = eap->arg;
6674 char_u *p_save = p;
6675
6676 tab_number = getdigits(&p);
6677 if (p == p_save || *p_save == '-' || *p != NUL
6678 || tab_number == 0)
6679 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006680 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006681 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006682 return;
6683 }
6684 }
6685 else
6686 {
6687 if (eap->addr_count == 0)
6688 tab_number = 1;
6689 else
6690 {
6691 tab_number = eap->line2;
6692 if (tab_number < 1)
6693 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006694 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006695 return;
6696 }
6697 }
6698 }
6699 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006700 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006701 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006702 tab_number = get_tabpage_arg(eap);
6703 if (eap->errmsg == NULL)
6704 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006705 break;
6706 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006707}
6708
6709/*
6710 * :tabmove command
6711 */
6712 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006713ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006714{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006715 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006716
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006717 tab_number = get_tabpage_arg(eap);
6718 if (eap->errmsg == NULL)
6719 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006720}
6721
6722/*
6723 * :tabs command: List tabs and their contents.
6724 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006725 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006726ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006727{
6728 tabpage_T *tp;
6729 win_T *wp;
6730 int tabcount = 1;
6731
6732 msg_start();
6733 msg_scroll = TRUE;
6734 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6735 {
6736 msg_putchar('\n');
6737 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006738 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006739 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006740 ui_breakcheck();
6741
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006742 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006743 wp = firstwin;
6744 else
6745 wp = tp->tp_firstwin;
6746 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6747 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006748 msg_putchar('\n');
6749 msg_putchar(wp == curwin ? '>' : ' ');
6750 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006751 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6752 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006753 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006754 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006755 else
6756 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6757 IObuff, IOSIZE, TRUE);
6758 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006759 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006760 ui_breakcheck();
6761 }
6762 }
6763}
6764
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765/*
6766 * ":mode": Set screen mode.
6767 * If no argument given, just get the screen size and redraw.
6768 */
6769 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006770ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771{
6772 if (*eap->arg == NUL)
6773 shell_resized();
6774 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00006775 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776}
6777
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778/*
6779 * ":resize".
6780 * set, increment or decrement current window height
6781 */
6782 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006783ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784{
6785 int n;
6786 win_T *wp = curwin;
6787
6788 if (eap->addr_count > 0)
6789 {
6790 n = eap->line2;
6791 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6792 ;
6793 }
6794
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006795# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006797# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006799 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 {
6801 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006802 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006803 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 n = 9999;
6805 win_setwidth_win((int)n, wp);
6806 }
6807 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 {
6809 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006810 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006811 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 n = 9999;
6813 win_setheight_win((int)n, wp);
6814 }
6815}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816
6817/*
6818 * ":find [+command] <file>" command.
6819 */
6820 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006821ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822{
6823#ifdef FEAT_SEARCHPATH
6824 char_u *fname;
6825 int count;
6826
6827 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6828 TRUE, curbuf->b_ffname);
6829 if (eap->addr_count > 0)
6830 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006831 // Repeat finding the file "count" times. This matters when it
6832 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 count = eap->line2;
6834 while (fname != NULL && --count > 0)
6835 {
6836 vim_free(fname);
6837 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6838 FALSE, curbuf->b_ffname);
6839 }
6840 }
6841
6842 if (fname != NULL)
6843 {
6844 eap->arg = fname;
6845#endif
6846 do_exedit(eap, NULL);
6847#ifdef FEAT_SEARCHPATH
6848 vim_free(fname);
6849 }
6850#endif
6851}
6852
6853/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006854 * ":open" simulation: for now just work like ":visual".
6855 */
6856 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006857ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006858{
6859 regmatch_T regmatch;
6860 char_u *p;
6861
Bram Moolenaar65088802021-03-13 21:07:21 +01006862#ifdef FEAT_EVAL
6863 if (not_in_vim9(eap) == FAIL)
6864 return;
6865#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006866 curwin->w_cursor.lnum = eap->line2;
6867 beginline(BL_SOL | BL_FIX);
6868 if (*eap->arg == '/')
6869 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006870 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006871 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006872 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00006873 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006874 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006875 if (regmatch.regprog != NULL)
6876 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00006877 // make a copy of the line, when searching for a mark it might be
6878 // flushed
6879 char_u *line = vim_strsave(ml_get_curline());
6880
Bram Moolenaardf177f62005-02-22 08:39:57 +00006881 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00006882 if (vim_regexec(&regmatch, line, (colnr_T)0))
6883 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006884 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006885 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02006886 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00006887 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006888 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006889 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006890 eap->arg += STRLEN(eap->arg);
6891 }
6892 check_cursor();
6893
6894 eap->cmdidx = CMD_visual;
6895 do_exedit(eap, NULL);
6896}
6897
6898/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006899 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 */
6901 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006902ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903{
6904 do_exedit(eap, NULL);
6905}
6906
6907/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006908 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006911do_exedit(
6912 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006913 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914{
6915 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006917 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006919 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6920 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006921 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 /*
6923 * ":vi" command ends Ex mode.
6924 */
6925 if (exmode_active && (eap->cmdidx == CMD_visual
6926 || eap->cmdidx == CMD_view))
6927 {
6928 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006929 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006931 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006932 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006933 if (global_busy)
6934 {
6935 int rd = RedrawingDisabled;
6936 int nwr = no_wait_return;
6937 int ms = msg_scroll;
6938#ifdef FEAT_GUI
6939 int he = hold_gui_events;
6940#endif
6941
6942 if (eap->nextcmd != NULL)
6943 {
6944 stuffReadbuff(eap->nextcmd);
6945 eap->nextcmd = NULL;
6946 }
6947
6948 if (exmode_was != EXMODE_VIM)
6949 settmode(TMODE_RAW);
6950 RedrawingDisabled = 0;
6951 no_wait_return = 0;
6952 need_wait_return = FALSE;
6953 msg_scroll = 0;
6954#ifdef FEAT_GUI
6955 hold_gui_events = 0;
6956#endif
6957 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006958 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006959
6960 main_loop(FALSE, TRUE);
6961
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006962 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006963 RedrawingDisabled = rd;
6964 no_wait_return = nwr;
6965 msg_scroll = ms;
6966#ifdef FEAT_GUI
6967 hold_gui_events = he;
6968#endif
6969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 }
6973
6974 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006975 || eap->cmdidx == CMD_tabnew
6976 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006977 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00006979 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 setpcmark();
6981 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006982 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6983 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006985 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 || *eap->arg != NUL
6987#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006988 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989#endif
6990 )
6991 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006992 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6993 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006994 if (*eap->arg != NUL && curbuf_locked())
6995 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006996
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 n = readonlymode;
6998 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6999 readonlymode = TRUE;
7000 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007001 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7002 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007003 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7004 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7006 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007007 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7009 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7010 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007011 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007013 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007014 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007015 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7016 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007017 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007019 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 if (old_curwin != NULL)
7021 {
7022 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007023 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007025#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007026 cleanup_T cs;
7027
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007028 // Reset the error/interrupt/exception state here so that
7029 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007030 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007031#endif
7032#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007034#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007035 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007036
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007037#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007038 // Restore the error/interrupt/exception state if not
7039 // discarded by a new aborting error, interrupt, or
7040 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007041 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 }
7044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 }
7046 else if (readonlymode && curbuf->b_nwindows == 1)
7047 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007048 // When editing an already visited buffer, 'readonly' won't be set
7049 // but the previous value is kept. With ":view" and ":sview" we
7050 // want the file to be readonly, except when another window is
7051 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 curbuf->b_p_ro = TRUE;
7053 }
7054 readonlymode = n;
7055 }
7056 else
7057 {
7058 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007059 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 if (n != curwin->w_arg_idx_invalid)
7063 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 }
7065
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 /*
7067 * if ":split file" worked, set alternate file name in old window to new
7068 * file
7069 */
7070 if (old_curwin != NULL
7071 && *eap->arg != NUL
7072 && curwin != old_curwin
7073 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007074 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007075 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077
7078 ex_no_reprint = TRUE;
7079}
7080
7081#ifndef FEAT_GUI
7082/*
7083 * ":gui" and ":gvim" when there is no GUI.
7084 */
7085 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007086ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007088 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089}
7090#endif
7091
Bram Moolenaar4f974752019-02-17 17:44:42 +01007092#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007094ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095{
7096 gui_make_tearoff(eap->arg);
7097}
7098#endif
7099
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007100#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7101 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007103ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007105# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7106 if (gui.in_use)
7107 gui_make_popup(eap->arg, eap->forceit);
7108# ifdef FEAT_TERM_POPUP_MENU
7109 else
7110# endif
7111# endif
7112# ifdef FEAT_TERM_POPUP_MENU
7113 pum_make_popup(eap->arg, eap->forceit);
7114# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115}
7116#endif
7117
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007119ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120{
7121 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007122 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007124 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125}
7126
7127/*
7128 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7129 * offset.
7130 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007133ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007136 win_T *save_curwin = curwin;
7137 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 long topline;
7139 long y;
7140 linenr_T old_linenr = curwin->w_cursor.lnum;
7141
7142 setpcmark();
7143
7144 /*
7145 * determine max topline
7146 */
7147 if (curwin->w_p_scb)
7148 {
7149 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007150 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 {
7152 if (wp->w_p_scb && wp->w_buffer)
7153 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007154 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 if (topline > y)
7156 topline = y;
7157 }
7158 }
7159 if (topline < 1)
7160 topline = 1;
7161 }
7162 else
7163 {
7164 topline = 1;
7165 }
7166
7167
7168 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007169 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007171 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 {
7173 if (curwin->w_p_scb)
7174 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007175 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 y = topline - curwin->w_topline;
7177 if (y > 0)
7178 scrollup(y, TRUE);
7179 else
7180 scrolldown(-y, TRUE);
7181 curwin->w_scbind_pos = topline;
7182 redraw_later(VALID);
7183 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 }
7186 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007187 curwin = save_curwin;
7188 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 if (curwin->w_p_scb)
7190 {
7191 did_syncbind = TRUE;
7192 checkpcmark();
7193 if (old_linenr != curwin->w_cursor.lnum)
7194 {
7195 char_u ctrl_o[2];
7196
7197 ctrl_o[0] = Ctrl_O;
7198 ctrl_o[1] = 0;
7199 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7200 }
7201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202}
7203
7204
7205 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007206ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007208 int i;
7209 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7210 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007212 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 do_bang(1, eap, FALSE, FALSE, TRUE);
7214 else
7215 {
7216 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7217 return;
7218
7219#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007220 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 {
7222 char_u *browseFile;
7223
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007224 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 NULL, NULL, NULL, curbuf);
7226 if (browseFile != NULL)
7227 {
7228 i = readfile(browseFile, NULL,
7229 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7230 vim_free(browseFile);
7231 }
7232 else
7233 i = OK;
7234 }
7235 else
7236#endif
7237 if (*eap->arg == NUL)
7238 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007239 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 return;
7241 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7242 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7243 }
7244 else
7245 {
7246 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7247 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7248 i = readfile(eap->arg, NULL,
7249 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7250
7251 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007252 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007254#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 if (!aborting())
7256#endif
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007257 semsg(_(e_cant_open_file_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 }
7259 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007260 {
7261 if (empty && exmode_active)
7262 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007263 // Delete the empty line that remains. Historically ex does
7264 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007265 if (eap->line2 == 0)
7266 lnum = curbuf->b_ml.ml_line_count;
7267 else
7268 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007269 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007270 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007271 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007272 if (curwin->w_cursor.lnum > 1
7273 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007274 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007275 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007276 }
7277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 }
7281}
7282
Bram Moolenaarea408852005-06-25 22:49:46 +00007283static char_u *prev_dir = NULL;
7284
7285#if defined(EXITFREE) || defined(PROTO)
7286 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007287free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007288{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007289 VIM_CLEAR(prev_dir);
7290 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007291}
7292#endif
7293
Bram Moolenaarf4258302013-06-02 18:20:17 +02007294/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007295 * Get the previous directory for the given chdir scope.
7296 */
7297 static char_u *
7298get_prevdir(cdscope_T scope)
7299{
7300 if (scope == CDSCOPE_WINDOW)
7301 return curwin->w_prevdir;
7302 else if (scope == CDSCOPE_TABPAGE)
7303 return curtab->tp_prevdir;
7304 return prev_dir;
7305}
7306
7307/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007308 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007309 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7310 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007311 */
7312 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007313post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007314{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007315 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007316 // Clear tab local directory for both :cd and :tcd
7317 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007318 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007319 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007320 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007321 char_u *pdir = get_prevdir(scope);
7322
7323 // If still in the global directory, need to remember current
7324 // directory as the global directory.
7325 if (globaldir == NULL && pdir != NULL)
7326 globaldir = vim_strsave(pdir);
7327
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007328 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007329 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007330 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007331 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007332 curtab->tp_localdir = vim_strsave(NameBuff);
7333 else
7334 curwin->w_localdir = vim_strsave(NameBuff);
7335 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007336 }
7337 else
7338 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007339 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007340 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007341 }
7342
zeertzjq64be6aa2021-11-19 11:59:08 +00007343 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007344 shorten_fnames(TRUE);
7345}
7346
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007347/*
7348 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007349 * chdir() function.
7350 * scope == CDSCOPE_WINDOW: changes the window-local directory
7351 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7352 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007353 * Returns TRUE if the directory is successfully changed.
7354 */
7355 int
7356changedir_func(
7357 char_u *new_dir,
7358 int forceit,
7359 cdscope_T scope)
7360{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007361 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007362 int dir_differs;
7363 int retval = FALSE;
7364
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007365 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007366 return FALSE;
7367
7368 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7369 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007370 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007371 return FALSE;
7372 }
7373
7374 // ":cd -": Change to previous directory
7375 if (STRCMP(new_dir, "-") == 0)
7376 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007377 pdir = get_prevdir(scope);
7378 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007379 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007380 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007381 return FALSE;
7382 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007383 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007384 }
7385
7386 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007387 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007388 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007389 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007390 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007391
Bakudankun29f3a452021-12-11 12:28:08 +00007392 // For UNIX ":cd" means: go to home directory.
7393 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007394#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007395 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007396#else
7397 if (*new_dir == NUL && p_cdh)
7398#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007399 {
7400 // use NameBuff for home directory name
7401# ifdef VMS
7402 char_u *p;
7403
7404 p = mch_getenv((char_u *)"SYS$LOGIN");
7405 if (p == NULL || *p == NUL) // empty is the same as not set
7406 NameBuff[0] = NUL;
7407 else
7408 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7409# else
7410 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7411# endif
7412 new_dir = NameBuff;
7413 }
zeertzjqf38aad82021-12-30 13:45:57 +00007414 dir_differs = pdir == NULL
Bram Moolenaar002bc792020-06-05 22:33:42 +02007415 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
zeertzjqf38aad82021-12-30 13:45:57 +00007416 if (dir_differs && vim_chdir(new_dir))
Richard Doty3d0abad2021-12-29 14:39:08 +00007417 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007418 emsg(_(e_command_failed));
Richard Doty3d0abad2021-12-29 14:39:08 +00007419 vim_free(pdir);
7420 }
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007421 else
7422 {
7423 char_u *acmd_fname;
Richard Doty3d0abad2021-12-29 14:39:08 +00007424 char_u **pp;
7425
7426 if (scope == CDSCOPE_WINDOW)
7427 pp = &curwin->w_prevdir;
7428 else if (scope == CDSCOPE_TABPAGE)
7429 pp = &curtab->tp_prevdir;
7430 else
7431 pp = &prev_dir;
7432 vim_free(*pp);
7433 *pp = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007434
7435 post_chdir(scope);
7436
7437 if (dir_differs)
7438 {
7439 if (scope == CDSCOPE_WINDOW)
7440 acmd_fname = (char_u *)"window";
7441 else if (scope == CDSCOPE_TABPAGE)
7442 acmd_fname = (char_u *)"tabpage";
7443 else
7444 acmd_fname = (char_u *)"global";
7445 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7446 curbuf);
7447 }
7448 retval = TRUE;
7449 }
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007450
7451 return retval;
7452}
Bram Moolenaarea408852005-06-25 22:49:46 +00007453
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007455 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007457 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007458ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007460 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461
7462 new_dir = eap->arg;
7463#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007464 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7465 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 ex_pwd(NULL);
7467 else
7468#endif
7469 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007470 cdscope_T scope = CDSCOPE_GLOBAL;
7471
7472 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7473 scope = CDSCOPE_WINDOW;
7474 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7475 scope = CDSCOPE_TABPAGE;
7476
7477 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007478 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007479 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007480 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 ex_pwd(eap);
7482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 }
7484}
7485
7486/*
7487 * ":pwd".
7488 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007490ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491{
7492 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7493 {
7494#ifdef BACKSLASH_IN_FILENAME
7495 slash_adjust(NameBuff);
7496#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007497 if (p_verbose > 0)
7498 {
7499 char *context = "global";
7500
Bram Moolenaar05268152021-11-18 18:53:45 +00007501 if (last_chdir_reason != NULL)
7502 context = last_chdir_reason;
7503 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007504 context = "window";
7505 else if (curtab->tp_localdir != NULL)
7506 context = "tabpage";
7507 smsg("[%s] %s", context, (char *)NameBuff);
7508 }
7509 else
7510 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 }
7512 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00007513 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514}
7515
7516/*
7517 * ":=".
7518 */
7519 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007520ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007522 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007523 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524}
7525
7526 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007527ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007529 int n;
7530 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531
7532 if (cursor_valid())
7533 {
7534 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7535 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007536 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007538
7539 len = eap->line2;
7540 switch (*eap->arg)
7541 {
7542 case 'm': break;
7543 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007544 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007545 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007546
7547 // Hide the cursor if invoked with !
7548 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549}
7550
7551/*
7552 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007553 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 */
7555 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007556do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557{
Bram Moolenaar52953192019-08-16 10:27:13 +02007558 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007559 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007560# ifdef ELAPSED_FUNC
7561 elapsed_T start_tv;
7562
7563 // Remember at what time we started, so that we know how much longer we
7564 // should wait after waiting for a bit.
7565 ELAPSED_INIT(start_tv);
7566# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007568 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007569 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007570 else
Richard Doty3d0abad2021-12-29 14:39:08 +00007571 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007572
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007573 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007574 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007576 wait_now = msec - done > 1000L ? 1000L : msec - done;
7577#ifdef FEAT_TIMERS
7578 {
7579 long due_time = check_due_timer();
7580
7581 if (due_time > 0 && due_time < wait_now)
7582 wait_now = due_time;
7583 }
7584#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007585#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007586 if (has_any_channel() && wait_now > 20L)
7587 wait_now = 20L;
7588#endif
7589#ifdef FEAT_SOUND
7590 if (has_any_sound_callback() && wait_now > 20L)
7591 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007592#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007593 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007594
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007595#ifdef FEAT_JOB_CHANNEL
7596 if (has_any_channel())
7597 ui_breakcheck_force(TRUE);
7598 else
7599#endif
7600 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007601#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007602 // Process the netbeans and clientserver messages that may have been
7603 // received in the call to ui_breakcheck() when the GUI is in use. This
7604 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007605 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007606#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007607
7608# ifdef ELAPSED_FUNC
7609 // actual time passed
7610 done = ELAPSED_FUNC(start_tv);
7611# else
7612 // guestimate time passed (will actually be more)
7613 done += wait_now;
7614# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007616
7617 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7618 // input buffer, otherwise a following call to input() fails.
7619 if (got_int)
7620 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007621
7622 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007623 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624}
7625
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626/*
7627 * ":winsize" command (obsolete).
7628 */
7629 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007630ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631{
7632 int w, h;
7633 char_u *arg = eap->arg;
7634 char_u *p;
7635
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007636 if (!isdigit(*arg))
7637 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007638 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007639 return;
7640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 w = getdigits(&arg);
7642 arg = skipwhite(arg);
7643 p = arg;
7644 h = getdigits(&arg);
7645 if (*p != NUL && *arg == NUL)
7646 set_shellsize(w, h, TRUE);
7647 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007648 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649}
7650
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007652ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653{
7654 int xchar = NUL;
7655 char_u *p;
7656
7657 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7658 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007659 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 if (eap->arg[1] == NUL)
7661 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007662 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 return;
7664 }
7665 xchar = eap->arg[1];
7666 p = eap->arg + 2;
7667 }
7668 else
7669 p = eap->arg + 1;
7670
Bram Moolenaar63b91732021-08-05 20:40:03 +02007671 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007673 if (*p != NUL && *p != (
7674#ifdef FEAT_EVAL
7675 in_vim9script() ? '#' :
7676#endif
7677 '"')
7678 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007679 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007680 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007682 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007683 postponed_split_flags = cmdmod.cmod_split;
7684 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7686 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007687 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 }
7689}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690
Bram Moolenaar843ee412004-06-30 16:16:41 +00007691#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692/*
7693 * ":winpos".
7694 */
7695 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007696ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697{
7698 int x, y;
7699 char_u *arg = eap->arg;
7700 char_u *p;
7701
7702 if (*arg == NUL)
7703 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007704# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007705# ifdef VIMDLL
7706 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7707 mch_get_winpos(&x, &y) != FAIL)
7708# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007710# else
7711 if (mch_get_winpos(&x, &y) != FAIL)
7712# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 {
7714 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007715 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 }
7717 else
7718# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00007719 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 }
7721 else
7722 {
7723 x = getdigits(&arg);
7724 arg = skipwhite(arg);
7725 p = arg;
7726 y = getdigits(&arg);
7727 if (*p == NUL || *arg != NUL)
7728 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007729 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 return;
7731 }
7732# ifdef FEAT_GUI
7733 if (gui.in_use)
7734 gui_mch_set_winpos(x, y);
7735 else if (gui.starting)
7736 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007737 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 gui_win_x = x;
7739 gui_win_y = y;
7740 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007741# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 else
7743# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007744# endif
7745# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007746 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747# endif
7748# ifdef HAVE_TGETENT
7749 if (*T_CWP)
7750 term_set_winpos(x, y);
7751# endif
7752 }
7753}
7754#endif
7755
7756/*
7757 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7758 */
7759 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007760ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761{
7762 oparg_T oa;
7763
7764 clear_oparg(&oa);
7765 oa.regname = eap->regname;
7766 oa.start.lnum = eap->line1;
7767 oa.end.lnum = eap->line2;
7768 oa.line_count = eap->line2 - eap->line1 + 1;
7769 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007771 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 {
7773 setpcmark();
7774 curwin->w_cursor.lnum = eap->line1;
7775 beginline(BL_SOL | BL_FIX);
7776 }
7777
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007778 if (VIsual_active)
7779 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007780
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 switch (eap->cmdidx)
7782 {
7783 case CMD_delete:
7784 oa.op_type = OP_DELETE;
7785 op_delete(&oa);
7786 break;
7787
7788 case CMD_yank:
7789 oa.op_type = OP_YANK;
7790 (void)op_yank(&oa, FALSE, TRUE);
7791 break;
7792
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007793 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007794 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007796 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7797#else
7798 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007800 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 oa.op_type = OP_RSHIFT;
7802 else
7803 oa.op_type = OP_LSHIFT;
7804 op_shift(&oa, FALSE, eap->amount);
7805 break;
7806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007808 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809}
7810
7811/*
7812 * ":put".
7813 */
7814 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007815ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007817 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 if (eap->line2 == 0)
7819 {
7820 eap->line2 = 1;
7821 eap->forceit = TRUE;
7822 }
7823 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00007824 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007825 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007826 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827}
7828
7829/*
7830 * Handle ":copy" and ":move".
7831 */
7832 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007833ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834{
7835 long n;
7836
Bram Moolenaar491799b2020-08-01 19:23:43 +02007837#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007838 if (not_in_vim9(eap) == FAIL)
7839 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007840#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007841 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007842 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 {
7844 eap->nextcmd = NULL;
7845 return;
7846 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007847 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848
7849 /*
7850 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7851 */
7852 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7853 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007854 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 return;
7856 }
7857
7858 if (eap->cmdidx == CMD_move)
7859 {
7860 if (do_move(eap->line1, eap->line2, n) == FAIL)
7861 return;
7862 }
7863 else
7864 ex_copy(eap->line1, eap->line2, n);
7865 u_clearline();
7866 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007867 ex_may_print(eap);
7868}
7869
7870/*
7871 * Print the current line if flags were given to the Ex command.
7872 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007873 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007874ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007875{
7876 if (eap->flags != 0)
7877 {
7878 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7879 (eap->flags & EXFLAG_LIST));
7880 ex_no_reprint = TRUE;
7881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882}
7883
7884/*
7885 * ":smagic" and ":snomagic".
7886 */
7887 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007888ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007890 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007892 magic_overruled = eap->cmdidx == CMD_smagic
7893 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02007894 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007895 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896}
7897
7898/*
7899 * ":join".
7900 */
7901 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007902ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903{
7904 curwin->w_cursor.lnum = eap->line1;
7905 if (eap->line1 == eap->line2)
7906 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007907 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 return;
7909 if (eap->line2 == curbuf->b_ml.ml_line_count)
7910 {
7911 beep_flush();
7912 return;
7913 }
7914 ++eap->line2;
7915 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007916 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007918 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919}
7920
7921/*
7922 * ":[addr]@r" or ":[addr]*r": execute register
7923 */
7924 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007925ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926{
7927 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007928 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929
7930 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007931 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932
7933#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007934 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935#endif
7936
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007937 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 c = *eap->arg;
7939 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7940 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007941 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007942 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7943 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007944 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 else
7948 {
7949 int save_efr = exec_from_reg;
7950
7951 exec_from_reg = TRUE;
7952
7953 /*
7954 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007955 * Continue until the stuff buffer is empty and all added characters
7956 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007958 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7960
7961 exec_from_reg = save_efr;
7962 }
7963}
7964
7965/*
7966 * ":!".
7967 */
7968 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007969ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970{
7971 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7972}
7973
7974/*
7975 * ":undo".
7976 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007978ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007980 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007981 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007982 else
7983 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984}
7985
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007986#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007987 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007988ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007989{
7990 char_u hash[UNDO_HASH_SIZE];
7991
7992 u_compute_hash(hash);
7993 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7994}
7995
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007996 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007997ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007998{
7999 char_u hash[UNDO_HASH_SIZE];
8000
8001 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008002 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008003}
8004#endif
8005
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006/*
8007 * ":redo".
8008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008010ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011{
8012 u_redo(1);
8013}
8014
8015/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008016 * ":earlier" and ":later".
8017 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008018 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008019ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008020{
8021 long count = 0;
8022 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008023 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008024 char_u *p = eap->arg;
8025
8026 if (*p == NUL)
8027 count = 1;
8028 else if (isdigit(*p))
8029 {
8030 count = getdigits(&p);
8031 switch (*p)
8032 {
8033 case 's': ++p; sec = TRUE; break;
8034 case 'm': ++p; sec = TRUE; count *= 60; break;
8035 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008036 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8037 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008038 }
8039 }
8040
8041 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008042 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008043 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008044 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8045 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008046}
8047
8048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 * ":redir": start/stop redirection.
8050 */
8051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008052ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053{
8054 char *mode;
8055 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008056 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057
Bram Moolenaarba768492016-07-08 15:32:54 +02008058#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008059 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008060 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008061 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008062 return;
8063 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008064#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008065
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 if (STRICMP(eap->arg, "END") == 0)
8067 close_redir();
8068 else
8069 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008070 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008072 ++arg;
8073 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008075 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 mode = "a";
8077 }
8078 else
8079 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008080 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081
8082 close_redir();
8083
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008084 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008085 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 if (fname == NULL)
8087 return;
8088#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008089 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 {
8091 char_u *browseFile;
8092
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008093 browseFile = do_browse(BROWSE_SAVE,
8094 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008095 fname, NULL, NULL,
8096 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008098 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 vim_free(fname);
8100 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008101 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 }
8103#endif
8104
8105 redir_fd = open_exfile(fname, eap->forceit, mode);
8106 vim_free(fname);
8107 }
8108#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008109 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008111 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008113 ++arg;
8114 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008116 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008117 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008119 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008121 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008122 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008123 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008124 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008126 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008127 if (*arg == '>')
8128 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008129 // Make register empty when not using @A-@Z and the
8130 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00008131 if (*arg == NUL && !isupper(redir_reg))
8132 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008134 }
8135 if (*arg != NUL)
8136 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008137 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008138 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008139 }
8140 }
8141 else if (*arg == '=' && arg[1] == '>')
8142 {
8143 int append;
8144
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008145 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008146 close_redir();
8147 arg += 2;
8148
8149 if (*arg == '>')
8150 {
8151 ++arg;
8152 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 }
8154 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008155 append = FALSE;
8156
8157 if (var_redir_start(skipwhite(arg), append) == OK)
8158 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 }
8160#endif
8161
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008162 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008165 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008167
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008168 // Make sure redirection is not off. Can happen for cmdline completion
8169 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008170 if (redir_fd != NULL
8171#ifdef FEAT_EVAL
8172 || redir_reg || redir_vname
8173#endif
8174 )
8175 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176}
8177
8178/*
8179 * ":redraw": force redraw
8180 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008181 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008182ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183{
8184 int r = RedrawingDisabled;
8185 int p = p_lz;
8186
8187 RedrawingDisabled = 0;
8188 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008189 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008191 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 if (need_maketitle)
8193 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008194#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8195# ifdef VIMDLL
8196 if (!gui.in_use)
8197# endif
8198 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 RedrawingDisabled = r;
8201 p_lz = p;
8202
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008203 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 msg_didout = FALSE;
8205 msg_col = 0;
8206
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008207 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008208 need_wait_return = FALSE;
8209
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 out_flush();
8211}
8212
8213/*
8214 * ":redrawstatus": force redraw of status line(s)
8215 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008217ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 int r = RedrawingDisabled;
8220 int p = p_lz;
8221
8222 RedrawingDisabled = 0;
8223 p_lz = FALSE;
8224 if (eap->forceit)
8225 status_redraw_all();
8226 else
8227 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008228 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 RedrawingDisabled = r;
8230 p_lz = p;
8231 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232}
8233
Bram Moolenaare12bab32019-01-08 22:02:56 +01008234/*
8235 * ":redrawtabline": force redraw of the tabline
8236 */
8237 static void
8238ex_redrawtabline(exarg_T *eap UNUSED)
8239{
8240 int r = RedrawingDisabled;
8241 int p = p_lz;
8242
8243 RedrawingDisabled = 0;
8244 p_lz = FALSE;
8245
8246 draw_tabline();
8247
8248 RedrawingDisabled = r;
8249 p_lz = p;
8250 out_flush();
8251}
8252
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008254close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255{
8256 if (redir_fd != NULL)
8257 {
8258 fclose(redir_fd);
8259 redir_fd = NULL;
8260 }
8261#ifdef FEAT_EVAL
8262 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008263 if (redir_vname)
8264 {
8265 var_redir_stop();
8266 redir_vname = 0;
8267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268#endif
8269}
8270
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008271#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008272 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008273vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008274{
8275 if (vim_mkdir(name, prot) != 0)
8276 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008277 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008278 return FAIL;
8279 }
8280 return OK;
8281}
8282#endif
8283
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284/*
8285 * Open a file for writing for an Ex command, with some checks.
8286 * Return file descriptor, or NULL on failure.
8287 */
8288 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008289open_exfile(
8290 char_u *fname,
8291 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008292 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293{
8294 FILE *fd;
8295
8296#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008297 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 if (mch_isdir(fname))
8299 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008300 semsg(_(e_src_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 return NULL;
8302 }
8303#endif
8304 if (!forceit && *mode != 'a' && vim_fexists(fname))
8305 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008306 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 return NULL;
8308 }
8309
8310 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008311 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312
8313 return fd;
8314}
8315
8316/*
8317 * ":mark" and ":k".
8318 */
8319 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008320ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321{
8322 pos_T pos;
8323
Bram Moolenaar10b94212021-02-19 21:42:57 +01008324#ifdef FEAT_EVAL
8325 if (not_in_vim9(eap) == FAIL)
8326 return;
8327#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008328 if (*eap->arg == NUL) // No argument?
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008329 emsg(_(e_argument_required));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008330 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar74409f62022-01-01 15:58:22 +00008331 semsg(_(e_trailing_characters_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 else
8333 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008334 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 curwin->w_cursor.lnum = eap->line2;
8336 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008337 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaar6d057012021-12-31 18:49:43 +00008338 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008339 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 }
8341}
8342
8343/*
8344 * Update w_topline, w_leftcol and the cursor position.
8345 */
8346 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008347update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008349 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 update_topline();
8351 if (!curwin->w_p_wrap)
8352 validate_cursor();
8353 update_curswant();
8354}
8355
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008357 * Save the current State and go to Normal mode.
8358 * Return TRUE if the typeahead could be saved.
8359 */
8360 int
8361save_current_state(save_state_T *sst)
8362{
8363 sst->save_msg_scroll = msg_scroll;
8364 sst->save_restart_edit = restart_edit;
8365 sst->save_msg_didout = msg_didout;
8366 sst->save_State = State;
8367 sst->save_insertmode = p_im;
8368 sst->save_finish_op = finish_op;
8369 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008370 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008371
Bram Moolenaar4324d872020-12-01 20:12:24 +01008372 msg_scroll = FALSE; // no msg scrolling in Normal mode
8373 restart_edit = 0; // don't go to Insert mode
8374 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008375
Bram Moolenaara452b802020-12-01 21:47:59 +01008376 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008377 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008378
8379 /*
8380 * Save the current typeahead. This is required to allow using ":normal"
8381 * from an event handler and makes sure we don't hang when the argument
8382 * ends with half a command.
8383 */
8384 save_typeahead(&sst->tabuf);
8385 return sst->tabuf.typebuf_valid;
8386}
8387
8388 void
8389restore_current_state(save_state_T *sst)
8390{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008391 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008392 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008393
8394 msg_scroll = sst->save_msg_scroll;
8395 restart_edit = sst->save_restart_edit;
8396 p_im = sst->save_insertmode;
8397 finish_op = sst->save_finish_op;
8398 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008399 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008400 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008401 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008402
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008403 // Restore the state (needed when called from a function executed for
8404 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008405 State = sst->save_State;
8406#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008407 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008408#endif
8409}
8410
8411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 * ":normal[!] {commands}": Execute normal mode commands.
8413 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008414 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008415ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008417 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 char_u *arg = NULL;
8419 int l;
8420 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008422 if (ex_normal_lock > 0)
8423 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00008424 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008425 return;
8426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 if (ex_normal_busy >= p_mmd)
8428 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00008429 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 return;
8431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 /*
8434 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8435 * this for the K_SPECIAL leading byte, otherwise special keys will not
8436 * work.
8437 */
8438 if (has_mbyte)
8439 {
8440 int len = 0;
8441
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008442 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 for (p = eap->arg; *p != NUL; ++p)
8444 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008445#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008446 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008448#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008449 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008450 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008451#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 )
8455 len += 2;
8456 }
8457 if (len > 0)
8458 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008459 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 if (arg != NULL)
8461 {
8462 len = 0;
8463 for (p = eap->arg; *p != NUL; ++p)
8464 {
8465 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008466#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 if (*p == CSI)
8468 {
8469 arg[len++] = KS_EXTRA;
8470 arg[len++] = (int)KE_CSI;
8471 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008472#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008473 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 {
8475 arg[len++] = *++p;
8476 if (*p == K_SPECIAL)
8477 {
8478 arg[len++] = KS_SPECIAL;
8479 arg[len++] = KE_FILLER;
8480 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008481#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 else if (*p == CSI)
8483 {
8484 arg[len++] = KS_EXTRA;
8485 arg[len++] = (int)KE_CSI;
8486 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 }
8489 arg[len] = NUL;
8490 }
8491 }
8492 }
8493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008495 ++ex_normal_busy;
8496 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 {
8498 /*
8499 * Repeat the :normal command for each line in the range. When no
8500 * range given, execute it just once, without positioning the cursor
8501 * first.
8502 */
8503 do
8504 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 if (eap->addr_count != 0)
8506 {
8507 curwin->w_cursor.lnum = eap->line1++;
8508 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008509 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 }
8511
Bram Moolenaar13505972019-01-24 15:04:48 +01008512 exec_normal_cmd(arg != NULL
8513 ? arg
8514 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 }
8516 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8517 }
8518
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008519 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 update_topline_cursor();
8521
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008522 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008524 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008525#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008526 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008527#endif
8528
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530}
8531
8532/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008533 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 */
8535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008536ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008538 if (eap->forceit)
8539 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008540 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008541 if (!curwin->w_cursor.lnum)
8542 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008543 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008544 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008545#ifdef FEAT_TERMINAL
8546 // Ignore this when running in an active terminal.
8547 if (term_job_running(curbuf->b_term))
8548 return;
8549#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008550
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008551 // Ignore the command when already in Insert mode. Inserting an
8552 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008553 if (State & INSERT)
8554 return;
8555
Bram Moolenaar64969662005-12-14 21:59:55 +00008556 if (eap->cmdidx == CMD_startinsert)
8557 restart_edit = 'a';
8558 else if (eap->cmdidx == CMD_startreplace)
8559 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008561 restart_edit = 'V';
8562
8563 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008565 if (eap->cmdidx == CMD_startinsert)
8566 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008567 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008569
8570 if (VIsual_active)
8571 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572}
8573
8574/*
8575 * ":stopinsert"
8576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008578ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579{
8580 restart_edit = 0;
8581 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008582 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008585/*
8586 * Execute normal mode command "cmd".
8587 * "remap" can be REMAP_NONE or REMAP_YES.
8588 */
8589 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008590exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008591{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008592 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008593 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008594 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008595}
Bram Moolenaar25281632016-01-21 23:32:32 +01008596
Bram Moolenaar25281632016-01-21 23:32:32 +01008597/*
8598 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008599 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008600 */
8601 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008602exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008603{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008604 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008605 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008606
Bram Moolenaar905dd902019-04-07 14:21:47 +02008607 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8608 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008609 clear_oparg(&oa);
8610 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008611 while ((!stuff_empty()
8612 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008613 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008614 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008615 {
8616 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008617#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008618 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008619 && oa.op_type == OP_NOP && oa.regname == NUL
8620 && !VIsual_active)
8621 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008622 // If terminal_loop() returns OK we got a key that is handled
8623 // in Normal model. With FAIL we first need to position the
8624 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008625 if (terminal_loop(TRUE) == OK)
8626 normal_cmd(&oa, TRUE);
8627 }
8628 else
8629#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008630 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008631 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008632 }
8633}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008634
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635#ifdef FEAT_FIND_ID
8636 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008637ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638{
8639 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8640 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8641 (linenr_T)1, (linenr_T)MAXLNUM);
8642}
8643
Bram Moolenaar4033c552017-09-16 20:54:51 +02008644#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645/*
8646 * ":psearch"
8647 */
8648 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008649ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650{
8651 g_do_tagpreview = p_pvh;
8652 ex_findpat(eap);
8653 g_do_tagpreview = 0;
8654}
8655#endif
8656
8657 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008658ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659{
8660 int whole = TRUE;
8661 long n;
8662 char_u *p;
8663 int action;
8664
8665 switch (cmdnames[eap->cmdidx].cmd_name[2])
8666 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008667 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8669 action = ACTION_GOTO;
8670 else
8671 action = ACTION_SHOW;
8672 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008673 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 action = ACTION_SHOW_ALL;
8675 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008676 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 action = ACTION_GOTO;
8678 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008679 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 action = ACTION_SPLIT;
8681 break;
8682 }
8683
8684 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008685 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 {
8687 n = getdigits(&eap->arg);
8688 eap->arg = skipwhite(eap->arg);
8689 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008690 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 {
8692 whole = FALSE;
8693 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008694 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 if (*p)
8696 {
8697 *p++ = NUL;
8698 p = skipwhite(p);
8699
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008700 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008701 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00008702 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02008704 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 }
8706 }
8707 if (!eap->skip)
8708 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8709 whole, !eap->forceit,
8710 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8711 n, action, eap->line1, eap->line2);
8712}
8713#endif
8714
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715
Bram Moolenaar4033c552017-09-16 20:54:51 +02008716#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717/*
8718 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8719 */
8720 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008721ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008723 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8725}
8726
8727/*
8728 * ":pedit"
8729 */
8730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008731ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732{
8733 win_T *curwin_save = curwin;
8734
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008735 if (ERROR_IF_ANY_POPUP_WINDOW)
8736 return;
8737
Bram Moolenaar026587b2019-08-17 15:08:00 +02008738 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008740 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008741
Bram Moolenaar026587b2019-08-17 15:08:00 +02008742 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008744
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 if (curwin != curwin_save && win_valid(curwin_save))
8746 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008747 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 validate_cursor();
8749 redraw_later(VALID);
8750 win_enter(curwin_save, TRUE);
8751 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008752# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008753 else if (WIN_IS_POPUP(curwin))
8754 {
8755 // can't keep focus in popup window
8756 win_enter(firstwin, TRUE);
8757 }
8758# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 g_do_tagpreview = 0;
8760}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762
8763/*
8764 * ":stag", ":stselect" and ":stjump".
8765 */
8766 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008767ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768{
8769 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008770 postponed_split_flags = cmdmod.cmod_split;
8771 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8773 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008774 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776
8777/*
8778 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8779 */
8780 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008781ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782{
8783 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8784}
8785
8786 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008787ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788{
8789 int cmd;
8790
8791 switch (name[1])
8792 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008793 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008795 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008797 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008799 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008801 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008803 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008805 case 'f': // ":tfirst"
8806 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008808 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008810 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008812 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008814 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 return;
8816 }
8817#endif
8818 cmd = DT_TAG;
8819 break;
8820 }
8821
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008822 if (name[0] == 'l')
8823 {
8824#ifndef FEAT_QUICKFIX
8825 ex_ni(eap);
8826 return;
8827#else
8828 cmd = DT_LTAG;
8829#endif
8830 }
8831
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8833 eap->forceit, TRUE);
8834}
8835
8836/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008837 * Check "str" for starting with a special cmdline variable.
8838 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8839 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8840 */
8841 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008842find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008843{
8844 int len;
8845 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008846 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008847 "%",
8848#define SPEC_PERC 0
8849 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008850#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008851 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008852#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008853 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008854#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008855 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008856#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008857 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008858#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008859 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008860#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008861 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008862#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008863 "<stack>", // call stack
8864#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008865 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008866#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008867 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008868#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008869 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008870#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008871 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008872#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008873 "<SID>", // script ID: <SNR>123_
8874#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008875#ifdef FEAT_CLIENTSERVER
8876 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008877# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008878#endif
8879 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008880
K.Takataeeec2542021-06-02 13:28:16 +02008881 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008882 {
8883 len = (int)STRLEN(spec_str[i]);
8884 if (STRNCMP(src, spec_str[i], len) == 0)
8885 {
8886 *usedlen = len;
8887 return i;
8888 }
8889 }
8890 return -1;
8891}
8892
8893/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 * Evaluate cmdline variables.
8895 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008896 * change "%" to curbuf->b_ffname
8897 * "#" to curwin->w_alt_fnum
8898 * "%%" to curwin->w_alt_fnum in Vim9 script
8899 * "<cword>" to word under the cursor
8900 * "<cWORD>" to WORD under the cursor
8901 * "<cexpr>" to C-expression under the cursor
8902 * "<cfile>" to path name under the cursor
8903 * "<sfile>" to sourced file name
8904 * "<stack>" to call stack
8905 * "<slnum>" to sourced file line number
8906 * "<afile>" to file name for autocommand
8907 * "<abuf>" to buffer number for autocommand
8908 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 *
8910 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8911 * "" for error without a message) and NULL is returned.
8912 * Returns an allocated string if a valid match was found.
8913 * Returns NULL if no match was found. "usedlen" then still contains the
8914 * number of characters to skip.
8915 */
8916 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008917eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008918 char_u *src, // pointer into commandline
8919 char_u *srcstart, // beginning of valid memory for src
8920 int *usedlen, // characters after src that are used
8921 linenr_T *lnump, // line number for :e command, or NULL
8922 char **errormsg, // pointer to error message
8923 int *escaped) // return value has escaped white space (can
8924 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925{
8926 int i;
8927 char_u *s;
8928 char_u *result;
8929 char_u *resultbuf = NULL;
8930 int resultlen;
8931 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008932 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008934 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937
8938 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008939 if (escaped != NULL)
8940 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941
8942 /*
8943 * Check if there is something to do.
8944 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008945 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008946 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 {
8948 *usedlen = 1;
8949 return NULL;
8950 }
8951
8952 /*
8953 * Skip when preceded with a backslash "\%" and "\#".
8954 * Note: In "\\%" the % is also not recognized!
8955 */
8956 if (src > srcstart && src[-1] == '\\')
8957 {
8958 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008959 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 return NULL;
8961 }
8962
8963 /*
8964 * word or WORD under cursor
8965 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008966 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8967 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008969 resultlen = find_ident_under_cursor(&result,
8970 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8971 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8972 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 if (resultlen == 0)
8974 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008975 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 return NULL;
8977 }
8978 }
8979
8980 /*
8981 * '#': Alternate file name
8982 * '%': Current file name
8983 * File name under the cursor
8984 * File name for autocommand
8985 * and following modifiers
8986 */
8987 else
8988 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008989 int off = 0;
8990
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 switch (spec_idx)
8992 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008993 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008994#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008995 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008998 // '%': current file
8999 if (curbuf->b_fname == NULL)
9000 {
9001 result = (char_u *)"";
9002 valid = 0; // Must have ":p:h" to be valid
9003 }
9004 else
9005 {
9006 result = curbuf->b_fname;
9007 tilde_file = STRCMP(result, "~") == 0;
9008 }
9009 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009011#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009012 // "%%" alternate file
9013 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009014#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009015 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009016 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009017 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009019 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 result = arg_all();
9021 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009022 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009023 if (escaped != NULL)
9024 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 break;
9027 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009028 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009029 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009030 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009032 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009033 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009034 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009035 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009037 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009039 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009040 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009041 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009042 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009043 return NULL;
9044 }
9045#ifdef FEAT_EVAL
9046 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9047 (long)i);
9048 if (result == NULL)
9049 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009050 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009051 return NULL;
9052 }
9053#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009054 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 }
9058 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009059 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009060 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9061 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009062 buf = buflist_findnr(i);
9063 if (buf == NULL)
9064 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009065 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009066 return NULL;
9067 }
9068 if (lnump != NULL)
9069 *lnump = ECMD_LAST;
9070 if (buf->b_fname == NULL)
9071 {
9072 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009073 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009074 }
9075 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009076 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009077 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009078 tilde_file = STRCMP(result, "~") == 0;
9079 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 break;
9082
9083#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009084 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009085 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 if (result == NULL)
9087 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009088 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 return NULL;
9090 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009091 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 break;
9093#endif
9094
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009095 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009097 if (result != NULL && !autocmd_fname_full)
9098 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009099 // Still need to turn the fname into a full path. It is
9100 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009101 autocmd_fname_full = TRUE;
9102 result = FullName_save(autocmd_fname, FALSE);
9103 vim_free(autocmd_fname);
9104 autocmd_fname = result;
9105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 if (result == NULL)
9107 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009108 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 return NULL;
9110 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009111 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 break;
9113
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009114 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 if (autocmd_bufnr <= 0)
9116 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009117 *errormsg = _(e_no_autocommand_buffer_name_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 return NULL;
9119 }
9120 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9121 result = strbuf;
9122 break;
9123
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009124 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 result = autocmd_match;
9126 if (result == NULL)
9127 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009128 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 return NULL;
9130 }
9131 break;
9132
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009133 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02009134 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02009135 result = estack_sfile(spec_idx == SPEC_SFILE
9136 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 if (result == NULL)
9138 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02009139 *errormsg = spec_idx == SPEC_SFILE
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009140 ? _(e_no_source_file_name_to_substitute_for_sfile)
9141 : _(e_no_call_stack_to_substitute_for_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 return NULL;
9143 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009144 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009146
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009147 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009148 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009149 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009150 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009151 return NULL;
9152 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009153 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009154 result = strbuf;
9155 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009156
9157#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009158 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009159 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009160 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009161 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009162 return NULL;
9163 }
9164 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009165 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009166 result = strbuf;
9167 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009168
Bram Moolenaar90944302020-08-01 20:45:11 +02009169 case SPEC_SID:
9170 if (current_sctx.sc_sid <= 0)
9171 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009172 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009173 return NULL;
9174 }
9175 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9176 result = strbuf;
9177 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009178#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009179
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009180#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009181 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009182 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9183 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 result = strbuf;
9185 break;
9186#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009187
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009188 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009189 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009190 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 }
9192
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009193 resultlen = (int)STRLEN(result); // length of new string
9194 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 {
9196 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 resultlen = (int)(s - result);
9199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 else if (!skip_mod)
9201 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009202 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 &resultlen);
9204 if (result == NULL)
9205 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009206 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 return NULL;
9208 }
9209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 }
9211
9212 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9213 {
9214 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009215 // xgettext:no-c-format
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009216 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009218 *errormsg = _(e_evaluates_to_an_empty_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 result = NULL;
9220 }
9221 else
9222 result = vim_strnsave(result, resultlen);
9223 vim_free(resultbuf);
9224 return result;
9225}
9226
9227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 * Expand the <sfile> string in "arg".
9229 *
9230 * Returns an allocated string, or NULL for any error.
9231 */
9232 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009233expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009235 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 int len;
9237 char_u *result;
9238 char_u *newres;
9239 char_u *repl;
9240 int srclen;
9241 char_u *p;
9242
9243 result = vim_strsave(arg);
9244 if (result == NULL)
9245 return NULL;
9246
9247 for (p = result; *p; )
9248 {
9249 if (STRNCMP(p, "<sfile>", 7) != 0)
9250 ++p;
9251 else
9252 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009253 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00009254 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 if (errormsg != NULL)
9256 {
9257 if (*errormsg)
9258 emsg(errormsg);
9259 vim_free(result);
9260 return NULL;
9261 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009262 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 {
9264 p += srclen;
9265 continue;
9266 }
9267 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9268 newres = alloc(len);
9269 if (newres == NULL)
9270 {
9271 vim_free(repl);
9272 vim_free(result);
9273 return NULL;
9274 }
9275 mch_memmove(newres, result, (size_t)(p - result));
9276 STRCPY(newres + (p - result), repl);
9277 len = (int)STRLEN(newres);
9278 STRCAT(newres, p + srclen);
9279 vim_free(repl);
9280 vim_free(result);
9281 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009282 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 }
9284 }
9285
9286 return result;
9287}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009290/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009291 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009292 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009293 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009295dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 if (fname == NULL)
9298 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009299 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300}
9301#endif
9302
9303/*
9304 * ":behave {mswin,xterm}"
9305 */
9306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009307ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308{
9309 if (STRCMP(eap->arg, "mswin") == 0)
9310 {
9311 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9312 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9313 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9314 set_option_value((char_u *)"keymodel", 0L,
9315 (char_u *)"startsel,stopsel", 0);
9316 }
9317 else if (STRCMP(eap->arg, "xterm") == 0)
9318 {
9319 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9320 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9321 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9322 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9323 }
9324 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009325 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326}
9327
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328static int filetype_detect = FALSE;
9329static int filetype_plugin = FALSE;
9330static int filetype_indent = FALSE;
9331
9332/*
9333 * ":filetype [plugin] [indent] {on,off,detect}"
9334 * on: Load the filetype.vim file to install autocommands for file types.
9335 * off: Load the ftoff.vim file to remove all autocommands for file types.
9336 * plugin on: load filetype.vim and ftplugin.vim
9337 * plugin off: load ftplugof.vim
9338 * indent on: load filetype.vim and indent.vim
9339 * indent off: load indoff.vim
9340 */
9341 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009342ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343{
9344 char_u *arg = eap->arg;
9345 int plugin = FALSE;
9346 int indent = FALSE;
9347
9348 if (*eap->arg == NUL)
9349 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009350 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009351 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 filetype_detect ? "ON" : "OFF",
9353 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9354 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9355 return;
9356 }
9357
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009358 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 for (;;)
9360 {
9361 if (STRNCMP(arg, "plugin", 6) == 0)
9362 {
9363 plugin = TRUE;
9364 arg = skipwhite(arg + 6);
9365 continue;
9366 }
9367 if (STRNCMP(arg, "indent", 6) == 0)
9368 {
9369 indent = TRUE;
9370 arg = skipwhite(arg + 6);
9371 continue;
9372 }
9373 break;
9374 }
9375 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9376 {
9377 if (*arg == 'o' || !filetype_detect)
9378 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009379 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 filetype_detect = TRUE;
9381 if (plugin)
9382 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009383 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 filetype_plugin = TRUE;
9385 }
9386 if (indent)
9387 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009388 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 filetype_indent = TRUE;
9390 }
9391 }
9392 if (*arg == 'd')
9393 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009394 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009395 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 }
9397 }
9398 else if (STRCMP(arg, "off") == 0)
9399 {
9400 if (plugin || indent)
9401 {
9402 if (plugin)
9403 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009404 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 filetype_plugin = FALSE;
9406 }
9407 if (indent)
9408 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009409 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 filetype_indent = FALSE;
9411 }
9412 }
9413 else
9414 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009415 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 filetype_detect = FALSE;
9417 }
9418 }
9419 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009420 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421}
9422
9423/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009424 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 */
9426 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009427ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428{
9429 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009430 {
9431 char_u *arg = eap->arg;
9432
9433 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9434 arg += 9;
9435
9436 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9437 if (arg != eap->arg)
9438 did_filetype = FALSE;
9439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009443ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444{
9445#ifdef FEAT_DIGRAPHS
9446 if (*eap->arg != NUL)
9447 putdigraph(eap->arg);
9448 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009449 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450#else
mityu61065042021-07-19 20:07:21 +02009451 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452#endif
9453}
9454
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009455#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9456 void
9457set_no_hlsearch(int flag)
9458{
9459 no_hlsearch = flag;
9460# ifdef FEAT_EVAL
9461 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9462# endif
9463}
9464
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465/*
9466 * ":nohlsearch"
9467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009469ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009471 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009472 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474#endif
9475
9476#ifdef FEAT_CRYPT
9477/*
9478 * ":X": Get crypt key
9479 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009481ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009483 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009484 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485}
9486#endif
9487
9488#ifdef FEAT_FOLDING
9489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009490ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491{
9492 if (foldManualAllowed(TRUE))
9493 foldCreate(eap->line1, eap->line2);
9494}
9495
9496 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009497ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498{
9499 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9500 eap->forceit, FALSE);
9501}
9502
9503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009504ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505{
9506 linenr_T lnum;
9507
Bram Moolenaar4facea32019-10-12 20:17:40 +02009508# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009509 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009510# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009511
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009512 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9514 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9515 ml_setmarked(lnum);
9516
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009517 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009519 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009520# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009521 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009522# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523}
9524#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009525
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009526#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009527/*
9528 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9529 * instead of a quickfix command.
9530 */
9531 int
9532is_loclist_cmd(int cmdidx)
9533{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009534 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009535 return FALSE;
9536 return cmdnames[cmdidx].cmd_name[0] == 'l';
9537}
9538#endif
9539
Bram Moolenaar4facea32019-10-12 20:17:40 +02009540#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009541 int
9542get_pressedreturn(void)
9543{
9544 return ex_pressedreturn;
9545}
9546
9547 void
9548set_pressedreturn(int val)
9549{
9550 ex_pressedreturn = val;
9551}
9552#endif