blob: 4066f8de94d299a007ac72c89590fdbe99fa23d1 [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 Moolenaar04935fb2022-01-08 16:19:22 +0000694 ga_init2(&lines_ga, sizeof(wcmd_T), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695
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 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001720do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001721 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001722 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001724 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001726 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001727 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001729 char_u *p;
1730 linenr_T lnum;
1731 long n;
1732 char *errormsg = NULL; // error message
1733 char_u *after_modifier = NULL;
1734 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001735 cmdmod_T save_cmdmod;
1736 int save_reg_executing = reg_executing;
1737 int ni; // set when Not Implemented
1738 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001739 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001740#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001741 int may_have_range;
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001742 int vim9script;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001743 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001744#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001745 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
Bram Moolenaara80faa82020-04-12 19:37:17 +02001747 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 ea.line1 = 1;
1749 ea.line2 = 1;
1750#ifdef FEAT_EVAL
1751 ++ex_nesting_level;
1752#endif
1753
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001754 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 if (quitmore
1756#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001757 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001758 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001759#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001760 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001761 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 --quitmore;
1763
1764 /*
1765 * Reset browse, confirm, etc.. They are restored when returning, for
1766 * recursive calls.
1767 */
1768 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001770 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001771 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1772 goto doend;
1773
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001774/*
1775 * 1. Skip comment lines and leading white space and colons.
1776 * 2. Handle command modifiers.
1777 */
1778 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001780 ea.cmdlinep = cmdlinep;
1781 ea.getline = fgetline;
1782 ea.cookie = cookie;
1783#ifdef FEAT_EVAL
1784 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001785 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001787 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001788 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001789 apply_cmdmod(&cmdmod);
Bram Moolenaar2379f872021-02-14 14:07:34 +01001790#ifdef FEAT_EVAL
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001791 vim9script = in_vim9script();
Bram Moolenaar2379f872021-02-14 14:07:34 +01001792#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001793 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
1795#ifdef FEAT_EVAL
1796 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1797 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1798#else
1799 ea.skip = (if_level > 0);
1800#endif
1801
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001803 * 3. Skip over the range to find the command. Let "p" point to after it.
1804 *
1805 * We need the command to know what kind of range it uses.
1806 */
1807 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001808#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001809 // In Vim9 script a colon is required before the range. This may also be
1810 // after command modifiers.
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001811 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001812 {
1813 may_have_range = FALSE;
1814 for (p = ea.cmd; p >= *cmdlinep; --p)
1815 {
1816 if (*p == ':')
1817 may_have_range = TRUE;
1818 if (p < ea.cmd && !VIM_ISWHITE(*p))
1819 break;
1820 }
1821 }
1822 else
1823 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001824 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001825#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001826 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827
1828#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001829 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001830 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001831 if (ea.cmd == cmd + 1 && *cmd == '$')
1832 // should be "$VAR = val"
1833 --ea.cmd;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001834 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001835 if (ea.cmdidx == CMD_SIZE)
1836 {
1837 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1838
1839 // If a ':' before the range is missing, give a clearer error
1840 // message.
Bram Moolenaar8ac681a2021-06-15 20:06:34 +02001841 if (ar > ea.cmd && !ea.skip)
Bram Moolenaar36113e42020-11-02 21:08:47 +01001842 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001843 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001844 goto doend;
1845 }
1846 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001847 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848 else
1849#endif
1850 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001851
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001852#ifdef FEAT_EVAL
1853# ifdef FEAT_PROFILE
1854 // Count this line for profiling if skip is TRUE.
1855 if (do_profiling == PROF_YES
1856 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1857 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1858 {
1859 int skip = did_emsg || got_int || did_throw;
1860
1861 if (ea.cmdidx == CMD_catch)
1862 skip = !skip && !(cstack->cs_idx >= 0
1863 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1864 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1865 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1866 skip = skip || !(cstack->cs_idx >= 0
1867 && !(cstack->cs_flags[cstack->cs_idx]
1868 & (CSF_ACTIVE | CSF_TRUE)));
1869 else if (ea.cmdidx == CMD_finally)
1870 skip = FALSE;
1871 else if (ea.cmdidx != CMD_endif
1872 && ea.cmdidx != CMD_endfor
1873 && ea.cmdidx != CMD_endtry
1874 && ea.cmdidx != CMD_endwhile)
1875 skip = ea.skip;
1876
1877 if (!skip)
1878 {
1879 if (getline_equal(fgetline, cookie, get_func_line))
1880 func_line_exec(getline_cookie(fgetline, cookie));
1881 else if (getline_equal(fgetline, cookie, getsourceline))
1882 script_line_exec();
1883 }
1884 }
1885# endif
1886
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001887 // May go to debug mode. If this happens and the ">quit" debug command is
1888 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001889 dbg_check_breakpoint(&ea);
1890 if (!ea.skip && got_int)
1891 {
1892 ea.skip = TRUE;
1893 (void)do_intthrow(cstack);
1894 }
1895#endif
1896
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001897/*
1898 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 *
1900 * where 'addr' is:
1901 *
1902 * % (entire file)
1903 * $ [+-NUM]
1904 * 'x [+-NUM] (where x denotes a currently defined mark)
1905 * . [+-NUM]
1906 * [+-NUM]..
1907 * NUM
1908 *
1909 * The ea.cmd pointer is updated to point to the first character following the
1910 * range spec. If an initial address is found, but no second, the upper bound
1911 * is equal to the lower.
1912 */
1913
Bram Moolenaar25190db2019-05-04 15:05:28 +02001914 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001915 if (!IS_USER_CMDIDX(ea.cmdidx))
1916 {
1917 if (ea.cmdidx != CMD_SIZE)
1918 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1919 else
1920 ea.addr_type = ADDR_LINES;
1921
Bram Moolenaar25190db2019-05-04 15:05:28 +02001922 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001923 if (ea.cmdidx == CMD_wincmd && p != NULL)
1924 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001925#ifdef FEAT_QUICKFIX
1926 // :.cc in quickfix window uses line number
1927 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1928 ea.addr_type = ADDR_OTHER;
1929#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001930 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001931
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001932 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001933#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001934 if (!may_have_range)
1935 ea.line1 = ea.line2 = default_address(&ea);
1936 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001937#endif
1938 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1939 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001942 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 */
1944
1945 /*
1946 * Skip ':' and any white space
1947 */
1948 ea.cmd = skipwhite(ea.cmd);
1949 while (*ea.cmd == ':')
1950 ea.cmd = skipwhite(ea.cmd + 1);
1951
1952 /*
1953 * If we got a line, but no command, then go to the line.
1954 * If we find a '|' or '\n' we set ea.nextcmd.
1955 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001956 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1957 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 {
1959 /*
1960 * strange vi behaviour:
1961 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001962 * ":3|..." prints line 3 (not in Vim9 script)
1963 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001965 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 goto doend;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001967 errormsg = ex_range_without_command(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 goto doend;
1969 }
1970
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001971 // If this looks like an undefined user command and there are CmdUndefined
1972 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001973 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1974 && ASCII_ISUPPER(*ea.cmd)
1975 && has_cmdundefined())
1976 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001977 int ret;
1978
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001979 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001980 while (ASCII_ISALNUM(*p))
1981 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001982 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001983 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1984 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001985 // If the autocommands did something and didn't cause an error, try
1986 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001987 p = (ret
1988#ifdef FEAT_EVAL
1989 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001990#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001992 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001993
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 if (p == NULL)
1995 {
1996 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01001997 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 goto doend;
1999 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002000 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002001 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2002 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 {
2004 errormsg = uc_fun_cmd();
2005 goto doend;
2006 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002007
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 if (ea.cmdidx == CMD_SIZE)
2009 {
2010 if (!ea.skip)
2011 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002012 STRCPY(IObuff, _(e_not_an_editor_command));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002014 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002015 // If the modifier was parsed OK the error must be in the
2016 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002017 if (after_modifier != NULL)
2018 append_command(after_modifier);
2019 else
2020 append_command(*cmdlinep);
2021 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002022 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002023 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 }
2025 goto doend;
2026 }
2027
Bram Moolenaar958636c2014-10-21 20:01:58 +02002028 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2029 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002030#ifdef HAVE_EX_SCRIPT_NI
2031 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2032#endif
2033 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034
2035#ifndef FEAT_EVAL
2036 /*
2037 * When the expression evaluation is disabled, recognize the ":if" and
2038 * ":endif" commands and ignore everything in between it.
2039 */
2040 if (ea.cmdidx == CMD_if)
2041 ++if_level;
2042 if (if_level)
2043 {
2044 if (ea.cmdidx == CMD_endif)
2045 --if_level;
2046 goto doend;
2047 }
2048
2049#endif
2050
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002051 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002052 if (*p == '!' && ea.cmdidx != CMD_substitute
2053 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 {
2055 ++p;
2056 ea.forceit = TRUE;
2057 }
2058 else
2059 ea.forceit = FALSE;
2060
2061/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002062 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002064 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002065 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066
2067 if (!ea.skip)
2068 {
2069#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002070 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002072 // Command not allowed in sandbox.
Bram Moolenaard8e44472021-07-21 22:20:33 +02002073 errormsg = _(e_not_allowed_in_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 goto doend;
2075 }
2076#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002077 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002078 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002079 errormsg = _(e_command_not_allowed_in_rvim);
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002080 goto doend;
2081 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002082 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002084 // Command not allowed in non-'modifiable' buffer
Bram Moolenaar108010a2021-06-27 22:03:33 +02002085 errormsg = _(e_cannot_make_changes_modifiable_is_off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 goto doend;
2087 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002088
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002089 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002091#ifdef FEAT_CMDWIN
2092 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2093 {
2094 // Command not allowed in the command line window
Bram Moolenaar108010a2021-06-27 22:03:33 +02002095 errormsg = _(e_invalid_in_cmdline_window);
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002096 goto doend;
2097 }
2098#endif
2099 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2100 {
2101 // Command not allowed when text is locked
2102 errormsg = _(get_text_locked_msg());
2103 goto doend;
2104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002106
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002107 // Disallow editing another buffer when "curbuf_lock" is set.
2108 // Do allow ":checktime" (it is postponed).
2109 // Do allow ":edit" (check for an argument later).
2110 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002111 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002112 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002113 && ea.cmdidx != CMD_edit
2114 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002115 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002116 && curbuf_locked())
2117 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002119 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002121 errormsg = _(e_no_range_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 goto doend;
2123 }
2124 }
2125
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002126 if (!ni && !(ea.argt & EX_BANG) && ea.forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002128 errormsg = _(e_no_bang_allowed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 goto doend;
2130 }
2131
2132 /*
2133 * Don't complain about the range if it is not used
2134 * (could happen if line_count is accidentally set to 0).
2135 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002136 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 {
2138 /*
2139 * If the range is backwards, ask for confirmation and, if given, swap
2140 * ea.line1 & ea.line2 so it's forwards again.
2141 * When global command is busy, don't ask, will fail below.
2142 */
2143 if (!global_busy && ea.line1 > ea.line2)
2144 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002145 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002147 if (sourcing || exmode_active)
2148 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002149 errormsg = _(e_backwards_range_given);
Bram Moolenaara5792f52005-11-23 21:25:05 +00002150 goto doend;
2151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 if (ask_yesno((char_u *)
2153 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002154 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 }
2156 lnum = ea.line1;
2157 ea.line1 = ea.line2;
2158 ea.line2 = lnum;
2159 }
2160 if ((errormsg = invalid_range(&ea)) != NULL)
2161 goto doend;
2162 }
2163
Bram Moolenaarb7316892019-05-01 18:08:42 +02002164 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2165 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 ea.line2 = 1;
2167
2168 correct_range(&ea);
2169
2170#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002171 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002172 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002174 // Put the first line at the start of a closed fold, put the last line
2175 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 (void)hasFolding(ea.line1, &ea.line1, NULL);
2177 (void)hasFolding(ea.line2, NULL, &ea.line2);
2178 }
2179#endif
2180
2181#ifdef FEAT_QUICKFIX
2182 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002183 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 * option here, so things like % get expanded.
2185 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002186 p = replace_makeprg(&ea, p, cmdlinep);
2187 if (p == NULL)
2188 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189#endif
2190
2191 /*
2192 * Skip to start of argument.
2193 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2194 */
2195 if (ea.cmdidx == CMD_bang)
2196 ea.arg = p;
2197 else
2198 ea.arg = skipwhite(p);
2199
Bram Moolenaar379fb762018-08-30 15:58:28 +02002200 // ":file" cannot be run with an argument when "curbuf_lock" is set
2201 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2202 goto doend;
2203
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 /*
2205 * Check for "++opt=val" argument.
2206 * Must be first, allow ":w ++enc=utf8 !cmd"
2207 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002208 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2210 if (getargopt(&ea) == FAIL && !ni)
2211 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002212 errormsg = _(e_invalid_argument);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 goto doend;
2214 }
2215
2216 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2217 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002218 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002220 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00002222 errormsg = _(e_use_w_or_w_gt_gt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 goto doend;
2224 }
2225 ea.arg = skipwhite(ea.arg + 1);
2226 ea.append = TRUE;
2227 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002228 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 {
2230 ++ea.arg;
2231 ea.usefilter = TRUE;
2232 }
2233 }
2234
2235 if (ea.cmdidx == CMD_read)
2236 {
2237 if (ea.forceit)
2238 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002239 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 ea.forceit = FALSE;
2241 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002242 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 {
2244 ++ea.arg;
2245 ea.usefilter = TRUE;
2246 }
2247 }
2248
2249 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2250 {
2251 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002252 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 {
2254 ++ea.arg;
2255 ++ea.amount;
2256 }
2257 ea.arg = skipwhite(ea.arg);
2258 }
2259
2260 /*
2261 * Check for "+command" argument, before checking for next command.
2262 * Don't do this for ":read !cmd" and ":write !cmd".
2263 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002264 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2266
2267 /*
Bram Moolenaar63b91732021-08-05 20:40:03 +02002268 * For commands that do not use '|' inside their argument: Check for '|' to
2269 * separate commands and '"' or '#' to start comments.
2270 *
2271 * Otherwise: Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002272 * Also do this for ":read !cmd", ":write !cmd" and ":global".
Bram Moolenaar63b91732021-08-05 20:40:03 +02002273 * Also do this inside a { - } block after :command and :autocmd.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002274 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 */
Bram Moolenaar63b91732021-08-05 20:40:03 +02002276 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
2277 {
2278 separate_nextcmd(&ea);
2279 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002280 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002281 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002282 || ea.cmdidx == CMD_global
2283 || ea.cmdidx == CMD_vglobal
Bram Moolenaar63b91732021-08-05 20:40:03 +02002284 || ea.usefilter
Bram Moolenaar6f6d58c2021-08-05 21:17:32 +02002285#ifdef FEAT_EVAL
2286 || inside_block(&ea)
2287#endif
2288 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
2290 for (p = ea.arg; *p; ++p)
2291 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002292 // Remove one backslash before a newline, so that it's possible to
2293 // pass a newline to the shell and also a newline that is preceded
2294 // with a backslash. This makes it impossible to end a shell
2295 // command in a backslash, but that doesn't appear useful.
2296 // Halving the number of backslashes is incompatible with previous
2297 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002299 STRMOVE(p, p + 1);
Bram Moolenaarf87dac02021-12-15 17:53:40 +00002300 else if (*p == '\n' && !(ea.argt & EX_EXPR_ARG))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
2302 ea.nextcmd = p + 1;
2303 *p = NUL;
2304 break;
2305 }
2306 }
2307 }
2308
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002309 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002310 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002312 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002313 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002315 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002316 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002317 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002319#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002320 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002321 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002323 errormsg = _(e_invalid_register_name);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002324 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 }
2326#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002327 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2328 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002329 {
2330 ea.regname = *ea.arg++;
2331#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002332 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002333 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2334 {
kuuote08d7b1c2021-10-04 22:17:36 +01002335 if (!ea.skip)
2336 {
2337 set_expr_line(vim_strsave(ea.arg), &ea);
2338 did_set_expr_line = TRUE;
2339 }
Bram Moolenaar85de2062011-05-05 14:26:41 +02002340 ea.arg += STRLEN(ea.arg);
2341 }
2342#endif
2343 ea.arg = skipwhite(ea.arg);
2344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
2346
2347 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002348 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 * count, it's a buffer name.
2350 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002351 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02002352 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg + 1)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002353 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00002355 n = getdigits_quoted(&ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002357 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002359 errormsg = _(e_positive_count_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 goto doend;
2361 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002362 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {
2364 ea.line2 = n;
2365 if (ea.addr_count == 0)
2366 ea.addr_count = 1;
2367 }
2368 else
2369 {
2370 ea.line1 = ea.line2;
Bram Moolenaar3cf21b32022-01-11 19:34:16 +00002371 if (ea.line2 >= LONG_MAX - (n - 1))
2372 ea.line2 = LONG_MAX; // avoid overflow
2373 else
2374 ea.line2 += n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 ++ea.addr_count;
2376 /*
2377 * Be vi compatible: no error message for out of range.
2378 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002379 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 ea.line2 = curbuf->b_ml.ml_line_count;
2381 }
2382 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002383
2384 /*
2385 * Check for flags: 'l', 'p' and '#'.
2386 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002387 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002388 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002389 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2390 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002392 // no arguments allowed but there is something
Bram Moolenaar74409f62022-01-01 15:58:22 +00002393 errormsg = ex_errmsg(e_trailing_characters_str, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 goto doend;
2395 }
2396
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002397 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002399 errormsg = _(e_argument_required);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 goto doend;
2401 }
2402
2403#ifdef FEAT_EVAL
2404 /*
2405 * Skip the command when it's not going to be executed.
2406 * The commands like :if, :endif, etc. always need to be executed.
2407 * Also make an exception for commands that handle a trailing command
2408 * themselves.
2409 */
2410 if (ea.skip)
2411 {
2412 switch (ea.cmdidx)
2413 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002414 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 case CMD_while:
2416 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002417 case CMD_for:
2418 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 case CMD_if:
2420 case CMD_elseif:
2421 case CMD_else:
2422 case CMD_endif:
2423 case CMD_try:
2424 case CMD_catch:
2425 case CMD_finally:
2426 case CMD_endtry:
2427 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002428 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 break;
2430
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002431 // Commands that handle '|' themselves. Check: A command should
2432 // either have the EX_TRLBAR flag, appear in this list or appear in
2433 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 case CMD_aboveleft:
2435 case CMD_and:
2436 case CMD_belowright:
2437 case CMD_botright:
2438 case CMD_browse:
2439 case CMD_call:
2440 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002441 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 case CMD_delfunction:
2443 case CMD_djump:
2444 case CMD_dlist:
2445 case CMD_dsearch:
2446 case CMD_dsplit:
2447 case CMD_echo:
2448 case CMD_echoerr:
2449 case CMD_echomsg:
2450 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002451 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002453 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002454 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 case CMD_help:
2456 case CMD_hide:
2457 case CMD_ijump:
2458 case CMD_ilist:
2459 case CMD_isearch:
2460 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002461 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 case CMD_keepjumps:
2463 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002464 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 case CMD_leftabove:
2466 case CMD_let:
2467 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002468 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002469 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002471 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002472 case CMD_noautocmd:
2473 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 case CMD_perl:
2475 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002476 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002477 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002478 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 case CMD_return:
2480 case CMD_rightbelow:
2481 case CMD_ruby:
2482 case CMD_silent:
2483 case CMD_smagic:
2484 case CMD_snomagic:
2485 case CMD_substitute:
2486 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002487 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 case CMD_tcl:
2489 case CMD_throw:
2490 case CMD_tilde:
2491 case CMD_topleft:
2492 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002493 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002494 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 case CMD_verbose:
2496 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002497 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 break;
2499
2500 default: goto doend;
2501 }
2502 }
2503#endif
2504
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002505 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
2507 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2508 goto doend;
2509 }
2510
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 /*
2512 * Accept buffer name. Cannot be used at the same time with a buffer
2513 * number. Don't do this for a user command.
2514 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002515 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002516 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 {
2518 /*
2519 * :bdelete, :bwipeout and :bunload take several arguments, separated
2520 * by spaces: find next space (skipping over escaped characters).
2521 * The others take one argument: ignore trailing spaces.
2522 */
2523 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2524 || ea.cmdidx == CMD_bunload)
2525 p = skiptowhite_esc(ea.arg);
2526 else
2527 {
2528 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002529 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 --p;
2531 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002532 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002533 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002534 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 goto doend;
2536 ea.addr_count = 1;
2537 ea.arg = skipwhite(p);
2538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002540 // The :try command saves the emsg_silent flag, reset it here when
2541 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002542 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002543 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002544 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002545 if (emsg_silent < 0)
2546 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002547 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002548 }
2549
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002551 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
Bram Moolenaar958636c2014-10-21 20:01:58 +02002554 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 {
2556 /*
2557 * Execute a user-defined command.
2558 */
2559 do_ucmd(&ea);
2560 }
2561 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
2563 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002564 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 */
2566 ea.errmsg = NULL;
2567 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2568 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002569 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 }
2571
2572#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002573 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002574 // Not if this was a command that wasn't executed or :endif.
Bram Moolenaar16531552020-02-08 16:00:46 +01002575 if (getline_equal(ea.getline, ea.cookie, getsourceline)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002576 && current_sctx.sc_sid > 0
2577 && ea.cmdidx != CMD_endif
2578 && (cstack->cs_idx < 0
2579 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002580 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 /*
2583 * If the command just executed called do_cmdline(), any throw or ":return"
2584 * or ":finish" encountered there must also check the cstack of the still
2585 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2586 * exception, or reanimate a returned function or finished script file and
2587 * return or finish it again.
2588 */
2589 if (need_rethrow)
2590 do_throw(cstack);
2591 else if (check_cstack)
2592 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002593 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002595 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 && current_func_returned())
2597 do_return(&ea, TRUE, FALSE, NULL);
2598 }
2599 need_rethrow = check_cstack = FALSE;
2600#endif
2601
2602doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002603 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002604 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002606 curwin->w_cursor.col = 0;
2607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608
2609 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2610 {
Bram Moolenaarbed34f02022-01-19 20:48:37 +00002611 if (sourcing || !KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002613 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 {
2615 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002616 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002618 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 }
2620 emsg(errormsg);
2621 }
2622#ifdef FEAT_EVAL
2623 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002624 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2625 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002626
2627 if (did_set_expr_line)
2628 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629#endif
2630
Bram Moolenaare1004402020-10-24 20:49:43 +02002631 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002633 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002635 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 ea.nextcmd = NULL;
2637
2638#ifdef FEAT_EVAL
2639 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002640 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641#endif
2642
2643 return ea.nextcmd;
2644}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002646static char ex_error_buf[MSG_BUF_LEN];
2647
2648/*
2649 * Return an error message with argument included.
2650 * Uses a static buffer, only the last error will be kept.
2651 * "msg" will be translated, caller should use N_().
2652 */
2653 char *
2654ex_errmsg(char *msg, char_u *arg)
2655{
2656 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2657 return ex_error_buf;
2658}
2659
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660/*
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002661 * Handle a range without a command.
2662 * Returns an error message on failure.
2663 */
2664 char *
2665ex_range_without_command(exarg_T *eap)
2666{
2667 char *errormsg = NULL;
2668
2669 if ((*eap->cmd == '|' || (exmode_active && eap->line1 != eap->line2))
2670#ifdef FEAT_EVAL
2671 && !in_vim9script()
2672#endif
2673 )
2674 {
2675 eap->cmdidx = CMD_print;
2676 eap->argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
2677 if ((errormsg = invalid_range(eap)) == NULL)
2678 {
2679 correct_range(eap);
2680 ex_print(eap);
2681 }
2682 }
2683 else if (eap->addr_count != 0)
2684 {
2685 if (eap->line2 > curbuf->b_ml.ml_line_count)
2686 {
2687 // With '-' in 'cpoptions' a line number past the file is an
2688 // error, otherwise put it at the end of the file.
2689 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2690 eap->line2 = -1;
2691 else
2692 eap->line2 = curbuf->b_ml.ml_line_count;
2693 }
2694
2695 if (eap->line2 < 0)
2696 errormsg = _(e_invalid_range);
2697 else
2698 {
2699 if (eap->line2 == 0)
2700 curwin->w_cursor.lnum = 1;
2701 else
2702 curwin->w_cursor.lnum = eap->line2;
2703 beginline(BL_SOL | BL_FIX);
2704 }
2705 }
2706 return errormsg;
2707}
2708
2709/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002710 * Check for an Ex command with optional tail.
2711 * If there is a match advance "pp" to the argument and return TRUE.
Bram Moolenaar68854a82022-01-31 18:59:13 +00002712 * If "noparen" is TRUE do not recognize the command followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002713 */
2714 static int
2715checkforcmd_opt(
2716 char_u **pp, // start of command
2717 char *cmd, // name of command
2718 int len, // required length
2719 int noparen)
2720{
2721 int i;
2722
2723 for (i = 0; cmd[i] != NUL; ++i)
2724 if (((char_u *)cmd)[i] != (*pp)[i])
2725 break;
Bram Moolenaar68854a82022-01-31 18:59:13 +00002726 if (i >= len && !isalpha((*pp)[i]) && (*pp)[i] != '_'
2727 && (!noparen || ((*pp)[i] != '(' && (*pp)[i] != '.')))
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002728 {
2729 *pp = skipwhite(*pp + i);
2730 return TRUE;
2731 }
2732 return FALSE;
2733}
2734
2735/*
2736 * Check for an Ex command with optional tail.
2737 * If there is a match advance "pp" to the argument and return TRUE.
2738 */
2739 int
2740checkforcmd(
2741 char_u **pp, // start of command
2742 char *cmd, // name of command
2743 int len) // required length
2744{
2745 return checkforcmd_opt(pp, cmd, len, FALSE);
2746}
2747
2748/*
Bram Moolenaar68854a82022-01-31 18:59:13 +00002749 * Check for an Ex command with optional tail, not followed by "(" or ".".
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002750 * If there is a match advance "pp" to the argument and return TRUE.
2751 */
Bram Moolenaare4db17f2021-08-01 21:19:43 +02002752 int
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002753checkforcmd_noparen(
2754 char_u **pp, // start of command
2755 char *cmd, // name of command
2756 int len) // required length
2757{
2758 return checkforcmd_opt(pp, cmd, len, TRUE);
2759}
2760
2761/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002762 * Parse and skip over command modifiers:
2763 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002764 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002765 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002766 * When "skip_only" is TRUE the global variables are not changed, except for
2767 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002768 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2769 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002770 * Call apply_cmdmod() to get the side effects of the modifiers:
2771 * - Increment "sandbox" for ":sandbox"
2772 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002773 * - set msg_silent for ":silent"
2774 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002775 * Return FAIL when the command is not to be executed.
2776 * May set "errormsg" to an error message.
2777 */
2778 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002779parse_command_modifiers(
2780 exarg_T *eap,
2781 char **errormsg,
2782 cmdmod_T *cmod,
2783 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002784{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002785 char_u *p;
2786 int starts_with_colon = FALSE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002787 int vim9script = in_vim9script();
Bram Moolenaareffed932018-08-14 13:38:17 +02002788
Bram Moolenaare1004402020-10-24 20:49:43 +02002789 CLEAR_POINTER(cmod);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00002790 cmod->cmod_flags = sticky_cmdmod_flags;
Bram Moolenaareffed932018-08-14 13:38:17 +02002791
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002792 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002793 for (;;)
2794 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002795 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002796 {
2797 if (*eap->cmd == ':')
2798 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002799 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002800 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002801
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002802 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002803 if (*eap->cmd == NUL && exmode_active
2804 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2805 || getline_equal(eap->getline, eap->cookie, getexline))
2806 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2807 {
2808 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002809 if (!skip_only)
2810 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002811 }
2812
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002813 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002814 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002815 {
2816 // a comment ends at a NL
2817 if (eap->nextcmd == NULL)
2818 {
2819 eap->nextcmd = vim_strchr(eap->cmd, '\n');
2820 if (eap->nextcmd != NULL)
2821 ++eap->nextcmd;
2822 }
Bram Moolenaarbc510062022-02-14 21:19:04 +00002823 if (vim9script && has_cmdmod(cmod, FALSE))
2824 *errormsg = _(e_command_modifier_without_command);
Bram Moolenaareffed932018-08-14 13:38:17 +02002825 return FAIL;
Bram Moolenaarec1b0962021-09-06 17:10:59 +02002826 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002827 if (*eap->cmd == NUL)
2828 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002829 if (!skip_only)
Bram Moolenaarbc510062022-02-14 21:19:04 +00002830 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002831 ex_pressedreturn = TRUE;
Bram Moolenaarbc510062022-02-14 21:19:04 +00002832 if (vim9script && has_cmdmod(cmod, FALSE))
2833 *errormsg = _(e_command_modifier_without_command);
2834 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002835 return FAIL;
2836 }
2837
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002838 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002839
2840 // In Vim9 script a variable can shadow a command modifier:
2841 // verbose = 123
2842 // verbose += 123
2843 // silent! verbose = func()
2844 // verbose.member = 2
2845 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002846 // But not:
2847 // verbose [a, b] = list
Bram Moolenaarbc510062022-02-14 21:19:04 +00002848 if (vim9script)
Bram Moolenaar17126b12021-01-07 22:03:02 +01002849 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002850 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002851
2852 for (s = p; ASCII_ISALPHA(*s); ++s)
2853 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002854 n = skipwhite(s);
2855 if (vim_strchr((char_u *)".=", *n) != NULL
2856 || *s == '['
2857 || (*n != NUL && n[1] == '='))
Bram Moolenaar17126b12021-01-07 22:03:02 +01002858 break;
2859 }
2860
Bram Moolenaareffed932018-08-14 13:38:17 +02002861 switch (*p)
2862 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002863 // When adding an entry, also modify cmd_exists().
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002864 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002865 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002866 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002867 continue;
2868
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002869 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002870 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002871 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002872 continue;
2873 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002874 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002875 {
2876#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002877 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002878#endif
2879 continue;
2880 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002881 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002882 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002883 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002884 continue;
2885
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002886 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002887 break;
2888#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002889 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002890#endif
2891 continue;
2892
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002893 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002894 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002895 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002896 continue;
2897 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002898 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002899 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002900 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002901 continue;
2902 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002903 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002904 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002905 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002906 continue;
2907 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002908 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002909 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002910 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002911 continue;
2912
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002913 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002914 {
Bram Moolenaare729ce22021-06-06 21:38:09 +02002915 char_u *reg_pat;
2916 char_u *nulp = NULL;
2917 int c = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002918
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002919 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002920 || *p == NUL
2921 || (ends_excmd(*p)
2922#ifdef FEAT_EVAL
2923 // in ":filter #pat# cmd" # does not
2924 // start a comment
Bram Moolenaarbc510062022-02-14 21:19:04 +00002925 && (!vim9script || VIM_ISWHITE(p[1]))
Bram Moolenaar06bffe82021-11-28 20:24:17 +00002926#endif
2927 ))
Bram Moolenaareffed932018-08-14 13:38:17 +02002928 break;
2929 if (*p == '!')
2930 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002931 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002932 p = skipwhite(p + 1);
2933 if (*p == NUL || ends_excmd(*p))
2934 break;
2935 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002936#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002937 // Avoid that "filter(arg)" is recognized.
Bram Moolenaarbc510062022-02-14 21:19:04 +00002938 if (vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002939 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002940#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002941 if (skip_only)
2942 p = skip_vimgrep_pat(p, NULL, NULL);
2943 else
2944 // NOTE: This puts a NUL after the pattern.
Bram Moolenaare729ce22021-06-06 21:38:09 +02002945 p = skip_vimgrep_pat_ext(p, &reg_pat, NULL,
2946 &nulp, &c);
Bram Moolenaareffed932018-08-14 13:38:17 +02002947 if (p == NULL || *p == NUL)
2948 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002949 if (!skip_only)
2950 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002951 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002952 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002953 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002954 break;
Bram Moolenaare729ce22021-06-06 21:38:09 +02002955 // restore the character overwritten by NUL
2956 if (nulp != NULL)
2957 *nulp = c;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002958 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002959 eap->cmd = p;
2960 continue;
2961 }
2962
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002963 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002964 case 'h': if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02002965 || *p == NUL || ends_excmd(*p))
2966 break;
2967 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002968 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002969 continue;
2970
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002971 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002972 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002973 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002974 continue;
2975 }
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002976 if (checkforcmd_noparen(&eap->cmd, "legacy", 3))
2977 {
2978 if (ends_excmd2(p, eap->cmd))
2979 {
2980 *errormsg =
Bram Moolenaardd9de502021-08-15 13:49:42 +02002981 _(e_legacy_must_be_followed_by_command);
Bram Moolenaar96cf4ba2021-04-24 14:15:41 +02002982 return FAIL;
2983 }
2984 cmod->cmod_flags |= CMOD_LEGACY;
2985 continue;
2986 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002987
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002988 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002989 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002990 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002991 continue;
2992
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002993 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002994 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002995 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002996 continue;
2997 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002998 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002999 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003000 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02003001 continue;
3002
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003003 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02003004 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003005 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02003006 continue;
3007
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003008 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003009 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003010 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02003011 continue;
3012 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003013 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003014 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003015 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003016 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
3017 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003018 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02003019 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02003020 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003021 }
3022 continue;
3023
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003024 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003025 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003026 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02003027 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003028 long tabnr = get_address(eap, &eap->cmd,
3029 ADDR_TABS, eap->skip,
3030 skip_only, FALSE, 1);
3031 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02003032 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01003033 else
Bram Moolenaareffed932018-08-14 13:38:17 +02003034 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01003035 if (tabnr < 0 || tabnr > LAST_TAB_NR)
3036 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003037 *errormsg = _(e_invalid_range);
Bram Moolenaar548e5982018-12-26 21:45:00 +01003038 return FAIL;
3039 }
Bram Moolenaare1004402020-10-24 20:49:43 +02003040 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003041 }
Bram Moolenaareffed932018-08-14 13:38:17 +02003042 }
3043 eap->cmd = p;
3044 continue;
3045 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003046 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02003047 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003048 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02003049 continue;
3050
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003051 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02003052 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02003053 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003054 continue;
3055
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003056 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003057 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003058 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02003059 continue;
3060 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003061 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01003062 {
3063 if (ends_excmd2(p, eap->cmd))
3064 {
3065 *errormsg =
3066 _(e_vim9cmd_must_be_followed_by_command);
3067 return FAIL;
3068 }
3069 cmod->cmod_flags |= CMOD_VIM9CMD;
3070 continue;
3071 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003072 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003073 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003074 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02003075 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003076 else
Bram Moolenaare1004402020-10-24 20:49:43 +02003077 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003078 eap->cmd = p;
3079 continue;
3080 }
3081 break;
3082 }
3083
3084 return OK;
3085}
3086
3087/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003088 * Return TRUE if "cmod" has anything set.
3089 */
3090 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003091has_cmdmod(cmdmod_T *cmod, int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003092{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003093 return (cmod->cmod_flags != 0 && (!ignore_silent
3094 || (cmod->cmod_flags
3095 & ~(CMOD_SILENT | CMOD_ERRSILENT | CMOD_UNSILENT)) != 0))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003096 || cmod->cmod_split != 0
3097 || cmod->cmod_verbose != 0
3098 || cmod->cmod_tab != 0
3099 || cmod->cmod_filter_regmatch.regprog != NULL;
3100}
3101
Bram Moolenaar8991be22022-02-14 21:51:46 +00003102#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003103/*
3104 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3105 */
3106 int
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003107cmdmod_error(int ignore_silent)
Bram Moolenaarfa984412021-03-25 22:15:28 +01003108{
Bram Moolenaar917c46a2021-08-10 19:53:01 +02003109 if (in_vim9script() && has_cmdmod(&cmdmod, ignore_silent))
Bram Moolenaarfa984412021-03-25 22:15:28 +01003110 {
3111 emsg(_(e_misplaced_command_modifier));
3112 return TRUE;
3113 }
3114 return FALSE;
3115}
Dominique Pelle748b3082022-01-08 12:41:16 +00003116#endif
Bram Moolenaarfa984412021-03-25 22:15:28 +01003117
3118/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003119 * Apply the command modifiers. Saves current state in "cmdmod", call
3120 * undo_cmdmod() later.
3121 */
3122 void
3123apply_cmdmod(cmdmod_T *cmod)
3124{
3125#ifdef HAVE_SANDBOX
3126 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3127 {
3128 ++sandbox;
3129 cmod->cmod_did_sandbox = TRUE;
3130 }
3131#endif
3132 if (cmod->cmod_verbose > 0)
3133 {
3134 if (cmod->cmod_verbose_save == 0)
3135 cmod->cmod_verbose_save = p_verbose + 1;
3136 p_verbose = cmod->cmod_verbose;
3137 }
3138
3139 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3140 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003141 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003142 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003143 cmod->cmod_save_msg_scroll = msg_scroll;
3144 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003145 if (cmod->cmod_flags & CMOD_SILENT)
3146 ++msg_silent;
3147 if (cmod->cmod_flags & CMOD_UNSILENT)
3148 msg_silent = 0;
3149
3150 if (cmod->cmod_flags & CMOD_ERRSILENT)
3151 {
3152 ++emsg_silent;
3153 ++cmod->cmod_did_esilent;
3154 }
3155
Bram Moolenaare1004402020-10-24 20:49:43 +02003156 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003157 {
3158 // Set 'eventignore' to "all".
3159 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003160 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003161 set_string_option_direct((char_u *)"ei", -1,
3162 (char_u *)"all", OPT_FREE, SID_NONE);
3163 }
3164}
3165
3166/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003167 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003168 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003169 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003170undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003171{
Bram Moolenaare1004402020-10-24 20:49:43 +02003172 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003173 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003174 p_verbose = cmod->cmod_verbose_save - 1;
3175 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003176 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003177
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003178#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003179 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003180 {
3181 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003182 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003183 }
3184#endif
3185
Bram Moolenaare1004402020-10-24 20:49:43 +02003186 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003187 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003188 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003189 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3190 OPT_FREE, SID_NONE);
3191 free_string_option(cmod->cmod_save_ei);
3192 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003193 }
3194
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003195 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003196
Bram Moolenaare1004402020-10-24 20:49:43 +02003197 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003198 {
3199 // messages could be enabled for a serious error, need to check if the
3200 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003201 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3202 msg_silent = cmod->cmod_save_msg_silent - 1;
3203 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003204 if (emsg_silent < 0)
3205 emsg_silent = 0;
3206 // Restore msg_scroll, it's set by file I/O commands, even when no
3207 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003208 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003209
3210 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3211 // somewhere in the line. Put it back in the first column.
3212 if (redirecting())
3213 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003214
Bram Moolenaare1004402020-10-24 20:49:43 +02003215 cmod->cmod_save_msg_silent = 0;
3216 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003217 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003218}
3219
3220/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003221 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003222 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003223 * Return FAIL and set "errormsg" or return OK.
3224 */
3225 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003226parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003227{
3228 int address_count = 1;
3229 linenr_T lnum;
3230
3231 // Repeat for all ',' or ';' separated addresses.
3232 for (;;)
3233 {
3234 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003235 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003236 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003237 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003238 eap->addr_count == 0, address_count++);
3239 if (eap->cmd == NULL) // error detected
3240 return FAIL;
3241 if (lnum == MAXLNUM)
3242 {
3243 if (*eap->cmd == '%') // '%' - all lines
3244 {
3245 ++eap->cmd;
3246 switch (eap->addr_type)
3247 {
3248 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003249 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003250 eap->line1 = 1;
3251 eap->line2 = curbuf->b_ml.ml_line_count;
3252 break;
3253 case ADDR_LOADED_BUFFERS:
3254 {
3255 buf_T *buf = firstbuf;
3256
3257 while (buf->b_next != NULL
3258 && buf->b_ml.ml_mfp == NULL)
3259 buf = buf->b_next;
3260 eap->line1 = buf->b_fnum;
3261 buf = lastbuf;
3262 while (buf->b_prev != NULL
3263 && buf->b_ml.ml_mfp == NULL)
3264 buf = buf->b_prev;
3265 eap->line2 = buf->b_fnum;
3266 break;
3267 }
3268 case ADDR_BUFFERS:
3269 eap->line1 = firstbuf->b_fnum;
3270 eap->line2 = lastbuf->b_fnum;
3271 break;
3272 case ADDR_WINDOWS:
3273 case ADDR_TABS:
3274 if (IS_USER_CMDIDX(eap->cmdidx))
3275 {
3276 eap->line1 = 1;
3277 eap->line2 = eap->addr_type == ADDR_WINDOWS
3278 ? LAST_WIN_NR : LAST_TAB_NR;
3279 }
3280 else
3281 {
3282 // there is no Vim command which uses '%' and
3283 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaar108010a2021-06-27 22:03:33 +02003284 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003285 return FAIL;
3286 }
3287 break;
3288 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003289 case ADDR_UNSIGNED:
3290 case ADDR_QUICKFIX:
Bram Moolenaar108010a2021-06-27 22:03:33 +02003291 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003292 return FAIL;
3293 case ADDR_ARGUMENTS:
3294 if (ARGCOUNT == 0)
3295 eap->line1 = eap->line2 = 0;
3296 else
3297 {
3298 eap->line1 = 1;
3299 eap->line2 = ARGCOUNT;
3300 }
3301 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003302 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003303#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003304 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003305 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003306 if (eap->line2 == 0)
3307 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003308#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003309 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003310 case ADDR_NONE:
3311 // Will give an error later if a range is found.
3312 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003313 }
3314 ++eap->addr_count;
3315 }
3316 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3317 {
3318 pos_T *fp;
3319
3320 // '*' - visual area
3321 if (eap->addr_type != ADDR_LINES)
3322 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003323 *errormsg = _(e_invalid_range);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003324 return FAIL;
3325 }
3326
3327 ++eap->cmd;
3328 if (!eap->skip)
3329 {
3330 fp = getmark('<', FALSE);
3331 if (check_mark(fp) == FAIL)
3332 return FAIL;
3333 eap->line1 = fp->lnum;
3334 fp = getmark('>', FALSE);
3335 if (check_mark(fp) == FAIL)
3336 return FAIL;
3337 eap->line2 = fp->lnum;
3338 ++eap->addr_count;
3339 }
3340 }
3341 }
3342 else
3343 eap->line2 = lnum;
3344 eap->addr_count++;
3345
3346 if (*eap->cmd == ';')
3347 {
3348 if (!eap->skip)
3349 {
3350 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003351 // Don't leave the cursor on an illegal line or column, but do
3352 // accept zero as address, so 0;/PATTERN/ works correctly.
3353 if (eap->line2 > 0)
3354 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003355 }
3356 }
3357 else if (*eap->cmd != ',')
3358 break;
3359 ++eap->cmd;
3360 }
3361
3362 // One address given: set start and end lines.
3363 if (eap->addr_count == 1)
3364 {
3365 eap->line1 = eap->line2;
3366 // ... but only implicit: really no address given
3367 if (lnum == MAXLNUM)
3368 eap->addr_count = 0;
3369 }
3370 return OK;
3371}
3372
3373/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003374 * Append "cmd" to the error message in IObuff.
3375 * Takes care of limiting the length and handling 0xa0, which would be
3376 * invisible otherwise.
3377 */
3378 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003379append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003380{
3381 char_u *s = cmd;
3382 char_u *d;
3383
3384 STRCAT(IObuff, ": ");
3385 d = IObuff + STRLEN(IObuff);
3386 while (*s != NUL && d - IObuff < IOSIZE - 7)
3387 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003388 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003389 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003390 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003391 STRCPY(d, "<a0>");
3392 d += 4;
3393 }
3394 else
3395 MB_COPY_CHAR(s, d);
3396 }
3397 *d = NUL;
3398}
3399
Dominique Pelle748b3082022-01-08 12:41:16 +00003400#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003401/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003402 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3403 * the name. Otherwise just return "start".
3404 */
3405 char_u *
3406skip_option_env_lead(char_u *start)
3407{
3408 char_u *name = start;
3409
3410 if (*start == '&')
3411 {
3412 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3413 name += 3;
3414 else
3415 name += 1;
3416 }
3417 else if (*start == '$')
3418 name += 1;
3419 return name;
3420}
Dominique Pelle748b3082022-01-08 12:41:16 +00003421#endif
Bram Moolenaar2e800952020-08-23 19:34:48 +02003422
3423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003425 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003426 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003427 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 *
3429 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3430 * assignment without "let". Sets eap->cmdidx to the command while returning
3431 * "eap->cmd".
3432 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 * Returns NULL for an ambiguous user command.
3434 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 char_u *
3436find_ex_command(
3437 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003438 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003439 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003440 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441{
3442 int len;
3443 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003444 int i;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003445#ifndef FEAT_EVAL
3446 int vim9 = FALSE;
3447#else
3448 int vim9 = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 /*
3451 * Recognize a Vim9 script function/method call and assignment:
3452 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3453 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003454 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003455 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003457 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003459 if (vim_strchr((char_u *)"{('[\"@&$", *p) != NULL
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003460 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL)
3461 || (p[0] == '0' && p[1] == 'z'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003463 int oplen;
3464 int heredoc;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003465 char_u *swp;
3466
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003467 if (*eap->cmd == '&'
3468 || *eap->cmd == '$'
3469 || (eap->cmd[0] == '@'
Bram Moolenaar73170912021-08-22 22:44:11 +02003470 && (valid_yank_reg(eap->cmd[1], FALSE)
3471 || eap->cmd[1] == '@')))
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003472 {
Bram Moolenaar73170912021-08-22 22:44:11 +02003473 if (*eap->cmd == '&')
3474 {
3475 p = eap->cmd + 1;
3476 if (STRNCMP("l:", p, 2) == 0 || STRNCMP("g:", p, 2) == 0)
3477 p += 2;
3478 p = to_name_end(p, FALSE);
3479 }
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003480 else if (*eap->cmd == '$')
3481 p = to_name_end(eap->cmd + 1, FALSE);
Bram Moolenaar73170912021-08-22 22:44:11 +02003482 else
3483 p = eap->cmd + 2;
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003484 if (ends_excmd(*skipwhite(p)))
3485 {
Bram Moolenaar5ca5cc62021-08-24 21:56:03 +02003486 // "&option <NL>", "$ENV <NL>" and "@r <NL>" are the start
3487 // of an expression.
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003488 eap->cmdidx = CMD_eval;
3489 return eap->cmd;
3490 }
3491 // "&option" can be followed by "->" or "=", check below
3492 }
3493
3494 swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003496 if (
3497 // "(..." is an expression.
3498 // "funcname(" is always a function call.
3499 *p == '('
3500 || (p == eap->cmd
3501 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003502 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003503 *eap->cmd == '{'
3504 // "'string'->func()" is an expression.
3505 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003506 // '"string"->func()' is an expression.
Bram Moolenaarc1e6c7b2022-02-20 18:26:46 +00003507 || (eap->cmd[0] == '0' && eap->cmd[1] == 'z')
3508 // '"string"->func()' is an expression.
Bram Moolenaar10409562020-07-29 20:00:38 +02003509 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003510 // "g:varname" is an expression.
3511 || eap->cmd[1] == ':'
3512 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003513 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003514 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003515 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003516 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3517 {
3518 // "{" by itself is the start of a block.
3519 eap->cmdidx = CMD_block;
3520 return eap->cmd + 1;
3521 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003522 eap->cmdidx = CMD_eval;
3523 return eap->cmd;
3524 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003525
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003526 if (p != eap->cmd && (
3527 // "varname[]" is an expression.
3528 *p == '['
3529 // "varname.key" is an expression.
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02003530 || (*p == '.' && (ASCII_ISALPHA(p[1])
3531 || p[1] == '_'))))
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003532 {
Bram Moolenaar68452172021-04-12 21:21:02 +02003533 char_u *after = eap->cmd;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003534
3535 // When followed by "=" or "+=" then it is an assignment.
Bram Moolenaar68452172021-04-12 21:21:02 +02003536 // Skip over the whole thing, it can be:
3537 // name.member = val
3538 // name[a : b] = val
3539 // name[idx] = val
3540 // name[idx].member = val
3541 // etc.
3542 eap->cmdidx = CMD_eval;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003543 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003544 if (skip_expr(&after, NULL) == OK)
Bram Moolenaar68452172021-04-12 21:21:02 +02003545 {
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003546 after = skipwhite(after);
Bram Moolenaar68452172021-04-12 21:21:02 +02003547 if (*after == '=' || (*after != NUL && after[1] == '=')
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003548 || (after[0] == '.' && after[1] == '.'
3549 && after[2] == '='))
Bram Moolenaar68452172021-04-12 21:21:02 +02003550 eap->cmdidx = CMD_var;
3551 }
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003552 --emsg_silent;
3553 return eap->cmd;
3554 }
3555
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003556 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3557 // an assignment.
3558 // If there is no line break inside the "[...]" then "p" is
3559 // advanced to after the "]" by to_name_const_end(): check if a "="
3560 // follows.
3561 // If "[...]" has a line break "p" still points at the "[" and it
3562 // can't be an assignment.
3563 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003564 {
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003565 char_u *eq;
3566
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003567 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003568 if (p == eap->cmd && *p == '[')
3569 {
3570 int count = 0;
3571 int semicolon = FALSE;
3572
3573 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3574 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003575 eq = p;
3576 if (eq != NULL)
3577 {
3578 eq = skipwhite(eq);
3579 if (vim_strchr((char_u *)"+-*/%", *eq) != NULL)
3580 ++eq;
3581 }
3582 if (p == NULL || p == eap->cmd || *eq != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003583 {
3584 eap->cmdidx = CMD_eval;
3585 return eap->cmd;
3586 }
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02003587 if (p > eap->cmd && *eq == '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003588 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003589 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003590 return eap->cmd;
3591 }
3592 }
3593
3594 // Recognize an assignment if we recognize the variable name:
3595 // "g:var = expr"
Bram Moolenaarbf5f2872021-08-21 20:50:35 +02003596 // "@r = expr"
3597 // "&opt = expr"
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003598 // "var = expr" where "var" is a variable name or we are skipping
3599 // (variable declaration might have been skipped).
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003600 oplen = assignment_len(skipwhite(p), &heredoc);
3601 if (oplen > 0)
3602 {
3603 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3604 || *eap->cmd == '&'
3605 || *eap->cmd == '$'
3606 || *eap->cmd == '@'
Bram Moolenaaraa1959b2021-04-15 22:13:39 +02003607 || eap->skip
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003608 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003609 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003610 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003611 return eap->cmd;
3612 }
3613 }
3614
3615 // Recognize using a type for a w:, b:, t: or g: variable:
3616 // "w:varname: number = 123".
3617 if (eap->cmd[1] == ':' && *p == ':')
3618 {
3619 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003620 return eap->cmd;
3621 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003622 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003623
Bram Moolenaar7b829262021-10-13 15:04:34 +01003624 // "g:", "s:" and "l:" are always assumed to be a variable, thus start
3625 // an expression. A global/substitute/list command needs to use a
3626 // longer name.
3627 if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
3628 {
3629 eap->cmdidx = CMD_eval;
3630 return eap->cmd;
3631 }
3632
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003633 // If it is an ID it might be a variable with an operator on the next
3634 // line, if the variable exists it can't be an Ex command.
3635 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003636 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003637 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3638 {
3639 eap->cmdidx = CMD_eval;
3640 return eap->cmd;
3641 }
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003642
3643 // Check for "++nr" and "--nr".
Bram Moolenaard3a11782022-01-05 16:50:40 +00003644 if (p == eap->cmd && p[0] != NUL && p[0] == p[1]
3645 && (*p == '+' || *p == '-'))
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003646 {
3647 eap->cmdidx = *p == '+' ? CMD_increment : CMD_decrement;
3648 return eap->cmd + 2;
3649 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 }
3651#endif
3652
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 /*
3654 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003655 * Exceptions:
Bram Moolenaar10b94212021-02-19 21:42:57 +01003656 * - The 'k' command can directly be followed by any character.
3657 * But it is not used in Vim9 script.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003659 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003661 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 */
3663 p = eap->cmd;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003664 if (!vim9 && *p == 'k')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 {
3666 eap->cmdidx = CMD_k;
3667 ++p;
3668 }
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003669 else if (!vim9
3670 && p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003671 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3672 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 || p[1] == 'g'
3674 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3675 || p[1] == 'I'
3676 || (p[1] == 'r' && p[2] != 'e')))
3677 {
3678 eap->cmdidx = CMD_substitute;
3679 ++p;
3680 }
3681 else
3682 {
3683 while (ASCII_ISALPHA(*p))
3684 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003685 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003686 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003688 while (ASCII_ISALNUM(*p))
3689 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690 }
3691 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3692 {
Bram Moolenaardf749a22021-03-28 15:29:43 +02003693 // include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 ++p;
3695 while (ASCII_ISALPHA(*p))
3696 ++p;
3697 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003698
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003699 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003700 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 ++p;
3702 len = (int)(p - eap->cmd);
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003703 if (!vim9 && *eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
Bram Moolenaardf177f62005-02-22 08:39:57 +00003704 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003705 // Check for ":dl", ":dell", etc. to ":deletel": that's
3706 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003707 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003708 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003709 break;
3710 if (i == len - 1)
3711 {
3712 --len;
3713 if (p[-1] == 'l')
3714 eap->flags |= EXFLAG_LIST;
3715 else
3716 eap->flags |= EXFLAG_PRINT;
3717 }
3718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003720 if (ASCII_ISLOWER(eap->cmd[0]))
3721 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003722 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003723 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003724
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003725 if (command_count != (int)CMD_SIZE)
3726 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003727 iemsg(_(e_command_table_needs_to_be_updated_run_make_cmdidxs));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003728 getout(1);
3729 }
3730
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003731 // Use a precomputed index for fast look-up in cmdnames[]
3732 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003733 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3734 if (ASCII_ISLOWER(c2))
3735 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3736 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003737 else if (ASCII_ISUPPER(eap->cmd[0]))
3738 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003740 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741
3742 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3743 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3744 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3745 (size_t)len) == 0)
3746 {
3747#ifdef FEAT_EVAL
Bram Moolenaar204852a2022-03-05 12:56:44 +00003748 if (full != NULL && cmdnames[eap->cmdidx].cmd_name[len] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 *full = TRUE;
3750#endif
3751 break;
3752 }
3753
Bram Moolenaar204852a2022-03-05 12:56:44 +00003754 // :Print and :mode are not supported in Vim9 script.
3755 // Some commands cannot be shortened in Vim9 script.
Bram Moolenaar204852a2022-03-05 12:56:44 +00003756 if (vim9 && eap->cmdidx != CMD_SIZE)
3757 {
3758 if (eap->cmdidx == CMD_mode || eap->cmdidx == CMD_Print)
3759 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb2175222022-03-05 20:24:41 +00003760 else if ((cmdnames[eap->cmdidx].cmd_argt & EX_WHOLE)
Bram Moolenaar204852a2022-03-05 12:56:44 +00003761 && len < (int)STRLEN(cmdnames[eap->cmdidx].cmd_name))
Bram Moolenaar204852a2022-03-05 12:56:44 +00003762 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00003763 semsg(_(e_command_cannot_be_shortened_str), eap->cmd);
Bram Moolenaar204852a2022-03-05 12:56:44 +00003764 eap->cmdidx = CMD_SIZE;
3765 }
3766 }
Bram Moolenaar6aca4d32022-03-04 17:10:19 +00003767
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003768 // Do not recognize ":*" as the star command unless '*' is in
Bram Moolenaar95388e32020-11-20 21:07:00 +01003769 // 'cpoptions'.
3770 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3771 p = eap->cmd;
3772
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003773 // Look for a user defined command as a last resort. Let ":Print" be
3774 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003775 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3776 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003778 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 while (ASCII_ISALNUM(*p))
3780 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003781 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003783 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 eap->cmdidx = CMD_SIZE;
3785 }
3786
Bram Moolenaar204852a2022-03-05 12:56:44 +00003787 // ":fina" means ":finally" in legacy script, for backwards compatibility.
3788 if (eap->cmdidx == CMD_final && p - eap->cmd == 4 && !vim9)
Bram Moolenaar373863e2020-09-26 17:20:53 +02003789 eap->cmdidx = CMD_finally;
3790
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003791#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003792 if (eap->cmdidx < CMD_SIZE
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003793 && vim9
Bram Moolenaar9fa5dab2021-07-20 19:18:44 +02003794 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!' && *p != '|'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003795 && (eap->cmdidx < 0 ||
3796 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003797 {
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003798 char_u *cmd = vim_strnsave(eap->cmd, p - eap->cmd);
3799
3800 semsg(_(e_command_str_not_followed_by_white_space_str), cmd, eap->cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003801 eap->cmdidx = CMD_SIZE;
Bram Moolenaarb98cec22021-04-25 16:35:55 +02003802 vim_free(cmd);
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003803 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003804#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003805
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 return p;
3807}
3808
3809#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003810static struct cmdmod
3811{
3812 char *name;
3813 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003814 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003815} cmdmods[] = {
3816 {"aboveleft", 3, FALSE},
3817 {"belowright", 3, FALSE},
3818 {"botright", 2, FALSE},
3819 {"browse", 3, FALSE},
3820 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003821 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003822 {"hide", 3, FALSE},
3823 {"keepalt", 5, FALSE},
3824 {"keepjumps", 5, FALSE},
3825 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003826 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003827 {"leftabove", 5, FALSE},
3828 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003829 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003830 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003831 {"rightbelow", 6, FALSE},
3832 {"sandbox", 3, FALSE},
3833 {"silent", 3, FALSE},
3834 {"tab", 3, TRUE},
3835 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003836 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003837 {"verbose", 4, TRUE},
3838 {"vertical", 4, FALSE},
3839};
3840
3841/*
3842 * Return length of a command modifier (including optional count).
3843 * Return zero when it's not a modifier.
3844 */
3845 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003846modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003847{
3848 int i, j;
3849 char_u *p = cmd;
3850
3851 if (VIM_ISDIGIT(*cmd))
Dominique Pelle4781d6f2021-05-18 21:46:31 +02003852 p = skipwhite(skipdigits(cmd + 1));
K.Takataeeec2542021-06-02 13:28:16 +02003853 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003854 {
3855 for (j = 0; p[j] != NUL; ++j)
3856 if (p[j] != cmdmods[i].name[j])
3857 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003858 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003859 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003860 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003861 }
3862 return 0;
3863}
3864
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865/*
3866 * Return > 0 if an Ex command "name" exists.
3867 * Return 2 if there is an exact match.
3868 * Return 3 if there is an ambiguous match.
3869 */
3870 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003871cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872{
3873 exarg_T ea;
3874 int full = FALSE;
3875 int i;
3876 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003877 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003879 // Check command modifiers.
K.Takataeeec2542021-06-02 13:28:16 +02003880 for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 {
3882 for (j = 0; name[j] != NUL; ++j)
3883 if (name[j] != cmdmods[i].name[j])
3884 break;
3885 if (name[j] == NUL && j >= cmdmods[i].minlen)
3886 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3887 }
3888
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003889 // Check built-in commands and user defined commands.
3890 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003891 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003894 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003896 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3897 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003898 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003899 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3901}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003902
3903/*
3904 * "fullcommand" function
3905 */
3906 void
3907f_fullcommand(typval_T *argvars, typval_T *rettv)
3908{
3909 exarg_T ea;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003910 char_u *name;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003911 char_u *p;
3912
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003913 rettv->v_type = VAR_STRING;
3914 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003915
3916 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
3917 return;
3918
3919 name = argvars[0].vval.v_string;
Bram Moolenaar4c8e8c62021-05-26 19:49:09 +02003920 if (name == NULL)
3921 return;
3922
zeertzjqc024ed92022-01-04 16:22:52 +00003923 while (*name == ':')
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003924 name++;
3925 name = skip_range(name, TRUE, NULL);
3926
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003927 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
3928 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003929 ea.addr_count = 0;
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003930 p = find_ex_command(&ea, NULL, NULL, NULL);
3931 if (p == NULL || ea.cmdidx == CMD_SIZE)
3932 return;
Bram Moolenaar7d840e92021-05-26 21:10:11 +02003933 if (in_vim9script())
3934 {
3935 int res;
3936
3937 ++emsg_silent;
3938 res = not_in_vim9(&ea);
3939 --emsg_silent;
3940
3941 if (res == FAIL)
3942 return;
3943 }
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003944
3945 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02003946 ? get_user_command_name(ea.useridx, ea.cmdidx)
3947 : cmdnames[ea.cmdidx].cmd_name);
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003948}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949#endif
3950
Bram Moolenaard0190392019-08-23 21:17:35 +02003951 cmdidx_T
3952excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953{
Bram Moolenaard0190392019-08-23 21:17:35 +02003954 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955
Bram Moolenaard0190392019-08-23 21:17:35 +02003956 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3957 idx = (cmdidx_T)((int)idx + 1))
3958 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 break;
3960
Bram Moolenaard0190392019-08-23 21:17:35 +02003961 return idx;
3962}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963
Bram Moolenaard0190392019-08-23 21:17:35 +02003964 long
3965excmd_get_argt(cmdidx_T idx)
3966{
3967 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968}
3969
3970/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003971 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 *
3973 * Backslashed delimiters after / or ? will be skipped, and commands will
3974 * not be expanded between /'s and ?'s or after "'".
3975 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003976 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 * Returns the "cmd" pointer advanced to beyond the range.
3978 */
3979 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003980skip_range(
Bram Moolenaaraf377e32021-11-29 12:12:43 +00003981 char_u *cmd_start,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003982 int skip_star, // skip "*" used for Visual range
3983 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984{
Bram Moolenaaraf377e32021-11-29 12:12:43 +00003985 char_u *cmd = cmd_start;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003986 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003988 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003990 if (*cmd == '\\')
3991 {
3992 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3993 ++cmd;
3994 else
3995 break;
3996 }
3997 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 {
Bram Moolenaaraf377e32021-11-29 12:12:43 +00003999 char_u *p = cmd;
4000
4001 // a quote is only valid at the start or after a separator
4002 while (p > cmd_start)
4003 {
4004 --p;
4005 if (!VIM_ISWHITE(*p))
4006 break;
4007 }
4008 if (cmd > cmd_start && !VIM_ISWHITE(*p) && *p != ',' && *p != ';')
4009 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 if (*++cmd == NUL && ctx != NULL)
4011 *ctx = EXPAND_NOTHING;
4012 }
4013 else if (*cmd == '/' || *cmd == '?')
4014 {
4015 delim = *cmd++;
4016 while (*cmd != NUL && *cmd != delim)
4017 if (*cmd++ == '\\' && *cmd != NUL)
4018 ++cmd;
4019 if (*cmd == NUL && ctx != NULL)
4020 *ctx = EXPAND_NOTHING;
4021 }
4022 if (*cmd != NUL)
4023 ++cmd;
4024 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004025
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004026 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00004027 while (*cmd == ':')
4028 cmd = skipwhite(cmd + 1);
4029
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02004030 // Skip "*" used for Visual range.
4031 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
4032 cmd = skipwhite(cmd + 1);
4033
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 return cmd;
4035}
4036
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004037 static void
4038addr_error(cmd_addr_T addr_type)
4039{
4040 if (addr_type == ADDR_NONE)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004041 emsg(_(e_no_range_allowed));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004042 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02004043 emsg(_(e_invalid_range));
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004044}
4045
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004047 * Return the default address for an address type.
4048 */
4049 static linenr_T
4050default_address(exarg_T *eap)
4051{
4052 linenr_T lnum = 0;
4053
4054 switch (eap->addr_type)
4055 {
4056 case ADDR_LINES:
4057 case ADDR_OTHER:
4058 // Default is the cursor line number. Avoid using an invalid
4059 // line number though.
4060 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4061 lnum = curbuf->b_ml.ml_line_count;
4062 else
4063 lnum = curwin->w_cursor.lnum;
4064 break;
4065 case ADDR_WINDOWS:
4066 lnum = CURRENT_WIN_NR;
4067 break;
4068 case ADDR_ARGUMENTS:
4069 lnum = curwin->w_arg_idx + 1;
4070 if (lnum > ARGCOUNT)
4071 lnum = ARGCOUNT;
4072 break;
4073 case ADDR_LOADED_BUFFERS:
4074 case ADDR_BUFFERS:
4075 lnum = curbuf->b_fnum;
4076 break;
4077 case ADDR_TABS:
4078 lnum = CURRENT_TAB_NR;
4079 break;
4080 case ADDR_TABS_RELATIVE:
4081 case ADDR_UNSIGNED:
4082 lnum = 1;
4083 break;
4084 case ADDR_QUICKFIX:
4085#ifdef FEAT_QUICKFIX
4086 lnum = qf_get_cur_idx(eap);
4087#endif
4088 break;
4089 case ADDR_QUICKFIX_VALID:
4090#ifdef FEAT_QUICKFIX
4091 lnum = qf_get_cur_valid_idx(eap);
4092#endif
4093 break;
4094 case ADDR_NONE:
4095 // Will give an error later if a range is found.
4096 break;
4097 }
4098 return lnum;
4099}
4100
4101/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004102 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 *
4104 * Set ptr to the next character after the part that was interpreted.
4105 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004106 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 *
4108 * Return MAXLNUM when no Ex address was found.
4109 */
4110 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004111get_address(
4112 exarg_T *eap UNUSED,
4113 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01004114 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004115 int skip, // only skip the address, don't use it
4116 int silent, // no errors or side effects
4117 int to_other_file, // flag: may jump to other file
4118 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119{
4120 int c;
4121 int i;
4122 long n;
4123 char_u *cmd;
4124 pos_T pos;
4125 pos_T *fp;
4126 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004127 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128
4129 cmd = skipwhite(*ptr);
4130 lnum = MAXLNUM;
4131 do
4132 {
4133 switch (*cmd)
4134 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004135 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004136 ++cmd;
4137 switch (addr_type)
4138 {
4139 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004140 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 lnum = curwin->w_cursor.lnum;
4142 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004143 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004144 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004145 break;
4146 case ADDR_ARGUMENTS:
4147 lnum = curwin->w_arg_idx + 1;
4148 break;
4149 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004150 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004151 lnum = curbuf->b_fnum;
4152 break;
4153 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004154 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004155 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004156 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004157 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004158 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004159 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004160 cmd = NULL;
4161 goto error;
4162 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004163 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004164#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004165 lnum = qf_get_cur_idx(eap);
4166#endif
4167 break;
4168 case ADDR_QUICKFIX_VALID:
4169#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004170 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004171#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004172 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004173 }
4174 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004176 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004177 ++cmd;
4178 switch (addr_type)
4179 {
4180 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004181 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 lnum = curbuf->b_ml.ml_line_count;
4183 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004184 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004185 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004186 break;
4187 case ADDR_ARGUMENTS:
4188 lnum = ARGCOUNT;
4189 break;
4190 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004191 buf = lastbuf;
4192 while (buf->b_ml.ml_mfp == NULL)
4193 {
4194 if (buf->b_prev == NULL)
4195 break;
4196 buf = buf->b_prev;
4197 }
4198 lnum = buf->b_fnum;
4199 break;
4200 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004201 lnum = lastbuf->b_fnum;
4202 break;
4203 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004204 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004205 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004206 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004207 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004208 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004209 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004210 cmd = NULL;
4211 goto error;
4212 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004213 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004214#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004215 lnum = qf_get_size(eap);
4216 if (lnum == 0)
4217 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004218#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004219 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004220 case ADDR_QUICKFIX_VALID:
4221#ifdef FEAT_QUICKFIX
4222 lnum = qf_get_valid_size(eap);
4223 if (lnum == 0)
4224 lnum = 1;
4225#endif
4226 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004227 }
4228 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004230 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004231 if (*++cmd == NUL)
4232 {
4233 cmd = NULL;
4234 goto error;
4235 }
4236 if (addr_type != ADDR_LINES)
4237 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004238 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004239 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004240 goto error;
4241 }
4242 if (skip)
4243 ++cmd;
4244 else
4245 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004246 // Only accept a mark in another file when it is
4247 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004248 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4249 ++cmd;
4250 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004251 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004252 lnum = curwin->w_cursor.lnum;
4253 else
4254 {
4255 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 {
4257 cmd = NULL;
4258 goto error;
4259 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004260 lnum = fp->lnum;
4261 }
4262 }
4263 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264
4265 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004266 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004267 c = *cmd++;
4268 if (addr_type != ADDR_LINES)
4269 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004270 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004271 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004272 goto error;
4273 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004274 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004275 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004276 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004277 if (*cmd == c)
4278 ++cmd;
4279 }
4280 else
4281 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004282 int flags;
4283
4284 pos = curwin->w_cursor; // save curwin->w_cursor
4285
4286 // When '/' or '?' follows another address, start from
4287 // there.
Bram Moolenaar35a319b2021-10-09 13:58:55 +01004288 if (lnum > 0 && lnum != MAXLNUM)
4289 curwin->w_cursor.lnum =
4290 lnum > curbuf->b_ml.ml_line_count
4291 ? curbuf->b_ml.ml_line_count : lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004292
4293 // Start a forward search at the end of the line (unless
4294 // before the first line).
4295 // Start a backward search at the start of the line.
4296 // This makes sure we never match in the current
4297 // line, and can match anywhere in the
4298 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004299 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004300 curwin->w_cursor.col = MAXCOL;
4301 else
4302 curwin->w_cursor.col = 0;
4303 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004304 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004305 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004306 {
4307 curwin->w_cursor = pos;
4308 cmd = NULL;
4309 goto error;
4310 }
4311 lnum = curwin->w_cursor.lnum;
4312 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004313 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004314 cmd += searchcmdlen;
4315 }
4316 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004318 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004319 ++cmd;
4320 if (addr_type != ADDR_LINES)
4321 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004322 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004323 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004324 goto error;
4325 }
4326 if (*cmd == '&')
4327 i = RE_SUBST;
4328 else if (*cmd == '?' || *cmd == '/')
4329 i = RE_SEARCH;
4330 else
4331 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004332 emsg(_(e_backslash_should_be_followed_by));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004333 cmd = NULL;
4334 goto error;
4335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004337 if (!skip)
4338 {
4339 /*
4340 * When search follows another address, start from
4341 * there.
4342 */
4343 if (lnum != MAXLNUM)
4344 pos.lnum = lnum;
4345 else
4346 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004348 /*
4349 * Start the search just like for the above
4350 * do_search().
4351 */
4352 if (*cmd != '?')
4353 pos.col = MAXCOL;
4354 else
4355 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004356 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004357 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004358 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004359 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004360 lnum = pos.lnum;
4361 else
4362 {
4363 cmd = NULL;
4364 goto error;
4365 }
4366 }
4367 ++cmd;
4368 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369
4370 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004371 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004372 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 }
4374
4375 for (;;)
4376 {
4377 cmd = skipwhite(cmd);
4378 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4379 break;
4380
4381 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004382 {
4383 switch (addr_type)
4384 {
4385 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004386 case ADDR_OTHER:
4387 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004388 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004389 break;
4390 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004391 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004392 break;
4393 case ADDR_ARGUMENTS:
4394 lnum = curwin->w_arg_idx + 1;
4395 break;
4396 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004397 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004398 lnum = curbuf->b_fnum;
4399 break;
4400 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004401 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004402 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004403 case ADDR_TABS_RELATIVE:
4404 lnum = 1;
4405 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004406 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004407#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004408 lnum = qf_get_cur_idx(eap);
4409#endif
4410 break;
4411 case ADDR_QUICKFIX_VALID:
4412#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004413 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004414#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004415 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004416 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004417 case ADDR_UNSIGNED:
4418 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004419 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004420 }
4421 }
4422
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004424 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 else
4426 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004427 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 n = 1;
4429 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004430 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 n = getdigits(&cmd);
Bram Moolenaar03725c52021-11-24 12:17:53 +00004432 if (n == MAXLNUM)
4433 {
4434 emsg(_(e_line_number_out_of_range));
4435 goto error;
4436 }
4437 }
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004438
4439 if (addr_type == ADDR_TABS_RELATIVE)
4440 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004441 emsg(_(e_invalid_range));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004442 cmd = NULL;
4443 goto error;
4444 }
4445 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004446 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004447 lnum = compute_buffer_local_count(
4448 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004450 {
4451#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004452 // Relative line addressing, need to adjust for folded lines
4453 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004454 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
Bram Moolenaar03725c52021-11-24 12:17:53 +00004455 && address_count >= 2)
Bram Moolenaarded27822017-01-02 14:27:34 +01004456 (void)hasFolding(lnum, NULL, &lnum);
4457#endif
4458 if (i == '-')
4459 lnum -= n;
4460 else
Bram Moolenaar03725c52021-11-24 12:17:53 +00004461 {
4462 if (n >= LONG_MAX - lnum)
4463 {
4464 emsg(_(e_line_number_out_of_range));
4465 goto error;
4466 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004467 lnum += n;
Bram Moolenaar03725c52021-11-24 12:17:53 +00004468 }
Bram Moolenaarded27822017-01-02 14:27:34 +01004469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
4471 } while (*cmd == '/' || *cmd == '?');
4472
4473error:
4474 *ptr = cmd;
4475 return lnum;
4476}
4477
4478/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004479 * Set eap->line1 and eap->line2 to the whole range.
4480 * Used for commands with the EX_DFLALL flag and no range given.
4481 */
4482 static void
4483address_default_all(exarg_T *eap)
4484{
4485 eap->line1 = 1;
4486 switch (eap->addr_type)
4487 {
4488 case ADDR_LINES:
4489 case ADDR_OTHER:
4490 eap->line2 = curbuf->b_ml.ml_line_count;
4491 break;
4492 case ADDR_LOADED_BUFFERS:
4493 {
4494 buf_T *buf = firstbuf;
4495
4496 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4497 buf = buf->b_next;
4498 eap->line1 = buf->b_fnum;
4499 buf = lastbuf;
4500 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4501 buf = buf->b_prev;
4502 eap->line2 = buf->b_fnum;
4503 }
4504 break;
4505 case ADDR_BUFFERS:
4506 eap->line1 = firstbuf->b_fnum;
4507 eap->line2 = lastbuf->b_fnum;
4508 break;
4509 case ADDR_WINDOWS:
4510 eap->line2 = LAST_WIN_NR;
4511 break;
4512 case ADDR_TABS:
4513 eap->line2 = LAST_TAB_NR;
4514 break;
4515 case ADDR_TABS_RELATIVE:
4516 eap->line2 = 1;
4517 break;
4518 case ADDR_ARGUMENTS:
4519 if (ARGCOUNT == 0)
4520 eap->line1 = eap->line2 = 0;
4521 else
4522 eap->line2 = ARGCOUNT;
4523 break;
4524 case ADDR_QUICKFIX_VALID:
4525#ifdef FEAT_QUICKFIX
4526 eap->line2 = qf_get_valid_size(eap);
4527 if (eap->line2 == 0)
4528 eap->line2 = 1;
4529#endif
4530 break;
4531 case ADDR_NONE:
4532 case ADDR_UNSIGNED:
4533 case ADDR_QUICKFIX:
4534 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4535 break;
4536 }
4537}
4538
4539
4540/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004541 * Get flags from an Ex command argument.
4542 */
4543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004544get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004545{
4546 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4547 {
4548 if (*eap->arg == 'l')
4549 eap->flags |= EXFLAG_LIST;
4550 else if (*eap->arg == 'p')
4551 eap->flags |= EXFLAG_PRINT;
4552 else
4553 eap->flags |= EXFLAG_NR;
4554 eap->arg = skipwhite(eap->arg + 1);
4555 }
4556}
4557
4558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 * Function called for command which is Not Implemented. NI!
4560 */
4561 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004562ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563{
4564 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004565 eap->errmsg =
Bram Moolenaareaaac012022-01-02 17:00:40 +00004566 _(e_sorry_command_is_not_available_in_this_version);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567}
4568
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004569#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570/*
4571 * Function called for script command which is Not Implemented. NI!
4572 * Skips over ":perl <<EOF" constructs.
4573 */
4574 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004575ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576{
4577 if (!eap->skip)
4578 ex_ni(eap);
4579 else
4580 vim_free(script_get(eap, eap->arg));
4581}
4582#endif
4583
4584/*
4585 * Check range in Ex command for validity.
4586 * Return NULL when valid, error message when invalid.
4587 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004588 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004589invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004591 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004592
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 if ( eap->line1 < 0
4594 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004595 || eap->line1 > eap->line2)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004596 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004597
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004598 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004599 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004600 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004601 {
4602 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004603 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004604#ifdef FEAT_DIFF
4605 + (eap->cmdidx == CMD_diffget)
4606#endif
4607 )
Bram Moolenaar108010a2021-06-27 22:03:33 +02004608 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004609 break;
4610 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004611 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004612 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaar108010a2021-06-27 22:03:33 +02004613 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004614 break;
4615 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004616 // Only a boundary check, not whether the buffers actually
4617 // exist.
4618 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaar108010a2021-06-27 22:03:33 +02004619 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004620 break;
4621 case ADDR_LOADED_BUFFERS:
4622 buf = firstbuf;
4623 while (buf->b_ml.ml_mfp == NULL)
4624 {
4625 if (buf->b_next == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004626 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004627 buf = buf->b_next;
4628 }
4629 if (eap->line1 < buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004630 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004631 buf = lastbuf;
4632 while (buf->b_ml.ml_mfp == NULL)
4633 {
4634 if (buf->b_prev == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004635 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004636 buf = buf->b_prev;
4637 }
4638 if (eap->line2 > buf->b_fnum)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004639 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004640 break;
4641 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004642 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004643 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004644 break;
4645 case ADDR_TABS:
4646 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004647 return _(e_invalid_range);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004648 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004649 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004650 case ADDR_OTHER:
4651 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004652 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004653 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004654#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004655 // No error for value that is too big, will use the last entry.
4656 if (eap->line2 <= 0)
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004657 {
4658 if (eap->addr_count == 0)
4659 return _(e_no_errors);
Bram Moolenaar108010a2021-06-27 22:03:33 +02004660 return _(e_invalid_range);
Bram Moolenaara5d78d12021-12-15 12:28:22 +00004661 }
Bram Moolenaare906c502015-09-09 21:10:39 +02004662#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004663 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004664 case ADDR_QUICKFIX_VALID:
4665#ifdef FEAT_QUICKFIX
4666 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4667 || eap->line2 < 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02004668 return _(e_invalid_range);
Bram Moolenaar25190db2019-05-04 15:05:28 +02004669#endif
4670 break;
4671 case ADDR_UNSIGNED:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004672 case ADDR_NONE:
4673 // Will give an error elsewhere.
4674 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004675 }
4676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 return NULL;
4678}
4679
4680/*
4681 * Correct the range for zero line number, if required.
4682 */
4683 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004684correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004686 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 {
4688 if (eap->line1 == 0)
4689 eap->line1 = 1;
4690 if (eap->line2 == 0)
4691 eap->line2 = 1;
4692 }
4693}
4694
Bram Moolenaar748bf032005-02-02 23:04:36 +00004695#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004696/*
4697 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4698 * pattern. Otherwise return eap->arg.
4699 */
4700 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004701skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004702{
4703 char_u *p = eap->arg;
4704
Bram Moolenaara37420f2006-02-04 22:37:47 +00004705 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4706 || eap->cmdidx == CMD_vimgrepadd
4707 || eap->cmdidx == CMD_lvimgrepadd
4708 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004709 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004710 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004711 if (p == NULL)
4712 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004713 }
4714 return p;
4715}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004716
4717/*
4718 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4719 * in the command line, so that things like % get expanded.
4720 */
4721 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004722replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004723{
4724 char_u *new_cmdline;
4725 char_u *program;
4726 char_u *pos;
4727 char_u *ptr;
4728 int len;
4729 int i;
4730
4731 /*
4732 * Don't do it when ":vimgrep" is used for ":grep".
4733 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004734 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4735 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4736 || eap->cmdidx == CMD_grepadd
4737 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004738 && !grep_internal(eap->cmdidx))
4739 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004740 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4741 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004742 {
4743 if (*curbuf->b_p_gp == NUL)
4744 program = p_gp;
4745 else
4746 program = curbuf->b_p_gp;
4747 }
4748 else
4749 {
4750 if (*curbuf->b_p_mp == NUL)
4751 program = p_mp;
4752 else
4753 program = curbuf->b_p_mp;
4754 }
4755
4756 p = skipwhite(p);
4757
4758 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4759 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004760 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004761 i = 1;
4762 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4763 ++i;
4764 len = (int)STRLEN(p);
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004765 new_cmdline = alloc(STRLEN(program) + (size_t)i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004766 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004767 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004768 ptr = new_cmdline;
4769 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4770 {
4771 i = (int)(pos - program);
4772 STRNCPY(ptr, program, i);
4773 STRCPY(ptr += i, p);
4774 ptr += len;
4775 program = pos + 2;
4776 }
4777 STRCPY(ptr, program);
4778 }
4779 else
4780 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004781 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004782 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004783 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004784 STRCPY(new_cmdline, program);
4785 STRCAT(new_cmdline, " ");
4786 STRCAT(new_cmdline, p);
4787 }
4788 msg_make(p);
4789
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004790 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004791 vim_free(*cmdlinep);
4792 *cmdlinep = new_cmdline;
4793 p = new_cmdline;
4794 }
4795 return p;
4796}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004797#endif
4798
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799/*
4800 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004801 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 * Return FAIL for failure, OK otherwise.
4803 */
4804 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004805expand_filename(
4806 exarg_T *eap,
4807 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004808 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004810 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 char_u *repl;
4812 int srclen;
4813 char_u *p;
4814 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004815 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816
Bram Moolenaar748bf032005-02-02 23:04:36 +00004817#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004818 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004819 p = skip_grep_pat(eap);
4820#else
4821 p = eap->arg;
4822#endif
4823
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 /*
4825 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4826 * the file name contains a wildcard it should not cause expanding.
4827 * (it will be expanded anyway if there is a wildcard before replacing).
4828 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004829 has_wildcards = mch_has_wildcard(p);
4830 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004832#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004833 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004834 if (p[0] == '`' && p[1] == '=')
4835 {
4836 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004837 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004838 if (*p == '`')
4839 ++p;
4840 continue;
4841 }
4842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 /*
4844 * Quick check if this cannot be the start of a special string.
4845 * Also removes backslash before '%', '#' and '<'.
4846 */
4847 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4848 {
4849 ++p;
4850 continue;
4851 }
4852
4853 /*
4854 * Try to find a match at this position.
4855 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004856 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4857 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004858 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004860 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 {
4862 p += srclen;
4863 continue;
4864 }
4865
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004866 // Wildcards won't be expanded below, the replacement is taken
4867 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004868 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4869 {
4870 char_u *l = repl;
4871
4872 repl = expand_env_save(repl);
4873 vim_free(l);
4874 }
4875
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004876 // Need to escape white space et al. with a backslash.
4877 // Don't do this for:
4878 // - replacement that already has been escaped: "##"
4879 // - shell commands (may have to use quotes instead).
4880 // - non-unix systems when there is a single argument (spaces don't
4881 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004883 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 && eap->cmdidx != CMD_grep
4886 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004887 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004888 && eap->cmdidx != CMD_lgrep
4889 && eap->cmdidx != CMD_lgrepadd
4890 && eap->cmdidx != CMD_lmake
4891 && eap->cmdidx != CMD_make
4892 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004894 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895#endif
4896 )
4897 {
4898 char_u *l;
4899#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004900 // Don't escape a backslash here, because rem_backslash() doesn't
4901 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 static char_u *nobslash = (char_u *)" \t\"|";
4903# define ESCAPE_CHARS nobslash
4904#else
4905# define ESCAPE_CHARS escape_chars
4906#endif
4907
4908 for (l = repl; *l; ++l)
4909 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4910 {
4911 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4912 if (l != NULL)
4913 {
4914 vim_free(repl);
4915 repl = l;
4916 }
4917 break;
4918 }
4919 }
4920
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004921 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004922 if ((eap->usefilter || eap->cmdidx == CMD_bang
4923 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004924 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 {
4926 char_u *l;
4927
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004928 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 if (l != NULL)
4930 {
4931 vim_free(repl);
4932 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
4934 }
4935
4936 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4937 vim_free(repl);
4938 if (p == NULL)
4939 return FAIL;
4940 }
4941
4942 /*
4943 * One file argument: Expand wildcards.
4944 * Don't do this with ":r !command" or ":w !command".
4945 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004946 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 {
4948 /*
4949 * May do this twice:
4950 * 1. Replace environment variables.
4951 * 2. Replace any other wildcards, remove backslashes.
4952 */
4953 for (n = 1; n <= 2; ++n)
4954 {
4955 if (n == 2)
4956 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 /*
4958 * Halve the number of backslashes (this is Vi compatible).
4959 * For Unix and OS/2, when wildcards are expanded, this is
4960 * done by ExpandOne() below.
4961 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004962#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 if (!has_wildcards)
4964#endif
4965 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 }
4967
4968 if (has_wildcards)
4969 {
4970 if (n == 1)
4971 {
4972 /*
4973 * First loop: May expand environment variables. This
4974 * can be done much faster with expand_env() than with
4975 * something else (e.g., calling a shell).
4976 * After expanding environment variables, check again
4977 * if there are still wildcards present.
4978 */
4979 if (vim_strchr(eap->arg, '$') != NULL
4980 || vim_strchr(eap->arg, '~') != NULL)
4981 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004982 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004983 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 has_wildcards = mch_has_wildcard(NameBuff);
4985 p = NameBuff;
4986 }
4987 else
4988 p = NULL;
4989 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004990 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 {
4992 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004993 int options = WILD_LIST_NOTFOUND
4994 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995
4996 ExpandInit(&xpc);
4997 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004998 if (p_wic)
4999 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005001 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 if (p == NULL)
5003 return FAIL;
5004 }
5005 if (p != NULL)
5006 {
5007 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5008 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005009 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 vim_free(p);
5011 }
5012 }
5013 }
5014 }
5015 return OK;
5016}
5017
5018/*
5019 * Replace part of the command line, keeping eap->cmd, eap->arg and
5020 * eap->nextcmd correct.
5021 * "src" points to the part that is to be replaced, of length "srclen".
5022 * "repl" is the replacement string.
5023 * Returns a pointer to the character after the replaced string.
5024 * Returns NULL for failure.
5025 */
5026 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005027repl_cmdline(
5028 exarg_T *eap,
5029 char_u *src,
5030 int srclen,
5031 char_u *repl,
5032 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033{
5034 int len;
5035 int i;
5036 char_u *new_cmdline;
5037
5038 /*
5039 * The new command line is build in new_cmdline[].
5040 * First allocate it.
5041 * Careful: a "+cmd" argument may have been NUL terminated.
5042 */
5043 len = (int)STRLEN(repl);
5044 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005045 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005046 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02005047 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005048 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049
5050 /*
5051 * Copy the stuff before the expanded part.
5052 * Copy the expanded stuff.
5053 * Copy what came after the expanded part.
5054 * Copy the next commands, if there are any.
5055 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005056 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005058
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005060 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005062 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005064 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 {
5066 i = (int)STRLEN(new_cmdline) + 1;
5067 STRCPY(new_cmdline + i, eap->nextcmd);
5068 eap->nextcmd = new_cmdline + i;
5069 }
5070 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5071 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5072 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5073 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5074 vim_free(*cmdlinep);
5075 *cmdlinep = new_cmdline;
5076
5077 return src;
5078}
5079
5080/*
5081 * Check for '|' to separate commands and '"' to start comments.
5082 */
5083 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005084separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085{
5086 char_u *p;
5087
Bram Moolenaar86b68352004-12-27 21:59:20 +00005088#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005089 p = skip_grep_pat(eap);
5090#else
5091 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005092#endif
5093
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005094 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 {
5096 if (*p == Ctrl_V)
5097 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005098 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005099 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005101 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00005102 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005103 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 break;
5105 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005106
5107#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005108 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005109 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005110 {
5111 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02005112 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01005113 if (*p == NUL) // stop at NUL after CTRL-V
5114 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005115 }
5116#endif
5117
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005118 // Check for '"': start of comment or '|': next command
5119 // :@" and :*" do not start a comment!
5120 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005121 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02005122#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005123 && !in_vim9script()
5124#endif
5125 && !(eap->argt & EX_NOTRLCOM)
5126 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5127 || p != eap->arg)
5128 && (eap->cmdidx != CMD_redir
5129 || p != eap->arg + 1 || p[-1] != '@'))
5130#ifdef FEAT_EVAL
5131 || (*p == '#'
5132 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02005133 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005134 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02005135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 || *p == '|' || *p == '\n')
5137 {
5138 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005139 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 * AND 'b' is present in 'cpoptions'.
5141 */
5142 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005143 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005145 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 --p;
5147 }
5148 else
5149 {
5150 eap->nextcmd = check_nextcmd(p);
5151 *p = NUL;
5152 break;
5153 }
5154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005156
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005157 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 del_trailing_spaces(eap->arg);
5159}
5160
5161/*
5162 * get + command from ex argument
5163 */
5164 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005165getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166{
5167 char_u *arg = *argp;
5168 char_u *command = NULL;
5169
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005170 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 {
5172 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005173 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 command = dollar_command;
5175 else
5176 {
5177 command = arg;
5178 arg = skip_cmd_arg(command, TRUE);
5179 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005180 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 }
5182
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005183 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 *argp = arg;
5185 }
5186 return command;
5187}
5188
5189/*
5190 * Find end of "+command" argument. Skip over "\ " and "\\".
5191 */
Bram Moolenaard0190392019-08-23 21:17:35 +02005192 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005193skip_cmd_arg(
5194 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005195 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196{
5197 while (*p && !vim_isspace(*p))
5198 {
5199 if (*p == '\\' && p[1] != NUL)
5200 {
5201 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005202 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 else
5204 ++p;
5205 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005206 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 }
5208 return p;
5209}
5210
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005211 int
5212get_bad_opt(char_u *p, exarg_T *eap)
5213{
5214 if (STRICMP(p, "keep") == 0)
5215 eap->bad_char = BAD_KEEP;
5216 else if (STRICMP(p, "drop") == 0)
5217 eap->bad_char = BAD_DROP;
5218 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5219 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005220 else
5221 return FAIL;
5222 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005223}
5224
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225/*
5226 * Get "++opt=arg" argument.
5227 * Return FAIL or OK.
5228 */
5229 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005230getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231{
5232 char_u *arg = eap->arg + 2;
5233 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005234 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005237 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5239 {
5240 if (*arg == 'n')
5241 {
5242 arg += 2;
5243 eap->force_bin = FORCE_NOBIN;
5244 }
5245 else
5246 eap->force_bin = FORCE_BIN;
5247 if (!checkforcmd(&arg, "binary", 3))
5248 return FAIL;
5249 eap->arg = skipwhite(arg);
5250 return OK;
5251 }
5252
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005253 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005254 if (STRNCMP(arg, "edit", 4) == 0)
5255 {
5256 eap->read_edit = TRUE;
5257 eap->arg = skipwhite(arg + 4);
5258 return OK;
5259 }
5260
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 if (STRNCMP(arg, "ff", 2) == 0)
5262 {
5263 arg += 2;
5264 pp = &eap->force_ff;
5265 }
5266 else if (STRNCMP(arg, "fileformat", 10) == 0)
5267 {
5268 arg += 10;
5269 pp = &eap->force_ff;
5270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 else if (STRNCMP(arg, "enc", 3) == 0)
5272 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005273 if (STRNCMP(arg, "encoding", 8) == 0)
5274 arg += 8;
5275 else
5276 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 pp = &eap->force_enc;
5278 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005279 else if (STRNCMP(arg, "bad", 3) == 0)
5280 {
5281 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005282 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284
5285 if (pp == NULL || *arg != '=')
5286 return FAIL;
5287
5288 ++arg;
5289 *pp = (int)(arg - eap->cmd);
5290 arg = skip_cmd_arg(arg, FALSE);
5291 eap->arg = skipwhite(arg);
5292 *arg = NUL;
5293
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 if (pp == &eap->force_ff)
5295 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5297 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005298 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005300 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005302 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5304 *p = TOLOWER_ASC(*p);
5305 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005306 else
5307 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005308 // Check ++bad= argument. Must be a single-byte character, "keep" or
5309 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005310 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005311 return FAIL;
5312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313
5314 return OK;
5315}
5316
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005318ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319{
5320 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005321 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 * directory for security reasons.
5323 */
5324 if (secure)
5325 {
5326 secure = 2;
Bram Moolenaar108010a2021-06-27 22:03:33 +02005327 eap->errmsg =
5328 _(e_command_not_allowed_from_vimrc_in_current_dir_or_tag_search);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 }
5330 else if (eap->cmdidx == CMD_autocmd)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02005331 do_autocmd(eap, eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 else
5333 do_augroup(eap->arg, eap->forceit);
5334}
5335
5336/*
5337 * ":doautocmd": Apply the automatic commands to the current buffer.
5338 */
5339 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005340ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005342 char_u *arg = eap->arg;
5343 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005344 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005345
Bram Moolenaar1610d052016-06-09 22:53:01 +02005346 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005347 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005348 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005349 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352/*
5353 * :[N]bunload[!] [N] [bufname] unload buffer
5354 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5355 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5356 */
5357 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005358ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005360 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005361 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 eap->errmsg = do_bufdel(
5363 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5364 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5365 : DOBUF_UNLOAD, eap->arg,
5366 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5367}
5368
5369/*
5370 * :[N]buffer [N] to buffer N
5371 * :[N]sbuffer [N] to buffer N
5372 */
5373 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005374ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005376 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005377 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 if (*eap->arg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005379 eap->errmsg = ex_errmsg(e_trailing_characters_str, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 else
5381 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005382 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5384 else
5385 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005386 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005387 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 }
5389}
5390
5391/*
5392 * :[N]bmodified [N] to next mod. buffer
5393 * :[N]sbmodified [N] to next mod. buffer
5394 */
5395 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005396ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397{
5398 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005399 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005400 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401}
5402
5403/*
5404 * :[N]bnext [N] to next buffer
5405 * :[N]sbnext [N] split and to next buffer
5406 */
5407 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005408ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005410 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005411 return;
5412
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005414 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005415 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416}
5417
5418/*
5419 * :[N]bNext [N] to previous buffer
5420 * :[N]bprevious [N] to previous buffer
5421 * :[N]sbNext [N] split and to previous buffer
5422 * :[N]sbprevious [N] split and to previous buffer
5423 */
5424 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005425ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005427 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005428 return;
5429
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005431 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005432 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433}
5434
5435/*
5436 * :brewind to first buffer
5437 * :bfirst to first buffer
5438 * :sbrewind split and to first buffer
5439 * :sbfirst split and to first buffer
5440 */
5441 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005442ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005444 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005445 return;
5446
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005448 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005449 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450}
5451
5452/*
5453 * :blast to last buffer
5454 * :sblast split and to last buffer
5455 */
5456 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005457ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005459 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005460 return;
5461
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005463 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005464 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466
Bram Moolenaar7a092242020-04-16 22:10:49 +02005467/*
5468 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005469 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005472ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005474 int comment_char = '"';
5475
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005476#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005477 if (in_vim9script())
5478 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005479#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005480 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005481}
5482
5483/*
5484 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5485 * to "cmd_start" or has a white space character before it.
5486 */
5487 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005488ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005489{
5490 int c = *cmd;
5491
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005492 if (c == NUL || c == '|' || c == '\n')
5493 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005494#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005495 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005496 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5497 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005498 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005499#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005500 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501}
5502
5503#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5504 || defined(PROTO)
5505/*
5506 * Return the next command, after the first '|' or '\n'.
5507 * Return NULL if not found.
5508 */
5509 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005510find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511{
5512 while (*p != '|' && *p != '\n')
5513 {
5514 if (*p == NUL)
5515 return NULL;
5516 ++p;
5517 }
5518 return (p + 1);
5519}
5520#endif
5521
5522/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005523 * Check if *p is a separator between Ex commands, skipping over white space.
5524 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 */
5526 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005527check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005529 char_u *s = skipwhite(p);
5530
5531 if (*s == '|' || *s == '\n')
5532 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 else
5534 return NULL;
5535}
5536
5537/*
Bram Moolenaar63b91732021-08-05 20:40:03 +02005538 * If "eap->nextcmd" is not set, check for a next command at "p".
5539 */
5540 void
5541set_nextcmd(exarg_T *eap, char_u *arg)
5542{
5543 char_u *p = check_nextcmd(arg);
5544
5545 if (eap->nextcmd == NULL)
5546 eap->nextcmd = p;
5547 else if (p != NULL)
5548 // cannot use "| command" inside a {} block
5549 semsg(_(e_cannot_use_bar_to_separate_commands_here_str), arg);
5550}
5551
5552/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 * - if there are more files to edit
5554 * - and this is the last window
5555 * - and forceit not used
5556 * - and not repeated twice on a row
5557 * return FAIL and give error message if 'message' TRUE
5558 * return OK otherwise
5559 */
5560 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005561check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005562 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005563 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564{
5565 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5566
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005567 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005568 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 {
5570 if (message)
5571 {
5572#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005573 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5574 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005576 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005578 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5579 NGETTEXT("%d more file to edit. Quit anyway?",
5580 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5582 return OK;
5583 return FAIL;
5584 }
5585#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00005586 semsg(NGETTEXT(e_nr_more_file_to_edit,
5587 e_nr_more_files_to_edit , n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005588 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 }
5590 return FAIL;
5591 }
5592 return OK;
5593}
5594
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595/*
5596 * Function given to ExpandGeneric() to obtain the list of command names.
5597 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005599get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600{
5601 if (idx >= (int)CMD_SIZE)
Bram Moolenaar80c88ea2021-09-08 14:29:46 +02005602 return expand_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 return cmdnames[idx].cmd_name;
5604}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005607ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608{
Bram Moolenaare6850792010-05-14 15:28:44 +02005609 if (*eap->arg == NUL)
5610 {
5611#ifdef FEAT_EVAL
5612 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5613 char_u *p = NULL;
5614
5615 if (expr != NULL)
5616 {
5617 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005618 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005619 --emsg_off;
5620 vim_free(expr);
5621 }
5622 if (p != NULL)
5623 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005624 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005625 vim_free(p);
5626 }
5627 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005628 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005629#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005630 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005631#endif
5632 }
5633 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005634 semsg(_(e_cannot_find_color_scheme_str), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005635
5636#ifdef FEAT_VTP
5637 else if (has_vtp_working())
5638 {
5639 // background color change requires clear + redraw
5640 update_screen(CLEAR);
5641 redrawcmd();
5642 }
5643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644}
5645
5646 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005647ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648{
5649 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005650 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 do_highlight(eap->arg, eap->forceit, FALSE);
5652}
5653
5654
5655/*
5656 * Call this function if we thought we were going to exit, but we won't
5657 * (because of an error). May need to restore the terminal mode.
5658 */
5659 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005660not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661{
5662 exiting = FALSE;
5663 settmode(TMODE_RAW);
5664}
5665
Bram Moolenaar3552e742021-05-29 12:21:58 +02005666 int
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005667before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5668{
5669 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5670
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005671 // Bail out when autocommands closed the window.
5672 // Refuse to quit when the buffer in the last window is being closed (can
5673 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005674 if (!win_valid(wp)
5675 || curbuf_locked()
5676 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5677 return TRUE;
5678
5679 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5680 {
5681 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005682 // Refuse to quit when locked or when the window was closed or the
5683 // buffer in the last window is being closed (can only happen in
5684 // autocommands).
5685 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005686 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5687 return TRUE;
5688 }
5689
5690 return FALSE;
5691}
5692
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005694 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005695 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005696 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005698 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005699ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005701 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005702
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703#ifdef FEAT_CMDWIN
5704 if (cmdwin_type != 0)
5705 {
5706 cmdwin_result = Ctrl_C;
5707 return;
5708 }
5709#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005710 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005711 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005712 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005713 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005714 return;
5715 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005716 if (eap->addr_count > 0)
5717 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005718 int wnr = eap->line2;
5719
5720 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5721 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005722 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005723 }
5724 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005725 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005726
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005727 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005728 if (curbuf_locked())
5729 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005730
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005731 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005732 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005733 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734
5735#ifdef FEAT_NETBEANS_INTG
5736 netbeansForcedQuit = eap->forceit;
5737#endif
5738
5739 /*
Bram Moolenaar3552e742021-05-29 12:21:58 +02005740 * If there is only one relevant window we will exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 */
5742 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5743 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005744 if ((!buf_hide(wp->w_buffer)
5745 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005746 | (eap->forceit ? CCGD_FORCEIT : 0)
5747 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005749 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 {
5751 not_exiting();
5752 }
5753 else
5754 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005755 // quit last window
5756 // Note: only_one_window() returns true, even so a help window is
5757 // still open. In that case only quit, if no address has been
5758 // specified. Example:
5759 // :h|wincmd w|1q - don't quit
5760 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005761 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005763 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005764#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005766#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005767 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005768 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 }
5770}
5771
5772/*
5773 * ":cquit".
5774 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005776ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005778 // this does not always pass on the exit code to the Manx compiler. why?
5779 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780}
5781
5782/*
5783 * ":qall": try to quit all windows
5784 */
5785 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005786ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787{
5788# ifdef FEAT_CMDWIN
5789 if (cmdwin_type != 0)
5790 {
5791 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005792 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 else
5794 cmdwin_result = K_XF2;
5795 return;
5796 }
5797# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005798
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005799 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005800 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005801 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005802 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005803 return;
5804 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005805
5806 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005807 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005808
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005810 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 getout(0);
5812 not_exiting();
5813}
5814
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815/*
5816 * ":close": close current window, unless it is the last one
5817 */
5818 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005819ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005821 win_T *win;
5822 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005823#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005825 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005827#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005828 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005829 {
5830 if (eap->addr_count == 0)
5831 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005832 else
5833 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005834 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005835 {
5836 winnr++;
5837 if (winnr == eap->line2)
5838 break;
5839 }
5840 if (win == NULL)
5841 win = lastwin;
5842 ex_win_close(eap->forceit, win, NULL);
5843 }
5844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845}
5846
Bram Moolenaar4033c552017-09-16 20:54:51 +02005847#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005848/*
5849 * ":pclose": Close any preview window.
5850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005852ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005853{
5854 win_T *win;
5855
Bram Moolenaar79648732019-07-18 21:43:07 +02005856 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005857 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005858 if (win->w_p_pvw)
5859 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005860 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005861 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005862 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005863# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005864 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005865 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005866# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005867}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005868#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005869
Bram Moolenaarf740b292006-02-16 22:11:02 +00005870/*
5871 * Close window "win" and take care of handling closing the last window for a
5872 * modified buffer.
5873 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005874 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005875ex_win_close(
5876 int forceit,
5877 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005878 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879{
5880 int need_hide;
5881 buf_T *buf = win->w_buffer;
5882
Bram Moolenaarcf844172020-06-26 19:44:06 +02005883 // Never close the autocommand window.
5884 if (win == aucmd_win)
5885 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00005886 emsg(_(e_cannot_close_autocmd_or_popup_window));
Bram Moolenaarcf844172020-06-26 19:44:06 +02005887 return;
5888 }
5889
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005891 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005893#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005894 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005896 bufref_T bufref;
5897
5898 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005900 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 return;
5902 need_hide = FALSE;
5903 }
5904 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005907 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 return;
5909 }
5910 }
5911
Bram Moolenaar4033c552017-09-16 20:54:51 +02005912#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005914#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005915
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005916 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005917 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005918 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005919 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005920 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921}
5922
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005924 * Handle the argument for a tabpage related ex command.
5925 * Returns a tabpage number.
5926 * When an error is encountered then eap->errmsg is set.
5927 */
5928 static int
5929get_tabpage_arg(exarg_T *eap)
5930{
5931 int tab_number;
5932 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5933
5934 if (eap->arg && *eap->arg != NUL)
5935 {
5936 char_u *p = eap->arg;
5937 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005938 int relative = 0; // argument +N/-N means: go to N places to the
5939 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005940
5941 if (*p == '-')
5942 {
5943 relative = -1;
5944 p++;
5945 }
5946 else if (*p == '+')
5947 {
5948 relative = 1;
5949 p++;
5950 }
5951
5952 p_save = p;
5953 tab_number = getdigits(&p);
5954
5955 if (relative == 0)
5956 {
5957 if (STRCMP(p, "$") == 0)
5958 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005959 else if (STRCMP(p, "#") == 0)
5960 if (valid_tabpage(lastused_tabpage))
5961 tab_number = tabpage_index(lastused_tabpage);
5962 else
5963 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005964 eap->errmsg = ex_errmsg(e_invalid_value_for_argument_str, eap->arg);
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005965 tab_number = 0;
5966 goto theend;
5967 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005968 else if (p == p_save || *p_save == '-' || *p != NUL
5969 || tab_number > LAST_TAB_NR)
5970 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005971 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005972 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005973 goto theend;
5974 }
5975 }
5976 else
5977 {
5978 if (*p_save == NUL)
5979 tab_number = 1;
5980 else if (p == p_save || *p_save == '-' || *p != NUL
5981 || tab_number == 0)
5982 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005983 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005984 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005985 goto theend;
5986 }
5987 tab_number = tab_number * relative + tabpage_index(curtab);
5988 if (!unaccept_arg0 && relative == -1)
5989 --tab_number;
5990 }
5991 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005992 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005993 }
5994 else if (eap->addr_count > 0)
5995 {
5996 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005997 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02005998 eap->errmsg = _(e_invalid_range);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005999 tab_number = 0;
6000 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006001 else
6002 {
6003 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006004 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006005 {
6006 --tab_number;
6007 if (tab_number < unaccept_arg0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006008 eap->errmsg = _(e_invalid_range);
Bram Moolenaardea25702017-01-29 15:18:10 +01006009 }
6010 }
6011 }
6012 else
6013 {
6014 switch (eap->cmdidx)
6015 {
6016 case CMD_tabnext:
6017 tab_number = tabpage_index(curtab) + 1;
6018 if (tab_number > LAST_TAB_NR)
6019 tab_number = 1;
6020 break;
6021 case CMD_tabmove:
6022 tab_number = LAST_TAB_NR;
6023 break;
6024 default:
6025 tab_number = tabpage_index(curtab);
6026 }
6027 }
6028
6029theend:
6030 return tab_number;
6031}
6032
6033/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006034 * ":tabclose": close current tab page, unless it is the last one.
6035 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 */
6037 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006038ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006040 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006041 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006042
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006043# ifdef FEAT_CMDWIN
6044 if (cmdwin_type != 0)
6045 cmdwin_result = K_IGNORE;
6046 else
6047# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006048 if (first_tabpage->tp_next == NULL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00006049 emsg(_(e_cannot_close_last_tab_page));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006050 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006052 tab_number = get_tabpage_arg(eap);
6053 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006054 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006055 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006056 if (tp == NULL)
6057 {
6058 beep_flush();
6059 return;
6060 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006061 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006062 {
6063 tabpage_close_other(tp, eap->forceit);
6064 return;
6065 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006066 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006067 tabpage_close(eap->forceit);
6068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006070}
6071
6072/*
6073 * ":tabonly": close all tab pages except the current one
6074 */
6075 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006076ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006077{
6078 tabpage_T *tp;
6079 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006080 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006081
6082# ifdef FEAT_CMDWIN
6083 if (cmdwin_type != 0)
6084 cmdwin_result = K_IGNORE;
6085 else
6086# endif
6087 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006088 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006089 else
6090 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006091 tab_number = get_tabpage_arg(eap);
6092 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006093 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006094 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006095 // Repeat this up to a 1000 times, because autocommands may
6096 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006097 for (done = 0; done < 1000; ++done)
6098 {
6099 FOR_ALL_TABPAGES(tp)
6100 if (tp->tp_topframe != topframe)
6101 {
6102 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006103 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006104 if (valid_tabpage(tp))
6105 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006106 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006107 break;
6108 }
6109 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006110 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006111 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006112 }
6113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115
6116/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006117 * Close the current tab page.
6118 */
6119 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006120tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006121{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006122 // First close all the windows but the current one. If that worked then
6123 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01006124 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006125 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006126 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006127 ex_win_close(forceit, curwin, NULL);
6128# ifdef FEAT_GUI
6129 need_mouse_correct = TRUE;
6130# endif
6131}
6132
6133/*
6134 * Close tab page "tp", which is not the current tab page.
6135 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006136 * Also takes care of the tab pages line disappearing when closing the
6137 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006138 */
6139 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006140tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006141{
6142 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006143 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006144 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006145
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006146 // Limit to 1000 windows, autocommands may add a window while we close
6147 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00006148 while (++done < 1000)
6149 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006150 wp = tp->tp_firstwin;
6151 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006152
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006153 // Autocommands may delete the tab page under our fingers and we may
6154 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006155 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006156 break;
6157 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006158
Bram Moolenaar29323592016-07-24 22:04:11 +02006159 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006160
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006161 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006162 if (h != tabline_height())
6163 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006164}
6165
6166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 * ":only".
6168 */
6169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006170ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006172 win_T *wp;
6173 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174# ifdef FEAT_GUI
6175 need_mouse_correct = TRUE;
6176# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006177 if (eap->addr_count > 0)
6178 {
6179 wnr = eap->line2;
6180 for (wp = firstwin; --wnr > 0; )
6181 {
6182 if (wp->w_next == NULL)
6183 break;
6184 else
6185 wp = wp->w_next;
6186 }
6187 win_goto(wp);
6188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 close_others(TRUE, eap->forceit);
6190}
6191
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006193ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006195 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01006196 if (!eap->skip)
6197 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006198#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006199 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006200#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006201 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006202 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01006203 else
6204 {
6205 int winnr = 0;
6206 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006207
Bram Moolenaar2256c992016-11-15 21:17:07 +01006208 FOR_ALL_WINDOWS(win)
6209 {
6210 winnr++;
6211 if (winnr == eap->line2)
6212 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006213 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006214 if (win == NULL)
6215 win = lastwin;
6216 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 }
6219}
6220
6221/*
6222 * ":stop" and ":suspend": Suspend Vim.
6223 */
dbivolaruab16ad32021-12-29 19:41:47 +00006224 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006225ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226{
6227 /*
6228 * Disallow suspending for "rvim".
6229 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006230 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 {
6232 if (!eap->forceit)
6233 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01006234 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 windgoto((int)Rows - 1, 0);
6236 out_char('\n');
6237 out_flush();
6238 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006239 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006240 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006241 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006243 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006245 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006247 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006248 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 }
6250}
6251
6252/*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006253 * ":exit", ":xit" and ":wq": Write file and quit the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 */
6255 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006256ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006258#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006259 if (not_in_vim9(eap) == FAIL)
6260 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262#ifdef FEAT_CMDWIN
6263 if (cmdwin_type != 0)
6264 {
6265 cmdwin_result = Ctrl_C;
6266 return;
6267 }
6268#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006269 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006270 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006271 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006272 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006273 return;
6274 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006275
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 /*
Bram Moolenaar1174b012021-05-29 14:30:43 +02006277 * we plan to exit if there is only one relevant window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 */
6279 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6280 exiting = TRUE;
Bram Moolenaar1174b012021-05-29 14:30:43 +02006281
6282 // Write the buffer for ":wq" or when it was changed.
6283 // Trigger QuitPre and ExitPre.
6284 // Check if we can exit now, after autocommands have changed things.
6285 if (((eap->cmdidx == CMD_wq || curbufIsChanged()) && do_write(eap) == FAIL)
6286 || before_quit_autocmds(curwin, FALSE, eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006288 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 {
6290 not_exiting();
6291 }
6292 else
6293 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006294 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006296 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297# ifdef FEAT_GUI
6298 need_mouse_correct = TRUE;
6299# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006300 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006301 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 }
6303}
6304
6305/*
6306 * ":print", ":list", ":number".
6307 */
6308 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006309ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006311 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar74409f62022-01-01 15:58:22 +00006312 emsg(_(e_empty_buffer));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006313 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006315 for ( ;!got_int; ui_breakcheck())
6316 {
6317 print_line(eap->line1,
6318 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6319 || (eap->flags & EXFLAG_NR)),
6320 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6321 if (++eap->line1 > eap->line2)
6322 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006323 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006324 }
6325 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006326 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006327 curwin->w_cursor.lnum = eap->line2;
6328 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 }
6330
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332}
6333
6334#ifdef FEAT_BYTEOFF
6335 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006336ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337{
6338 goto_byte(eap->line2);
6339}
6340#endif
6341
6342/*
6343 * ":shell".
6344 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006346ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347{
6348 do_shell(NULL, 0);
6349}
6350
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006351#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006353static int drop_busy = FALSE;
6354static int drop_filec;
6355static char_u **drop_filev = NULL;
6356static int drop_split;
6357static void (*drop_callback)(void *);
6358static void *drop_cookie;
6359
6360 static void
6361handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362{
6363 exarg_T ea;
6364 int save_msg_scroll = msg_scroll;
6365
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006366 // Setting the argument list may cause screen updates and being called
6367 // recursively. Avoid that by setting drop_busy.
6368 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006370 // Check whether the current buffer is changed. If so, we will need
6371 // to split the current window or data could be lost.
6372 // We don't need to check if the 'hidden' option is set, as in this
6373 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006374 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 {
6376 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006377 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 --emsg_off;
6379 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006380 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 if (win_split(0, 0) == FAIL)
6383 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006384 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006386 // When splitting the window, create a new alist. Otherwise the
6387 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 alist_unlink(curwin->w_alist);
6389 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 }
6391
6392 /*
6393 * Set up the new argument list.
6394 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006395 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396
6397 /*
6398 * Move to the first file.
6399 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006400 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006401 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 ea.cmd = (char_u *)"next";
6403 do_argfile(&ea, 0);
6404
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006405 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6406 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 need_start_insertmode = FALSE;
6408
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006409 // Restore msg_scroll, otherwise a following command may cause scrolling
6410 // unexpectedly. The screen will be redrawn by the caller, thus
6411 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006413
6414 if (drop_callback != NULL)
6415 drop_callback(drop_cookie);
6416
6417 drop_filev = NULL;
6418 drop_busy = FALSE;
6419}
6420
6421/*
6422 * Handle a file drop. The code is here because a drop is *nearly* like an
6423 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006424 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006425 * code is very similar to :args and hence needs access to a lot of the static
6426 * functions in this file.
6427 *
6428 * The "filev" list must have been allocated using alloc(), as should each item
6429 * in the list. This function takes over responsibility for freeing the "filev"
6430 * list.
6431 */
6432 void
6433handle_drop(
6434 int filec, // the number of files dropped
6435 char_u **filev, // the list of files dropped
6436 int split, // force splitting the window
6437 void (*callback)(void *), // to be called after setting the argument
6438 // list
6439 void *cookie) // argument for "callback" (allocated)
6440{
6441 // Cannot handle recursive drops, finish the pending one.
6442 if (drop_busy)
6443 {
6444 FreeWild(filec, filev);
6445 vim_free(cookie);
6446 return;
6447 }
6448
6449 // When calling handle_drop() more than once in a row we only use the last
6450 // one.
6451 if (drop_filev != NULL)
6452 {
6453 FreeWild(drop_filec, drop_filev);
6454 vim_free(drop_cookie);
6455 }
6456
6457 drop_filec = filec;
6458 drop_filev = filev;
6459 drop_split = split;
6460 drop_callback = callback;
6461 drop_cookie = cookie;
6462
6463 // Postpone this when:
6464 // - editing the command line
6465 // - not possible to change the current buffer
6466 // - updating the screen
6467 // As it may change buffers and window structures that are in use and cause
6468 // freed memory to be used.
6469 if (text_locked() || curbuf_locked() || updating_screen)
6470 return;
6471
6472 handle_drop_internal();
6473}
6474
6475/*
6476 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6477 * reset: Handle a postponed drop.
6478 */
6479 void
6480handle_any_postponed_drop(void)
6481{
6482 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006483 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006484 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485}
6486#endif
6487
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 * ":preserve".
6490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006492ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006494 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 ml_preserve(curbuf, TRUE);
6496}
6497
6498/*
6499 * ":recover".
6500 */
6501 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006502ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006504 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006506 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6507 | CCGD_MULTWIN
6508 | (eap->forceit ? CCGD_FORCEIT : 0)
6509 | CCGD_EXCMD)
6510
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 && (*eap->arg == NUL
6512 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006513 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 recoverymode = FALSE;
6515}
6516
6517/*
6518 * Command modifier used in a wrong way.
6519 */
6520 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006521ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006523 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524}
6525
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526/*
6527 * :sview [+command] file split window with new file, read-only
6528 * :split [[+command] file] split window with current or new file
6529 * :vsplit [[+command] file] split window vertically with current or new file
6530 * :new [[+command] file] split window with no or new file
6531 * :vnew [[+command] file] split vertically window with no or new file
6532 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006533 *
6534 * :tabedit open new Tab page with empty window
6535 * :tabedit [+command] file open new Tab page and edit "file"
6536 * :tabnew [[+command] file] just like :tabedit
6537 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 */
6539 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006540ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006542 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006543#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006545#endif
6546#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006547 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006548 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006549#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006550 int use_tab = eap->cmdidx == CMD_tabedit
6551 || eap->cmdidx == CMD_tabfind
6552 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006554 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006555 return;
6556
Bram Moolenaar4033c552017-09-16 20:54:51 +02006557#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560
Bram Moolenaar4033c552017-09-16 20:54:51 +02006561#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006562 // A ":split" in the quickfix window works like ":new". Don't want two
6563 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006564 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 {
6566 if (eap->cmdidx == CMD_split)
6567 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 if (eap->cmdidx == CMD_vsplit)
6569 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572
Bram Moolenaar4033c552017-09-16 20:54:51 +02006573#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006574 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 {
6576 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6577 FNAME_MESS, TRUE, curbuf->b_ffname);
6578 if (fname == NULL)
6579 goto theend;
6580 eap->arg = fname;
6581 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006582# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006583 else
6584# endif
6585#endif
6586#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006587 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 && eap->cmdidx != CMD_new)
6590 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006591 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006592# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006593 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006594# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006595 au_has_group((char_u *)"FileExplorer"))
6596 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006597 // No browsing supported but we do have the file explorer:
6598 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006599 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006600 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006601 }
6602 else
6603 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006604 fname = do_browse(0, (char_u *)(use_tab
6605 ? _("Edit File in new tab page")
6606 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006608 if (fname == NULL)
6609 goto theend;
6610 eap->arg = fname;
6611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006613 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006616 /*
6617 * Either open new tab page or split the window.
6618 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006619 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006620 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006621 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006622 : eap->addr_count == 0 ? 0
6623 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006624 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006625 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006626
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006627 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006628 if (curwin != old_curwin
6629 && win_valid(old_curwin)
6630 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006631 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006632 old_curwin->w_alt_fnum = curbuf->b_fnum;
6633 }
6634 }
6635 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6637 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006638 // Reset 'scrollbind' when editing another file, but keep it when
6639 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006640 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006641 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 else
6643 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 do_exedit(eap, old_curwin);
6645 }
6646
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006647# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006648 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006649# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006651# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652theend:
6653 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006654# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006656
6657/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006658 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006659 */
6660 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006661tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006662{
6663 exarg_T ea;
6664
Bram Moolenaara80faa82020-04-12 19:37:17 +02006665 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006666 ea.cmdidx = CMD_tabnew;
6667 ea.cmd = (char_u *)"tabn";
6668 ea.arg = (char_u *)"";
6669 ex_splitview(&ea);
6670}
6671
6672/*
6673 * :tabnext command
6674 */
6675 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006676ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006677{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006678 int tab_number;
6679
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006680 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006681 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006682 switch (eap->cmdidx)
6683 {
6684 case CMD_tabfirst:
6685 case CMD_tabrewind:
6686 goto_tabpage(1);
6687 break;
6688 case CMD_tablast:
6689 goto_tabpage(9999);
6690 break;
6691 case CMD_tabprevious:
6692 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006693 if (eap->arg && *eap->arg != NUL)
6694 {
6695 char_u *p = eap->arg;
6696 char_u *p_save = p;
6697
6698 tab_number = getdigits(&p);
6699 if (p == p_save || *p_save == '-' || *p != NUL
6700 || tab_number == 0)
6701 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006702 // No numbers as argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006703 eap->errmsg = ex_errmsg(e_invalid_argument_str, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006704 return;
6705 }
6706 }
6707 else
6708 {
6709 if (eap->addr_count == 0)
6710 tab_number = 1;
6711 else
6712 {
6713 tab_number = eap->line2;
6714 if (tab_number < 1)
6715 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006716 eap->errmsg = _(e_invalid_range);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006717 return;
6718 }
6719 }
6720 }
6721 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006722 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006723 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006724 tab_number = get_tabpage_arg(eap);
6725 if (eap->errmsg == NULL)
6726 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006727 break;
6728 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006729}
6730
6731/*
6732 * :tabmove command
6733 */
6734 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006735ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006736{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006737 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006738
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006739 tab_number = get_tabpage_arg(eap);
6740 if (eap->errmsg == NULL)
6741 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006742}
6743
6744/*
6745 * :tabs command: List tabs and their contents.
6746 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006748ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006749{
6750 tabpage_T *tp;
6751 win_T *wp;
6752 int tabcount = 1;
6753
6754 msg_start();
6755 msg_scroll = TRUE;
6756 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6757 {
6758 msg_putchar('\n');
6759 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006760 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006761 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006762 ui_breakcheck();
6763
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006764 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006765 wp = firstwin;
6766 else
6767 wp = tp->tp_firstwin;
6768 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6769 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006770 msg_putchar('\n');
6771 msg_putchar(wp == curwin ? '>' : ' ');
6772 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006773 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6774 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006775 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006776 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006777 else
6778 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6779 IObuff, IOSIZE, TRUE);
6780 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006781 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006782 ui_breakcheck();
6783 }
6784 }
6785}
6786
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787/*
6788 * ":mode": Set screen mode.
6789 * If no argument given, just get the screen size and redraw.
6790 */
6791 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006792ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793{
6794 if (*eap->arg == NUL)
6795 shell_resized();
6796 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00006797 emsg(_(e_screen_mode_setting_not_supported));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798}
6799
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800/*
6801 * ":resize".
6802 * set, increment or decrement current window height
6803 */
6804 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006805ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806{
6807 int n;
6808 win_T *wp = curwin;
6809
6810 if (eap->addr_count > 0)
6811 {
6812 n = eap->line2;
6813 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6814 ;
6815 }
6816
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006817# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006819# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006821 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 {
6823 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006824 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006825 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00006827 win_setwidth_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 }
6829 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 {
6831 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006832 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006833 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 n = 9999;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00006835 win_setheight_win(n, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 }
6837}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838
6839/*
6840 * ":find [+command] <file>" command.
6841 */
6842 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006843ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844{
6845#ifdef FEAT_SEARCHPATH
6846 char_u *fname;
6847 int count;
6848
6849 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6850 TRUE, curbuf->b_ffname);
6851 if (eap->addr_count > 0)
6852 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006853 // Repeat finding the file "count" times. This matters when it
6854 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 count = eap->line2;
6856 while (fname != NULL && --count > 0)
6857 {
6858 vim_free(fname);
6859 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6860 FALSE, curbuf->b_ffname);
6861 }
6862 }
6863
6864 if (fname != NULL)
6865 {
6866 eap->arg = fname;
6867#endif
6868 do_exedit(eap, NULL);
6869#ifdef FEAT_SEARCHPATH
6870 vim_free(fname);
6871 }
6872#endif
6873}
6874
6875/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006876 * ":open" simulation: for now just work like ":visual".
6877 */
6878 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006879ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006880{
6881 regmatch_T regmatch;
6882 char_u *p;
6883
Bram Moolenaar65088802021-03-13 21:07:21 +01006884#ifdef FEAT_EVAL
6885 if (not_in_vim9(eap) == FAIL)
6886 return;
6887#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006888 curwin->w_cursor.lnum = eap->line2;
6889 beginline(BL_SOL | BL_FIX);
6890 if (*eap->arg == '/')
6891 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006892 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006893 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006894 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00006895 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006896 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006897 if (regmatch.regprog != NULL)
6898 {
Bram Moolenaare031fe92021-12-05 12:06:24 +00006899 // make a copy of the line, when searching for a mark it might be
6900 // flushed
6901 char_u *line = vim_strsave(ml_get_curline());
6902
Bram Moolenaardf177f62005-02-22 08:39:57 +00006903 regmatch.rm_ic = p_ic;
Bram Moolenaare031fe92021-12-05 12:06:24 +00006904 if (vim_regexec(&regmatch, line, (colnr_T)0))
6905 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006906 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006907 emsg(_(e_no_match));
Bram Moolenaar473de612013-06-08 18:19:48 +02006908 vim_regfree(regmatch.regprog);
Bram Moolenaare031fe92021-12-05 12:06:24 +00006909 vim_free(line);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006910 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006911 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006912 eap->arg += STRLEN(eap->arg);
6913 }
6914 check_cursor();
6915
6916 eap->cmdidx = CMD_visual;
6917 do_exedit(eap, NULL);
6918}
6919
6920/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006921 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 */
6923 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006924ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925{
6926 do_exedit(eap, NULL);
6927}
6928
6929/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006930 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006933do_exedit(
6934 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006935 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936{
6937 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006939 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006941 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6942 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006943 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 /*
6945 * ":vi" command ends Ex mode.
6946 */
6947 if (exmode_active && (eap->cmdidx == CMD_visual
6948 || eap->cmdidx == CMD_view))
6949 {
6950 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006951 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006953 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006954 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006955 if (global_busy)
6956 {
6957 int rd = RedrawingDisabled;
6958 int nwr = no_wait_return;
6959 int ms = msg_scroll;
6960#ifdef FEAT_GUI
6961 int he = hold_gui_events;
6962#endif
6963
6964 if (eap->nextcmd != NULL)
6965 {
6966 stuffReadbuff(eap->nextcmd);
6967 eap->nextcmd = NULL;
6968 }
6969
6970 if (exmode_was != EXMODE_VIM)
6971 settmode(TMODE_RAW);
6972 RedrawingDisabled = 0;
6973 no_wait_return = 0;
6974 need_wait_return = FALSE;
6975 msg_scroll = 0;
6976#ifdef FEAT_GUI
6977 hold_gui_events = 0;
6978#endif
6979 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006980 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006981
6982 main_loop(FALSE, TRUE);
6983
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006984 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006985 RedrawingDisabled = rd;
6986 no_wait_return = nwr;
6987 msg_scroll = ms;
6988#ifdef FEAT_GUI
6989 hold_gui_events = he;
6990#endif
6991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 }
6995
6996 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006997 || eap->cmdidx == CMD_tabnew
6998 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006999 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007001 // ":new" or ":tabnew" without argument: edit a new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 setpcmark();
7003 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007004 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7005 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007007 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 || *eap->arg != NUL
7009#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007010 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011#endif
7012 )
7013 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007014 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
7015 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007016 if (*eap->arg != NUL && curbuf_locked())
7017 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007018
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 n = readonlymode;
7020 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7021 readonlymode = TRUE;
7022 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007023 readonlymode = FALSE; // 'readonly' doesn't make sense in an
7024 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01007025 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
7026 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7028 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007029 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7031 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7032 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007033 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007035 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01007036 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01007037 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
7038 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007039 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007041 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 if (old_curwin != NULL)
7043 {
7044 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007045 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007047#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007048 cleanup_T cs;
7049
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007050 // Reset the error/interrupt/exception state here so that
7051 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007052 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007053#endif
7054#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007056#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007057 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007058
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007059#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007060 // Restore the error/interrupt/exception state if not
7061 // discarded by a new aborting error, interrupt, or
7062 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007063 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 }
7066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 }
7068 else if (readonlymode && curbuf->b_nwindows == 1)
7069 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007070 // When editing an already visited buffer, 'readonly' won't be set
7071 // but the previous value is kept. With ":view" and ":sview" we
7072 // want the file to be readonly, except when another window is
7073 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 curbuf->b_p_ro = TRUE;
7075 }
7076 readonlymode = n;
7077 }
7078 else
7079 {
7080 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01007081 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 n = curwin->w_arg_idx_invalid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 check_arg_idx(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 if (n != curwin->w_arg_idx_invalid)
7085 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 }
7087
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 /*
7089 * if ":split file" worked, set alternate file name in old window to new
7090 * file
7091 */
7092 if (old_curwin != NULL
7093 && *eap->arg != NUL
7094 && curwin != old_curwin
7095 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007096 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02007097 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099
7100 ex_no_reprint = TRUE;
7101}
7102
7103#ifndef FEAT_GUI
7104/*
7105 * ":gui" and ":gvim" when there is no GUI.
7106 */
7107 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007108ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109{
Bram Moolenaare29a27f2021-07-20 21:07:36 +02007110 eap->errmsg = _(e_gui_cannot_be_used_not_enabled_at_compile_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111}
7112#endif
7113
Bram Moolenaar4f974752019-02-17 17:44:42 +01007114#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007116ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117{
7118 gui_make_tearoff(eap->arg);
7119}
7120#endif
7121
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007122#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7123 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007125ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007127# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7128 if (gui.in_use)
7129 gui_make_popup(eap->arg, eap->forceit);
7130# ifdef FEAT_TERM_POPUP_MENU
7131 else
7132# endif
7133# endif
7134# ifdef FEAT_TERM_POPUP_MENU
7135 pum_make_popup(eap->arg, eap->forceit);
7136# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137}
7138#endif
7139
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007141ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142{
7143 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007144 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007146 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147}
7148
7149/*
7150 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7151 * offset.
7152 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007155ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007158 win_T *save_curwin = curwin;
7159 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 long topline;
7161 long y;
7162 linenr_T old_linenr = curwin->w_cursor.lnum;
7163
7164 setpcmark();
7165
7166 /*
7167 * determine max topline
7168 */
7169 if (curwin->w_p_scb)
7170 {
7171 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007172 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 {
7174 if (wp->w_p_scb && wp->w_buffer)
7175 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007176 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 if (topline > y)
7178 topline = y;
7179 }
7180 }
7181 if (topline < 1)
7182 topline = 1;
7183 }
7184 else
7185 {
7186 topline = 1;
7187 }
7188
7189
7190 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007191 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007193 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 {
7195 if (curwin->w_p_scb)
7196 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007197 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 y = topline - curwin->w_topline;
7199 if (y > 0)
7200 scrollup(y, TRUE);
7201 else
7202 scrolldown(-y, TRUE);
7203 curwin->w_scbind_pos = topline;
7204 redraw_later(VALID);
7205 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 }
7208 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007209 curwin = save_curwin;
7210 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 if (curwin->w_p_scb)
7212 {
7213 did_syncbind = TRUE;
7214 checkpcmark();
7215 if (old_linenr != curwin->w_cursor.lnum)
7216 {
7217 char_u ctrl_o[2];
7218
7219 ctrl_o[0] = Ctrl_O;
7220 ctrl_o[1] = 0;
7221 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7222 }
7223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224}
7225
7226
7227 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007228ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007230 int i;
7231 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7232 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007234 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 do_bang(1, eap, FALSE, FALSE, TRUE);
7236 else
7237 {
7238 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7239 return;
7240
7241#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007242 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 {
7244 char_u *browseFile;
7245
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007246 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 NULL, NULL, NULL, curbuf);
7248 if (browseFile != NULL)
7249 {
7250 i = readfile(browseFile, NULL,
7251 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7252 vim_free(browseFile);
7253 }
7254 else
7255 i = OK;
7256 }
7257 else
7258#endif
7259 if (*eap->arg == NUL)
7260 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007261 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 return;
7263 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7264 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7265 }
7266 else
7267 {
7268 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7269 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7270 i = readfile(eap->arg, NULL,
7271 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7272
7273 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007274 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007276#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 if (!aborting())
7278#endif
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007279 semsg(_(e_cant_open_file_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 }
7281 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007282 {
7283 if (empty && exmode_active)
7284 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007285 // Delete the empty line that remains. Historically ex does
7286 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007287 if (eap->line2 == 0)
7288 lnum = curbuf->b_ml.ml_line_count;
7289 else
7290 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007291 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007292 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007293 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007294 if (curwin->w_cursor.lnum > 1
7295 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007296 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007297 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007298 }
7299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 }
7303}
7304
Bram Moolenaarea408852005-06-25 22:49:46 +00007305static char_u *prev_dir = NULL;
7306
7307#if defined(EXITFREE) || defined(PROTO)
7308 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007309free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007310{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007311 VIM_CLEAR(prev_dir);
7312 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007313}
7314#endif
7315
Bram Moolenaarf4258302013-06-02 18:20:17 +02007316/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007317 * Get the previous directory for the given chdir scope.
7318 */
7319 static char_u *
7320get_prevdir(cdscope_T scope)
7321{
7322 if (scope == CDSCOPE_WINDOW)
7323 return curwin->w_prevdir;
7324 else if (scope == CDSCOPE_TABPAGE)
7325 return curtab->tp_prevdir;
7326 return prev_dir;
7327}
7328
7329/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007330 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007331 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7332 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007333 */
7334 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007335post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007336{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007337 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007338 // Clear tab local directory for both :cd and :tcd
7339 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007340 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007341 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007342 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007343 char_u *pdir = get_prevdir(scope);
7344
7345 // If still in the global directory, need to remember current
7346 // directory as the global directory.
7347 if (globaldir == NULL && pdir != NULL)
7348 globaldir = vim_strsave(pdir);
7349
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007350 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007351 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007352 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007353 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007354 curtab->tp_localdir = vim_strsave(NameBuff);
7355 else
7356 curwin->w_localdir = vim_strsave(NameBuff);
7357 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007358 }
7359 else
7360 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007361 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007362 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007363 }
7364
zeertzjq64be6aa2021-11-19 11:59:08 +00007365 last_chdir_reason = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02007366 shorten_fnames(TRUE);
7367}
7368
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007369/*
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007370 * Trigger DirChangedPre for "acmd_fname" with directory "new_dir".
7371 */
7372 void
7373trigger_DirChangedPre(char_u *acmd_fname, char_u *new_dir)
7374{
7375#ifdef FEAT_EVAL
7376 dict_T *v_event;
7377 save_v_event_T save_v_event;
7378
7379 v_event = get_v_event(&save_v_event);
7380 (void)dict_add_string(v_event, "directory", new_dir);
7381 dict_set_items_ro(v_event);
7382#endif
7383 apply_autocmds(EVENT_DIRCHANGEDPRE, acmd_fname, new_dir, FALSE, curbuf);
7384#ifdef FEAT_EVAL
7385 restore_v_event(v_event, &save_v_event);
7386#endif
7387}
7388
7389/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007390 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007391 * chdir() function.
7392 * scope == CDSCOPE_WINDOW: changes the window-local directory
7393 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7394 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007395 * Returns TRUE if the directory is successfully changed.
7396 */
7397 int
7398changedir_func(
7399 char_u *new_dir,
7400 int forceit,
7401 cdscope_T scope)
7402{
Bram Moolenaar002bc792020-06-05 22:33:42 +02007403 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007404 int dir_differs;
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007405 char_u *acmd_fname = NULL;
zeertzjq73257142022-02-02 13:16:37 +00007406 char_u **pp;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007407
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007408 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007409 return FALSE;
7410
7411 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7412 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00007413 emsg(_(e_cannot_change_directory_buffer_is_modified_add_bang_to_override));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007414 return FALSE;
7415 }
7416
7417 // ":cd -": Change to previous directory
7418 if (STRCMP(new_dir, "-") == 0)
7419 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007420 pdir = get_prevdir(scope);
7421 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007422 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00007423 emsg(_(e_no_previous_directory));
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007424 return FALSE;
7425 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007426 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007427 }
7428
7429 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007430 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007431 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007432 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007433 pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007434
Bakudankun29f3a452021-12-11 12:28:08 +00007435 // For UNIX ":cd" means: go to home directory.
7436 // On other systems too if 'cdhome' is set.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007437#if defined(UNIX) || defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007438 if (*new_dir == NUL)
Bakudankun29f3a452021-12-11 12:28:08 +00007439#else
7440 if (*new_dir == NUL && p_cdh)
7441#endif
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007442 {
7443 // use NameBuff for home directory name
7444# ifdef VMS
7445 char_u *p;
7446
7447 p = mch_getenv((char_u *)"SYS$LOGIN");
7448 if (p == NULL || *p == NUL) // empty is the same as not set
7449 NameBuff[0] = NUL;
7450 else
7451 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7452# else
7453 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7454# endif
7455 new_dir = NameBuff;
7456 }
zeertzjqf38aad82021-12-30 13:45:57 +00007457 dir_differs = pdir == NULL
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007458 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
7459 if (dir_differs)
Richard Doty3d0abad2021-12-29 14:39:08 +00007460 {
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007461 if (scope == CDSCOPE_WINDOW)
7462 acmd_fname = (char_u *)"window";
7463 else if (scope == CDSCOPE_TABPAGE)
7464 acmd_fname = (char_u *)"tabpage";
7465 else
7466 acmd_fname = (char_u *)"global";
7467 trigger_DirChangedPre(acmd_fname, new_dir);
7468
7469 if (vim_chdir(new_dir))
7470 {
7471 emsg(_(e_command_failed));
7472 vim_free(pdir);
7473 return FALSE;
7474 }
Richard Doty3d0abad2021-12-29 14:39:08 +00007475 }
zeertzjq73257142022-02-02 13:16:37 +00007476
7477 if (scope == CDSCOPE_WINDOW)
7478 pp = &curwin->w_prevdir;
7479 else if (scope == CDSCOPE_TABPAGE)
7480 pp = &curtab->tp_prevdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007481 else
zeertzjq73257142022-02-02 13:16:37 +00007482 pp = &prev_dir;
7483 vim_free(*pp);
7484 *pp = pdir;
7485
7486 post_chdir(scope);
7487
7488 if (dir_differs)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00007489 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
zeertzjq73257142022-02-02 13:16:37 +00007490 return TRUE;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007491}
Bram Moolenaarea408852005-06-25 22:49:46 +00007492
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007494 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007496 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007497ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007499 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500
7501 new_dir = eap->arg;
7502#if !defined(UNIX) && !defined(VMS)
Bakudankun29f3a452021-12-11 12:28:08 +00007503 // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set
7504 if (*new_dir == NUL && !p_cdh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 ex_pwd(NULL);
7506 else
7507#endif
7508 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007509 cdscope_T scope = CDSCOPE_GLOBAL;
7510
7511 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7512 scope = CDSCOPE_WINDOW;
7513 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7514 scope = CDSCOPE_TABPAGE;
7515
7516 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007517 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007518 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007519 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 ex_pwd(eap);
7521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 }
7523}
7524
7525/*
7526 * ":pwd".
7527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007529ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530{
7531 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7532 {
7533#ifdef BACKSLASH_IN_FILENAME
7534 slash_adjust(NameBuff);
7535#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007536 if (p_verbose > 0)
7537 {
7538 char *context = "global";
7539
Bram Moolenaar05268152021-11-18 18:53:45 +00007540 if (last_chdir_reason != NULL)
7541 context = last_chdir_reason;
7542 else if (curwin->w_localdir != NULL)
Bram Moolenaar95058722020-06-01 16:26:19 +02007543 context = "window";
7544 else if (curtab->tp_localdir != NULL)
7545 context = "tabpage";
7546 smsg("[%s] %s", context, (char *)NameBuff);
7547 }
7548 else
7549 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 }
7551 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00007552 emsg(_(e_directory_unknown));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553}
7554
7555/*
7556 * ":=".
7557 */
7558 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007559ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007561 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007562 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563}
7564
7565 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007566ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007568 int n;
7569 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570
7571 if (cursor_valid())
7572 {
7573 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7574 if (n >= 0)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00007575 windgoto(n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007577
7578 len = eap->line2;
7579 switch (*eap->arg)
7580 {
7581 case 'm': break;
7582 case NUL: len *= 1000L; break;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007583 default: semsg(_(e_invalid_argument_str), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007584 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007585
7586 // Hide the cursor if invoked with !
7587 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588}
7589
7590/*
7591 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007592 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 */
7594 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007595do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596{
Bram Moolenaar52953192019-08-16 10:27:13 +02007597 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007598 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007599# ifdef ELAPSED_FUNC
7600 elapsed_T start_tv;
7601
7602 // Remember at what time we started, so that we know how much longer we
7603 // should wait after waiting for a bit.
7604 ELAPSED_INIT(start_tv);
7605# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007607 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007608 cursor_sleep();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007609 else
Richard Doty3d0abad2021-12-29 14:39:08 +00007610 cursor_on();
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007611
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007612 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007613 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007615 wait_now = msec - done > 1000L ? 1000L : msec - done;
7616#ifdef FEAT_TIMERS
7617 {
7618 long due_time = check_due_timer();
7619
7620 if (due_time > 0 && due_time < wait_now)
7621 wait_now = due_time;
7622 }
7623#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007624#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007625 if (has_any_channel() && wait_now > 20L)
7626 wait_now = 20L;
7627#endif
7628#ifdef FEAT_SOUND
7629 if (has_any_sound_callback() && wait_now > 20L)
7630 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007631#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007632 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007633
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007634#ifdef FEAT_JOB_CHANNEL
7635 if (has_any_channel())
7636 ui_breakcheck_force(TRUE);
7637 else
7638#endif
7639 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007640#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007641 // Process the netbeans and clientserver messages that may have been
7642 // received in the call to ui_breakcheck() when the GUI is in use. This
7643 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007644 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007645#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007646
7647# ifdef ELAPSED_FUNC
7648 // actual time passed
7649 done = ELAPSED_FUNC(start_tv);
7650# else
7651 // guestimate time passed (will actually be more)
7652 done += wait_now;
7653# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007655
7656 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7657 // input buffer, otherwise a following call to input() fails.
7658 if (got_int)
7659 (void)vpeekc();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02007660
7661 if (hide_cursor)
Richard Doty3d0abad2021-12-29 14:39:08 +00007662 cursor_unsleep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663}
7664
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665/*
7666 * ":winsize" command (obsolete).
7667 */
7668 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007669ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670{
7671 int w, h;
7672 char_u *arg = eap->arg;
7673 char_u *p;
7674
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007675 if (!isdigit(*arg))
7676 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007677 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007678 return;
7679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 w = getdigits(&arg);
7681 arg = skipwhite(arg);
7682 p = arg;
7683 h = getdigits(&arg);
7684 if (*p != NUL && *arg == NUL)
7685 set_shellsize(w, h, TRUE);
7686 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007687 emsg(_(e_winsize_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688}
7689
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007691ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692{
7693 int xchar = NUL;
7694 char_u *p;
7695
7696 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7697 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007698 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 if (eap->arg[1] == NUL)
7700 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007701 emsg(_(e_invalid_argument));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 return;
7703 }
7704 xchar = eap->arg[1];
7705 p = eap->arg + 2;
7706 }
7707 else
7708 p = eap->arg + 1;
7709
Bram Moolenaar63b91732021-08-05 20:40:03 +02007710 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007712 if (*p != NUL && *p != (
7713#ifdef FEAT_EVAL
7714 in_vim9script() ? '#' :
7715#endif
7716 '"')
7717 && eap->nextcmd == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007718 emsg(_(e_invalid_argument));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007719 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007721 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007722 postponed_split_flags = cmdmod.cmod_split;
7723 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7725 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007726 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 }
7728}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729
Bram Moolenaar843ee412004-06-30 16:16:41 +00007730#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731/*
7732 * ":winpos".
7733 */
7734 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007735ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736{
7737 int x, y;
7738 char_u *arg = eap->arg;
7739 char_u *p;
7740
7741 if (*arg == NUL)
7742 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007743# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007744# ifdef VIMDLL
7745 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7746 mch_get_winpos(&x, &y) != FAIL)
7747# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007749# else
7750 if (mch_get_winpos(&x, &y) != FAIL)
7751# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 {
7753 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007754 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 }
7756 else
7757# endif
Bram Moolenaar1a992222021-12-31 17:25:48 +00007758 emsg(_(e_obtaining_window_position_not_implemented_for_this_platform));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 }
7760 else
7761 {
7762 x = getdigits(&arg);
7763 arg = skipwhite(arg);
7764 p = arg;
7765 y = getdigits(&arg);
7766 if (*p == NUL || *arg != NUL)
7767 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00007768 emsg(_(e_winpos_requires_two_number_arguments));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 return;
7770 }
7771# ifdef FEAT_GUI
7772 if (gui.in_use)
7773 gui_mch_set_winpos(x, y);
7774 else if (gui.starting)
7775 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007776 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 gui_win_x = x;
7778 gui_win_y = y;
7779 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007780# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 else
7782# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007783# endif
7784# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007785 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786# endif
7787# ifdef HAVE_TGETENT
7788 if (*T_CWP)
7789 term_set_winpos(x, y);
7790# endif
7791 }
7792}
7793#endif
7794
7795/*
7796 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7797 */
7798 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007799ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800{
7801 oparg_T oa;
7802
7803 clear_oparg(&oa);
7804 oa.regname = eap->regname;
7805 oa.start.lnum = eap->line1;
7806 oa.end.lnum = eap->line2;
7807 oa.line_count = eap->line2 - eap->line1 + 1;
7808 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007810 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 {
7812 setpcmark();
7813 curwin->w_cursor.lnum = eap->line1;
7814 beginline(BL_SOL | BL_FIX);
7815 }
7816
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007817 if (VIsual_active)
7818 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007819
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 switch (eap->cmdidx)
7821 {
7822 case CMD_delete:
7823 oa.op_type = OP_DELETE;
7824 op_delete(&oa);
7825 break;
7826
7827 case CMD_yank:
7828 oa.op_type = OP_YANK;
7829 (void)op_yank(&oa, FALSE, TRUE);
7830 break;
7831
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007832 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007833 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007835 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7836#else
7837 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007839 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 oa.op_type = OP_RSHIFT;
7841 else
7842 oa.op_type = OP_LSHIFT;
7843 op_shift(&oa, FALSE, eap->amount);
7844 break;
7845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007847 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848}
7849
7850/*
7851 * ":put".
7852 */
7853 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007854ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007856 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 if (eap->line2 == 0)
7858 {
7859 eap->line2 = 1;
7860 eap->forceit = TRUE;
7861 }
7862 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +00007863 check_cursor_col();
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007864 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007865 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866}
7867
7868/*
7869 * Handle ":copy" and ":move".
7870 */
7871 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007872ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873{
7874 long n;
7875
Bram Moolenaar491799b2020-08-01 19:23:43 +02007876#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007877 if (not_in_vim9(eap) == FAIL)
7878 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007879#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007880 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007881 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 {
7883 eap->nextcmd = NULL;
7884 return;
7885 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007886 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887
7888 /*
7889 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7890 */
7891 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7892 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02007893 emsg(_(e_invalid_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 return;
7895 }
7896
7897 if (eap->cmdidx == CMD_move)
7898 {
7899 if (do_move(eap->line1, eap->line2, n) == FAIL)
7900 return;
7901 }
7902 else
7903 ex_copy(eap->line1, eap->line2, n);
7904 u_clearline();
7905 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007906 ex_may_print(eap);
7907}
7908
7909/*
7910 * Print the current line if flags were given to the Ex command.
7911 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007912 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007913ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007914{
7915 if (eap->flags != 0)
7916 {
7917 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7918 (eap->flags & EXFLAG_LIST));
7919 ex_no_reprint = TRUE;
7920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921}
7922
7923/*
7924 * ":smagic" and ":snomagic".
7925 */
7926 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007927ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007929 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007931 magic_overruled = eap->cmdidx == CMD_smagic
7932 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02007933 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007934 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935}
7936
7937/*
7938 * ":join".
7939 */
7940 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007941ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942{
7943 curwin->w_cursor.lnum = eap->line1;
7944 if (eap->line1 == eap->line2)
7945 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007946 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 return;
7948 if (eap->line2 == curbuf->b_ml.ml_line_count)
7949 {
7950 beep_flush();
7951 return;
7952 }
7953 ++eap->line2;
7954 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007955 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007957 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958}
7959
7960/*
7961 * ":[addr]@r" or ":[addr]*r": execute register
7962 */
7963 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007964ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965{
7966 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007967 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968
7969 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007970 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971
7972#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007973 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974#endif
7975
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007976 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 c = *eap->arg;
7978 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7979 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007980 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007981 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7982 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007983 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 else
7987 {
7988 int save_efr = exec_from_reg;
7989
7990 exec_from_reg = TRUE;
7991
7992 /*
7993 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007994 * Continue until the stuff buffer is empty and all added characters
7995 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007997 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7999
8000 exec_from_reg = save_efr;
8001 }
8002}
8003
8004/*
8005 * ":!".
8006 */
8007 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008008ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009{
8010 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8011}
8012
8013/*
8014 * ":undo".
8015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008017ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008019 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02008020 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008021 else
8022 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023}
8024
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008025#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008026 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008027ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008028{
8029 char_u hash[UNDO_HASH_SIZE];
8030
8031 u_compute_hash(hash);
8032 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8033}
8034
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008035 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008036ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008037{
8038 char_u hash[UNDO_HASH_SIZE];
8039
8040 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008041 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008042}
8043#endif
8044
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045/*
8046 * ":redo".
8047 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008049ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050{
8051 u_redo(1);
8052}
8053
8054/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008055 * ":earlier" and ":later".
8056 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008057 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008058ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008059{
8060 long count = 0;
8061 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008062 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008063 char_u *p = eap->arg;
8064
8065 if (*p == NUL)
8066 count = 1;
8067 else if (isdigit(*p))
8068 {
8069 count = getdigits(&p);
8070 switch (*p)
8071 {
8072 case 's': ++p; sec = TRUE; break;
8073 case 'm': ++p; sec = TRUE; count *= 60; break;
8074 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008075 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8076 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008077 }
8078 }
8079
8080 if (*p != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008081 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008082 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008083 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8084 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008085}
8086
8087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 * ":redir": start/stop redirection.
8089 */
8090 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008091ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092{
8093 char *mode;
8094 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008095 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096
Bram Moolenaarba768492016-07-08 15:32:54 +02008097#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008098 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008099 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00008100 emsg(_(e_cannot_use_redir_inside_execute));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008101 return;
8102 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008103#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008104
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 if (STRICMP(eap->arg, "END") == 0)
8106 close_redir();
8107 else
8108 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008109 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008111 ++arg;
8112 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008114 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 mode = "a";
8116 }
8117 else
8118 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008119 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120
8121 close_redir();
8122
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008123 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00008124 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 if (fname == NULL)
8126 return;
8127#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02008128 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {
8130 char_u *browseFile;
8131
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008132 browseFile = do_browse(BROWSE_SAVE,
8133 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008134 fname, NULL, NULL,
8135 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008137 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 vim_free(fname);
8139 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008140 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 }
8142#endif
8143
8144 redir_fd = open_exfile(fname, eap->forceit, mode);
8145 vim_free(fname);
8146 }
8147#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008148 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008150 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008152 ++arg;
8153 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008155 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008156 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008158 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008160 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008161 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00008162 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008163 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008165 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008166 if (*arg == '>')
8167 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008168 // Make register empty when not using @A-@Z and the
8169 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00008170 if (*arg == NUL && !isupper(redir_reg))
8171 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008173 }
8174 if (*arg != NUL)
8175 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008176 redir_reg = 0;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008177 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008178 }
8179 }
8180 else if (*arg == '=' && arg[1] == '>')
8181 {
8182 int append;
8183
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008184 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00008185 close_redir();
8186 arg += 2;
8187
8188 if (*arg == '>')
8189 {
8190 ++arg;
8191 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 }
8193 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008194 append = FALSE;
8195
8196 if (var_redir_start(skipwhite(arg), append) == OK)
8197 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 }
8199#endif
8200
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008201 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008204 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008206
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008207 // Make sure redirection is not off. Can happen for cmdline completion
8208 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008209 if (redir_fd != NULL
8210#ifdef FEAT_EVAL
8211 || redir_reg || redir_vname
8212#endif
8213 )
8214 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215}
8216
8217/*
8218 * ":redraw": force redraw
8219 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008220 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008221ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222{
8223 int r = RedrawingDisabled;
8224 int p = p_lz;
8225
8226 RedrawingDisabled = 0;
8227 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008228 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008230 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 if (need_maketitle)
8232 maketitle();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008233#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8234# ifdef VIMDLL
8235 if (!gui.in_use)
8236# endif
8237 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 RedrawingDisabled = r;
8240 p_lz = p;
8241
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008242 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 msg_didout = FALSE;
8244 msg_col = 0;
8245
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008246 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008247 need_wait_return = FALSE;
8248
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 out_flush();
8250}
8251
8252/*
8253 * ":redrawstatus": force redraw of status line(s)
8254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008256ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 int r = RedrawingDisabled;
8259 int p = p_lz;
8260
8261 RedrawingDisabled = 0;
8262 p_lz = FALSE;
8263 if (eap->forceit)
8264 status_redraw_all();
8265 else
8266 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008267 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 RedrawingDisabled = r;
8269 p_lz = p;
8270 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271}
8272
Bram Moolenaare12bab32019-01-08 22:02:56 +01008273/*
8274 * ":redrawtabline": force redraw of the tabline
8275 */
8276 static void
8277ex_redrawtabline(exarg_T *eap UNUSED)
8278{
8279 int r = RedrawingDisabled;
8280 int p = p_lz;
8281
8282 RedrawingDisabled = 0;
8283 p_lz = FALSE;
8284
8285 draw_tabline();
8286
8287 RedrawingDisabled = r;
8288 p_lz = p;
8289 out_flush();
8290}
8291
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008293close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294{
8295 if (redir_fd != NULL)
8296 {
8297 fclose(redir_fd);
8298 redir_fd = NULL;
8299 }
8300#ifdef FEAT_EVAL
8301 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008302 if (redir_vname)
8303 {
8304 var_redir_stop();
8305 redir_vname = 0;
8306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307#endif
8308}
8309
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008310#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008311 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008312vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008313{
8314 if (vim_mkdir(name, prot) != 0)
8315 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00008316 semsg(_(e_cannot_create_directory_str), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008317 return FAIL;
8318 }
8319 return OK;
8320}
8321#endif
8322
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323/*
8324 * Open a file for writing for an Ex command, with some checks.
8325 * Return file descriptor, or NULL on failure.
8326 */
8327 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008328open_exfile(
8329 char_u *fname,
8330 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008331 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332{
8333 FILE *fd;
8334
8335#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008336 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 if (mch_isdir(fname))
8338 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02008339 semsg(_(e_src_is_directory), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 return NULL;
8341 }
8342#endif
8343 if (!forceit && *mode != 'a' && vim_fexists(fname))
8344 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00008345 semsg(_(e_str_exists_add_bang_to_override), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 return NULL;
8347 }
8348
8349 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00008350 semsg(_(e_cannot_open_str_for_writing_2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351
8352 return fd;
8353}
8354
8355/*
8356 * ":mark" and ":k".
8357 */
8358 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008359ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360{
8361 pos_T pos;
8362
Bram Moolenaar10b94212021-02-19 21:42:57 +01008363#ifdef FEAT_EVAL
8364 if (not_in_vim9(eap) == FAIL)
8365 return;
8366#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008367 if (*eap->arg == NUL) // No argument?
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00008368 emsg(_(e_argument_required));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008369 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar74409f62022-01-01 15:58:22 +00008370 semsg(_(e_trailing_characters_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 else
8372 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008373 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 curwin->w_cursor.lnum = eap->line2;
8375 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008376 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaar6d057012021-12-31 18:49:43 +00008377 emsg(_(e_argument_must_be_letter_or_forward_backward_quote));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008378 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 }
8380}
8381
8382/*
8383 * Update w_topline, w_leftcol and the cursor position.
8384 */
8385 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008386update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008388 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 update_topline();
8390 if (!curwin->w_p_wrap)
8391 validate_cursor();
8392 update_curswant();
8393}
8394
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008396 * Save the current State and go to Normal mode.
8397 * Return TRUE if the typeahead could be saved.
8398 */
8399 int
8400save_current_state(save_state_T *sst)
8401{
8402 sst->save_msg_scroll = msg_scroll;
8403 sst->save_restart_edit = restart_edit;
8404 sst->save_msg_didout = msg_didout;
8405 sst->save_State = State;
8406 sst->save_insertmode = p_im;
8407 sst->save_finish_op = finish_op;
8408 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008409 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008410
Bram Moolenaar4324d872020-12-01 20:12:24 +01008411 msg_scroll = FALSE; // no msg scrolling in Normal mode
8412 restart_edit = 0; // don't go to Insert mode
8413 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008414
Bram Moolenaara452b802020-12-01 21:47:59 +01008415 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008416 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008417
8418 /*
8419 * Save the current typeahead. This is required to allow using ":normal"
8420 * from an event handler and makes sure we don't hang when the argument
8421 * ends with half a command.
8422 */
8423 save_typeahead(&sst->tabuf);
8424 return sst->tabuf.typebuf_valid;
8425}
8426
8427 void
8428restore_current_state(save_state_T *sst)
8429{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008430 // Restore the previous typeahead.
Bram Moolenaarc41badb2021-06-07 22:04:52 +02008431 restore_typeahead(&sst->tabuf, FALSE);
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008432
8433 msg_scroll = sst->save_msg_scroll;
8434 restart_edit = sst->save_restart_edit;
8435 p_im = sst->save_insertmode;
8436 finish_op = sst->save_finish_op;
8437 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008438 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008439 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008440 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008441
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008442 // Restore the state (needed when called from a function executed for
8443 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008444 State = sst->save_State;
8445#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008446 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008447#endif
8448}
8449
8450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 * ":normal[!] {commands}": Execute normal mode commands.
8452 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008453 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008454ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008456 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 char_u *arg = NULL;
8458 int l;
8459 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008461 if (ex_normal_lock > 0)
8462 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00008463 emsg(_(e_not_allowed_here));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008464 return;
8465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 if (ex_normal_busy >= p_mmd)
8467 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00008468 emsg(_(e_recursive_use_of_normal_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 return;
8470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 /*
8473 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8474 * this for the K_SPECIAL leading byte, otherwise special keys will not
8475 * work.
8476 */
8477 if (has_mbyte)
8478 {
8479 int len = 0;
8480
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008481 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 for (p = eap->arg; *p != NUL; ++p)
8483 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008484#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008485 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008487#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008488 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008489 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008490#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 )
8494 len += 2;
8495 }
8496 if (len > 0)
8497 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008498 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 if (arg != NULL)
8500 {
8501 len = 0;
8502 for (p = eap->arg; *p != NUL; ++p)
8503 {
8504 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008505#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 if (*p == CSI)
8507 {
8508 arg[len++] = KS_EXTRA;
8509 arg[len++] = (int)KE_CSI;
8510 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008511#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008512 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 {
8514 arg[len++] = *++p;
8515 if (*p == K_SPECIAL)
8516 {
8517 arg[len++] = KS_SPECIAL;
8518 arg[len++] = KE_FILLER;
8519 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008520#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 else if (*p == CSI)
8522 {
8523 arg[len++] = KS_EXTRA;
8524 arg[len++] = (int)KE_CSI;
8525 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 }
8528 arg[len] = NUL;
8529 }
8530 }
8531 }
8532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008534 ++ex_normal_busy;
8535 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 {
8537 /*
8538 * Repeat the :normal command for each line in the range. When no
8539 * range given, execute it just once, without positioning the cursor
8540 * first.
8541 */
8542 do
8543 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 if (eap->addr_count != 0)
8545 {
8546 curwin->w_cursor.lnum = eap->line1++;
8547 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008548 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 }
8550
Bram Moolenaar13505972019-01-24 15:04:48 +01008551 exec_normal_cmd(arg != NULL
8552 ? arg
8553 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 }
8555 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8556 }
8557
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008558 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 update_topline_cursor();
8560
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008561 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008563 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008564#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008565 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008566#endif
8567
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569}
8570
8571/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008572 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 */
8574 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008575ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008577 if (eap->forceit)
8578 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008579 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008580 if (!curwin->w_cursor.lnum)
8581 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008582 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008583 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008584#ifdef FEAT_TERMINAL
8585 // Ignore this when running in an active terminal.
8586 if (term_job_running(curbuf->b_term))
8587 return;
8588#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008589
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008590 // Ignore the command when already in Insert mode. Inserting an
8591 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008592 if (State & INSERT)
8593 return;
8594
Bram Moolenaar64969662005-12-14 21:59:55 +00008595 if (eap->cmdidx == CMD_startinsert)
8596 restart_edit = 'a';
8597 else if (eap->cmdidx == CMD_startreplace)
8598 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008600 restart_edit = 'V';
8601
8602 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008604 if (eap->cmdidx == CMD_startinsert)
8605 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008606 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008608
8609 if (VIsual_active)
8610 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611}
8612
8613/*
8614 * ":stopinsert"
8615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008617ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618{
8619 restart_edit = 0;
8620 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008621 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008624/*
8625 * Execute normal mode command "cmd".
8626 * "remap" can be REMAP_NONE or REMAP_YES.
8627 */
8628 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008629exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008630{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008631 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008632 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008633 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008634}
Bram Moolenaar25281632016-01-21 23:32:32 +01008635
Bram Moolenaar25281632016-01-21 23:32:32 +01008636/*
8637 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008638 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008639 */
8640 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008641exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008642{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008643 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008644 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008645
Bram Moolenaar905dd902019-04-07 14:21:47 +02008646 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8647 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008648 clear_oparg(&oa);
8649 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008650 while ((!stuff_empty()
8651 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008652 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008653 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008654 {
8655 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008656#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008657 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008658 && oa.op_type == OP_NOP && oa.regname == NUL
8659 && !VIsual_active)
8660 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008661 // If terminal_loop() returns OK we got a key that is handled
8662 // in Normal model. With FAIL we first need to position the
8663 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008664 if (terminal_loop(TRUE) == OK)
8665 normal_cmd(&oa, TRUE);
8666 }
8667 else
8668#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008669 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008670 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008671 }
8672}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008673
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674#ifdef FEAT_FIND_ID
8675 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008676ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677{
8678 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8679 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8680 (linenr_T)1, (linenr_T)MAXLNUM);
8681}
8682
Bram Moolenaar4033c552017-09-16 20:54:51 +02008683#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684/*
8685 * ":psearch"
8686 */
8687 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008688ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689{
8690 g_do_tagpreview = p_pvh;
8691 ex_findpat(eap);
8692 g_do_tagpreview = 0;
8693}
8694#endif
8695
8696 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008697ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698{
8699 int whole = TRUE;
8700 long n;
8701 char_u *p;
8702 int action;
8703
8704 switch (cmdnames[eap->cmdidx].cmd_name[2])
8705 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008706 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8708 action = ACTION_GOTO;
8709 else
8710 action = ACTION_SHOW;
8711 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008712 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 action = ACTION_SHOW_ALL;
8714 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008715 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 action = ACTION_GOTO;
8717 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008718 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 action = ACTION_SPLIT;
8720 break;
8721 }
8722
8723 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008724 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 {
8726 n = getdigits(&eap->arg);
8727 eap->arg = skipwhite(eap->arg);
8728 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008729 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 {
8731 whole = FALSE;
8732 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008733 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 if (*p)
8735 {
8736 *p++ = NUL;
8737 p = skipwhite(p);
8738
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008739 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008740 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar74409f62022-01-01 15:58:22 +00008741 eap->errmsg = ex_errmsg(e_trailing_characters_str, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02008743 set_nextcmd(eap, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 }
8745 }
8746 if (!eap->skip)
8747 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8748 whole, !eap->forceit,
8749 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8750 n, action, eap->line1, eap->line2);
8751}
8752#endif
8753
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754
Bram Moolenaar4033c552017-09-16 20:54:51 +02008755#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756/*
8757 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8758 */
8759 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008760ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008762 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8764}
8765
8766/*
8767 * ":pedit"
8768 */
8769 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008770ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771{
8772 win_T *curwin_save = curwin;
8773
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008774 if (ERROR_IF_ANY_POPUP_WINDOW)
8775 return;
8776
Bram Moolenaar026587b2019-08-17 15:08:00 +02008777 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008779 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008780
Bram Moolenaar026587b2019-08-17 15:08:00 +02008781 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008783
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 if (curwin != curwin_save && win_valid(curwin_save))
8785 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008786 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 validate_cursor();
8788 redraw_later(VALID);
8789 win_enter(curwin_save, TRUE);
8790 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008791# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008792 else if (WIN_IS_POPUP(curwin))
8793 {
8794 // can't keep focus in popup window
8795 win_enter(firstwin, TRUE);
8796 }
8797# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 g_do_tagpreview = 0;
8799}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801
8802/*
8803 * ":stag", ":stselect" and ":stjump".
8804 */
8805 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008806ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807{
8808 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008809 postponed_split_flags = cmdmod.cmod_split;
8810 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8812 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008813 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815
8816/*
8817 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8818 */
8819 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008820ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821{
8822 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8823}
8824
8825 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008826ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827{
8828 int cmd;
8829
8830 switch (name[1])
8831 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008832 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008834 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008836 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008838 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008840 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008842 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008844 case 'f': // ":tfirst"
8845 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008847 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008849 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008851 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008853 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 return;
8855 }
8856#endif
8857 cmd = DT_TAG;
8858 break;
8859 }
8860
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008861 if (name[0] == 'l')
8862 {
8863#ifndef FEAT_QUICKFIX
8864 ex_ni(eap);
8865 return;
8866#else
8867 cmd = DT_LTAG;
8868#endif
8869 }
8870
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8872 eap->forceit, TRUE);
8873}
8874
8875/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008876 * Check "str" for starting with a special cmdline variable.
8877 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8878 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8879 */
8880 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008881find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008882{
8883 int len;
8884 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008885 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008886 "%",
8887#define SPEC_PERC 0
8888 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008889#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008890 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008891#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008892 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008893#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008894 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008895#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008896 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008897#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008898 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008899#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008900 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008901#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008902 "<stack>", // call stack
8903#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008904 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008905#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008906 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008907#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008908 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008909#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008910 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008911#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008912 "<SID>", // script ID: <SNR>123_
8913#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008914#ifdef FEAT_CLIENTSERVER
8915 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008916# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008917#endif
8918 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008919
K.Takataeeec2542021-06-02 13:28:16 +02008920 for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008921 {
8922 len = (int)STRLEN(spec_str[i]);
8923 if (STRNCMP(src, spec_str[i], len) == 0)
8924 {
8925 *usedlen = len;
8926 return i;
8927 }
8928 }
8929 return -1;
8930}
8931
8932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 * Evaluate cmdline variables.
8934 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008935 * change "%" to curbuf->b_ffname
8936 * "#" to curwin->w_alt_fnum
8937 * "%%" to curwin->w_alt_fnum in Vim9 script
8938 * "<cword>" to word under the cursor
8939 * "<cWORD>" to WORD under the cursor
8940 * "<cexpr>" to C-expression under the cursor
8941 * "<cfile>" to path name under the cursor
8942 * "<sfile>" to sourced file name
8943 * "<stack>" to call stack
8944 * "<slnum>" to sourced file line number
8945 * "<afile>" to file name for autocommand
8946 * "<abuf>" to buffer number for autocommand
8947 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 *
8949 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8950 * "" for error without a message) and NULL is returned.
8951 * Returns an allocated string if a valid match was found.
8952 * Returns NULL if no match was found. "usedlen" then still contains the
8953 * number of characters to skip.
8954 */
8955 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008956eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008957 char_u *src, // pointer into commandline
8958 char_u *srcstart, // beginning of valid memory for src
8959 int *usedlen, // characters after src that are used
8960 linenr_T *lnump, // line number for :e command, or NULL
8961 char **errormsg, // pointer to error message
8962 int *escaped) // return value has escaped white space (can
8963 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964{
8965 int i;
8966 char_u *s;
8967 char_u *result;
8968 char_u *resultbuf = NULL;
8969 int resultlen;
8970 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008971 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008973 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976
8977 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008978 if (escaped != NULL)
8979 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980
8981 /*
8982 * Check if there is something to do.
8983 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008984 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008985 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 {
8987 *usedlen = 1;
8988 return NULL;
8989 }
8990
8991 /*
8992 * Skip when preceded with a backslash "\%" and "\#".
8993 * Note: In "\\%" the % is also not recognized!
8994 */
8995 if (src > srcstart && src[-1] == '\\')
8996 {
8997 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008998 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 return NULL;
9000 }
9001
9002 /*
9003 * word or WORD under cursor
9004 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009005 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9006 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009008 resultlen = find_ident_under_cursor(&result,
9009 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9010 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9011 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 if (resultlen == 0)
9013 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009014 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 return NULL;
9016 }
9017 }
9018
9019 /*
9020 * '#': Alternate file name
9021 * '%': Current file name
9022 * File name under the cursor
9023 * File name for autocommand
9024 * and following modifiers
9025 */
9026 else
9027 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009028 int off = 0;
9029
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 switch (spec_idx)
9031 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009032 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009033#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009034 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009037 // '%': current file
9038 if (curbuf->b_fname == NULL)
9039 {
9040 result = (char_u *)"";
9041 valid = 0; // Must have ":p:h" to be valid
9042 }
9043 else
9044 {
9045 result = curbuf->b_fname;
9046 tilde_file = STRCMP(result, "~") == 0;
9047 }
9048 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009050#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009051 // "%%" alternate file
9052 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01009053#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01009054 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009055 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009056 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009058 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 result = arg_all();
9060 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009061 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009062 if (escaped != NULL)
9063 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 break;
9066 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009067 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009068 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00009069 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009071 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009072 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009073 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009074 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009076 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009078 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00009079 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009080 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009081 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009082 return NULL;
9083 }
9084#ifdef FEAT_EVAL
9085 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9086 (long)i);
9087 if (result == NULL)
9088 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009089 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009090 return NULL;
9091 }
9092#else
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009093 *errormsg = _(e_hashsmall_is_not_available_without_the_eval_feature);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 }
9097 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009098 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01009099 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
9100 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009101 buf = buflist_findnr(i);
9102 if (buf == NULL)
9103 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00009104 *errormsg = _(e_no_alternate_file_name_to_substitute_for_hash);
Bram Moolenaard812df62008-11-09 12:46:09 +00009105 return NULL;
9106 }
9107 if (lnump != NULL)
9108 *lnump = ECMD_LAST;
9109 if (buf->b_fname == NULL)
9110 {
9111 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009112 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00009113 }
9114 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009115 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009116 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009117 tilde_file = STRCMP(result, "~") == 0;
9118 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 break;
9121
9122#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009123 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009124 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 if (result == NULL)
9126 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009127 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 return NULL;
9129 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009130 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 break;
9132#endif
9133
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009134 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009136 if (result != NULL && !autocmd_fname_full)
9137 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009138 // Still need to turn the fname into a full path. It is
9139 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009140 autocmd_fname_full = TRUE;
9141 result = FullName_save(autocmd_fname, FALSE);
9142 vim_free(autocmd_fname);
9143 autocmd_fname = result;
9144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 if (result == NULL)
9146 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009147 *errormsg = _(e_no_autocommand_file_name_to_substitute_for_afile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 return NULL;
9149 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009150 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 break;
9152
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009153 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 if (autocmd_bufnr <= 0)
9155 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009156 *errormsg = _(e_no_autocommand_buffer_name_to_substitute_for_abuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 return NULL;
9158 }
9159 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9160 result = strbuf;
9161 break;
9162
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009163 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 result = autocmd_match;
9165 if (result == NULL)
9166 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009167 *errormsg = _(e_no_autocommand_match_name_to_substitute_for_amatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 return NULL;
9169 }
9170 break;
9171
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009172 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02009173 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02009174 result = estack_sfile(spec_idx == SPEC_SFILE
9175 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 if (result == NULL)
9177 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02009178 *errormsg = spec_idx == SPEC_SFILE
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009179 ? _(e_no_source_file_name_to_substitute_for_sfile)
9180 : _(e_no_call_stack_to_substitute_for_stack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 return NULL;
9182 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009183 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009185
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009186 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009187 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009188 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00009189 *errormsg = _(e_no_line_number_to_use_for_slnum);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009190 return NULL;
9191 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009192 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009193 result = strbuf;
9194 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009195
9196#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009197 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009198 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009199 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00009200 *errormsg = _(e_no_line_number_to_use_for_sflnum);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009201 return NULL;
9202 }
9203 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01009204 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009205 result = strbuf;
9206 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009207
Bram Moolenaar90944302020-08-01 20:45:11 +02009208 case SPEC_SID:
9209 if (current_sctx.sc_sid <= 0)
9210 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00009211 *errormsg = _(e_using_sid_not_in_script_context);
Bram Moolenaar90944302020-08-01 20:45:11 +02009212 return NULL;
9213 }
9214 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
9215 result = strbuf;
9216 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02009217#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02009218
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009219#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009220 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009221 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9222 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 result = strbuf;
9224 break;
9225#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009226
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009227 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009228 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009229 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 }
9231
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009232 resultlen = (int)STRLEN(result); // length of new string
9233 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 {
9235 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 resultlen = (int)(s - result);
9238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 else if (!skip_mod)
9240 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009241 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 &resultlen);
9243 if (result == NULL)
9244 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009245 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 return NULL;
9247 }
9248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 }
9250
9251 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9252 {
9253 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009254 *errormsg = _(e_empty_file_name_for_percent_or_hash_only_works_with_ph);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 else
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00009256 *errormsg = _(e_evaluates_to_an_empty_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 result = NULL;
9258 }
9259 else
9260 result = vim_strnsave(result, resultlen);
9261 vim_free(resultbuf);
9262 return result;
9263}
9264
9265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 * Expand the <sfile> string in "arg".
9267 *
9268 * Returns an allocated string, or NULL for any error.
9269 */
9270 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009271expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009273 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 int len;
9275 char_u *result;
9276 char_u *newres;
9277 char_u *repl;
9278 int srclen;
9279 char_u *p;
9280
9281 result = vim_strsave(arg);
9282 if (result == NULL)
9283 return NULL;
9284
9285 for (p = result; *p; )
9286 {
9287 if (STRNCMP(p, "<sfile>", 7) != 0)
9288 ++p;
9289 else
9290 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009291 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00009292 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 if (errormsg != NULL)
9294 {
9295 if (*errormsg)
9296 emsg(errormsg);
9297 vim_free(result);
9298 return NULL;
9299 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009300 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 {
9302 p += srclen;
9303 continue;
9304 }
9305 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9306 newres = alloc(len);
9307 if (newres == NULL)
9308 {
9309 vim_free(repl);
9310 vim_free(result);
9311 return NULL;
9312 }
9313 mch_memmove(newres, result, (size_t)(p - result));
9314 STRCPY(newres + (p - result), repl);
9315 len = (int)STRLEN(newres);
9316 STRCAT(newres, p + srclen);
9317 vim_free(repl);
9318 vim_free(result);
9319 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009320 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 }
9322 }
9323
9324 return result;
9325}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009328/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009329 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009330 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009333dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 if (fname == NULL)
9336 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009337 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338}
9339#endif
9340
9341/*
9342 * ":behave {mswin,xterm}"
9343 */
9344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009345ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346{
9347 if (STRCMP(eap->arg, "mswin") == 0)
9348 {
9349 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9350 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9351 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9352 set_option_value((char_u *)"keymodel", 0L,
9353 (char_u *)"startsel,stopsel", 0);
9354 }
9355 else if (STRCMP(eap->arg, "xterm") == 0)
9356 {
9357 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9358 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9359 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9360 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9361 }
9362 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009363 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364}
9365
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366static int filetype_detect = FALSE;
9367static int filetype_plugin = FALSE;
9368static int filetype_indent = FALSE;
9369
9370/*
9371 * ":filetype [plugin] [indent] {on,off,detect}"
9372 * on: Load the filetype.vim file to install autocommands for file types.
9373 * off: Load the ftoff.vim file to remove all autocommands for file types.
9374 * plugin on: load filetype.vim and ftplugin.vim
9375 * plugin off: load ftplugof.vim
9376 * indent on: load filetype.vim and indent.vim
9377 * indent off: load indoff.vim
9378 */
9379 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009380ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381{
9382 char_u *arg = eap->arg;
9383 int plugin = FALSE;
9384 int indent = FALSE;
9385
9386 if (*eap->arg == NUL)
9387 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009388 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009389 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390 filetype_detect ? "ON" : "OFF",
9391 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9392 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9393 return;
9394 }
9395
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009396 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 for (;;)
9398 {
9399 if (STRNCMP(arg, "plugin", 6) == 0)
9400 {
9401 plugin = TRUE;
9402 arg = skipwhite(arg + 6);
9403 continue;
9404 }
9405 if (STRNCMP(arg, "indent", 6) == 0)
9406 {
9407 indent = TRUE;
9408 arg = skipwhite(arg + 6);
9409 continue;
9410 }
9411 break;
9412 }
9413 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9414 {
9415 if (*arg == 'o' || !filetype_detect)
9416 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009417 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 filetype_detect = TRUE;
9419 if (plugin)
9420 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009421 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 filetype_plugin = TRUE;
9423 }
9424 if (indent)
9425 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009426 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 filetype_indent = TRUE;
9428 }
9429 }
9430 if (*arg == 'd')
9431 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009432 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009433 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 }
9435 }
9436 else if (STRCMP(arg, "off") == 0)
9437 {
9438 if (plugin || indent)
9439 {
9440 if (plugin)
9441 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009442 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 filetype_plugin = FALSE;
9444 }
9445 if (indent)
9446 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009447 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 filetype_indent = FALSE;
9449 }
9450 }
9451 else
9452 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009453 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 filetype_detect = FALSE;
9455 }
9456 }
9457 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00009458 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459}
9460
9461/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009462 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 */
9464 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009465ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466{
9467 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009468 {
9469 char_u *arg = eap->arg;
9470
9471 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9472 arg += 9;
9473
9474 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9475 if (arg != eap->arg)
9476 did_filetype = FALSE;
9477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009481ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482{
9483#ifdef FEAT_DIGRAPHS
9484 if (*eap->arg != NUL)
9485 putdigraph(eap->arg);
9486 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009487 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488#else
mityu61065042021-07-19 20:07:21 +02009489 emsg(_(e_no_digraphs_version));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490#endif
9491}
9492
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009493#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9494 void
9495set_no_hlsearch(int flag)
9496{
9497 no_hlsearch = flag;
9498# ifdef FEAT_EVAL
9499 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9500# endif
9501}
9502
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503/*
9504 * ":nohlsearch"
9505 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009507ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009509 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009510 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512#endif
9513
9514#ifdef FEAT_CRYPT
9515/*
9516 * ":X": Get crypt key
9517 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009519ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009521 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009522 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523}
9524#endif
9525
9526#ifdef FEAT_FOLDING
9527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009528ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529{
9530 if (foldManualAllowed(TRUE))
9531 foldCreate(eap->line1, eap->line2);
9532}
9533
9534 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009535ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536{
9537 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9538 eap->forceit, FALSE);
9539}
9540
9541 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009542ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543{
9544 linenr_T lnum;
9545
Bram Moolenaar4facea32019-10-12 20:17:40 +02009546# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009547 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009548# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009549
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009550 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9552 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9553 ml_setmarked(lnum);
9554
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009555 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009557 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009558# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009559 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009560# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561}
9562#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009564#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009565/*
9566 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9567 * instead of a quickfix command.
9568 */
9569 int
9570is_loclist_cmd(int cmdidx)
9571{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009572 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009573 return FALSE;
9574 return cmdnames[cmdidx].cmd_name[0] == 'l';
9575}
9576#endif
9577
Bram Moolenaar4facea32019-10-12 20:17:40 +02009578#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009579 int
9580get_pressedreturn(void)
9581{
9582 return ex_pressedreturn;
9583}
9584
9585 void
9586set_pressedreturn(int val)
9587{
9588 ex_pressedreturn = val;
9589}
9590#endif