blob: 0bb5142d7d0c45c7a92b6067bd4979c6dc3ddd8d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +020023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaar20b23c62020-08-20 15:25:00 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
39static void ex_bmodified(exarg_T *eap);
40static void ex_bnext(exarg_T *eap);
41static void ex_bprevious(exarg_T *eap);
42static void ex_brewind(exarg_T *eap);
43static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifndef FEAT_QUICKFIX
47# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000048# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# define ex_cc ex_ni
50# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020051# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cfile ex_ni
53# define qf_list ex_ni
54# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020055# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000057# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020059#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define ex_cclose ex_ni
61# define ex_copen ex_ni
62# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020063# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000065#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
66# define ex_cexpr ex_ni
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020069static linenr_T default_address(exarg_T *eap);
Bram Moolenaarb7316892019-05-01 18:08:42 +020070static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020071static void address_default_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010072static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020073#if !defined(FEAT_PERL) \
74 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
75 || !defined(FEAT_TCL) \
76 || !defined(FEAT_RUBY) \
77 || !defined(FEAT_LUA) \
78 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000079# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010080static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010082static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000086#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010087static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
88static void ex_highlight(exarg_T *eap);
89static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_cquit(exarg_T *eap);
91static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010092static void ex_close(exarg_T *eap);
93static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
94static void ex_only(exarg_T *eap);
95static void ex_resize(exarg_T *eap);
96static void ex_stag(exarg_T *eap);
97static void ex_tabclose(exarg_T *eap);
98static void ex_tabonly(exarg_T *eap);
99static void ex_tabnext(exarg_T *eap);
100static void ex_tabmove(exarg_T *eap);
101static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200102#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100103static void ex_pclose(exarg_T *eap);
104static void ex_ptag(exarg_T *eap);
105static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106#else
107# define ex_pclose ex_ni
108# define ex_ptag ex_ni
109# define ex_pedit ex_ni
110#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100111static void ex_hide(exarg_T *eap);
112static void ex_stop(exarg_T *eap);
113static void ex_exit(exarg_T *eap);
114static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100116static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#else
118# define ex_goto ex_ni
119#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100120static void ex_shell(exarg_T *eap);
121static void ex_preserve(exarg_T *eap);
122static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100123static void ex_mode(exarg_T *eap);
124static void ex_wrongmodifier(exarg_T *eap);
125static void ex_find(exarg_T *eap);
126static void ex_open(exarg_T *eap);
127static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#ifndef FEAT_GUI
129# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100132#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100133static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#else
135# define ex_tearoff ex_ni
136#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100137#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
138 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#else
141# define ex_popup ex_ni
142#endif
143#ifndef FEAT_GUI_MSWIN
144# define ex_simalt ex_ni
145#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000146#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147# define gui_mch_find_dialog ex_ni
148# define gui_mch_replace_dialog ex_ni
149#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000150#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151# define ex_helpfind ex_ni
152#endif
153#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100154# define ex_cscope ex_ni
155# define ex_scscope ex_ni
156# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#endif
158#ifndef FEAT_SYN_HL
159# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200160# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000161#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200162#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200163# define ex_syntime ex_ni
164#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000165#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000166# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000167# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000168# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000169# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000170# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000171#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200172#ifndef FEAT_PERSISTENT_UNDO
173# define ex_rundo ex_ni
174# define ex_wundo ex_ni
175#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200176#ifndef FEAT_LUA
177# define ex_lua ex_script_ni
178# define ex_luado ex_ni
179# define ex_luafile ex_ni
180#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000181#ifndef FEAT_MZSCHEME
182# define ex_mzscheme ex_script_ni
183# define ex_mzfile ex_ni
184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#ifndef FEAT_PERL
186# define ex_perl ex_script_ni
187# define ex_perldo ex_ni
188#endif
189#ifndef FEAT_PYTHON
190# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200191# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192# define ex_pyfile ex_ni
193#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200194#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200195# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200196# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200197# define ex_py3file ex_ni
198#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100199#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
200# define ex_pyx ex_script_ni
201# define ex_pyxdo ex_ni
202# define ex_pyxfile ex_ni
203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#ifndef FEAT_TCL
205# define ex_tcl ex_script_ni
206# define ex_tcldo ex_ni
207# define ex_tclfile ex_ni
208#endif
209#ifndef FEAT_RUBY
210# define ex_ruby ex_script_ni
211# define ex_rubydo ex_ni
212# define ex_rubyfile ex_ni
213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#ifndef FEAT_KEYMAP
215# define ex_loadkeymap ex_ni
216#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100217static void ex_swapname(exarg_T *eap);
218static void ex_syncbind(exarg_T *eap);
219static void ex_read(exarg_T *eap);
220static void ex_pwd(exarg_T *eap);
221static void ex_equal(exarg_T *eap);
222static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000225#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#else
228# define ex_winpos ex_ni
229#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100230static void ex_operators(exarg_T *eap);
231static void ex_put(exarg_T *eap);
232static void ex_copymove(exarg_T *eap);
233static void ex_submagic(exarg_T *eap);
234static void ex_join(exarg_T *eap);
235static void ex_at(exarg_T *eap);
236static void ex_bang(exarg_T *eap);
237static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200238#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100239static void ex_wundo(exarg_T *eap);
240static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200241#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ex_redo(exarg_T *eap);
243static void ex_later(exarg_T *eap);
244static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100246static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100249static void ex_startinsert(exarg_T *eap);
250static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100252static void ex_checkpath(exarg_T *eap);
253static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254#else
255# define ex_findpat ex_ni
256# define ex_checkpath ex_ni
257#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200258#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100259static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260#else
261# define ex_psearch ex_ni
262#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static void ex_tag(exarg_T *eap);
264static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#ifndef FEAT_EVAL
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200266# define ex_block ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200267# define ex_break ex_ni
268# define ex_breakadd ex_ni
269# define ex_breakdel ex_ni
270# define ex_breaklist ex_ni
271# define ex_call ex_ni
272# define ex_catch ex_ni
273# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200274# define ex_continue ex_ni
275# define ex_debug ex_ni
276# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200277# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200278# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_echo ex_ni
281# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282# define ex_else ex_ni
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200283# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200284# define ex_endfunction ex_ni
285# define ex_endif ex_ni
286# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200288# define ex_eval ex_ni
289# define ex_execute ex_ni
290# define ex_finally ex_ni
291# define ex_finish ex_ni
292# define ex_function ex_ni
293# define ex_if ex_ni
294# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200295# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200296# define ex_lockvar ex_ni
297# define ex_oldfiles ex_ni
298# define ex_options ex_ni
299# define ex_packadd ex_ni
300# define ex_packloadall ex_ni
301# define ex_return ex_ni
302# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# define ex_throw ex_ni
304# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000306# define ex_unlockvar ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200307# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100308# define ex_import ex_ni
309# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200311#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_loadview ex_ni
313#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200314#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315# define ex_viminfo ex_ni
316#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_filetype(exarg_T *eap);
319static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000321# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322# define ex_diffpatch ex_ni
323# define ex_diffgetput ex_ni
324# define ex_diffsplit ex_ni
325# define ex_diffthis ex_ni
326# define ex_diffupdate ex_ni
327#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100328static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100330static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#else
332# define ex_nohlsearch ex_ni
333# define ex_match ex_ni
334#endif
335#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100336static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337#else
338# define ex_X ex_ni
339#endif
340#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100341static void ex_fold(exarg_T *eap);
342static void ex_foldopen(exarg_T *eap);
343static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344#else
345# define ex_fold ex_ni
346# define ex_foldopen ex_ni
347# define ex_folddo ex_ni
348#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100349#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350# define ex_language ex_ni
351#endif
352#ifndef FEAT_SIGNS
353# define ex_sign ex_ni
354#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000355#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200356# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000357# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200358# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361#ifndef FEAT_JUMPLIST
362# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200363# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364# define ex_changes ex_ni
365#endif
366
Bram Moolenaar05159a02005-02-26 23:04:13 +0000367#ifndef FEAT_PROFILE
368# define ex_profile ex_ni
369#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200370#ifndef FEAT_TERMINAL
371# define ex_terminal ex_ni
372#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200373#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
374# define ex_xrestore ex_ni
375#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100376#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200377# define ex_popupclear ex_ni
378#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000379
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380/*
381 * Declare cmdnames[].
382 */
383#define DO_DECLARE_EXCMD
384#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200385#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100386
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387static char_u dollar_command[2] = {'$', 0};
388
389
390#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100391// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392typedef struct
393{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100394 char_u *line; // command line
395 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396} wcmd_T;
397
398/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000399 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000401 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000403struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100405 garray_T *lines_gap; // growarray with line info
406 int current_line; // last read line from growarray
407 int repeating; // TRUE when looping a second time
408 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200409 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 void *cookie;
411};
412
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200413static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100414static int store_loop_line(garray_T *gap, char_u *line);
415static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000416
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100417// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000418struct dbg_stuff
419{
420 int trylevel;
421 int force_abort;
422 except_T *caught_stack;
423 char_u *vv_exception;
424 char_u *vv_throwpoint;
425 int did_emsg;
426 int got_int;
427 int did_throw;
428 int need_rethrow;
429 int check_cstack;
430 except_T *current_exception;
431};
432
Bram Moolenaared203462004-06-16 11:19:22 +0000433 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100434save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000435{
436 dsp->trylevel = trylevel; trylevel = 0;
437 dsp->force_abort = force_abort; force_abort = FALSE;
438 dsp->caught_stack = caught_stack; caught_stack = NULL;
439 dsp->vv_exception = v_exception(NULL);
440 dsp->vv_throwpoint = v_throwpoint(NULL);
441
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100442 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000443 dsp->did_emsg = did_emsg; did_emsg = FALSE;
444 dsp->got_int = got_int; got_int = FALSE;
445 dsp->did_throw = did_throw; did_throw = FALSE;
446 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
447 dsp->check_cstack = check_cstack; check_cstack = FALSE;
448 dsp->current_exception = current_exception; current_exception = NULL;
449}
450
451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100452restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000453{
454 suppress_errthrow = FALSE;
455 trylevel = dsp->trylevel;
456 force_abort = dsp->force_abort;
457 caught_stack = dsp->caught_stack;
458 (void)v_exception(dsp->vv_exception);
459 (void)v_throwpoint(dsp->vv_throwpoint);
460 did_emsg = dsp->did_emsg;
461 got_int = dsp->got_int;
462 did_throw = dsp->did_throw;
463 need_rethrow = dsp->need_rethrow;
464 check_cstack = dsp->check_cstack;
465 current_exception = dsp->current_exception;
466}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467#endif
468
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469/*
470 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
471 * command is given.
472 */
473 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100474do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100475 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476{
477 int save_msg_scroll;
478 int prev_msg_row;
479 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100480 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000481
482 if (improved)
483 exmode_active = EXMODE_VIM;
484 else
485 exmode_active = EXMODE_NORMAL;
486 State = NORMAL;
487
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100488 // When using ":global /pat/ visual" and then "Q" we return to continue
489 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000490 if (global_busy)
491 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492
493 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100494 ++RedrawingDisabled; // don't redisplay the window
495 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100497 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 ++hold_gui_events;
499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500
Bram Moolenaar32526b32019-01-19 17:43:09 +0100501 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 while (exmode_active)
503 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100504 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000505 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
506 {
507 exmode_active = FALSE;
508 break;
509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 msg_scroll = TRUE;
511 need_wait_return = FALSE;
512 ex_pressedreturn = FALSE;
513 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100514 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 prev_msg_row = msg_row;
516 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 if (improved)
518 {
519 cmdline_row = msg_row;
520 do_cmdline(NULL, getexline, NULL, 0);
521 }
522 else
523 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
524 lines_left = Rows - 1;
525
Bram Moolenaardf177f62005-02-22 08:39:57 +0000526 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100527 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000529 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100530 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000531 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000533 if (ex_pressedreturn)
534 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100535 // go up one line, to overwrite the ":<CR>" line, so the
536 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000537 msg_row = prev_msg_row;
538 if (prev_msg_row == Rows - 1)
539 msg_row--;
540 }
541 msg_col = 0;
542 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
543 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100546 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 {
548 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100549 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000550 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100551 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 }
554
555#ifdef FEAT_GUI
556 --hold_gui_events;
557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 --RedrawingDisabled;
559 --no_wait_return;
560 update_screen(CLEAR);
561 need_wait_return = FALSE;
562 msg_scroll = save_msg_scroll;
563}
564
565/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200566 * Print the executed command for when 'verbose' is set.
567 * When "lnum" is 0 only print the command.
568 */
569 static void
570msg_verbose_cmd(linenr_T lnum, char_u *cmd)
571{
572 ++no_wait_return;
573 verbose_enter_scroll();
574
575 if (lnum == 0)
576 smsg(_("Executing: %s"), cmd);
577 else
578 smsg(_("line %ld: %s"), (long)lnum, cmd);
579 if (msg_silent == 0)
580 msg_puts("\n"); // don't overwrite this
581
582 verbose_leave_scroll();
583 --no_wait_return;
584}
585
586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 * Execute a simple command line. Used for translated commands like "*".
588 */
589 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100590do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591{
592 return do_cmdline(cmd, NULL, NULL,
593 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
594}
595
596/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100597 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
598 * This allows for using a range without ":" in Vim9 script.
599 */
600 int
601do_cmd_argument(char_u *cmd)
602{
603 return do_cmdline(cmd, NULL, NULL,
604 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
605}
606
607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 * do_cmdline(): execute one Ex command line
609 *
610 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100611 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 * 2. Split up in parts separated with '|'.
613 *
614 * This function can be called recursively!
615 *
616 * flags:
617 * DOCMD_VERBOSE - The command will be included in the error message.
618 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100619 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 * DOCMD_KEYTYPED - Don't reset KeyTyped.
621 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
622 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
623 *
624 * return FAIL if cmdline could not be executed, OK otherwise
625 */
626 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100627do_cmdline(
628 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200629 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100630 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100631 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100633 char_u *next_cmdline; // next cmd to execute
634 char_u *cmdline_copy = NULL; // copy of cmd line
635 int used_getline = FALSE; // used "fgetline" to obtain command
636 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100638 int count = 0; // line number count
639 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 int retval = OK;
641#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100642 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100643 garray_T lines_ga; // keep lines for ":while"/":for"
644 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200645 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100646 char_u *fname = NULL; // function or script name
647 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
648 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
649 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200651 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200652 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100654 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200655 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000657 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000659 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100661# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662# define cmd_cookie cookie
663#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100664 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200665#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100666 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100668 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
669 // location for storing error messages to be converted to an exception.
670 // This ensures that the do_errthrow() call in do_one_cmd() does not
671 // combine the messages stored by an earlier invocation of do_one_cmd()
672 // with the command name of the later one. This would happen when
673 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 saved_msg_list = msg_list;
675 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676#endif
677
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100678 // It's possible to create an endless loop with ":execute", catch that
679 // here. The value of 200 allows nested function calls, ":source", etc.
680 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100681 if (call_depth >= 200
682#ifdef FEAT_EVAL
683 && call_depth >= p_mfd
684#endif
685 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100687 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100689 // When converting to an exception, we do not include the command name
690 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100691 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 msg_list = saved_msg_list;
693#endif
694 return FAIL;
695 }
696 ++call_depth;
697
698#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100699 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 cstack.cs_idx = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
702
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100703 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100705 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100706 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000707 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 ++ex_nesting_level;
709
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100710 // Get the function or script name and the address where the next breakpoint
711 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000712 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {
714 fname = func_name(real_cookie);
715 breakpoint = func_breakpoint(real_cookie);
716 dbg_tick = func_dbg_tick(real_cookie);
717 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100718 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100720 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 breakpoint = source_breakpoint(real_cookie);
722 dbg_tick = source_dbg_tick(real_cookie);
723 }
724
725 /*
726 * Initialize "force_abort" and "suppress_errthrow" at the top level.
727 */
728 if (!recursive)
729 {
730 force_abort = FALSE;
731 suppress_errthrow = FALSE;
732 }
733
734 /*
735 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000736 * exception handling (used when debugging). Otherwise clear it to avoid
737 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000739 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000740 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000741 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200742 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
744 initial_trylevel = trylevel;
745
746 /*
747 * "did_throw" will be set to TRUE when an exception is being thrown.
748 */
749 did_throw = FALSE;
750#endif
751 /*
752 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000753 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 * If force_abort is set, we cancel everything.
755 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100756#ifdef FEAT_EVAL
757 did_emsg_cumul += did_emsg;
758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 did_emsg = FALSE;
760
761 /*
762 * KeyTyped is only set when calling vgetc(). Reset it here when not
763 * calling vgetc() (sourced command lines).
764 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100765 if (!(flags & DOCMD_KEYTYPED)
766 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 KeyTyped = FALSE;
768
769 /*
770 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000771 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 * - for multiple commands on one line, separated with '|'
773 * - when repeating until there are no more lines (for ":source")
774 */
775 next_cmdline = cmdline;
776 do
777 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000778#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100779 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000780#endif
781
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100782 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 if (next_cmdline == NULL
784#ifdef FEAT_EVAL
785 && !force_abort
786 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000787 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788#endif
789 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100790 {
791#ifdef FEAT_EVAL
792 did_emsg_cumul += did_emsg;
793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
797 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000798 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100799 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 * 3. If a line is given: Make a copy, so we can mess with it.
801 */
802
803#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100804 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000805 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100807 // Each '|' separated command is stored separately in lines_ga, to
808 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100809 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100811 // Check if a function has returned or, unless it has an unclosed
812 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000813 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000815# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000816 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000817 func_line_end(real_cookie);
818# endif
819 if (func_has_ended(real_cookie))
820 {
821 retval = FAIL;
822 break;
823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000825#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000826 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000828 script_line_end();
829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100831 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100832 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 {
834 retval = FAIL;
835 break;
836 }
837
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100838 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (breakpoint != NULL && dbg_tick != NULL
840 && *dbg_tick != debug_tick)
841 {
842 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100843 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100844 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 *dbg_tick = debug_tick;
846 }
847
848 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100849 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100851 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100853 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100855 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100856 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100858 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100859 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 *dbg_tick = debug_tick;
861 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000862# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000863 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000864 {
865 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100866 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100867 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000868 script_line_start();
869 }
870# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872#endif
873
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100874 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (next_cmdline == NULL)
876 {
877 /*
878 * Need to set msg_didout for the first line after an ":if",
879 * otherwise the ":if" will be overwritten.
880 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100881 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100883 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#ifdef FEAT_EVAL
885 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
886#else
887 0
888#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100889 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
890 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100892 // Don't call wait_return for aborted command line. The NULL
893 // returned for the end of a sourced file or executed function
894 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 if (KeyTyped && !(flags & DOCMD_REPEAT))
896 need_wait_return = FALSE;
897 retval = FAIL;
898 break;
899 }
900 used_getline = TRUE;
901
902 /*
903 * Keep the first typed line. Clear it when more lines are typed.
904 */
905 if (flags & DOCMD_KEEPLINE)
906 {
907 vim_free(repeat_cmdline);
908 if (count == 0)
909 repeat_cmdline = vim_strsave(next_cmdline);
910 else
911 repeat_cmdline = NULL;
912 }
913 }
914
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100915 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 else if (cmdline_copy == NULL)
917 {
918 next_cmdline = vim_strsave(next_cmdline);
919 if (next_cmdline == NULL)
920 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100921 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 retval = FAIL;
923 break;
924 }
925 }
926 cmdline_copy = next_cmdline;
927
928#ifdef FEAT_EVAL
929 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200930 * Inside a while/for loop, and when the command looks like a ":while"
931 * or ":for", the line is stored, because we may need it later when
932 * looping.
933 *
934 * When there is a '|' and another command, it is stored separately,
935 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000936 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200937 *
938 * Pass a different "fgetline" function to do_one_cmd() below,
939 * that it stores lines in or reads them from "lines_ga". Makes it
940 * possible to define a function inside a while/for loop and handles
941 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200943 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200945 cmd_getline = get_loop_line;
946 cmd_cookie = (void *)&cmd_loop_cookie;
947 cmd_loop_cookie.lines_gap = &lines_ga;
948 cmd_loop_cookie.current_line = current_line;
949 cmd_loop_cookie.getline = fgetline;
950 cmd_loop_cookie.cookie = cookie;
951 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
952
953 // Save the current line when encountering it the first time.
954 if (current_line == lines_ga.ga_len
955 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 {
957 retval = FAIL;
958 break;
959 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200960 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200962 else
963 {
964 cmd_getline = fgetline;
965 cmd_cookie = cookie;
966 }
967
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 did_endif = FALSE;
969#endif
970
971 if (count++ == 0)
972 {
973 /*
974 * All output from the commands is put below each other, without
975 * waiting for a return. Don't do this when executing commands
976 * from a script or when being called recursive (e.g. for ":e
977 * +command file").
978 */
979 if (!(flags & DOCMD_NOWAIT) && !recursive)
980 {
981 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100982 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100984 msg_scroll = TRUE; // put messages below each other
985 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 ++RedrawingDisabled;
987 did_inc = TRUE;
988 }
989 }
990
Bram Moolenaar823654b2020-05-29 23:03:09 +0200991 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100992 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
994 /*
995 * 2. Execute one '|' separated command.
996 * do_one_cmd() will return NULL if there is no trailing '|'.
997 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
998 */
999 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001000 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001#ifdef FEAT_EVAL
1002 &cstack,
1003#endif
1004 cmd_getline, cmd_cookie);
1005 --recursive;
1006
1007#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001008 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001009 // Use "current_line" from "cmd_loop_cookie", it may have been
1010 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001011 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012#endif
1013
1014 if (next_cmdline == NULL)
1015 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001016 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001017
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 /*
1019 * If the command was typed, remember it for the ':' register.
1020 * Do this AFTER executing the command to make :@: work.
1021 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001022 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 && new_last_cmdline != NULL)
1024 {
1025 vim_free(last_cmdline);
1026 last_cmdline = new_last_cmdline;
1027 new_last_cmdline = NULL;
1028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 }
1030 else
1031 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001032 // need to copy the command after the '|' to cmdline_copy, for the
1033 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001034 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 next_cmdline = cmdline_copy;
1036 }
1037
1038
1039#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001040 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001042 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001044 {
1045 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001049 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 {
1051 ++current_line;
1052
1053 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001054 * An ":endwhile", ":endfor" and ":continue" is handled here.
1055 * If we were executing commands, jump back to the ":while" or
1056 * ":for".
1057 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001059 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001061 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001063 // Jump back to the matching ":while" or ":for". Be careful
1064 // not to use a cs_line[] from an entry that isn't a ":while"
1065 // or ":for": It would make "current_line" invalid and can
1066 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 if (!did_emsg && !got_int && !did_throw
1068 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001069 && (cstack.cs_flags[cstack.cs_idx]
1070 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 && cstack.cs_line[cstack.cs_idx] >= 0
1072 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1073 {
1074 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001075 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001076 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001077 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001079 // Check for the next breakpoint at or after the ":while"
1080 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 if (breakpoint != NULL)
1082 {
1083 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001084 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 fname,
1086 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1087 *dbg_tick = debug_tick;
1088 }
1089 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001090 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001092 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001094 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1095 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 }
1097 }
1098
1099 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001100 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001102 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001104 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001105 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 }
1107 }
1108
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001109 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001110 if (breakpoint != NULL && has_watchexpr())
1111 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001112 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001113 *dbg_tick = debug_tick;
1114 }
1115
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 /*
1117 * When not inside any ":while" loop, clear remembered lines.
1118 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001119 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 {
1121 if (lines_ga.ga_len > 0)
1122 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001123 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1125 free_cmdlines(&lines_ga);
1126 }
1127 current_line = 0;
1128 }
1129
1130 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1132 * being restored at the ":endtry". Reset them here and set the
1133 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1134 * This includes the case where a missing ":endif", ":endwhile" or
1135 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001137 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001139 cstack.cs_lflags &= ~CSL_HAD_FINA;
1140 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1141 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 did_throw ? (void *)current_exception : NULL);
1143 did_emsg = got_int = did_throw = FALSE;
1144 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1145 }
1146
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001147 // Update global "trylevel" for recursive calls to do_cmdline() from
1148 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 trylevel = initial_trylevel + cstack.cs_trylevel;
1150
1151 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001152 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 * files) is aborted because of an error, an interrupt, or an uncaught
1154 * exception, cancel everything. If it is left normally, reset
1155 * force_abort to get the non-EH compatible abortion behavior for
1156 * the rest of the script.
1157 */
1158 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1159 force_abort = FALSE;
1160
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001161 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001163#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164
1165 }
1166 /*
1167 * Continue executing command lines when:
1168 * - no CTRL-C typed, no aborting error, no exception thrown or try
1169 * conditionals need to be checked for executing finally clauses or
1170 * catching an interrupt exception
1171 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001172 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 * looping for ":source" command or function call.
1174 */
1175 while (!((got_int
1176#ifdef FEAT_EVAL
1177 || (did_emsg && force_abort) || did_throw
1178#endif
1179 )
1180#ifdef FEAT_EVAL
1181 && cstack.cs_trylevel == 0
1182#endif
1183 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001184 && !(did_emsg
1185#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001186 // Keep going when inside try/catch, so that the error can be
1187 // deal with, except when it is a syntax error, it may cause
1188 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001189 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1190#endif
1191 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001192 && (getline_equal(fgetline, cookie, getexmodeline)
1193 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 && (next_cmdline != NULL
1195#ifdef FEAT_EVAL
1196 || cstack.cs_idx >= 0
1197#endif
1198 || (flags & DOCMD_REPEAT)));
1199
1200 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001201 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202#ifdef FEAT_EVAL
1203 free_cmdlines(&lines_ga);
1204 ga_clear(&lines_ga);
1205
1206 if (cstack.cs_idx >= 0)
1207 {
1208 /*
1209 * If a sourced file or executed function ran to its end, report the
1210 * unclosed conditional.
1211 */
1212 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001213 && ((getline_equal(fgetline, cookie, getsourceline)
1214 && !source_finished(fgetline, cookie))
1215 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 && !func_has_ended(real_cookie))))
1217 {
1218 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001219 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001221 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001222 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001223 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001225 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 }
1227
1228 /*
1229 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1230 * ":endtry" in a sourced file or executed function. If the try
1231 * conditional is in its finally clause, ignore anything pending.
1232 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001233 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 */
1235 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001236 {
1237 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1238
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001239 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001240 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001241 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1242 &cstack.cs_looplevel);
1243 }
1244 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 trylevel = initial_trylevel;
1246 }
1247
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001248 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1249 // lack was reported above and the error message is to be converted to an
1250 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001251 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 ? (char_u *)"endfunction" : (char_u *)NULL);
1253
1254 if (trylevel == 0)
1255 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001256 // Just in case did_throw got set but current_exception wasn't.
1257 if (current_exception == NULL)
1258 did_throw = FALSE;
1259
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 /*
1261 * When an exception is being thrown out of the outermost try
1262 * conditional, discard the uncaught exception, disable the conversion
1263 * of interrupts or errors to exceptions, and ensure that no more
1264 * commands are executed.
1265 */
1266 if (did_throw)
1267 {
Bram Moolenaar033135e2021-01-14 21:40:22 +01001268 char *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001269 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
1271 /*
1272 * If the uncaught exception is a user exception, report it as an
1273 * error. If it is an error exception, display the saved error
1274 * message now. For an interrupt exception, do nothing; the
1275 * interrupt message is given elsewhere.
1276 */
1277 switch (current_exception->type)
1278 {
1279 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001280 vim_snprintf((char *)IObuff, IOSIZE,
1281 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 current_exception->value);
Bram Moolenaar033135e2021-01-14 21:40:22 +01001283 p = (char *)vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 break;
1285 case ET_ERROR:
1286 messages = current_exception->messages;
1287 current_exception->messages = NULL;
1288 break;
1289 case ET_INTERRUPT:
1290 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001293 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1294 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001295 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 current_exception->throw_name = NULL;
1297
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001298 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 suppress_errthrow = TRUE;
1300 force_abort = TRUE;
1301
1302 if (messages != NULL)
1303 {
1304 do
1305 {
1306 next = messages->next;
1307 emsg(messages->msg);
1308 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001309 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 vim_free(messages);
1311 messages = next;
1312 }
1313 while (messages != NULL);
1314 }
1315 else if (p != NULL)
1316 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001317 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 vim_free(p);
1319 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001320 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001321 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001322 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 }
1324
1325 /*
1326 * On an interrupt or an aborting error not converted to an exception,
1327 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001328 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 * when force_abort is set and did_emsg unset in case of an interrupt
1330 * from a finally clause after an error.
1331 */
1332 else if (got_int || (did_emsg && force_abort))
1333 suppress_errthrow = TRUE;
1334 }
1335
1336 /*
1337 * The current cstack will be freed when do_cmdline() returns. An uncaught
1338 * exception will have to be rethrown in the previous cstack. If a function
1339 * has just returned or a script file was just finished and the previous
1340 * cstack belongs to the same function or, respectively, script file, it
1341 * will have to be checked for finally clauses to be executed due to the
1342 * ":return" or ":finish". This is done in do_one_cmd().
1343 */
1344 if (did_throw)
1345 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001346 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001348 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 && ex_nesting_level > func_level(real_cookie) + 1))
1350 {
1351 if (!did_throw)
1352 check_cstack = TRUE;
1353 }
1354 else
1355 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001356 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001357 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 --ex_nesting_level;
1359 /*
1360 * Go to debug mode when returning from a function in which we are
1361 * single-stepping.
1362 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001363 if ((getline_equal(fgetline, cookie, getsourceline)
1364 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001366 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 ? (char_u *)_("End of sourced file")
1368 : (char_u *)_("End of function"));
1369 }
1370
1371 /*
1372 * Restore the exception environment (done after returning from the
1373 * debugger).
1374 */
1375 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001376 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377
1378 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001379
1380 // Cleanup if "cs_emsg_silent_list" remains.
1381 if (cstack.cs_emsg_silent_list != NULL)
1382 {
1383 eslist_T *elem, *temp;
1384
1385 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1386 {
1387 temp = elem->next;
1388 vim_free(elem);
1389 }
1390 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001391#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392
1393 /*
1394 * If there was too much output to fit on the command line, ask the user to
1395 * hit return before redrawing the screen. With the ":global" command we do
1396 * this only once after the command is finished.
1397 */
1398 if (did_inc)
1399 {
1400 --RedrawingDisabled;
1401 --no_wait_return;
1402 msg_scroll = FALSE;
1403
1404 /*
1405 * When just finished an ":if"-":else" which was typed, no need to
1406 * wait for hit-return. Also for an error situation.
1407 */
1408 if (retval == FAIL
1409#ifdef FEAT_EVAL
1410 || (did_endif && KeyTyped && !did_emsg)
1411#endif
1412 )
1413 {
1414 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001415 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 }
1417 else if (need_wait_return)
1418 {
1419 /*
1420 * The msg_start() above clears msg_didout. The wait_return we do
1421 * here should not overwrite the command that may be shown before
1422 * doing that.
1423 */
1424 msg_didout |= msg_didout_before_start;
1425 wait_return(FALSE);
1426 }
1427 }
1428
Bram Moolenaar2a942252012-11-28 23:03:07 +01001429#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001430 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001431#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 /*
1433 * Reset if_level, in case a sourced script file contains more ":if" than
1434 * ":endif" (could be ":if x | foo | endif").
1435 */
1436 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001437#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001438
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 --call_depth;
1440 return retval;
1441}
1442
1443#ifdef FEAT_EVAL
1444/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001445 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 */
1447 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001448get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001450 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 wcmd_T *wp;
1452 char_u *line;
1453
1454 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1455 {
1456 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001457 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001459 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001461 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001463 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001464 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 ++cp->current_line;
1466
1467 return line;
1468 }
1469
1470 KeyTyped = FALSE;
1471 ++cp->current_line;
1472 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001473 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 return vim_strsave(wp->line);
1475}
1476
1477/*
1478 * Store a line in "gap" so that a ":while" loop can execute it again.
1479 */
1480 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001481store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482{
1483 if (ga_grow(gap, 1) == FAIL)
1484 return FAIL;
1485 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001486 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 return OK;
1489}
1490
1491/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001492 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 */
1494 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001495free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496{
1497 while (gap->ga_len > 0)
1498 {
1499 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1500 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 }
1502}
1503#endif
1504
1505/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001506 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1507 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001510getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001511 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001512 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001513 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514{
1515#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001516 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001517 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001519 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1520 // function that's originally used to obtain the lines. This may be
1521 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001522 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001523 cp = (struct loop_cookie *)cookie;
1524 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 {
1526 gp = cp->getline;
1527 cp = cp->cookie;
1528 }
1529 return gp == func;
1530#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001531 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532#endif
1533}
1534
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001536 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 * getline function. Otherwise return "cookie".
1538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001540getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001541 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001542 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543{
Bram Moolenaar13505972019-01-24 15:04:48 +01001544#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001545 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001546 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001548 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1549 // cookie that's originally used to obtain the lines. This may be nested
1550 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001551 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001552 cp = (struct loop_cookie *)cookie;
1553 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 {
1555 gp = cp->getline;
1556 cp = cp->cookie;
1557 }
1558 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001559#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001562}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563
Bram Moolenaard5053d02020-06-28 15:51:16 +02001564#if defined(FEAT_EVAL) || defined(PROT)
1565/*
1566 * Get the next line source line without advancing.
1567 */
1568 char_u *
1569getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001570 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001571 void *cookie) // argument for fgetline()
1572{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001573 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001574 struct loop_cookie *cp;
1575 wcmd_T *wp;
1576
1577 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1578 // cookie that's originally used to obtain the lines. This may be nested
1579 // several levels.
1580 gp = fgetline;
1581 cp = (struct loop_cookie *)cookie;
1582 while (gp == get_loop_line)
1583 {
1584 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1585 {
1586 // executing lines a second time, use the stored copy
1587 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1588 return wp->line;
1589 }
1590 gp = cp->getline;
1591 cp = cp->cookie;
1592 }
1593 if (gp == getsourceline)
1594 return source_nextline(cp);
1595 return NULL;
1596}
1597#endif
1598
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001599
1600/*
1601 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1602 * ":bwipeout", etc.
1603 * Returns the buffer number.
1604 */
1605 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001606compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001607{
1608 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001609 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001610 int count = offset;
1611
1612 buf = firstbuf;
1613 while (buf->b_next != NULL && buf->b_fnum < lnum)
1614 buf = buf->b_next;
1615 while (count != 0)
1616 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001617 count += (offset < 0) ? 1 : -1;
1618 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1619 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001620 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001621 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001622 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001623 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001624 while (buf->b_ml.ml_mfp == NULL)
1625 {
1626 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1627 if (nextbuf == NULL)
1628 break;
1629 buf = nextbuf;
1630 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001631 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001632 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001633 if (addr_type == ADDR_LOADED_BUFFERS)
1634 while (buf->b_ml.ml_mfp == NULL)
1635 {
1636 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1637 if (nextbuf == NULL)
1638 break;
1639 buf = nextbuf;
1640 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001641 return buf->b_fnum;
1642}
1643
Bram Moolenaarb7316892019-05-01 18:08:42 +02001644/*
1645 * Return the window number of "win".
1646 * When "win" is NULL return the number of windows.
1647 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001648 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001649current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001650{
1651 win_T *wp;
1652 int nr = 0;
1653
Bram Moolenaar29323592016-07-24 22:04:11 +02001654 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001655 {
1656 ++nr;
1657 if (wp == win)
1658 break;
1659 }
1660 return nr;
1661}
1662
1663 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001664current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001665{
1666 tabpage_T *tp;
1667 int nr = 0;
1668
Bram Moolenaar29323592016-07-24 22:04:11 +02001669 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001670 {
1671 ++nr;
1672 if (tp == tab)
1673 break;
1674 }
1675 return nr;
1676}
1677
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001678 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001679comment_start(char_u *p, int starts_with_colon UNUSED)
1680{
1681#ifdef FEAT_EVAL
1682 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001683 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001684#endif
1685 return *p == '"';
1686}
1687
Bram Moolenaarf240e182014-11-27 18:33:02 +01001688# define CURRENT_WIN_NR current_win_nr(curwin)
1689# define LAST_WIN_NR current_win_nr(NULL)
1690# define CURRENT_TAB_NR current_tab_nr(curtab)
1691# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001692
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693/*
1694 * Execute one Ex command.
1695 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001696 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1697 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 *
1699 * 1. skip comment lines and leading space
1700 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001701 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001702 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001703 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001704 * 6. parse arguments
1705 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001707 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 *
1709 * This function may be called recursively!
1710 */
1711#if (_MSC_VER == 1200)
1712/*
Bram Moolenaared203462004-06-16 11:19:22 +00001713 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001715 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716#endif
1717 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001718do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001719 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001720 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001722 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001724 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001725 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001727 char_u *p;
1728 linenr_T lnum;
1729 long n;
1730 char *errormsg = NULL; // error message
1731 char_u *after_modifier = NULL;
1732 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001733 cmdmod_T save_cmdmod;
1734 int save_reg_executing = reg_executing;
1735 int ni; // set when Not Implemented
1736 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001737 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001738#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001739 int may_have_range;
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001740 int vim9script;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001741 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001742#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001743 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744
Bram Moolenaara80faa82020-04-12 19:37:17 +02001745 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 ea.line1 = 1;
1747 ea.line2 = 1;
1748#ifdef FEAT_EVAL
1749 ++ex_nesting_level;
1750#endif
1751
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001752 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 if (quitmore
1754#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001755 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001756 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001757#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001758 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001759 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 --quitmore;
1761
1762 /*
1763 * Reset browse, confirm, etc.. They are restored when returning, for
1764 * recursive calls.
1765 */
1766 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001768 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001769 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1770 goto doend;
1771
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001772/*
1773 * 1. Skip comment lines and leading white space and colons.
1774 * 2. Handle command modifiers.
1775 */
1776 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001778 ea.cmdlinep = cmdlinep;
1779 ea.getline = fgetline;
1780 ea.cookie = cookie;
1781#ifdef FEAT_EVAL
1782 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001783 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001785 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001786 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001787 apply_cmdmod(&cmdmod);
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001788 vim9script = in_vim9script();
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001789 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790
1791#ifdef FEAT_EVAL
1792 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1793 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1794#else
1795 ea.skip = (if_level > 0);
1796#endif
1797
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001799 * 3. Skip over the range to find the command. Let "p" point to after it.
1800 *
1801 * We need the command to know what kind of range it uses.
1802 */
1803 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001804#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001805 // In Vim9 script a colon is required before the range. This may also be
1806 // after command modifiers.
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001807 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001808 {
1809 may_have_range = FALSE;
1810 for (p = ea.cmd; p >= *cmdlinep; --p)
1811 {
1812 if (*p == ':')
1813 may_have_range = TRUE;
1814 if (p < ea.cmd && !VIM_ISWHITE(*p))
1815 break;
1816 }
1817 }
1818 else
1819 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001820 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001821#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001822 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001823
1824#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001825 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001826 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001827 if (ea.cmd == cmd + 1 && *cmd == '$')
1828 // should be "$VAR = val"
1829 --ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001831 if (ea.cmdidx == CMD_SIZE)
1832 {
1833 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1834
1835 // If a ':' before the range is missing, give a clearer error
1836 // message.
1837 if (ar > ea.cmd)
1838 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001839 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001840 goto doend;
1841 }
1842 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001843 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001844 else
1845#endif
1846 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001847
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001848#ifdef FEAT_EVAL
1849# ifdef FEAT_PROFILE
1850 // Count this line for profiling if skip is TRUE.
1851 if (do_profiling == PROF_YES
1852 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1853 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1854 {
1855 int skip = did_emsg || got_int || did_throw;
1856
1857 if (ea.cmdidx == CMD_catch)
1858 skip = !skip && !(cstack->cs_idx >= 0
1859 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1860 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1861 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1862 skip = skip || !(cstack->cs_idx >= 0
1863 && !(cstack->cs_flags[cstack->cs_idx]
1864 & (CSF_ACTIVE | CSF_TRUE)));
1865 else if (ea.cmdidx == CMD_finally)
1866 skip = FALSE;
1867 else if (ea.cmdidx != CMD_endif
1868 && ea.cmdidx != CMD_endfor
1869 && ea.cmdidx != CMD_endtry
1870 && ea.cmdidx != CMD_endwhile)
1871 skip = ea.skip;
1872
1873 if (!skip)
1874 {
1875 if (getline_equal(fgetline, cookie, get_func_line))
1876 func_line_exec(getline_cookie(fgetline, cookie));
1877 else if (getline_equal(fgetline, cookie, getsourceline))
1878 script_line_exec();
1879 }
1880 }
1881# endif
1882
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001883 // May go to debug mode. If this happens and the ">quit" debug command is
1884 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001885 dbg_check_breakpoint(&ea);
1886 if (!ea.skip && got_int)
1887 {
1888 ea.skip = TRUE;
1889 (void)do_intthrow(cstack);
1890 }
1891#endif
1892
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001893/*
1894 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 *
1896 * where 'addr' is:
1897 *
1898 * % (entire file)
1899 * $ [+-NUM]
1900 * 'x [+-NUM] (where x denotes a currently defined mark)
1901 * . [+-NUM]
1902 * [+-NUM]..
1903 * NUM
1904 *
1905 * The ea.cmd pointer is updated to point to the first character following the
1906 * range spec. If an initial address is found, but no second, the upper bound
1907 * is equal to the lower.
1908 */
1909
Bram Moolenaar25190db2019-05-04 15:05:28 +02001910 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001911 if (!IS_USER_CMDIDX(ea.cmdidx))
1912 {
1913 if (ea.cmdidx != CMD_SIZE)
1914 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1915 else
1916 ea.addr_type = ADDR_LINES;
1917
Bram Moolenaar25190db2019-05-04 15:05:28 +02001918 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001919 if (ea.cmdidx == CMD_wincmd && p != NULL)
1920 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001921#ifdef FEAT_QUICKFIX
1922 // :.cc in quickfix window uses line number
1923 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1924 ea.addr_type = ADDR_OTHER;
1925#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001926 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001927
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001928 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001929#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001930 if (!may_have_range)
1931 ea.line1 = ea.line2 = default_address(&ea);
1932 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001933#endif
1934 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1935 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001938 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 */
1940
1941 /*
1942 * Skip ':' and any white space
1943 */
1944 ea.cmd = skipwhite(ea.cmd);
1945 while (*ea.cmd == ':')
1946 ea.cmd = skipwhite(ea.cmd + 1);
1947
1948 /*
1949 * If we got a line, but no command, then go to the line.
1950 * If we find a '|' or '\n' we set ea.nextcmd.
1951 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001952 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1953 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 {
1955 /*
1956 * strange vi behaviour:
1957 * ":3" jumps to line 3
1958 * ":3|..." prints line 3
1959 * ":|" prints current line
1960 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001961 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001963 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
1965 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001966 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 if ((errormsg = invalid_range(&ea)) == NULL)
1968 {
1969 correct_range(&ea);
1970 ex_print(&ea);
1971 }
1972 }
1973 else if (ea.addr_count != 0)
1974 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001975 if (ea.line2 > curbuf->b_ml.ml_line_count)
1976 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001977 // With '-' in 'cpoptions' a line number past the file is an
1978 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001979 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1980 ea.line2 = -1;
1981 else
1982 ea.line2 = curbuf->b_ml.ml_line_count;
1983 }
1984
1985 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001986 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 else
1988 {
1989 if (ea.line2 == 0)
1990 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 else
1992 curwin->w_cursor.lnum = ea.line2;
1993 beginline(BL_SOL | BL_FIX);
1994 }
1995 }
1996 goto doend;
1997 }
1998
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001999 // If this looks like an undefined user command and there are CmdUndefined
2000 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002001 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2002 && ASCII_ISUPPER(*ea.cmd)
2003 && has_cmdundefined())
2004 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002005 int ret;
2006
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002007 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002008 while (ASCII_ISALNUM(*p))
2009 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002010 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002011 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2012 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002013 // If the autocommands did something and didn't cause an error, try
2014 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002015 p = (ret
2016#ifdef FEAT_EVAL
2017 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002018#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002020 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002021
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 if (p == NULL)
2023 {
2024 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002025 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 goto doend;
2027 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002028 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002029 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2030 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 {
2032 errormsg = uc_fun_cmd();
2033 goto doend;
2034 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002035
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 if (ea.cmdidx == CMD_SIZE)
2037 {
2038 if (!ea.skip)
2039 {
2040 STRCPY(IObuff, _("E492: Not an editor command"));
2041 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002042 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002043 // If the modifier was parsed OK the error must be in the
2044 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002045 if (after_modifier != NULL)
2046 append_command(after_modifier);
2047 else
2048 append_command(*cmdlinep);
2049 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002050 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002051 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 }
2053 goto doend;
2054 }
2055
Bram Moolenaar958636c2014-10-21 20:01:58 +02002056 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2057 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002058#ifdef HAVE_EX_SCRIPT_NI
2059 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2060#endif
2061 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062
2063#ifndef FEAT_EVAL
2064 /*
2065 * When the expression evaluation is disabled, recognize the ":if" and
2066 * ":endif" commands and ignore everything in between it.
2067 */
2068 if (ea.cmdidx == CMD_if)
2069 ++if_level;
2070 if (if_level)
2071 {
2072 if (ea.cmdidx == CMD_endif)
2073 --if_level;
2074 goto doend;
2075 }
2076
2077#endif
2078
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002079 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002080 if (*p == '!' && ea.cmdidx != CMD_substitute
2081 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 {
2083 ++p;
2084 ea.forceit = TRUE;
2085 }
2086 else
2087 ea.forceit = FALSE;
2088
2089/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002090 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002092 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002093 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094
2095 if (!ea.skip)
2096 {
2097#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002098 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002100 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002101 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 goto doend;
2103 }
2104#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002105 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002106 {
2107 errormsg = _("E981: Command not allowed in rvim");
2108 goto doend;
2109 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002110 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002112 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002113 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 goto doend;
2115 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002116
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002117 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002119#ifdef FEAT_CMDWIN
2120 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2121 {
2122 // Command not allowed in the command line window
2123 errormsg = _(e_cmdwin);
2124 goto doend;
2125 }
2126#endif
2127 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2128 {
2129 // Command not allowed when text is locked
2130 errormsg = _(get_text_locked_msg());
2131 goto doend;
2132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002134
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002135 // Disallow editing another buffer when "curbuf_lock" is set.
2136 // Do allow ":checktime" (it is postponed).
2137 // Do allow ":edit" (check for an argument later).
2138 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002139 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002140 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002141 && ea.cmdidx != CMD_edit
2142 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002143 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002144 && curbuf_locked())
2145 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002147 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002149 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002150 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 goto doend;
2152 }
2153 }
2154
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002155 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002157 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 goto doend;
2159 }
2160
2161 /*
2162 * Don't complain about the range if it is not used
2163 * (could happen if line_count is accidentally set to 0).
2164 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002165 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 {
2167 /*
2168 * If the range is backwards, ask for confirmation and, if given, swap
2169 * ea.line1 & ea.line2 so it's forwards again.
2170 * When global command is busy, don't ask, will fail below.
2171 */
2172 if (!global_busy && ea.line1 > ea.line2)
2173 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002174 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002176 if (sourcing || exmode_active)
2177 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002178 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002179 goto doend;
2180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 if (ask_yesno((char_u *)
2182 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002183 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 }
2185 lnum = ea.line1;
2186 ea.line1 = ea.line2;
2187 ea.line2 = lnum;
2188 }
2189 if ((errormsg = invalid_range(&ea)) != NULL)
2190 goto doend;
2191 }
2192
Bram Moolenaarb7316892019-05-01 18:08:42 +02002193 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2194 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 ea.line2 = 1;
2196
2197 correct_range(&ea);
2198
2199#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002200 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002201 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002203 // Put the first line at the start of a closed fold, put the last line
2204 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 (void)hasFolding(ea.line1, &ea.line1, NULL);
2206 (void)hasFolding(ea.line2, NULL, &ea.line2);
2207 }
2208#endif
2209
2210#ifdef FEAT_QUICKFIX
2211 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002212 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 * option here, so things like % get expanded.
2214 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002215 p = replace_makeprg(&ea, p, cmdlinep);
2216 if (p == NULL)
2217 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218#endif
2219
2220 /*
2221 * Skip to start of argument.
2222 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2223 */
2224 if (ea.cmdidx == CMD_bang)
2225 ea.arg = p;
2226 else
2227 ea.arg = skipwhite(p);
2228
Bram Moolenaar379fb762018-08-30 15:58:28 +02002229 // ":file" cannot be run with an argument when "curbuf_lock" is set
2230 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2231 goto doend;
2232
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 /*
2234 * Check for "++opt=val" argument.
2235 * Must be first, allow ":w ++enc=utf8 !cmd"
2236 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002237 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2239 if (getargopt(&ea) == FAIL && !ni)
2240 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002241 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 goto doend;
2243 }
2244
2245 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2246 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002247 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002249 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002251 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 goto doend;
2253 }
2254 ea.arg = skipwhite(ea.arg + 1);
2255 ea.append = TRUE;
2256 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002257 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 {
2259 ++ea.arg;
2260 ea.usefilter = TRUE;
2261 }
2262 }
2263
2264 if (ea.cmdidx == CMD_read)
2265 {
2266 if (ea.forceit)
2267 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002268 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 ea.forceit = FALSE;
2270 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002271 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
2273 ++ea.arg;
2274 ea.usefilter = TRUE;
2275 }
2276 }
2277
2278 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2279 {
2280 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002281 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 {
2283 ++ea.arg;
2284 ++ea.amount;
2285 }
2286 ea.arg = skipwhite(ea.arg);
2287 }
2288
2289 /*
2290 * Check for "+command" argument, before checking for next command.
2291 * Don't do this for ":read !cmd" and ":write !cmd".
2292 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002293 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2295
2296 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002297 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 * Don't do this for ":read !cmd" and ":write !cmd".
2299 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002300 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 separate_nextcmd(&ea);
2302
2303 /*
2304 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002305 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2306 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002308 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002309 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002310 || ea.cmdidx == CMD_global
2311 || ea.cmdidx == CMD_vglobal
2312 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
2314 for (p = ea.arg; *p; ++p)
2315 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002316 // Remove one backslash before a newline, so that it's possible to
2317 // pass a newline to the shell and also a newline that is preceded
2318 // with a backslash. This makes it impossible to end a shell
2319 // command in a backslash, but that doesn't appear useful.
2320 // Halving the number of backslashes is incompatible with previous
2321 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002323 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 else if (*p == '\n')
2325 {
2326 ea.nextcmd = p + 1;
2327 *p = NUL;
2328 break;
2329 }
2330 }
2331 }
2332
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002333 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002334 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002336 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002337 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002339 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002340 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002341 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002343#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002344 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002345 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002347 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002348 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 }
2350#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002351 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2352 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002353 {
2354 ea.regname = *ea.arg++;
2355#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002356 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002357 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2358 {
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002359 set_expr_line(vim_strsave(ea.arg), &ea);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002360 ea.arg += STRLEN(ea.arg);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002361 did_set_expr_line = TRUE;
Bram Moolenaar85de2062011-05-05 14:26:41 +02002362 }
2363#endif
2364 ea.arg = skipwhite(ea.arg);
2365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 }
2367
2368 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002369 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 * count, it's a buffer name.
2371 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002372 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2373 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002374 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 {
2376 n = getdigits(&ea.arg);
2377 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002378 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002380 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 goto doend;
2382 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002383 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 {
2385 ea.line2 = n;
2386 if (ea.addr_count == 0)
2387 ea.addr_count = 1;
2388 }
2389 else
2390 {
2391 ea.line1 = ea.line2;
2392 ea.line2 += n - 1;
2393 ++ea.addr_count;
2394 /*
2395 * Be vi compatible: no error message for out of range.
2396 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002397 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 ea.line2 = curbuf->b_ml.ml_line_count;
2399 }
2400 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002401
2402 /*
2403 * Check for flags: 'l', 'p' and '#'.
2404 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002405 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002406 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002407 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2408 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002410 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002411 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 goto doend;
2413 }
2414
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002415 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002417 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 goto doend;
2419 }
2420
2421#ifdef FEAT_EVAL
2422 /*
2423 * Skip the command when it's not going to be executed.
2424 * The commands like :if, :endif, etc. always need to be executed.
2425 * Also make an exception for commands that handle a trailing command
2426 * themselves.
2427 */
2428 if (ea.skip)
2429 {
2430 switch (ea.cmdidx)
2431 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002432 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 case CMD_while:
2434 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002435 case CMD_for:
2436 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 case CMD_if:
2438 case CMD_elseif:
2439 case CMD_else:
2440 case CMD_endif:
2441 case CMD_try:
2442 case CMD_catch:
2443 case CMD_finally:
2444 case CMD_endtry:
2445 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002446 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 break;
2448
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002449 // Commands that handle '|' themselves. Check: A command should
2450 // either have the EX_TRLBAR flag, appear in this list or appear in
2451 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 case CMD_aboveleft:
2453 case CMD_and:
2454 case CMD_belowright:
2455 case CMD_botright:
2456 case CMD_browse:
2457 case CMD_call:
2458 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002459 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 case CMD_delfunction:
2461 case CMD_djump:
2462 case CMD_dlist:
2463 case CMD_dsearch:
2464 case CMD_dsplit:
2465 case CMD_echo:
2466 case CMD_echoerr:
2467 case CMD_echomsg:
2468 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002469 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002471 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002472 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 case CMD_help:
2474 case CMD_hide:
2475 case CMD_ijump:
2476 case CMD_ilist:
2477 case CMD_isearch:
2478 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002479 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 case CMD_keepjumps:
2481 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002482 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 case CMD_leftabove:
2484 case CMD_let:
2485 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002486 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002487 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002489 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002490 case CMD_noautocmd:
2491 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 case CMD_perl:
2493 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002494 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002495 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002496 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 case CMD_return:
2498 case CMD_rightbelow:
2499 case CMD_ruby:
2500 case CMD_silent:
2501 case CMD_smagic:
2502 case CMD_snomagic:
2503 case CMD_substitute:
2504 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002505 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 case CMD_tcl:
2507 case CMD_throw:
2508 case CMD_tilde:
2509 case CMD_topleft:
2510 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002511 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002512 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 case CMD_verbose:
2514 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002515 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 break;
2517
2518 default: goto doend;
2519 }
2520 }
2521#endif
2522
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002523 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 {
2525 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2526 goto doend;
2527 }
2528
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 /*
2530 * Accept buffer name. Cannot be used at the same time with a buffer
2531 * number. Don't do this for a user command.
2532 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002533 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002534 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 {
2536 /*
2537 * :bdelete, :bwipeout and :bunload take several arguments, separated
2538 * by spaces: find next space (skipping over escaped characters).
2539 * The others take one argument: ignore trailing spaces.
2540 */
2541 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2542 || ea.cmdidx == CMD_bunload)
2543 p = skiptowhite_esc(ea.arg);
2544 else
2545 {
2546 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002547 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 --p;
2549 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002550 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002551 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002552 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 goto doend;
2554 ea.addr_count = 1;
2555 ea.arg = skipwhite(p);
2556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002558 // The :try command saves the emsg_silent flag, reset it here when
2559 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002560 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002561 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002562 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002563 if (emsg_silent < 0)
2564 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002565 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002566 }
2567
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002569 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571
Bram Moolenaar958636c2014-10-21 20:01:58 +02002572 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 {
2574 /*
2575 * Execute a user-defined command.
2576 */
2577 do_ucmd(&ea);
2578 }
2579 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 {
2581 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002582 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 */
2584 ea.errmsg = NULL;
2585 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2586 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002587 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 }
2589
2590#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002592 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2593 && current_sctx.sc_sid > 0)
Bram Moolenaar2b327002020-12-26 15:39:31 +01002594 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 /*
2597 * If the command just executed called do_cmdline(), any throw or ":return"
2598 * or ":finish" encountered there must also check the cstack of the still
2599 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2600 * exception, or reanimate a returned function or finished script file and
2601 * return or finish it again.
2602 */
2603 if (need_rethrow)
2604 do_throw(cstack);
2605 else if (check_cstack)
2606 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002607 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002609 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 && current_func_returned())
2611 do_return(&ea, TRUE, FALSE, NULL);
2612 }
2613 need_rethrow = check_cstack = FALSE;
2614#endif
2615
2616doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002617 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002618 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002620 curwin->w_cursor.col = 0;
2621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622
2623 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2624 {
2625 if (sourcing)
2626 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002627 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 {
2629 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002630 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002632 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 }
2634 emsg(errormsg);
2635 }
2636#ifdef FEAT_EVAL
2637 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002638 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2639 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002640
2641 if (did_set_expr_line)
2642 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643#endif
2644
Bram Moolenaare1004402020-10-24 20:49:43 +02002645 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002647 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002649 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 ea.nextcmd = NULL;
2651
2652#ifdef FEAT_EVAL
2653 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002654 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655#endif
2656
2657 return ea.nextcmd;
2658}
2659#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002660 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661#endif
2662
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002663static char ex_error_buf[MSG_BUF_LEN];
2664
2665/*
2666 * Return an error message with argument included.
2667 * Uses a static buffer, only the last error will be kept.
2668 * "msg" will be translated, caller should use N_().
2669 */
2670 char *
2671ex_errmsg(char *msg, char_u *arg)
2672{
2673 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2674 return ex_error_buf;
2675}
2676
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002678 * Parse and skip over command modifiers:
2679 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002680 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002681 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002682 * When "skip_only" is TRUE the global variables are not changed, except for
2683 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002684 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2685 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002686 * Call apply_cmdmod() to get the side effects of the modifiers:
2687 * - Increment "sandbox" for ":sandbox"
2688 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002689 * - set msg_silent for ":silent"
2690 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002691 * Return FAIL when the command is not to be executed.
2692 * May set "errormsg" to an error message.
2693 */
2694 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002695parse_command_modifiers(
2696 exarg_T *eap,
2697 char **errormsg,
2698 cmdmod_T *cmod,
2699 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002700{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002701 char_u *p;
2702 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002703
Bram Moolenaare1004402020-10-24 20:49:43 +02002704 CLEAR_POINTER(cmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002705
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002706 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002707 for (;;)
2708 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002709 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002710 {
2711 if (*eap->cmd == ':')
2712 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002713 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002714 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002715
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002716 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002717 if (*eap->cmd == NUL && exmode_active
2718 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2719 || getline_equal(eap->getline, eap->cookie, getexline))
2720 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2721 {
2722 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002723 if (!skip_only)
2724 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002725 }
2726
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002727 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002728 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002729 return FAIL;
2730 if (*eap->cmd == NUL)
2731 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002732 if (!skip_only)
2733 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002734 return FAIL;
2735 }
2736
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002737 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002738
2739 // In Vim9 script a variable can shadow a command modifier:
2740 // verbose = 123
2741 // verbose += 123
2742 // silent! verbose = func()
2743 // verbose.member = 2
2744 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002745 // But not:
2746 // verbose [a, b] = list
Bram Moolenaar17126b12021-01-07 22:03:02 +01002747 if (in_vim9script())
2748 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002749 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002750
2751 for (s = p; ASCII_ISALPHA(*s); ++s)
2752 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002753 n = skipwhite(s);
2754 if (vim_strchr((char_u *)".=", *n) != NULL
2755 || *s == '['
2756 || (*n != NUL && n[1] == '='))
Bram Moolenaar17126b12021-01-07 22:03:02 +01002757 break;
2758 }
2759
Bram Moolenaareffed932018-08-14 13:38:17 +02002760 switch (*p)
2761 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002762 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002763 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2764 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002765 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002766 continue;
2767
2768 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2769 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002770 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002771 continue;
2772 }
2773 if (checkforcmd(&eap->cmd, "browse", 3))
2774 {
2775#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002776 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002777#endif
2778 continue;
2779 }
2780 if (!checkforcmd(&eap->cmd, "botright", 2))
2781 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002782 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002783 continue;
2784
2785 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2786 break;
2787#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002788 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002789#endif
2790 continue;
2791
2792 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2793 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002794 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002795 continue;
2796 }
2797 if (checkforcmd(&eap->cmd, "keepalt", 5))
2798 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002799 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002800 continue;
2801 }
2802 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2803 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002804 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002805 continue;
2806 }
2807 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2808 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002809 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002810 continue;
2811
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002812 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002813 {
2814 char_u *reg_pat;
2815
2816 if (!checkforcmd(&p, "filter", 4)
2817 || *p == NUL || ends_excmd(*p))
2818 break;
2819 if (*p == '!')
2820 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002821 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002822 p = skipwhite(p + 1);
2823 if (*p == NUL || ends_excmd(*p))
2824 break;
2825 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002826#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002827 // Avoid that "filter(arg)" is recognized.
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +01002828 if (in_vim9script() && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002829 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002830#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002831 if (skip_only)
2832 p = skip_vimgrep_pat(p, NULL, NULL);
2833 else
2834 // NOTE: This puts a NUL after the pattern.
2835 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002836 if (p == NULL || *p == NUL)
2837 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002838 if (!skip_only)
2839 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002840 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002841 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002842 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002843 break;
2844 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002845 eap->cmd = p;
2846 continue;
2847 }
2848
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002849 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002850 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2851 || *p == NUL || ends_excmd(*p))
2852 break;
2853 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002854 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002855 continue;
2856
2857 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2858 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002859 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002860 continue;
2861 }
2862
2863 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2864 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002865 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002866 continue;
2867
2868 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2869 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002870 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002871 continue;
2872 }
2873 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2874 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002875 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002876 continue;
2877
2878 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2879 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002880 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002881 continue;
2882
2883 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2884 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002885 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02002886 continue;
2887 }
2888 if (!checkforcmd(&eap->cmd, "silent", 3))
2889 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002890 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002891 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2892 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002893 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002894 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02002895 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002896 }
2897 continue;
2898
2899 case 't': if (checkforcmd(&p, "tab", 3))
2900 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002901 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002902 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002903 long tabnr = get_address(eap, &eap->cmd,
2904 ADDR_TABS, eap->skip,
2905 skip_only, FALSE, 1);
2906 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02002907 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01002908 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002909 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002910 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2911 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002912 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002913 return FAIL;
2914 }
Bram Moolenaare1004402020-10-24 20:49:43 +02002915 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002916 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002917 }
2918 eap->cmd = p;
2919 continue;
2920 }
2921 if (!checkforcmd(&eap->cmd, "topleft", 2))
2922 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002923 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02002924 continue;
2925
2926 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2927 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002928 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002929 continue;
2930
2931 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2932 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002933 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002934 continue;
2935 }
Bram Moolenaar39f3b142021-02-14 12:57:36 +01002936 if (checkforcmd(&eap->cmd, "vim9cmd", 4))
2937 {
2938 if (ends_excmd2(p, eap->cmd))
2939 {
2940 *errormsg =
2941 _(e_vim9cmd_must_be_followed_by_command);
2942 return FAIL;
2943 }
2944 cmod->cmod_flags |= CMOD_VIM9CMD;
2945 continue;
2946 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002947 if (!checkforcmd(&p, "verbose", 4))
2948 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002949 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02002950 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002951 else
Bram Moolenaare1004402020-10-24 20:49:43 +02002952 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002953 eap->cmd = p;
2954 continue;
2955 }
2956 break;
2957 }
2958
2959 return OK;
2960}
2961
2962/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002963 * Apply the command modifiers. Saves current state in "cmdmod", call
2964 * undo_cmdmod() later.
2965 */
2966 void
2967apply_cmdmod(cmdmod_T *cmod)
2968{
2969#ifdef HAVE_SANDBOX
2970 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
2971 {
2972 ++sandbox;
2973 cmod->cmod_did_sandbox = TRUE;
2974 }
2975#endif
2976 if (cmod->cmod_verbose > 0)
2977 {
2978 if (cmod->cmod_verbose_save == 0)
2979 cmod->cmod_verbose_save = p_verbose + 1;
2980 p_verbose = cmod->cmod_verbose;
2981 }
2982
2983 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
2984 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02002985 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002986 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02002987 cmod->cmod_save_msg_scroll = msg_scroll;
2988 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002989 if (cmod->cmod_flags & CMOD_SILENT)
2990 ++msg_silent;
2991 if (cmod->cmod_flags & CMOD_UNSILENT)
2992 msg_silent = 0;
2993
2994 if (cmod->cmod_flags & CMOD_ERRSILENT)
2995 {
2996 ++emsg_silent;
2997 ++cmod->cmod_did_esilent;
2998 }
2999
Bram Moolenaare1004402020-10-24 20:49:43 +02003000 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003001 {
3002 // Set 'eventignore' to "all".
3003 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003004 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003005 set_string_option_direct((char_u *)"ei", -1,
3006 (char_u *)"all", OPT_FREE, SID_NONE);
3007 }
3008}
3009
3010/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003011 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003012 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003013 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003014undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003015{
Bram Moolenaare1004402020-10-24 20:49:43 +02003016 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003017 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003018 p_verbose = cmod->cmod_verbose_save - 1;
3019 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003020 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003021
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003022#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003023 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003024 {
3025 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003026 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003027 }
3028#endif
3029
Bram Moolenaare1004402020-10-24 20:49:43 +02003030 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003031 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003032 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003033 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3034 OPT_FREE, SID_NONE);
3035 free_string_option(cmod->cmod_save_ei);
3036 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003037 }
3038
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003039 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003040
Bram Moolenaare1004402020-10-24 20:49:43 +02003041 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003042 {
3043 // messages could be enabled for a serious error, need to check if the
3044 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003045 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3046 msg_silent = cmod->cmod_save_msg_silent - 1;
3047 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003048 if (emsg_silent < 0)
3049 emsg_silent = 0;
3050 // Restore msg_scroll, it's set by file I/O commands, even when no
3051 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003052 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003053
3054 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3055 // somewhere in the line. Put it back in the first column.
3056 if (redirecting())
3057 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003058
Bram Moolenaare1004402020-10-24 20:49:43 +02003059 cmod->cmod_save_msg_silent = 0;
3060 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003061 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003062}
3063
3064/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003065 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003066 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003067 * Return FAIL and set "errormsg" or return OK.
3068 */
3069 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003070parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003071{
3072 int address_count = 1;
3073 linenr_T lnum;
3074
3075 // Repeat for all ',' or ';' separated addresses.
3076 for (;;)
3077 {
3078 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003079 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003080 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003081 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003082 eap->addr_count == 0, address_count++);
3083 if (eap->cmd == NULL) // error detected
3084 return FAIL;
3085 if (lnum == MAXLNUM)
3086 {
3087 if (*eap->cmd == '%') // '%' - all lines
3088 {
3089 ++eap->cmd;
3090 switch (eap->addr_type)
3091 {
3092 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003093 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003094 eap->line1 = 1;
3095 eap->line2 = curbuf->b_ml.ml_line_count;
3096 break;
3097 case ADDR_LOADED_BUFFERS:
3098 {
3099 buf_T *buf = firstbuf;
3100
3101 while (buf->b_next != NULL
3102 && buf->b_ml.ml_mfp == NULL)
3103 buf = buf->b_next;
3104 eap->line1 = buf->b_fnum;
3105 buf = lastbuf;
3106 while (buf->b_prev != NULL
3107 && buf->b_ml.ml_mfp == NULL)
3108 buf = buf->b_prev;
3109 eap->line2 = buf->b_fnum;
3110 break;
3111 }
3112 case ADDR_BUFFERS:
3113 eap->line1 = firstbuf->b_fnum;
3114 eap->line2 = lastbuf->b_fnum;
3115 break;
3116 case ADDR_WINDOWS:
3117 case ADDR_TABS:
3118 if (IS_USER_CMDIDX(eap->cmdidx))
3119 {
3120 eap->line1 = 1;
3121 eap->line2 = eap->addr_type == ADDR_WINDOWS
3122 ? LAST_WIN_NR : LAST_TAB_NR;
3123 }
3124 else
3125 {
3126 // there is no Vim command which uses '%' and
3127 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003128 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003129 return FAIL;
3130 }
3131 break;
3132 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003133 case ADDR_UNSIGNED:
3134 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003135 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003136 return FAIL;
3137 case ADDR_ARGUMENTS:
3138 if (ARGCOUNT == 0)
3139 eap->line1 = eap->line2 = 0;
3140 else
3141 {
3142 eap->line1 = 1;
3143 eap->line2 = ARGCOUNT;
3144 }
3145 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003146 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003147#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003148 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003149 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003150 if (eap->line2 == 0)
3151 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003152#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003153 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003154 case ADDR_NONE:
3155 // Will give an error later if a range is found.
3156 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003157 }
3158 ++eap->addr_count;
3159 }
3160 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3161 {
3162 pos_T *fp;
3163
3164 // '*' - visual area
3165 if (eap->addr_type != ADDR_LINES)
3166 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003167 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003168 return FAIL;
3169 }
3170
3171 ++eap->cmd;
3172 if (!eap->skip)
3173 {
3174 fp = getmark('<', FALSE);
3175 if (check_mark(fp) == FAIL)
3176 return FAIL;
3177 eap->line1 = fp->lnum;
3178 fp = getmark('>', FALSE);
3179 if (check_mark(fp) == FAIL)
3180 return FAIL;
3181 eap->line2 = fp->lnum;
3182 ++eap->addr_count;
3183 }
3184 }
3185 }
3186 else
3187 eap->line2 = lnum;
3188 eap->addr_count++;
3189
3190 if (*eap->cmd == ';')
3191 {
3192 if (!eap->skip)
3193 {
3194 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003195 // Don't leave the cursor on an illegal line or column, but do
3196 // accept zero as address, so 0;/PATTERN/ works correctly.
3197 if (eap->line2 > 0)
3198 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003199 }
3200 }
3201 else if (*eap->cmd != ',')
3202 break;
3203 ++eap->cmd;
3204 }
3205
3206 // One address given: set start and end lines.
3207 if (eap->addr_count == 1)
3208 {
3209 eap->line1 = eap->line2;
3210 // ... but only implicit: really no address given
3211 if (lnum == MAXLNUM)
3212 eap->addr_count = 0;
3213 }
3214 return OK;
3215}
3216
3217/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003218 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 * If there is a match advance "pp" to the argument and return TRUE.
3220 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003221 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003222checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003223 char_u **pp, // start of command
3224 char *cmd, // name of command
3225 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226{
3227 int i;
3228
3229 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003230 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 break;
Bram Moolenaar7a66a172020-10-16 19:56:12 +02003232 if (i >= len && !isalpha((*pp)[i]) && (*pp)[i] != '_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 {
3234 *pp = skipwhite(*pp + i);
3235 return TRUE;
3236 }
3237 return FALSE;
3238}
3239
3240/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003241 * Append "cmd" to the error message in IObuff.
3242 * Takes care of limiting the length and handling 0xa0, which would be
3243 * invisible otherwise.
3244 */
3245 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003246append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003247{
3248 char_u *s = cmd;
3249 char_u *d;
3250
3251 STRCAT(IObuff, ": ");
3252 d = IObuff + STRLEN(IObuff);
3253 while (*s != NUL && d - IObuff < IOSIZE - 7)
3254 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003255 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003256 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003257 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003258 STRCPY(d, "<a0>");
3259 d += 4;
3260 }
3261 else
3262 MB_COPY_CHAR(s, d);
3263 }
3264 *d = NUL;
3265}
3266
3267/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003268 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3269 * the name. Otherwise just return "start".
3270 */
3271 char_u *
3272skip_option_env_lead(char_u *start)
3273{
3274 char_u *name = start;
3275
3276 if (*start == '&')
3277 {
3278 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3279 name += 3;
3280 else
3281 name += 1;
3282 }
3283 else if (*start == '$')
3284 name += 1;
3285 return name;
3286}
3287
3288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003290 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003291 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003292 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 *
3294 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3295 * assignment without "let". Sets eap->cmdidx to the command while returning
3296 * "eap->cmd".
3297 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 * Returns NULL for an ambiguous user command.
3299 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 char_u *
3301find_ex_command(
3302 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003303 int *full UNUSED,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003304 int (*lookup)(char_u *, size_t, void *, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003305 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306{
3307 int len;
3308 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003309 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311#ifdef FEAT_EVAL
3312 /*
3313 * Recognize a Vim9 script function/method call and assignment:
3314 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3315 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003316 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003317 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003319 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320
Bram Moolenaar83144542020-08-02 20:40:43 +02003321 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003322 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003324 int oplen;
3325 int heredoc;
3326 char_u *swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003328 if (
3329 // "(..." is an expression.
3330 // "funcname(" is always a function call.
3331 *p == '('
3332 || (p == eap->cmd
3333 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003334 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003335 *eap->cmd == '{'
3336 // "'string'->func()" is an expression.
3337 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003338 // '"string"->func()' is an expression.
3339 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003340 // "g:varname" is an expression.
3341 || eap->cmd[1] == ':'
3342 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003343 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003344 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003345 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003346 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3347 {
3348 // "{" by itself is the start of a block.
3349 eap->cmdidx = CMD_block;
3350 return eap->cmd + 1;
3351 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003352 eap->cmdidx = CMD_eval;
3353 return eap->cmd;
3354 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003355
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003356 if (p != eap->cmd && (
3357 // "varname[]" is an expression.
3358 *p == '['
3359 // "varname.key" is an expression.
3360 || (*p == '.' && ASCII_ISALPHA(p[1]))))
3361 {
3362 char_u *after = p;
3363
3364 // When followed by "=" or "+=" then it is an assignment.
3365 ++emsg_silent;
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01003366 if (*after == '.')
3367 after = skipwhite(after + 1);
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003368 if (skip_expr(&after, NULL) == OK)
3369 after = skipwhite(after);
3370 else
3371 after = (char_u *)"";
3372 if (*after == '=' || (*after != NUL && after[1] == '=')
3373 || (after[0] == '.' && after[1] == '.'
3374 && after[2] == '='))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003375 eap->cmdidx = CMD_var;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003376 else
3377 eap->cmdidx = CMD_eval;
3378 --emsg_silent;
3379 return eap->cmd;
3380 }
3381
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003382 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3383 // an assignment.
3384 // If there is no line break inside the "[...]" then "p" is
3385 // advanced to after the "]" by to_name_const_end(): check if a "="
3386 // follows.
3387 // If "[...]" has a line break "p" still points at the "[" and it
3388 // can't be an assignment.
3389 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003390 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003391 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003392 if (p == eap->cmd && *p == '[')
3393 {
3394 int count = 0;
3395 int semicolon = FALSE;
3396
3397 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3398 }
3399 if (p == NULL || p == eap->cmd || *skipwhite(p) != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003400 {
3401 eap->cmdidx = CMD_eval;
3402 return eap->cmd;
3403 }
3404 if (p > eap->cmd && *skipwhite(p) == '=')
3405 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003406 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003407 return eap->cmd;
3408 }
3409 }
3410
3411 // Recognize an assignment if we recognize the variable name:
3412 // "g:var = expr"
3413 // "var = expr" where "var" is a local var name.
Bram Moolenaar83144542020-08-02 20:40:43 +02003414 if (*eap->cmd == '@')
3415 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003416 oplen = assignment_len(skipwhite(p), &heredoc);
3417 if (oplen > 0)
3418 {
3419 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3420 || *eap->cmd == '&'
3421 || *eap->cmd == '$'
3422 || *eap->cmd == '@'
Bram Moolenaar709664c2020-12-12 14:33:41 +01003423 || lookup(eap->cmd, p - eap->cmd, NULL, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003424 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003425 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003426 return eap->cmd;
3427 }
3428 }
3429
3430 // Recognize using a type for a w:, b:, t: or g: variable:
3431 // "w:varname: number = 123".
3432 if (eap->cmd[1] == ':' && *p == ':')
3433 {
3434 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003435 return eap->cmd;
3436 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003437 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003438
3439 // If it is an ID it might be a variable with an operator on the next
3440 // line, if the variable exists it can't be an Ex command.
3441 if (p > eap->cmd && ends_excmd(*skipwhite(p))
3442 && (lookup(eap->cmd, p - eap->cmd, NULL, cctx) == OK
3443 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3444 {
3445 eap->cmdidx = CMD_eval;
3446 return eap->cmd;
3447 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448 }
3449#endif
3450
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 /*
3452 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003453 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 * - the 'k' command can directly be followed by any character.
3455 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003456 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003458 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 */
3460 p = eap->cmd;
3461 if (*p == 'k')
3462 {
3463 eap->cmdidx = CMD_k;
3464 ++p;
3465 }
3466 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003467 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3468 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 || p[1] == 'g'
3470 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3471 || p[1] == 'I'
3472 || (p[1] == 'r' && p[2] != 'e')))
3473 {
3474 eap->cmdidx = CMD_substitute;
3475 ++p;
3476 }
3477 else
3478 {
3479 while (ASCII_ISALPHA(*p))
3480 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003481 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003482 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003483 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003484 while (ASCII_ISALNUM(*p))
3485 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486 }
3487 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3488 {
3489 // include "9" for "vim9script"
3490 ++p;
3491 while (ASCII_ISALPHA(*p))
3492 ++p;
3493 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003494
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003495 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003496 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 ++p;
3498 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003499 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3500 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003501 // Check for ":dl", ":dell", etc. to ":deletel": that's
3502 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003503 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003504 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003505 break;
3506 if (i == len - 1)
3507 {
3508 --len;
3509 if (p[-1] == 'l')
3510 eap->flags |= EXFLAG_LIST;
3511 else
3512 eap->flags |= EXFLAG_PRINT;
3513 }
3514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003516 if (ASCII_ISLOWER(eap->cmd[0]))
3517 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003518 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003519 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003520
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003521 if (command_count != (int)CMD_SIZE)
3522 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003523 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003524 getout(1);
3525 }
3526
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003527 // Use a precomputed index for fast look-up in cmdnames[]
3528 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003529 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3530 if (ASCII_ISLOWER(c2))
3531 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3532 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003533 else if (ASCII_ISUPPER(eap->cmd[0]))
3534 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003536 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537
3538 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3539 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3540 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3541 (size_t)len) == 0)
3542 {
3543#ifdef FEAT_EVAL
3544 if (full != NULL
3545 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3546 *full = TRUE;
3547#endif
3548 break;
3549 }
3550
Bram Moolenaar95388e32020-11-20 21:07:00 +01003551 // Not not recognize ":*" as the star command unless '*' is in
3552 // 'cpoptions'.
3553 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3554 p = eap->cmd;
3555
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003556 // Look for a user defined command as a last resort. Let ":Print" be
3557 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003558 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3559 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003561 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 while (ASCII_ISALNUM(*p))
3563 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003564 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003566 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 eap->cmdidx = CMD_SIZE;
3568 }
3569
Bram Moolenaar373863e2020-09-26 17:20:53 +02003570 // ":fina" means ":finally" for backwards compatibility.
3571 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3572 eap->cmdidx = CMD_finally;
3573
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003574#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003575 if (eap->cmdidx < CMD_SIZE
3576 && in_vim9script()
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003577 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003578 && (eap->cmdidx < 0 ||
3579 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003580 {
3581 semsg(_(e_command_not_followed_by_white_space_str), eap->cmd);
3582 eap->cmdidx = CMD_SIZE;
3583 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003584#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003585
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 return p;
3587}
3588
3589#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003590static struct cmdmod
3591{
3592 char *name;
3593 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003594 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003595} cmdmods[] = {
3596 {"aboveleft", 3, FALSE},
3597 {"belowright", 3, FALSE},
3598 {"botright", 2, FALSE},
3599 {"browse", 3, FALSE},
3600 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003601 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003602 {"hide", 3, FALSE},
3603 {"keepalt", 5, FALSE},
3604 {"keepjumps", 5, FALSE},
3605 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003606 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003607 {"leftabove", 5, FALSE},
3608 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003609 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003610 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003611 {"rightbelow", 6, FALSE},
3612 {"sandbox", 3, FALSE},
3613 {"silent", 3, FALSE},
3614 {"tab", 3, TRUE},
3615 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003616 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003617 {"verbose", 4, TRUE},
3618 {"vertical", 4, FALSE},
3619};
3620
3621/*
3622 * Return length of a command modifier (including optional count).
3623 * Return zero when it's not a modifier.
3624 */
3625 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003626modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003627{
3628 int i, j;
3629 char_u *p = cmd;
3630
3631 if (VIM_ISDIGIT(*cmd))
3632 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003633 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003634 {
3635 for (j = 0; p[j] != NUL; ++j)
3636 if (p[j] != cmdmods[i].name[j])
3637 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003638 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003639 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003640 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003641 }
3642 return 0;
3643}
3644
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645/*
3646 * Return > 0 if an Ex command "name" exists.
3647 * Return 2 if there is an exact match.
3648 * Return 3 if there is an ambiguous match.
3649 */
3650 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003651cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652{
3653 exarg_T ea;
3654 int full = FALSE;
3655 int i;
3656 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003657 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003659 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003660 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 {
3662 for (j = 0; name[j] != NUL; ++j)
3663 if (name[j] != cmdmods[i].name[j])
3664 break;
3665 if (name[j] == NUL && j >= cmdmods[i].minlen)
3666 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3667 }
3668
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003669 // Check built-in commands and user defined commands.
3670 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003671 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003674 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003676 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3677 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003678 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003679 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3681}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003682
3683/*
3684 * "fullcommand" function
3685 */
3686 void
3687f_fullcommand(typval_T *argvars, typval_T *rettv)
3688{
3689 exarg_T ea;
3690 char_u *name = argvars[0].vval.v_string;
3691 char_u *p;
3692
3693 while (name[0] != NUL && name[0] == ':')
3694 name++;
3695 name = skip_range(name, TRUE, NULL);
3696
3697 rettv->v_type = VAR_STRING;
3698
3699 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
3700 ea.cmdidx = (cmdidx_T)0;
3701 p = find_ex_command(&ea, NULL, NULL, NULL);
3702 if (p == NULL || ea.cmdidx == CMD_SIZE)
3703 return;
3704
3705 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
3706 ? get_user_commands(NULL, ea.useridx)
3707 : cmdnames[ea.cmdidx].cmd_name);
3708}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709#endif
3710
Bram Moolenaard0190392019-08-23 21:17:35 +02003711 cmdidx_T
3712excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713{
Bram Moolenaard0190392019-08-23 21:17:35 +02003714 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715
Bram Moolenaard0190392019-08-23 21:17:35 +02003716 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3717 idx = (cmdidx_T)((int)idx + 1))
3718 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 break;
3720
Bram Moolenaard0190392019-08-23 21:17:35 +02003721 return idx;
3722}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723
Bram Moolenaard0190392019-08-23 21:17:35 +02003724 long
3725excmd_get_argt(cmdidx_T idx)
3726{
3727 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728}
3729
3730/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003731 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 *
3733 * Backslashed delimiters after / or ? will be skipped, and commands will
3734 * not be expanded between /'s and ?'s or after "'".
3735 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003736 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 * Returns the "cmd" pointer advanced to beyond the range.
3738 */
3739 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003740skip_range(
3741 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003742 int skip_star, // skip "*" used for Visual range
3743 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003745 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003747 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003749 if (*cmd == '\\')
3750 {
3751 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3752 ++cmd;
3753 else
3754 break;
3755 }
3756 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 {
3758 if (*++cmd == NUL && ctx != NULL)
3759 *ctx = EXPAND_NOTHING;
3760 }
3761 else if (*cmd == '/' || *cmd == '?')
3762 {
3763 delim = *cmd++;
3764 while (*cmd != NUL && *cmd != delim)
3765 if (*cmd++ == '\\' && *cmd != NUL)
3766 ++cmd;
3767 if (*cmd == NUL && ctx != NULL)
3768 *ctx = EXPAND_NOTHING;
3769 }
3770 if (*cmd != NUL)
3771 ++cmd;
3772 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003773
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003774 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003775 while (*cmd == ':')
3776 cmd = skipwhite(cmd + 1);
3777
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003778 // Skip "*" used for Visual range.
3779 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3780 cmd = skipwhite(cmd + 1);
3781
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 return cmd;
3783}
3784
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003785 static void
3786addr_error(cmd_addr_T addr_type)
3787{
3788 if (addr_type == ADDR_NONE)
3789 emsg(_(e_norange));
3790 else
3791 emsg(_(e_invrange));
3792}
3793
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003795 * Return the default address for an address type.
3796 */
3797 static linenr_T
3798default_address(exarg_T *eap)
3799{
3800 linenr_T lnum = 0;
3801
3802 switch (eap->addr_type)
3803 {
3804 case ADDR_LINES:
3805 case ADDR_OTHER:
3806 // Default is the cursor line number. Avoid using an invalid
3807 // line number though.
3808 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3809 lnum = curbuf->b_ml.ml_line_count;
3810 else
3811 lnum = curwin->w_cursor.lnum;
3812 break;
3813 case ADDR_WINDOWS:
3814 lnum = CURRENT_WIN_NR;
3815 break;
3816 case ADDR_ARGUMENTS:
3817 lnum = curwin->w_arg_idx + 1;
3818 if (lnum > ARGCOUNT)
3819 lnum = ARGCOUNT;
3820 break;
3821 case ADDR_LOADED_BUFFERS:
3822 case ADDR_BUFFERS:
3823 lnum = curbuf->b_fnum;
3824 break;
3825 case ADDR_TABS:
3826 lnum = CURRENT_TAB_NR;
3827 break;
3828 case ADDR_TABS_RELATIVE:
3829 case ADDR_UNSIGNED:
3830 lnum = 1;
3831 break;
3832 case ADDR_QUICKFIX:
3833#ifdef FEAT_QUICKFIX
3834 lnum = qf_get_cur_idx(eap);
3835#endif
3836 break;
3837 case ADDR_QUICKFIX_VALID:
3838#ifdef FEAT_QUICKFIX
3839 lnum = qf_get_cur_valid_idx(eap);
3840#endif
3841 break;
3842 case ADDR_NONE:
3843 // Will give an error later if a range is found.
3844 break;
3845 }
3846 return lnum;
3847}
3848
3849/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003850 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 *
3852 * Set ptr to the next character after the part that was interpreted.
3853 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003854 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 *
3856 * Return MAXLNUM when no Ex address was found.
3857 */
3858 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003859get_address(
3860 exarg_T *eap UNUSED,
3861 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003862 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003863 int skip, // only skip the address, don't use it
3864 int silent, // no errors or side effects
3865 int to_other_file, // flag: may jump to other file
3866 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867{
3868 int c;
3869 int i;
3870 long n;
3871 char_u *cmd;
3872 pos_T pos;
3873 pos_T *fp;
3874 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003875 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876
3877 cmd = skipwhite(*ptr);
3878 lnum = MAXLNUM;
3879 do
3880 {
3881 switch (*cmd)
3882 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003883 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003884 ++cmd;
3885 switch (addr_type)
3886 {
3887 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003888 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 lnum = curwin->w_cursor.lnum;
3890 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003891 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003892 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003893 break;
3894 case ADDR_ARGUMENTS:
3895 lnum = curwin->w_arg_idx + 1;
3896 break;
3897 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003898 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003899 lnum = curbuf->b_fnum;
3900 break;
3901 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003902 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003903 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003904 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003905 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003906 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003907 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003908 cmd = NULL;
3909 goto error;
3910 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003911 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003912#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003913 lnum = qf_get_cur_idx(eap);
3914#endif
3915 break;
3916 case ADDR_QUICKFIX_VALID:
3917#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003918 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003919#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003920 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003921 }
3922 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003924 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003925 ++cmd;
3926 switch (addr_type)
3927 {
3928 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003929 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 lnum = curbuf->b_ml.ml_line_count;
3931 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003932 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003933 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003934 break;
3935 case ADDR_ARGUMENTS:
3936 lnum = ARGCOUNT;
3937 break;
3938 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003939 buf = lastbuf;
3940 while (buf->b_ml.ml_mfp == NULL)
3941 {
3942 if (buf->b_prev == NULL)
3943 break;
3944 buf = buf->b_prev;
3945 }
3946 lnum = buf->b_fnum;
3947 break;
3948 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003949 lnum = lastbuf->b_fnum;
3950 break;
3951 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003952 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003953 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003954 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003955 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003956 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003957 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003958 cmd = NULL;
3959 goto error;
3960 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003961 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003962#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003963 lnum = qf_get_size(eap);
3964 if (lnum == 0)
3965 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003966#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003967 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003968 case ADDR_QUICKFIX_VALID:
3969#ifdef FEAT_QUICKFIX
3970 lnum = qf_get_valid_size(eap);
3971 if (lnum == 0)
3972 lnum = 1;
3973#endif
3974 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003975 }
3976 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003978 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003979 if (*++cmd == NUL)
3980 {
3981 cmd = NULL;
3982 goto error;
3983 }
3984 if (addr_type != ADDR_LINES)
3985 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003986 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003987 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003988 goto error;
3989 }
3990 if (skip)
3991 ++cmd;
3992 else
3993 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003994 // Only accept a mark in another file when it is
3995 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003996 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3997 ++cmd;
3998 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003999 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004000 lnum = curwin->w_cursor.lnum;
4001 else
4002 {
4003 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 {
4005 cmd = NULL;
4006 goto error;
4007 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004008 lnum = fp->lnum;
4009 }
4010 }
4011 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012
4013 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004014 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004015 c = *cmd++;
4016 if (addr_type != ADDR_LINES)
4017 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004018 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004019 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004020 goto error;
4021 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004022 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004023 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004024 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004025 if (*cmd == c)
4026 ++cmd;
4027 }
4028 else
4029 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004030 int flags;
4031
4032 pos = curwin->w_cursor; // save curwin->w_cursor
4033
4034 // When '/' or '?' follows another address, start from
4035 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004036 if (lnum != MAXLNUM)
4037 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004038
4039 // Start a forward search at the end of the line (unless
4040 // before the first line).
4041 // Start a backward search at the start of the line.
4042 // This makes sure we never match in the current
4043 // line, and can match anywhere in the
4044 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004045 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004046 curwin->w_cursor.col = MAXCOL;
4047 else
4048 curwin->w_cursor.col = 0;
4049 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004050 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004051 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004052 {
4053 curwin->w_cursor = pos;
4054 cmd = NULL;
4055 goto error;
4056 }
4057 lnum = curwin->w_cursor.lnum;
4058 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004059 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004060 cmd += searchcmdlen;
4061 }
4062 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004064 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004065 ++cmd;
4066 if (addr_type != ADDR_LINES)
4067 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004068 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004069 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004070 goto error;
4071 }
4072 if (*cmd == '&')
4073 i = RE_SUBST;
4074 else if (*cmd == '?' || *cmd == '/')
4075 i = RE_SEARCH;
4076 else
4077 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004078 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004079 cmd = NULL;
4080 goto error;
4081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004083 if (!skip)
4084 {
4085 /*
4086 * When search follows another address, start from
4087 * there.
4088 */
4089 if (lnum != MAXLNUM)
4090 pos.lnum = lnum;
4091 else
4092 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004094 /*
4095 * Start the search just like for the above
4096 * do_search().
4097 */
4098 if (*cmd != '?')
4099 pos.col = MAXCOL;
4100 else
4101 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004102 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004103 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004104 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004105 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004106 lnum = pos.lnum;
4107 else
4108 {
4109 cmd = NULL;
4110 goto error;
4111 }
4112 }
4113 ++cmd;
4114 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115
4116 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004117 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004118 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 }
4120
4121 for (;;)
4122 {
4123 cmd = skipwhite(cmd);
4124 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4125 break;
4126
4127 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004128 {
4129 switch (addr_type)
4130 {
4131 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004132 case ADDR_OTHER:
4133 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004134 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004135 break;
4136 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004137 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004138 break;
4139 case ADDR_ARGUMENTS:
4140 lnum = curwin->w_arg_idx + 1;
4141 break;
4142 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004143 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004144 lnum = curbuf->b_fnum;
4145 break;
4146 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004147 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004148 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004149 case ADDR_TABS_RELATIVE:
4150 lnum = 1;
4151 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004152 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004153#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004154 lnum = qf_get_cur_idx(eap);
4155#endif
4156 break;
4157 case ADDR_QUICKFIX_VALID:
4158#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004159 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004160#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004161 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004162 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004163 case ADDR_UNSIGNED:
4164 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004165 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004166 }
4167 }
4168
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004170 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 else
4172 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004173 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 n = 1;
4175 else
4176 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004177
4178 if (addr_type == ADDR_TABS_RELATIVE)
4179 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004180 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004181 cmd = NULL;
4182 goto error;
4183 }
4184 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004185 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004186 lnum = compute_buffer_local_count(
4187 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004189 {
4190#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004191 // Relative line addressing, need to adjust for folded lines
4192 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004193 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4194 && address_count >= 2)
4195 (void)hasFolding(lnum, NULL, &lnum);
4196#endif
4197 if (i == '-')
4198 lnum -= n;
4199 else
4200 lnum += n;
4201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 }
4203 } while (*cmd == '/' || *cmd == '?');
4204
4205error:
4206 *ptr = cmd;
4207 return lnum;
4208}
4209
4210/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004211 * Set eap->line1 and eap->line2 to the whole range.
4212 * Used for commands with the EX_DFLALL flag and no range given.
4213 */
4214 static void
4215address_default_all(exarg_T *eap)
4216{
4217 eap->line1 = 1;
4218 switch (eap->addr_type)
4219 {
4220 case ADDR_LINES:
4221 case ADDR_OTHER:
4222 eap->line2 = curbuf->b_ml.ml_line_count;
4223 break;
4224 case ADDR_LOADED_BUFFERS:
4225 {
4226 buf_T *buf = firstbuf;
4227
4228 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4229 buf = buf->b_next;
4230 eap->line1 = buf->b_fnum;
4231 buf = lastbuf;
4232 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4233 buf = buf->b_prev;
4234 eap->line2 = buf->b_fnum;
4235 }
4236 break;
4237 case ADDR_BUFFERS:
4238 eap->line1 = firstbuf->b_fnum;
4239 eap->line2 = lastbuf->b_fnum;
4240 break;
4241 case ADDR_WINDOWS:
4242 eap->line2 = LAST_WIN_NR;
4243 break;
4244 case ADDR_TABS:
4245 eap->line2 = LAST_TAB_NR;
4246 break;
4247 case ADDR_TABS_RELATIVE:
4248 eap->line2 = 1;
4249 break;
4250 case ADDR_ARGUMENTS:
4251 if (ARGCOUNT == 0)
4252 eap->line1 = eap->line2 = 0;
4253 else
4254 eap->line2 = ARGCOUNT;
4255 break;
4256 case ADDR_QUICKFIX_VALID:
4257#ifdef FEAT_QUICKFIX
4258 eap->line2 = qf_get_valid_size(eap);
4259 if (eap->line2 == 0)
4260 eap->line2 = 1;
4261#endif
4262 break;
4263 case ADDR_NONE:
4264 case ADDR_UNSIGNED:
4265 case ADDR_QUICKFIX:
4266 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4267 break;
4268 }
4269}
4270
4271
4272/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004273 * Get flags from an Ex command argument.
4274 */
4275 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004276get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004277{
4278 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4279 {
4280 if (*eap->arg == 'l')
4281 eap->flags |= EXFLAG_LIST;
4282 else if (*eap->arg == 'p')
4283 eap->flags |= EXFLAG_PRINT;
4284 else
4285 eap->flags |= EXFLAG_NR;
4286 eap->arg = skipwhite(eap->arg + 1);
4287 }
4288}
4289
4290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 * Function called for command which is Not Implemented. NI!
4292 */
4293 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004294ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295{
4296 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004297 eap->errmsg =
4298 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299}
4300
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004301#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302/*
4303 * Function called for script command which is Not Implemented. NI!
4304 * Skips over ":perl <<EOF" constructs.
4305 */
4306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004307ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308{
4309 if (!eap->skip)
4310 ex_ni(eap);
4311 else
4312 vim_free(script_get(eap, eap->arg));
4313}
4314#endif
4315
4316/*
4317 * Check range in Ex command for validity.
4318 * Return NULL when valid, error message when invalid.
4319 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004320 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004321invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004323 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004324
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 if ( eap->line1 < 0
4326 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004327 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004328 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004329
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004330 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004331 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004332 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004333 {
4334 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004335 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004336#ifdef FEAT_DIFF
4337 + (eap->cmdidx == CMD_diffget)
4338#endif
4339 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004340 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004341 break;
4342 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004343 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004344 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004345 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004346 break;
4347 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004348 // Only a boundary check, not whether the buffers actually
4349 // exist.
4350 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004351 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004352 break;
4353 case ADDR_LOADED_BUFFERS:
4354 buf = firstbuf;
4355 while (buf->b_ml.ml_mfp == NULL)
4356 {
4357 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004358 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004359 buf = buf->b_next;
4360 }
4361 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004362 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004363 buf = lastbuf;
4364 while (buf->b_ml.ml_mfp == NULL)
4365 {
4366 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004367 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004368 buf = buf->b_prev;
4369 }
4370 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004371 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004372 break;
4373 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004374 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004375 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004376 break;
4377 case ADDR_TABS:
4378 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004379 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004380 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004381 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004382 case ADDR_OTHER:
4383 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004384 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004385 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004386#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004387 // No error for value that is too big, will use the last entry.
4388 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004389 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004390#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004391 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004392 case ADDR_QUICKFIX_VALID:
4393#ifdef FEAT_QUICKFIX
4394 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4395 || eap->line2 < 0)
4396 return _(e_invrange);
4397#endif
4398 break;
4399 case ADDR_UNSIGNED:
4400 if (eap->line2 < 0)
4401 return _(e_invrange);
4402 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004403 case ADDR_NONE:
4404 // Will give an error elsewhere.
4405 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004406 }
4407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 return NULL;
4409}
4410
4411/*
4412 * Correct the range for zero line number, if required.
4413 */
4414 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004415correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004417 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 {
4419 if (eap->line1 == 0)
4420 eap->line1 = 1;
4421 if (eap->line2 == 0)
4422 eap->line2 = 1;
4423 }
4424}
4425
Bram Moolenaar748bf032005-02-02 23:04:36 +00004426#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004427/*
4428 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4429 * pattern. Otherwise return eap->arg.
4430 */
4431 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004432skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004433{
4434 char_u *p = eap->arg;
4435
Bram Moolenaara37420f2006-02-04 22:37:47 +00004436 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4437 || eap->cmdidx == CMD_vimgrepadd
4438 || eap->cmdidx == CMD_lvimgrepadd
4439 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004440 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004441 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004442 if (p == NULL)
4443 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004444 }
4445 return p;
4446}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004447
4448/*
4449 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4450 * in the command line, so that things like % get expanded.
4451 */
4452 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004453replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004454{
4455 char_u *new_cmdline;
4456 char_u *program;
4457 char_u *pos;
4458 char_u *ptr;
4459 int len;
4460 int i;
4461
4462 /*
4463 * Don't do it when ":vimgrep" is used for ":grep".
4464 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004465 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4466 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4467 || eap->cmdidx == CMD_grepadd
4468 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004469 && !grep_internal(eap->cmdidx))
4470 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004471 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4472 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004473 {
4474 if (*curbuf->b_p_gp == NUL)
4475 program = p_gp;
4476 else
4477 program = curbuf->b_p_gp;
4478 }
4479 else
4480 {
4481 if (*curbuf->b_p_mp == NUL)
4482 program = p_mp;
4483 else
4484 program = curbuf->b_p_mp;
4485 }
4486
4487 p = skipwhite(p);
4488
4489 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4490 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004491 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004492 i = 1;
4493 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4494 ++i;
4495 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004496 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004497 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004498 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004499 ptr = new_cmdline;
4500 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4501 {
4502 i = (int)(pos - program);
4503 STRNCPY(ptr, program, i);
4504 STRCPY(ptr += i, p);
4505 ptr += len;
4506 program = pos + 2;
4507 }
4508 STRCPY(ptr, program);
4509 }
4510 else
4511 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004512 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004513 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004514 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004515 STRCPY(new_cmdline, program);
4516 STRCAT(new_cmdline, " ");
4517 STRCAT(new_cmdline, p);
4518 }
4519 msg_make(p);
4520
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004521 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004522 vim_free(*cmdlinep);
4523 *cmdlinep = new_cmdline;
4524 p = new_cmdline;
4525 }
4526 return p;
4527}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004528#endif
4529
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530/*
4531 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004532 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 * Return FAIL for failure, OK otherwise.
4534 */
4535 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004536expand_filename(
4537 exarg_T *eap,
4538 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004539 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004541 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 char_u *repl;
4543 int srclen;
4544 char_u *p;
4545 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004546 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547
Bram Moolenaar748bf032005-02-02 23:04:36 +00004548#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004549 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004550 p = skip_grep_pat(eap);
4551#else
4552 p = eap->arg;
4553#endif
4554
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 /*
4556 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4557 * the file name contains a wildcard it should not cause expanding.
4558 * (it will be expanded anyway if there is a wildcard before replacing).
4559 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004560 has_wildcards = mch_has_wildcard(p);
4561 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004563#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004564 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004565 if (p[0] == '`' && p[1] == '=')
4566 {
4567 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004568 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004569 if (*p == '`')
4570 ++p;
4571 continue;
4572 }
4573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 /*
4575 * Quick check if this cannot be the start of a special string.
4576 * Also removes backslash before '%', '#' and '<'.
4577 */
4578 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4579 {
4580 ++p;
4581 continue;
4582 }
4583
4584 /*
4585 * Try to find a match at this position.
4586 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004587 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4588 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004589 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004591 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 {
4593 p += srclen;
4594 continue;
4595 }
4596
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004597 // Wildcards won't be expanded below, the replacement is taken
4598 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004599 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4600 {
4601 char_u *l = repl;
4602
4603 repl = expand_env_save(repl);
4604 vim_free(l);
4605 }
4606
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004607 // Need to escape white space et al. with a backslash.
4608 // Don't do this for:
4609 // - replacement that already has been escaped: "##"
4610 // - shell commands (may have to use quotes instead).
4611 // - non-unix systems when there is a single argument (spaces don't
4612 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004614 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 && eap->cmdidx != CMD_grep
4617 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004618 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004619 && eap->cmdidx != CMD_lgrep
4620 && eap->cmdidx != CMD_lgrepadd
4621 && eap->cmdidx != CMD_lmake
4622 && eap->cmdidx != CMD_make
4623 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004625 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626#endif
4627 )
4628 {
4629 char_u *l;
4630#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004631 // Don't escape a backslash here, because rem_backslash() doesn't
4632 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 static char_u *nobslash = (char_u *)" \t\"|";
4634# define ESCAPE_CHARS nobslash
4635#else
4636# define ESCAPE_CHARS escape_chars
4637#endif
4638
4639 for (l = repl; *l; ++l)
4640 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4641 {
4642 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4643 if (l != NULL)
4644 {
4645 vim_free(repl);
4646 repl = l;
4647 }
4648 break;
4649 }
4650 }
4651
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004652 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004653 if ((eap->usefilter || eap->cmdidx == CMD_bang
4654 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004655 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 {
4657 char_u *l;
4658
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004659 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 if (l != NULL)
4661 {
4662 vim_free(repl);
4663 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 }
4665 }
4666
4667 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4668 vim_free(repl);
4669 if (p == NULL)
4670 return FAIL;
4671 }
4672
4673 /*
4674 * One file argument: Expand wildcards.
4675 * Don't do this with ":r !command" or ":w !command".
4676 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004677 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 {
4679 /*
4680 * May do this twice:
4681 * 1. Replace environment variables.
4682 * 2. Replace any other wildcards, remove backslashes.
4683 */
4684 for (n = 1; n <= 2; ++n)
4685 {
4686 if (n == 2)
4687 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 /*
4689 * Halve the number of backslashes (this is Vi compatible).
4690 * For Unix and OS/2, when wildcards are expanded, this is
4691 * done by ExpandOne() below.
4692 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004693#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 if (!has_wildcards)
4695#endif
4696 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 }
4698
4699 if (has_wildcards)
4700 {
4701 if (n == 1)
4702 {
4703 /*
4704 * First loop: May expand environment variables. This
4705 * can be done much faster with expand_env() than with
4706 * something else (e.g., calling a shell).
4707 * After expanding environment variables, check again
4708 * if there are still wildcards present.
4709 */
4710 if (vim_strchr(eap->arg, '$') != NULL
4711 || vim_strchr(eap->arg, '~') != NULL)
4712 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004713 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004714 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 has_wildcards = mch_has_wildcard(NameBuff);
4716 p = NameBuff;
4717 }
4718 else
4719 p = NULL;
4720 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004721 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 {
4723 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004724 int options = WILD_LIST_NOTFOUND
4725 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726
4727 ExpandInit(&xpc);
4728 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004729 if (p_wic)
4730 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004732 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 if (p == NULL)
4734 return FAIL;
4735 }
4736 if (p != NULL)
4737 {
4738 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4739 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004740 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 vim_free(p);
4742 }
4743 }
4744 }
4745 }
4746 return OK;
4747}
4748
4749/*
4750 * Replace part of the command line, keeping eap->cmd, eap->arg and
4751 * eap->nextcmd correct.
4752 * "src" points to the part that is to be replaced, of length "srclen".
4753 * "repl" is the replacement string.
4754 * Returns a pointer to the character after the replaced string.
4755 * Returns NULL for failure.
4756 */
4757 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004758repl_cmdline(
4759 exarg_T *eap,
4760 char_u *src,
4761 int srclen,
4762 char_u *repl,
4763 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764{
4765 int len;
4766 int i;
4767 char_u *new_cmdline;
4768
4769 /*
4770 * The new command line is build in new_cmdline[].
4771 * First allocate it.
4772 * Careful: a "+cmd" argument may have been NUL terminated.
4773 */
4774 len = (int)STRLEN(repl);
4775 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004776 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004777 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004778 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004779 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780
4781 /*
4782 * Copy the stuff before the expanded part.
4783 * Copy the expanded stuff.
4784 * Copy what came after the expanded part.
4785 * Copy the next commands, if there are any.
4786 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004787 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004789
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004791 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004793 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004795 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 {
4797 i = (int)STRLEN(new_cmdline) + 1;
4798 STRCPY(new_cmdline + i, eap->nextcmd);
4799 eap->nextcmd = new_cmdline + i;
4800 }
4801 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4802 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4803 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4804 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4805 vim_free(*cmdlinep);
4806 *cmdlinep = new_cmdline;
4807
4808 return src;
4809}
4810
4811/*
4812 * Check for '|' to separate commands and '"' to start comments.
4813 */
4814 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004815separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816{
4817 char_u *p;
4818
Bram Moolenaar86b68352004-12-27 21:59:20 +00004819#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004820 p = skip_grep_pat(eap);
4821#else
4822 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004823#endif
4824
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004825 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 {
4827 if (*p == Ctrl_V)
4828 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004829 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004830 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004832 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004833 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004834 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 break;
4836 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004837
4838#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004839 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004840 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004841 {
4842 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004843 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01004844 if (*p == NUL) // stop at NUL after CTRL-V
4845 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004846 }
4847#endif
4848
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004849 // Check for '"': start of comment or '|': next command
4850 // :@" and :*" do not start a comment!
4851 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004852 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004853#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004854 && !in_vim9script()
4855#endif
4856 && !(eap->argt & EX_NOTRLCOM)
4857 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4858 || p != eap->arg)
4859 && (eap->cmdidx != CMD_redir
4860 || p != eap->arg + 1 || p[-1] != '@'))
4861#ifdef FEAT_EVAL
4862 || (*p == '#'
4863 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004864 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004865 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 || *p == '|' || *p == '\n')
4868 {
4869 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004870 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 * AND 'b' is present in 'cpoptions'.
4872 */
4873 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004874 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004876 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 --p;
4878 }
4879 else
4880 {
4881 eap->nextcmd = check_nextcmd(p);
4882 *p = NUL;
4883 break;
4884 }
4885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004887
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004888 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 del_trailing_spaces(eap->arg);
4890}
4891
4892/*
4893 * get + command from ex argument
4894 */
4895 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004896getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897{
4898 char_u *arg = *argp;
4899 char_u *command = NULL;
4900
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004901 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 {
4903 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004904 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 command = dollar_command;
4906 else
4907 {
4908 command = arg;
4909 arg = skip_cmd_arg(command, TRUE);
4910 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004911 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 }
4913
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004914 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 *argp = arg;
4916 }
4917 return command;
4918}
4919
4920/*
4921 * Find end of "+command" argument. Skip over "\ " and "\\".
4922 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004923 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004924skip_cmd_arg(
4925 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004926 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927{
4928 while (*p && !vim_isspace(*p))
4929 {
4930 if (*p == '\\' && p[1] != NUL)
4931 {
4932 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004933 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 else
4935 ++p;
4936 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004937 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 }
4939 return p;
4940}
4941
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004942 int
4943get_bad_opt(char_u *p, exarg_T *eap)
4944{
4945 if (STRICMP(p, "keep") == 0)
4946 eap->bad_char = BAD_KEEP;
4947 else if (STRICMP(p, "drop") == 0)
4948 eap->bad_char = BAD_DROP;
4949 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4950 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004951 else
4952 return FAIL;
4953 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004954}
4955
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956/*
4957 * Get "++opt=arg" argument.
4958 * Return FAIL or OK.
4959 */
4960 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004961getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962{
4963 char_u *arg = eap->arg + 2;
4964 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004965 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004968 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4970 {
4971 if (*arg == 'n')
4972 {
4973 arg += 2;
4974 eap->force_bin = FORCE_NOBIN;
4975 }
4976 else
4977 eap->force_bin = FORCE_BIN;
4978 if (!checkforcmd(&arg, "binary", 3))
4979 return FAIL;
4980 eap->arg = skipwhite(arg);
4981 return OK;
4982 }
4983
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004984 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004985 if (STRNCMP(arg, "edit", 4) == 0)
4986 {
4987 eap->read_edit = TRUE;
4988 eap->arg = skipwhite(arg + 4);
4989 return OK;
4990 }
4991
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 if (STRNCMP(arg, "ff", 2) == 0)
4993 {
4994 arg += 2;
4995 pp = &eap->force_ff;
4996 }
4997 else if (STRNCMP(arg, "fileformat", 10) == 0)
4998 {
4999 arg += 10;
5000 pp = &eap->force_ff;
5001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 else if (STRNCMP(arg, "enc", 3) == 0)
5003 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005004 if (STRNCMP(arg, "encoding", 8) == 0)
5005 arg += 8;
5006 else
5007 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 pp = &eap->force_enc;
5009 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005010 else if (STRNCMP(arg, "bad", 3) == 0)
5011 {
5012 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005013 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015
5016 if (pp == NULL || *arg != '=')
5017 return FAIL;
5018
5019 ++arg;
5020 *pp = (int)(arg - eap->cmd);
5021 arg = skip_cmd_arg(arg, FALSE);
5022 eap->arg = skipwhite(arg);
5023 *arg = NUL;
5024
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 if (pp == &eap->force_ff)
5026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5028 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005029 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005031 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005033 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5035 *p = TOLOWER_ASC(*p);
5036 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005037 else
5038 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005039 // Check ++bad= argument. Must be a single-byte character, "keep" or
5040 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005041 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005042 return FAIL;
5043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044
5045 return OK;
5046}
5047
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005049ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050{
5051 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005052 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 * directory for security reasons.
5054 */
5055 if (secure)
5056 {
5057 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005058 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 }
5060 else if (eap->cmdidx == CMD_autocmd)
5061 do_autocmd(eap->arg, eap->forceit);
5062 else
5063 do_augroup(eap->arg, eap->forceit);
5064}
5065
5066/*
5067 * ":doautocmd": Apply the automatic commands to the current buffer.
5068 */
5069 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005070ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005072 char_u *arg = eap->arg;
5073 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005074 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005075
Bram Moolenaar1610d052016-06-09 22:53:01 +02005076 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005077 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005078 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005079 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082/*
5083 * :[N]bunload[!] [N] [bufname] unload buffer
5084 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5085 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5086 */
5087 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005088ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005090 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005091 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 eap->errmsg = do_bufdel(
5093 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5094 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5095 : DOBUF_UNLOAD, eap->arg,
5096 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5097}
5098
5099/*
5100 * :[N]buffer [N] to buffer N
5101 * :[N]sbuffer [N] to buffer N
5102 */
5103 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005104ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005106 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005107 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005109 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 else
5111 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005112 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5114 else
5115 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005116 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005117 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 }
5119}
5120
5121/*
5122 * :[N]bmodified [N] to next mod. buffer
5123 * :[N]sbmodified [N] to next mod. buffer
5124 */
5125 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005126ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127{
5128 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005129 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005130 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131}
5132
5133/*
5134 * :[N]bnext [N] to next buffer
5135 * :[N]sbnext [N] split and to next buffer
5136 */
5137 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005138ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005140 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005141 return;
5142
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005144 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005145 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146}
5147
5148/*
5149 * :[N]bNext [N] to previous buffer
5150 * :[N]bprevious [N] to previous buffer
5151 * :[N]sbNext [N] split and to previous buffer
5152 * :[N]sbprevious [N] split and to previous buffer
5153 */
5154 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005155ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005157 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005158 return;
5159
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005161 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005162 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163}
5164
5165/*
5166 * :brewind to first buffer
5167 * :bfirst to first buffer
5168 * :sbrewind split and to first buffer
5169 * :sbfirst split and to first buffer
5170 */
5171 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005172ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005174 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005175 return;
5176
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005178 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005179 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180}
5181
5182/*
5183 * :blast to last buffer
5184 * :sblast split and to last buffer
5185 */
5186 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005187ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005189 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005190 return;
5191
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005193 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005194 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196
Bram Moolenaar7a092242020-04-16 22:10:49 +02005197/*
5198 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005199 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005202ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005204 int comment_char = '"';
5205
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005206#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005207 if (in_vim9script())
5208 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005209#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005210 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005211}
5212
5213/*
5214 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5215 * to "cmd_start" or has a white space character before it.
5216 */
5217 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005218ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005219{
5220 int c = *cmd;
5221
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005222 if (c == NUL || c == '|' || c == '\n')
5223 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005224#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005225 if (in_vim9script())
Bram Moolenaardee37dc2021-02-07 16:40:05 +01005226 return c == '#' && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005227#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005228 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229}
5230
5231#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5232 || defined(PROTO)
5233/*
5234 * Return the next command, after the first '|' or '\n'.
5235 * Return NULL if not found.
5236 */
5237 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005238find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239{
5240 while (*p != '|' && *p != '\n')
5241 {
5242 if (*p == NUL)
5243 return NULL;
5244 ++p;
5245 }
5246 return (p + 1);
5247}
5248#endif
5249
5250/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005251 * Check if *p is a separator between Ex commands, skipping over white space.
5252 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 */
5254 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005255check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005257 char_u *s = skipwhite(p);
5258
5259 if (*s == '|' || *s == '\n')
5260 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 else
5262 return NULL;
5263}
5264
5265/*
5266 * - if there are more files to edit
5267 * - and this is the last window
5268 * - and forceit not used
5269 * - and not repeated twice on a row
5270 * return FAIL and give error message if 'message' TRUE
5271 * return OK otherwise
5272 */
5273 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005274check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005275 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005276 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277{
5278 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5279
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005280 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005281 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 {
5283 if (message)
5284 {
5285#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005286 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5287 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005289 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005291 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5292 NGETTEXT("%d more file to edit. Quit anyway?",
5293 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5295 return OK;
5296 return FAIL;
5297 }
5298#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005299 semsg(NGETTEXT("E173: %d more file to edit",
5300 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005301 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 }
5303 return FAIL;
5304 }
5305 return OK;
5306}
5307
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308/*
5309 * Function given to ExpandGeneric() to obtain the list of command names.
5310 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005312get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313{
5314 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 return cmdnames[idx].cmd_name;
5317}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005320ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321{
Bram Moolenaare6850792010-05-14 15:28:44 +02005322 if (*eap->arg == NUL)
5323 {
5324#ifdef FEAT_EVAL
5325 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5326 char_u *p = NULL;
5327
5328 if (expr != NULL)
5329 {
5330 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005331 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005332 --emsg_off;
5333 vim_free(expr);
5334 }
5335 if (p != NULL)
5336 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005337 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005338 vim_free(p);
5339 }
5340 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005341 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005342#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005343 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005344#endif
5345 }
5346 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005347 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005348
5349#ifdef FEAT_VTP
5350 else if (has_vtp_working())
5351 {
5352 // background color change requires clear + redraw
5353 update_screen(CLEAR);
5354 redrawcmd();
5355 }
5356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357}
5358
5359 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005360ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361{
5362 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005363 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 do_highlight(eap->arg, eap->forceit, FALSE);
5365}
5366
5367
5368/*
5369 * Call this function if we thought we were going to exit, but we won't
5370 * (because of an error). May need to restore the terminal mode.
5371 */
5372 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005373not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374{
5375 exiting = FALSE;
5376 settmode(TMODE_RAW);
5377}
5378
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005379 static int
5380before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5381{
5382 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5383
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005384 // Bail out when autocommands closed the window.
5385 // Refuse to quit when the buffer in the last window is being closed (can
5386 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005387 if (!win_valid(wp)
5388 || curbuf_locked()
5389 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5390 return TRUE;
5391
5392 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5393 {
5394 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005395 // Refuse to quit when locked or when the window was closed or the
5396 // buffer in the last window is being closed (can only happen in
5397 // autocommands).
5398 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005399 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5400 return TRUE;
5401 }
5402
5403 return FALSE;
5404}
5405
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005407 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005408 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005409 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005411 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005412ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005414 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005415
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416#ifdef FEAT_CMDWIN
5417 if (cmdwin_type != 0)
5418 {
5419 cmdwin_result = Ctrl_C;
5420 return;
5421 }
5422#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005423 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005424 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005425 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005426 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005427 return;
5428 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005429 if (eap->addr_count > 0)
5430 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005431 int wnr = eap->line2;
5432
5433 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5434 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005435 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005436 }
5437 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005438 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005439
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005440 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005441 if (curbuf_locked())
5442 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005443
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005444 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005445 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005446 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447
5448#ifdef FEAT_NETBEANS_INTG
5449 netbeansForcedQuit = eap->forceit;
5450#endif
5451
5452 /*
5453 * If there are more files or windows we won't exit.
5454 */
5455 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5456 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005457 if ((!buf_hide(wp->w_buffer)
5458 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005459 | (eap->forceit ? CCGD_FORCEIT : 0)
5460 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005462 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 {
5464 not_exiting();
5465 }
5466 else
5467 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005468 // quit last window
5469 // Note: only_one_window() returns true, even so a help window is
5470 // still open. In that case only quit, if no address has been
5471 // specified. Example:
5472 // :h|wincmd w|1q - don't quit
5473 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005474 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005476 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005477#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005479#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005480 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005481 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 }
5483}
5484
5485/*
5486 * ":cquit".
5487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005489ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005491 // this does not always pass on the exit code to the Manx compiler. why?
5492 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493}
5494
5495/*
5496 * ":qall": try to quit all windows
5497 */
5498 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005499ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500{
5501# ifdef FEAT_CMDWIN
5502 if (cmdwin_type != 0)
5503 {
5504 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005505 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 else
5507 cmdwin_result = K_XF2;
5508 return;
5509 }
5510# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005511
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005512 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005513 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005514 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005515 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005516 return;
5517 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005518
5519 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005520 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005521
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005523 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 getout(0);
5525 not_exiting();
5526}
5527
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528/*
5529 * ":close": close current window, unless it is the last one
5530 */
5531 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005532ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005534 win_T *win;
5535 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005536#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005538 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005540#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005541 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005542 {
5543 if (eap->addr_count == 0)
5544 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005545 else
5546 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005547 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005548 {
5549 winnr++;
5550 if (winnr == eap->line2)
5551 break;
5552 }
5553 if (win == NULL)
5554 win = lastwin;
5555 ex_win_close(eap->forceit, win, NULL);
5556 }
5557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558}
5559
Bram Moolenaar4033c552017-09-16 20:54:51 +02005560#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005561/*
5562 * ":pclose": Close any preview window.
5563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005565ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005566{
5567 win_T *win;
5568
Bram Moolenaar79648732019-07-18 21:43:07 +02005569 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005570 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005571 if (win->w_p_pvw)
5572 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005573 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005574 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005575 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005576# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005577 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005578 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005579# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005580}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005581#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005582
Bram Moolenaarf740b292006-02-16 22:11:02 +00005583/*
5584 * Close window "win" and take care of handling closing the last window for a
5585 * modified buffer.
5586 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005587 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005588ex_win_close(
5589 int forceit,
5590 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005591 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592{
5593 int need_hide;
5594 buf_T *buf = win->w_buffer;
5595
Bram Moolenaarcf844172020-06-26 19:44:06 +02005596 // Never close the autocommand window.
5597 if (win == aucmd_win)
5598 {
5599 emsg(_(e_autocmd_close));
5600 return;
5601 }
5602
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005604 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005606#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005607 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005609 bufref_T bufref;
5610
5611 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005613 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 return;
5615 need_hide = FALSE;
5616 }
5617 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005620 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 return;
5622 }
5623 }
5624
Bram Moolenaar4033c552017-09-16 20:54:51 +02005625#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005627#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005628
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005629 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005630 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005631 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005632 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005633 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634}
5635
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005637 * Handle the argument for a tabpage related ex command.
5638 * Returns a tabpage number.
5639 * When an error is encountered then eap->errmsg is set.
5640 */
5641 static int
5642get_tabpage_arg(exarg_T *eap)
5643{
5644 int tab_number;
5645 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5646
5647 if (eap->arg && *eap->arg != NUL)
5648 {
5649 char_u *p = eap->arg;
5650 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005651 int relative = 0; // argument +N/-N means: go to N places to the
5652 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005653
5654 if (*p == '-')
5655 {
5656 relative = -1;
5657 p++;
5658 }
5659 else if (*p == '+')
5660 {
5661 relative = 1;
5662 p++;
5663 }
5664
5665 p_save = p;
5666 tab_number = getdigits(&p);
5667
5668 if (relative == 0)
5669 {
5670 if (STRCMP(p, "$") == 0)
5671 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005672 else if (STRCMP(p, "#") == 0)
5673 if (valid_tabpage(lastused_tabpage))
5674 tab_number = tabpage_index(lastused_tabpage);
5675 else
5676 {
5677 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5678 tab_number = 0;
5679 goto theend;
5680 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005681 else if (p == p_save || *p_save == '-' || *p != NUL
5682 || tab_number > LAST_TAB_NR)
5683 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005684 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005685 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005686 goto theend;
5687 }
5688 }
5689 else
5690 {
5691 if (*p_save == NUL)
5692 tab_number = 1;
5693 else if (p == p_save || *p_save == '-' || *p != NUL
5694 || tab_number == 0)
5695 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005696 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005697 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005698 goto theend;
5699 }
5700 tab_number = tab_number * relative + tabpage_index(curtab);
5701 if (!unaccept_arg0 && relative == -1)
5702 --tab_number;
5703 }
5704 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005705 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005706 }
5707 else if (eap->addr_count > 0)
5708 {
5709 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005710 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005711 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005712 tab_number = 0;
5713 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005714 else
5715 {
5716 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005717 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005718 {
5719 --tab_number;
5720 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005721 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005722 }
5723 }
5724 }
5725 else
5726 {
5727 switch (eap->cmdidx)
5728 {
5729 case CMD_tabnext:
5730 tab_number = tabpage_index(curtab) + 1;
5731 if (tab_number > LAST_TAB_NR)
5732 tab_number = 1;
5733 break;
5734 case CMD_tabmove:
5735 tab_number = LAST_TAB_NR;
5736 break;
5737 default:
5738 tab_number = tabpage_index(curtab);
5739 }
5740 }
5741
5742theend:
5743 return tab_number;
5744}
5745
5746/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005747 * ":tabclose": close current tab page, unless it is the last one.
5748 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 */
5750 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005751ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005753 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005754 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005755
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005756# ifdef FEAT_CMDWIN
5757 if (cmdwin_type != 0)
5758 cmdwin_result = K_IGNORE;
5759 else
5760# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005761 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005762 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005763 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005765 tab_number = get_tabpage_arg(eap);
5766 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005767 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005768 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005769 if (tp == NULL)
5770 {
5771 beep_flush();
5772 return;
5773 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005774 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005775 {
5776 tabpage_close_other(tp, eap->forceit);
5777 return;
5778 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005779 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005780 tabpage_close(eap->forceit);
5781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005783}
5784
5785/*
5786 * ":tabonly": close all tab pages except the current one
5787 */
5788 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005789ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005790{
5791 tabpage_T *tp;
5792 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005793 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005794
5795# ifdef FEAT_CMDWIN
5796 if (cmdwin_type != 0)
5797 cmdwin_result = K_IGNORE;
5798 else
5799# endif
5800 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005801 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005802 else
5803 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005804 tab_number = get_tabpage_arg(eap);
5805 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005806 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005807 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005808 // Repeat this up to a 1000 times, because autocommands may
5809 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005810 for (done = 0; done < 1000; ++done)
5811 {
5812 FOR_ALL_TABPAGES(tp)
5813 if (tp->tp_topframe != topframe)
5814 {
5815 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005816 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005817 if (valid_tabpage(tp))
5818 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005819 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005820 break;
5821 }
5822 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005823 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005824 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005825 }
5826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828
5829/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005830 * Close the current tab page.
5831 */
5832 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005833tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005834{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005835 // First close all the windows but the current one. If that worked then
5836 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005837 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005838 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005839 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005840 ex_win_close(forceit, curwin, NULL);
5841# ifdef FEAT_GUI
5842 need_mouse_correct = TRUE;
5843# endif
5844}
5845
5846/*
5847 * Close tab page "tp", which is not the current tab page.
5848 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005849 * Also takes care of the tab pages line disappearing when closing the
5850 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005851 */
5852 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005853tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005854{
5855 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005856 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005857 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005858
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005859 // Limit to 1000 windows, autocommands may add a window while we close
5860 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005861 while (++done < 1000)
5862 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005863 wp = tp->tp_firstwin;
5864 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005865
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005866 // Autocommands may delete the tab page under our fingers and we may
5867 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005868 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005869 break;
5870 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005871
Bram Moolenaar29323592016-07-24 22:04:11 +02005872 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005873
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005874 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005875 if (h != tabline_height())
5876 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005877}
5878
5879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 * ":only".
5881 */
5882 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005883ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005885 win_T *wp;
5886 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887# ifdef FEAT_GUI
5888 need_mouse_correct = TRUE;
5889# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005890 if (eap->addr_count > 0)
5891 {
5892 wnr = eap->line2;
5893 for (wp = firstwin; --wnr > 0; )
5894 {
5895 if (wp->w_next == NULL)
5896 break;
5897 else
5898 wp = wp->w_next;
5899 }
5900 win_goto(wp);
5901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 close_others(TRUE, eap->forceit);
5903}
5904
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005906ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005908 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005909 if (!eap->skip)
5910 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005911#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005912 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005913#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005914 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005915 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005916 else
5917 {
5918 int winnr = 0;
5919 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005920
Bram Moolenaar2256c992016-11-15 21:17:07 +01005921 FOR_ALL_WINDOWS(win)
5922 {
5923 winnr++;
5924 if (winnr == eap->line2)
5925 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005926 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005927 if (win == NULL)
5928 win = lastwin;
5929 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 }
5932}
5933
5934/*
5935 * ":stop" and ":suspend": Suspend Vim.
5936 */
5937 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005938ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939{
5940 /*
5941 * Disallow suspending for "rvim".
5942 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005943 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 {
5945 if (!eap->forceit)
5946 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01005947 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 windgoto((int)Rows - 1, 0);
5949 out_char('\n');
5950 out_flush();
5951 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005952 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005954 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005956 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957#ifdef FEAT_TITLE
5958 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005959 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960#endif
5961 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005962 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005964 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01005965 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 }
5967}
5968
5969/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005970 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 */
5972 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005973ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974{
Bram Moolenaar461f2122020-07-28 20:25:47 +02005975#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02005976 if (not_in_vim9(eap) == FAIL)
5977 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02005978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979#ifdef FEAT_CMDWIN
5980 if (cmdwin_type != 0)
5981 {
5982 cmdwin_result = Ctrl_C;
5983 return;
5984 }
5985#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005986 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005987 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005988 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005989 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005990 return;
5991 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005992
5993 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005994 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995
5996 /*
5997 * if more files or windows we won't exit
5998 */
5999 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6000 exiting = TRUE;
6001 if ( ((eap->cmdidx == CMD_wq
6002 || curbufIsChanged())
6003 && do_write(eap) == FAIL)
6004 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006005 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 {
6007 not_exiting();
6008 }
6009 else
6010 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006011 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006013 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014# ifdef FEAT_GUI
6015 need_mouse_correct = TRUE;
6016# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006017 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006018 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 }
6020}
6021
6022/*
6023 * ":print", ":list", ":number".
6024 */
6025 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006026ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006028 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006029 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006030 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006032 for ( ;!got_int; ui_breakcheck())
6033 {
6034 print_line(eap->line1,
6035 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6036 || (eap->flags & EXFLAG_NR)),
6037 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6038 if (++eap->line1 > eap->line2)
6039 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006040 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006041 }
6042 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006043 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006044 curwin->w_cursor.lnum = eap->line2;
6045 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 }
6047
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049}
6050
6051#ifdef FEAT_BYTEOFF
6052 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006053ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054{
6055 goto_byte(eap->line2);
6056}
6057#endif
6058
6059/*
6060 * ":shell".
6061 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006063ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064{
6065 do_shell(NULL, 0);
6066}
6067
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006068#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006070static int drop_busy = FALSE;
6071static int drop_filec;
6072static char_u **drop_filev = NULL;
6073static int drop_split;
6074static void (*drop_callback)(void *);
6075static void *drop_cookie;
6076
6077 static void
6078handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079{
6080 exarg_T ea;
6081 int save_msg_scroll = msg_scroll;
6082
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006083 // Setting the argument list may cause screen updates and being called
6084 // recursively. Avoid that by setting drop_busy.
6085 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006087 // Check whether the current buffer is changed. If so, we will need
6088 // to split the current window or data could be lost.
6089 // We don't need to check if the 'hidden' option is set, as in this
6090 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006091 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 {
6093 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006094 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 --emsg_off;
6096 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006097 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 if (win_split(0, 0) == FAIL)
6100 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006101 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006103 // When splitting the window, create a new alist. Otherwise the
6104 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 alist_unlink(curwin->w_alist);
6106 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 }
6108
6109 /*
6110 * Set up the new argument list.
6111 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006112 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113
6114 /*
6115 * Move to the first file.
6116 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006117 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006118 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 ea.cmd = (char_u *)"next";
6120 do_argfile(&ea, 0);
6121
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006122 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6123 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 need_start_insertmode = FALSE;
6125
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006126 // Restore msg_scroll, otherwise a following command may cause scrolling
6127 // unexpectedly. The screen will be redrawn by the caller, thus
6128 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006130
6131 if (drop_callback != NULL)
6132 drop_callback(drop_cookie);
6133
6134 drop_filev = NULL;
6135 drop_busy = FALSE;
6136}
6137
6138/*
6139 * Handle a file drop. The code is here because a drop is *nearly* like an
6140 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006141 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006142 * code is very similar to :args and hence needs access to a lot of the static
6143 * functions in this file.
6144 *
6145 * The "filev" list must have been allocated using alloc(), as should each item
6146 * in the list. This function takes over responsibility for freeing the "filev"
6147 * list.
6148 */
6149 void
6150handle_drop(
6151 int filec, // the number of files dropped
6152 char_u **filev, // the list of files dropped
6153 int split, // force splitting the window
6154 void (*callback)(void *), // to be called after setting the argument
6155 // list
6156 void *cookie) // argument for "callback" (allocated)
6157{
6158 // Cannot handle recursive drops, finish the pending one.
6159 if (drop_busy)
6160 {
6161 FreeWild(filec, filev);
6162 vim_free(cookie);
6163 return;
6164 }
6165
6166 // When calling handle_drop() more than once in a row we only use the last
6167 // one.
6168 if (drop_filev != NULL)
6169 {
6170 FreeWild(drop_filec, drop_filev);
6171 vim_free(drop_cookie);
6172 }
6173
6174 drop_filec = filec;
6175 drop_filev = filev;
6176 drop_split = split;
6177 drop_callback = callback;
6178 drop_cookie = cookie;
6179
6180 // Postpone this when:
6181 // - editing the command line
6182 // - not possible to change the current buffer
6183 // - updating the screen
6184 // As it may change buffers and window structures that are in use and cause
6185 // freed memory to be used.
6186 if (text_locked() || curbuf_locked() || updating_screen)
6187 return;
6188
6189 handle_drop_internal();
6190}
6191
6192/*
6193 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6194 * reset: Handle a postponed drop.
6195 */
6196 void
6197handle_any_postponed_drop(void)
6198{
6199 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006200 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006201 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202}
6203#endif
6204
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 * ":preserve".
6207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006209ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006211 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 ml_preserve(curbuf, TRUE);
6213}
6214
6215/*
6216 * ":recover".
6217 */
6218 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006219ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006221 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006223 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6224 | CCGD_MULTWIN
6225 | (eap->forceit ? CCGD_FORCEIT : 0)
6226 | CCGD_EXCMD)
6227
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 && (*eap->arg == NUL
6229 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006230 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 recoverymode = FALSE;
6232}
6233
6234/*
6235 * Command modifier used in a wrong way.
6236 */
6237 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006238ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006240 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241}
6242
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243/*
6244 * :sview [+command] file split window with new file, read-only
6245 * :split [[+command] file] split window with current or new file
6246 * :vsplit [[+command] file] split window vertically with current or new file
6247 * :new [[+command] file] split window with no or new file
6248 * :vnew [[+command] file] split vertically window with no or new file
6249 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006250 *
6251 * :tabedit open new Tab page with empty window
6252 * :tabedit [+command] file open new Tab page and edit "file"
6253 * :tabnew [[+command] file] just like :tabedit
6254 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 */
6256 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006257ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006259 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006260#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006262#endif
6263#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006264 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006265 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006266#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006267 int use_tab = eap->cmdidx == CMD_tabedit
6268 || eap->cmdidx == CMD_tabfind
6269 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006271 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006272 return;
6273
Bram Moolenaar4033c552017-09-16 20:54:51 +02006274#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277
Bram Moolenaar4033c552017-09-16 20:54:51 +02006278#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006279 // A ":split" in the quickfix window works like ":new". Don't want two
6280 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006281 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 {
6283 if (eap->cmdidx == CMD_split)
6284 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 if (eap->cmdidx == CMD_vsplit)
6286 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289
Bram Moolenaar4033c552017-09-16 20:54:51 +02006290#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006291 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 {
6293 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6294 FNAME_MESS, TRUE, curbuf->b_ffname);
6295 if (fname == NULL)
6296 goto theend;
6297 eap->arg = fname;
6298 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006299# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006300 else
6301# endif
6302#endif
6303#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006304 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 && eap->cmdidx != CMD_new)
6307 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006308 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006309# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006310 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006311# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006312 au_has_group((char_u *)"FileExplorer"))
6313 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006314 // No browsing supported but we do have the file explorer:
6315 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006316 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006317 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006318 }
6319 else
6320 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006321 fname = do_browse(0, (char_u *)(use_tab
6322 ? _("Edit File in new tab page")
6323 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006325 if (fname == NULL)
6326 goto theend;
6327 eap->arg = fname;
6328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006330 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006333 /*
6334 * Either open new tab page or split the window.
6335 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006336 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006337 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006338 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006339 : eap->addr_count == 0 ? 0
6340 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006341 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006342 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006343
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006344 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006345 if (curwin != old_curwin
6346 && win_valid(old_curwin)
6347 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006348 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006349 old_curwin->w_alt_fnum = curbuf->b_fnum;
6350 }
6351 }
6352 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6354 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006355 // Reset 'scrollbind' when editing another file, but keep it when
6356 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006357 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006358 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 else
6360 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 do_exedit(eap, old_curwin);
6362 }
6363
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006364# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006365 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006366# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006368# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369theend:
6370 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006371# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006373
6374/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006375 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006376 */
6377 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006378tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006379{
6380 exarg_T ea;
6381
Bram Moolenaara80faa82020-04-12 19:37:17 +02006382 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006383 ea.cmdidx = CMD_tabnew;
6384 ea.cmd = (char_u *)"tabn";
6385 ea.arg = (char_u *)"";
6386 ex_splitview(&ea);
6387}
6388
6389/*
6390 * :tabnext command
6391 */
6392 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006393ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006394{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006395 int tab_number;
6396
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006397 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006398 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006399 switch (eap->cmdidx)
6400 {
6401 case CMD_tabfirst:
6402 case CMD_tabrewind:
6403 goto_tabpage(1);
6404 break;
6405 case CMD_tablast:
6406 goto_tabpage(9999);
6407 break;
6408 case CMD_tabprevious:
6409 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006410 if (eap->arg && *eap->arg != NUL)
6411 {
6412 char_u *p = eap->arg;
6413 char_u *p_save = p;
6414
6415 tab_number = getdigits(&p);
6416 if (p == p_save || *p_save == '-' || *p != NUL
6417 || tab_number == 0)
6418 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006419 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006420 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006421 return;
6422 }
6423 }
6424 else
6425 {
6426 if (eap->addr_count == 0)
6427 tab_number = 1;
6428 else
6429 {
6430 tab_number = eap->line2;
6431 if (tab_number < 1)
6432 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006433 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006434 return;
6435 }
6436 }
6437 }
6438 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006439 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006440 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006441 tab_number = get_tabpage_arg(eap);
6442 if (eap->errmsg == NULL)
6443 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006444 break;
6445 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006446}
6447
6448/*
6449 * :tabmove command
6450 */
6451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006452ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006453{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006454 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006455
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006456 tab_number = get_tabpage_arg(eap);
6457 if (eap->errmsg == NULL)
6458 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006459}
6460
6461/*
6462 * :tabs command: List tabs and their contents.
6463 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006464 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006465ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006466{
6467 tabpage_T *tp;
6468 win_T *wp;
6469 int tabcount = 1;
6470
6471 msg_start();
6472 msg_scroll = TRUE;
6473 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6474 {
6475 msg_putchar('\n');
6476 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006477 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006478 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006479 ui_breakcheck();
6480
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006481 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006482 wp = firstwin;
6483 else
6484 wp = tp->tp_firstwin;
6485 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6486 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006487 msg_putchar('\n');
6488 msg_putchar(wp == curwin ? '>' : ' ');
6489 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006490 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6491 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006492 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006493 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006494 else
6495 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6496 IObuff, IOSIZE, TRUE);
6497 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006498 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006499 ui_breakcheck();
6500 }
6501 }
6502}
6503
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504/*
6505 * ":mode": Set screen mode.
6506 * If no argument given, just get the screen size and redraw.
6507 */
6508 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006509ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510{
6511 if (*eap->arg == NUL)
6512 shell_resized();
6513 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006514 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515}
6516
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517/*
6518 * ":resize".
6519 * set, increment or decrement current window height
6520 */
6521 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006522ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523{
6524 int n;
6525 win_T *wp = curwin;
6526
6527 if (eap->addr_count > 0)
6528 {
6529 n = eap->line2;
6530 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6531 ;
6532 }
6533
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006534# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006536# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006538 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 {
6540 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006541 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006542 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 n = 9999;
6544 win_setwidth_win((int)n, wp);
6545 }
6546 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 {
6548 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006549 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006550 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 n = 9999;
6552 win_setheight_win((int)n, wp);
6553 }
6554}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555
6556/*
6557 * ":find [+command] <file>" command.
6558 */
6559 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006560ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561{
6562#ifdef FEAT_SEARCHPATH
6563 char_u *fname;
6564 int count;
6565
6566 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6567 TRUE, curbuf->b_ffname);
6568 if (eap->addr_count > 0)
6569 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006570 // Repeat finding the file "count" times. This matters when it
6571 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 count = eap->line2;
6573 while (fname != NULL && --count > 0)
6574 {
6575 vim_free(fname);
6576 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6577 FALSE, curbuf->b_ffname);
6578 }
6579 }
6580
6581 if (fname != NULL)
6582 {
6583 eap->arg = fname;
6584#endif
6585 do_exedit(eap, NULL);
6586#ifdef FEAT_SEARCHPATH
6587 vim_free(fname);
6588 }
6589#endif
6590}
6591
6592/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006593 * ":open" simulation: for now just work like ":visual".
6594 */
6595 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006596ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006597{
6598 regmatch_T regmatch;
6599 char_u *p;
6600
6601 curwin->w_cursor.lnum = eap->line2;
6602 beginline(BL_SOL | BL_FIX);
6603 if (*eap->arg == '/')
6604 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006605 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006606 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006607 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00006608 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006609 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006610 if (regmatch.regprog != NULL)
6611 {
6612 regmatch.rm_ic = p_ic;
6613 p = ml_get_curline();
6614 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006615 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006616 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006617 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006618 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006619 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006620 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006621 eap->arg += STRLEN(eap->arg);
6622 }
6623 check_cursor();
6624
6625 eap->cmdidx = CMD_visual;
6626 do_exedit(eap, NULL);
6627}
6628
6629/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006630 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 */
6632 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006633ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634{
6635 do_exedit(eap, NULL);
6636}
6637
6638/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006639 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006642do_exedit(
6643 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006644 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645{
6646 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006648 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006650 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6651 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006652 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 /*
6654 * ":vi" command ends Ex mode.
6655 */
6656 if (exmode_active && (eap->cmdidx == CMD_visual
6657 || eap->cmdidx == CMD_view))
6658 {
6659 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006660 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006662 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006663 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006664 if (global_busy)
6665 {
6666 int rd = RedrawingDisabled;
6667 int nwr = no_wait_return;
6668 int ms = msg_scroll;
6669#ifdef FEAT_GUI
6670 int he = hold_gui_events;
6671#endif
6672
6673 if (eap->nextcmd != NULL)
6674 {
6675 stuffReadbuff(eap->nextcmd);
6676 eap->nextcmd = NULL;
6677 }
6678
6679 if (exmode_was != EXMODE_VIM)
6680 settmode(TMODE_RAW);
6681 RedrawingDisabled = 0;
6682 no_wait_return = 0;
6683 need_wait_return = FALSE;
6684 msg_scroll = 0;
6685#ifdef FEAT_GUI
6686 hold_gui_events = 0;
6687#endif
6688 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006689 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006690
6691 main_loop(FALSE, TRUE);
6692
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006693 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006694 RedrawingDisabled = rd;
6695 no_wait_return = nwr;
6696 msg_scroll = ms;
6697#ifdef FEAT_GUI
6698 hold_gui_events = he;
6699#endif
6700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 }
6704
6705 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006706 || eap->cmdidx == CMD_tabnew
6707 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006708 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006710 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 setpcmark();
6712 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006713 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6714 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006716 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 || *eap->arg != NUL
6718#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006719 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720#endif
6721 )
6722 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006723 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6724 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006725 if (*eap->arg != NUL && curbuf_locked())
6726 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006727
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 n = readonlymode;
6729 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6730 readonlymode = TRUE;
6731 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006732 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6733 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01006734 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
6735 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6737 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006738 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6740 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6741 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006742 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006744 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006745 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006746 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
6747 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006748 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006750 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 if (old_curwin != NULL)
6752 {
6753 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006754 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006756#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006757 cleanup_T cs;
6758
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006759 // Reset the error/interrupt/exception state here so that
6760 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006761 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006762#endif
6763#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006765#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006766 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006767
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006768#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006769 // Restore the error/interrupt/exception state if not
6770 // discarded by a new aborting error, interrupt, or
6771 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006772 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 }
6775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 }
6777 else if (readonlymode && curbuf->b_nwindows == 1)
6778 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006779 // When editing an already visited buffer, 'readonly' won't be set
6780 // but the previous value is kept. With ":view" and ":sview" we
6781 // want the file to be readonly, except when another window is
6782 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 curbuf->b_p_ro = TRUE;
6784 }
6785 readonlymode = n;
6786 }
6787 else
6788 {
6789 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01006790 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791#ifdef FEAT_TITLE
6792 n = curwin->w_arg_idx_invalid;
6793#endif
6794 check_arg_idx(curwin);
6795#ifdef FEAT_TITLE
6796 if (n != curwin->w_arg_idx_invalid)
6797 maketitle();
6798#endif
6799 }
6800
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 /*
6802 * if ":split file" worked, set alternate file name in old window to new
6803 * file
6804 */
6805 if (old_curwin != NULL
6806 && *eap->arg != NUL
6807 && curwin != old_curwin
6808 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006809 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006810 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812
6813 ex_no_reprint = TRUE;
6814}
6815
6816#ifndef FEAT_GUI
6817/*
6818 * ":gui" and ":gvim" when there is no GUI.
6819 */
6820 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006821ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006823 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824}
6825#endif
6826
Bram Moolenaar4f974752019-02-17 17:44:42 +01006827#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006829ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830{
6831 gui_make_tearoff(eap->arg);
6832}
6833#endif
6834
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006835#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6836 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006838ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006840# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6841 if (gui.in_use)
6842 gui_make_popup(eap->arg, eap->forceit);
6843# ifdef FEAT_TERM_POPUP_MENU
6844 else
6845# endif
6846# endif
6847# ifdef FEAT_TERM_POPUP_MENU
6848 pum_make_popup(eap->arg, eap->forceit);
6849# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850}
6851#endif
6852
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006854ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855{
6856 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006857 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006859 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860}
6861
6862/*
6863 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6864 * offset.
6865 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6866 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006868ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006871 win_T *save_curwin = curwin;
6872 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 long topline;
6874 long y;
6875 linenr_T old_linenr = curwin->w_cursor.lnum;
6876
6877 setpcmark();
6878
6879 /*
6880 * determine max topline
6881 */
6882 if (curwin->w_p_scb)
6883 {
6884 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006885 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 {
6887 if (wp->w_p_scb && wp->w_buffer)
6888 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006889 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 if (topline > y)
6891 topline = y;
6892 }
6893 }
6894 if (topline < 1)
6895 topline = 1;
6896 }
6897 else
6898 {
6899 topline = 1;
6900 }
6901
6902
6903 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006904 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006906 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 {
6908 if (curwin->w_p_scb)
6909 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006910 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 y = topline - curwin->w_topline;
6912 if (y > 0)
6913 scrollup(y, TRUE);
6914 else
6915 scrolldown(-y, TRUE);
6916 curwin->w_scbind_pos = topline;
6917 redraw_later(VALID);
6918 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 }
6921 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006922 curwin = save_curwin;
6923 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 if (curwin->w_p_scb)
6925 {
6926 did_syncbind = TRUE;
6927 checkpcmark();
6928 if (old_linenr != curwin->w_cursor.lnum)
6929 {
6930 char_u ctrl_o[2];
6931
6932 ctrl_o[0] = Ctrl_O;
6933 ctrl_o[1] = 0;
6934 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6935 }
6936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937}
6938
6939
6940 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006941ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006943 int i;
6944 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6945 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006947 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 do_bang(1, eap, FALSE, FALSE, TRUE);
6949 else
6950 {
6951 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6952 return;
6953
6954#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006955 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 {
6957 char_u *browseFile;
6958
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006959 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 NULL, NULL, NULL, curbuf);
6961 if (browseFile != NULL)
6962 {
6963 i = readfile(browseFile, NULL,
6964 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6965 vim_free(browseFile);
6966 }
6967 else
6968 i = OK;
6969 }
6970 else
6971#endif
6972 if (*eap->arg == NUL)
6973 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006974 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 return;
6976 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6977 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6978 }
6979 else
6980 {
6981 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6982 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6983 i = readfile(eap->arg, NULL,
6984 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6985
6986 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006987 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006989#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 if (!aborting())
6991#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006992 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 }
6994 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006995 {
6996 if (empty && exmode_active)
6997 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006998 // Delete the empty line that remains. Historically ex does
6999 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007000 if (eap->line2 == 0)
7001 lnum = curbuf->b_ml.ml_line_count;
7002 else
7003 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007004 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007005 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007006 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007007 if (curwin->w_cursor.lnum > 1
7008 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007009 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007010 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007011 }
7012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 }
7016}
7017
Bram Moolenaarea408852005-06-25 22:49:46 +00007018static char_u *prev_dir = NULL;
7019
7020#if defined(EXITFREE) || defined(PROTO)
7021 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007022free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007023{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007024 VIM_CLEAR(prev_dir);
7025 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007026}
7027#endif
7028
Bram Moolenaarf4258302013-06-02 18:20:17 +02007029/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007030 * Get the previous directory for the given chdir scope.
7031 */
7032 static char_u *
7033get_prevdir(cdscope_T scope)
7034{
7035 if (scope == CDSCOPE_WINDOW)
7036 return curwin->w_prevdir;
7037 else if (scope == CDSCOPE_TABPAGE)
7038 return curtab->tp_prevdir;
7039 return prev_dir;
7040}
7041
7042/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007043 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007044 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7045 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007046 */
7047 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007048post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007049{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007050 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007051 // Clear tab local directory for both :cd and :tcd
7052 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007053 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007054 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007055 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007056 char_u *pdir = get_prevdir(scope);
7057
7058 // If still in the global directory, need to remember current
7059 // directory as the global directory.
7060 if (globaldir == NULL && pdir != NULL)
7061 globaldir = vim_strsave(pdir);
7062
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007063 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007064 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007065 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007066 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007067 curtab->tp_localdir = vim_strsave(NameBuff);
7068 else
7069 curwin->w_localdir = vim_strsave(NameBuff);
7070 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007071 }
7072 else
7073 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007074 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007075 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007076 }
7077
7078 shorten_fnames(TRUE);
7079}
7080
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007081/*
7082 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007083 * chdir() function.
7084 * scope == CDSCOPE_WINDOW: changes the window-local directory
7085 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7086 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007087 * Returns TRUE if the directory is successfully changed.
7088 */
7089 int
7090changedir_func(
7091 char_u *new_dir,
7092 int forceit,
7093 cdscope_T scope)
7094{
7095 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02007096 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007097 int dir_differs;
7098 int retval = FALSE;
7099
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007100 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007101 return FALSE;
7102
7103 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7104 {
7105 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7106 return FALSE;
7107 }
7108
7109 // ":cd -": Change to previous directory
7110 if (STRCMP(new_dir, "-") == 0)
7111 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007112 pdir = get_prevdir(scope);
7113 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007114 {
7115 emsg(_("E186: No previous directory"));
7116 return FALSE;
7117 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007118 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007119 }
7120
Bram Moolenaar002bc792020-06-05 22:33:42 +02007121 // Free the previous directory
7122 tofree = get_prevdir(scope);
7123
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007124 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007125 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007126 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007127 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007128 pdir = NULL;
7129 if (scope == CDSCOPE_WINDOW)
7130 curwin->w_prevdir = pdir;
7131 else if (scope == CDSCOPE_TABPAGE)
7132 curtab->tp_prevdir = pdir;
7133 else
7134 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007135
7136#if defined(UNIX) || defined(VMS)
7137 // for UNIX ":cd" means: go to home directory
7138 if (*new_dir == NUL)
7139 {
7140 // use NameBuff for home directory name
7141# ifdef VMS
7142 char_u *p;
7143
7144 p = mch_getenv((char_u *)"SYS$LOGIN");
7145 if (p == NULL || *p == NUL) // empty is the same as not set
7146 NameBuff[0] = NUL;
7147 else
7148 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7149# else
7150 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7151# endif
7152 new_dir = NameBuff;
7153 }
7154#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02007155 dir_differs = new_dir == NULL || pdir == NULL
7156 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007157 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7158 emsg(_(e_failed));
7159 else
7160 {
7161 char_u *acmd_fname;
7162
7163 post_chdir(scope);
7164
7165 if (dir_differs)
7166 {
7167 if (scope == CDSCOPE_WINDOW)
7168 acmd_fname = (char_u *)"window";
7169 else if (scope == CDSCOPE_TABPAGE)
7170 acmd_fname = (char_u *)"tabpage";
7171 else
7172 acmd_fname = (char_u *)"global";
7173 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7174 curbuf);
7175 }
7176 retval = TRUE;
7177 }
7178 vim_free(tofree);
7179
7180 return retval;
7181}
Bram Moolenaarea408852005-06-25 22:49:46 +00007182
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007184 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007186 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007187ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007189 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190
7191 new_dir = eap->arg;
7192#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007193 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 if (*new_dir == NUL)
7195 ex_pwd(NULL);
7196 else
7197#endif
7198 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007199 cdscope_T scope = CDSCOPE_GLOBAL;
7200
7201 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7202 scope = CDSCOPE_WINDOW;
7203 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7204 scope = CDSCOPE_TABPAGE;
7205
7206 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007207 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007208 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007209 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 ex_pwd(eap);
7211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 }
7213}
7214
7215/*
7216 * ":pwd".
7217 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007219ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220{
7221 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7222 {
7223#ifdef BACKSLASH_IN_FILENAME
7224 slash_adjust(NameBuff);
7225#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007226 if (p_verbose > 0)
7227 {
7228 char *context = "global";
7229
7230 if (curwin->w_localdir != NULL)
7231 context = "window";
7232 else if (curtab->tp_localdir != NULL)
7233 context = "tabpage";
7234 smsg("[%s] %s", context, (char *)NameBuff);
7235 }
7236 else
7237 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 }
7239 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007240 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241}
7242
7243/*
7244 * ":=".
7245 */
7246 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007247ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007249 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007250 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251}
7252
7253 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007254ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007256 int n;
7257 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258
7259 if (cursor_valid())
7260 {
7261 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7262 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007263 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007265
7266 len = eap->line2;
7267 switch (*eap->arg)
7268 {
7269 case 'm': break;
7270 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007271 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007272 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007273
7274 // Hide the cursor if invoked with !
7275 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276}
7277
7278/*
7279 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007280 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 */
7282 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007283do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284{
Bram Moolenaar52953192019-08-16 10:27:13 +02007285 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007286 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007287# ifdef ELAPSED_FUNC
7288 elapsed_T start_tv;
7289
7290 // Remember at what time we started, so that we know how much longer we
7291 // should wait after waiting for a bit.
7292 ELAPSED_INIT(start_tv);
7293# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007295 if (hide_cursor)
7296 cursor_off();
7297 else
7298 cursor_on();
7299
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007300 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007301 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007303 wait_now = msec - done > 1000L ? 1000L : msec - done;
7304#ifdef FEAT_TIMERS
7305 {
7306 long due_time = check_due_timer();
7307
7308 if (due_time > 0 && due_time < wait_now)
7309 wait_now = due_time;
7310 }
7311#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007312#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007313 if (has_any_channel() && wait_now > 20L)
7314 wait_now = 20L;
7315#endif
7316#ifdef FEAT_SOUND
7317 if (has_any_sound_callback() && wait_now > 20L)
7318 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007319#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007320 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007321
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007322#ifdef FEAT_JOB_CHANNEL
7323 if (has_any_channel())
7324 ui_breakcheck_force(TRUE);
7325 else
7326#endif
7327 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007328#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007329 // Process the netbeans and clientserver messages that may have been
7330 // received in the call to ui_breakcheck() when the GUI is in use. This
7331 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007332 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007333#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007334
7335# ifdef ELAPSED_FUNC
7336 // actual time passed
7337 done = ELAPSED_FUNC(start_tv);
7338# else
7339 // guestimate time passed (will actually be more)
7340 done += wait_now;
7341# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007343
7344 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7345 // input buffer, otherwise a following call to input() fails.
7346 if (got_int)
7347 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348}
7349
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350/*
7351 * ":winsize" command (obsolete).
7352 */
7353 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007354ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355{
7356 int w, h;
7357 char_u *arg = eap->arg;
7358 char_u *p;
7359
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007360 if (!isdigit(*arg))
7361 {
7362 semsg(_(e_invarg2), arg);
7363 return;
7364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 w = getdigits(&arg);
7366 arg = skipwhite(arg);
7367 p = arg;
7368 h = getdigits(&arg);
7369 if (*p != NUL && *arg == NUL)
7370 set_shellsize(w, h, TRUE);
7371 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007372 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373}
7374
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007376ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377{
7378 int xchar = NUL;
7379 char_u *p;
7380
7381 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7382 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007383 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 if (eap->arg[1] == NUL)
7385 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007386 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 return;
7388 }
7389 xchar = eap->arg[1];
7390 p = eap->arg + 2;
7391 }
7392 else
7393 p = eap->arg + 1;
7394
7395 eap->nextcmd = check_nextcmd(p);
7396 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007397 if (*p != NUL && *p != (
7398#ifdef FEAT_EVAL
7399 in_vim9script() ? '#' :
7400#endif
7401 '"')
7402 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007403 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007404 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007406 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007407 postponed_split_flags = cmdmod.cmod_split;
7408 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7410 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007411 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 }
7413}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414
Bram Moolenaar843ee412004-06-30 16:16:41 +00007415#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416/*
7417 * ":winpos".
7418 */
7419 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007420ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421{
7422 int x, y;
7423 char_u *arg = eap->arg;
7424 char_u *p;
7425
7426 if (*arg == NUL)
7427 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007428# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007429# ifdef VIMDLL
7430 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7431 mch_get_winpos(&x, &y) != FAIL)
7432# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007434# else
7435 if (mch_get_winpos(&x, &y) != FAIL)
7436# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 {
7438 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007439 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 }
7441 else
7442# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007443 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 }
7445 else
7446 {
7447 x = getdigits(&arg);
7448 arg = skipwhite(arg);
7449 p = arg;
7450 y = getdigits(&arg);
7451 if (*p == NUL || *arg != NUL)
7452 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007453 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 return;
7455 }
7456# ifdef FEAT_GUI
7457 if (gui.in_use)
7458 gui_mch_set_winpos(x, y);
7459 else if (gui.starting)
7460 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007461 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 gui_win_x = x;
7463 gui_win_y = y;
7464 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007465# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 else
7467# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007468# endif
7469# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007470 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471# endif
7472# ifdef HAVE_TGETENT
7473 if (*T_CWP)
7474 term_set_winpos(x, y);
7475# endif
7476 }
7477}
7478#endif
7479
7480/*
7481 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7482 */
7483 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007484ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485{
7486 oparg_T oa;
7487
7488 clear_oparg(&oa);
7489 oa.regname = eap->regname;
7490 oa.start.lnum = eap->line1;
7491 oa.end.lnum = eap->line2;
7492 oa.line_count = eap->line2 - eap->line1 + 1;
7493 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007495 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 {
7497 setpcmark();
7498 curwin->w_cursor.lnum = eap->line1;
7499 beginline(BL_SOL | BL_FIX);
7500 }
7501
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007502 if (VIsual_active)
7503 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007504
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 switch (eap->cmdidx)
7506 {
7507 case CMD_delete:
7508 oa.op_type = OP_DELETE;
7509 op_delete(&oa);
7510 break;
7511
7512 case CMD_yank:
7513 oa.op_type = OP_YANK;
7514 (void)op_yank(&oa, FALSE, TRUE);
7515 break;
7516
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007517 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007518 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007520 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7521#else
7522 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007524 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 oa.op_type = OP_RSHIFT;
7526 else
7527 oa.op_type = OP_LSHIFT;
7528 op_shift(&oa, FALSE, eap->amount);
7529 break;
7530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007532 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533}
7534
7535/*
7536 * ":put".
7537 */
7538 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007539ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007541 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 if (eap->line2 == 0)
7543 {
7544 eap->line2 = 1;
7545 eap->forceit = TRUE;
7546 }
7547 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007548 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007549 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550}
7551
7552/*
7553 * Handle ":copy" and ":move".
7554 */
7555 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007556ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557{
7558 long n;
7559
Bram Moolenaar491799b2020-08-01 19:23:43 +02007560#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007561 if (not_in_vim9(eap) == FAIL)
7562 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007563#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007564 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007565 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 {
7567 eap->nextcmd = NULL;
7568 return;
7569 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007570 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571
7572 /*
7573 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7574 */
7575 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7576 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007577 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 return;
7579 }
7580
7581 if (eap->cmdidx == CMD_move)
7582 {
7583 if (do_move(eap->line1, eap->line2, n) == FAIL)
7584 return;
7585 }
7586 else
7587 ex_copy(eap->line1, eap->line2, n);
7588 u_clearline();
7589 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007590 ex_may_print(eap);
7591}
7592
7593/*
7594 * Print the current line if flags were given to the Ex command.
7595 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007596 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007597ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007598{
7599 if (eap->flags != 0)
7600 {
7601 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7602 (eap->flags & EXFLAG_LIST));
7603 ex_no_reprint = TRUE;
7604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605}
7606
7607/*
7608 * ":smagic" and ":snomagic".
7609 */
7610 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007611ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007613 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007615 magic_overruled = eap->cmdidx == CMD_smagic
7616 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02007617 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007618 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619}
7620
7621/*
7622 * ":join".
7623 */
7624 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007625ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626{
7627 curwin->w_cursor.lnum = eap->line1;
7628 if (eap->line1 == eap->line2)
7629 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007630 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 return;
7632 if (eap->line2 == curbuf->b_ml.ml_line_count)
7633 {
7634 beep_flush();
7635 return;
7636 }
7637 ++eap->line2;
7638 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007639 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007641 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642}
7643
7644/*
7645 * ":[addr]@r" or ":[addr]*r": execute register
7646 */
7647 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007648ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649{
7650 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007651 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652
7653 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007654 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655
7656#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007657 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658#endif
7659
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007660 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 c = *eap->arg;
7662 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7663 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007664 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007665 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7666 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007667 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 else
7671 {
7672 int save_efr = exec_from_reg;
7673
7674 exec_from_reg = TRUE;
7675
7676 /*
7677 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007678 * Continue until the stuff buffer is empty and all added characters
7679 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007681 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7683
7684 exec_from_reg = save_efr;
7685 }
7686}
7687
7688/*
7689 * ":!".
7690 */
7691 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007692ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693{
7694 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7695}
7696
7697/*
7698 * ":undo".
7699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007701ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007703 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007704 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007705 else
7706 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707}
7708
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007709#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007710 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007711ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007712{
7713 char_u hash[UNDO_HASH_SIZE];
7714
7715 u_compute_hash(hash);
7716 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7717}
7718
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007719 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007720ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007721{
7722 char_u hash[UNDO_HASH_SIZE];
7723
7724 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007725 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007726}
7727#endif
7728
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729/*
7730 * ":redo".
7731 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007733ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734{
7735 u_redo(1);
7736}
7737
7738/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007739 * ":earlier" and ":later".
7740 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007741 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007742ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007743{
7744 long count = 0;
7745 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007746 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007747 char_u *p = eap->arg;
7748
7749 if (*p == NUL)
7750 count = 1;
7751 else if (isdigit(*p))
7752 {
7753 count = getdigits(&p);
7754 switch (*p)
7755 {
7756 case 's': ++p; sec = TRUE; break;
7757 case 'm': ++p; sec = TRUE; count *= 60; break;
7758 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007759 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7760 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007761 }
7762 }
7763
7764 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007765 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007766 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007767 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7768 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007769}
7770
7771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 * ":redir": start/stop redirection.
7773 */
7774 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007775ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776{
7777 char *mode;
7778 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007779 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780
Bram Moolenaarba768492016-07-08 15:32:54 +02007781#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007782 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007783 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007784 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007785 return;
7786 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007787#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007788
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 if (STRICMP(eap->arg, "END") == 0)
7790 close_redir();
7791 else
7792 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007793 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007795 ++arg;
7796 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007798 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 mode = "a";
7800 }
7801 else
7802 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007803 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804
7805 close_redir();
7806
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007807 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007808 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 if (fname == NULL)
7810 return;
7811#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007812 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 {
7814 char_u *browseFile;
7815
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007816 browseFile = do_browse(BROWSE_SAVE,
7817 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007818 fname, NULL, NULL,
7819 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007821 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 vim_free(fname);
7823 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007824 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 }
7826#endif
7827
7828 redir_fd = open_exfile(fname, eap->forceit, mode);
7829 vim_free(fname);
7830 }
7831#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007832 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007834 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007836 ++arg;
7837 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007839 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007840 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007842 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007844 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007845 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007846 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007847 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007849 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007850 if (*arg == '>')
7851 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007852 // Make register empty when not using @A-@Z and the
7853 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007854 if (*arg == NUL && !isupper(redir_reg))
7855 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007857 }
7858 if (*arg != NUL)
7859 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007860 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007861 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007862 }
7863 }
7864 else if (*arg == '=' && arg[1] == '>')
7865 {
7866 int append;
7867
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007868 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007869 close_redir();
7870 arg += 2;
7871
7872 if (*arg == '>')
7873 {
7874 ++arg;
7875 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 }
7877 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007878 append = FALSE;
7879
7880 if (var_redir_start(skipwhite(arg), append) == OK)
7881 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 }
7883#endif
7884
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007885 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007888 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007890
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007891 // Make sure redirection is not off. Can happen for cmdline completion
7892 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007893 if (redir_fd != NULL
7894#ifdef FEAT_EVAL
7895 || redir_reg || redir_vname
7896#endif
7897 )
7898 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899}
7900
7901/*
7902 * ":redraw": force redraw
7903 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007904 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007905ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906{
7907 int r = RedrawingDisabled;
7908 int p = p_lz;
7909
7910 RedrawingDisabled = 0;
7911 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007912 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007914 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915#ifdef FEAT_TITLE
7916 if (need_maketitle)
7917 maketitle();
7918#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007919#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7920# ifdef VIMDLL
7921 if (!gui.in_use)
7922# endif
7923 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 RedrawingDisabled = r;
7926 p_lz = p;
7927
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007928 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 msg_didout = FALSE;
7930 msg_col = 0;
7931
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007932 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007933 need_wait_return = FALSE;
7934
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 out_flush();
7936}
7937
7938/*
7939 * ":redrawstatus": force redraw of status line(s)
7940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007942ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 int r = RedrawingDisabled;
7945 int p = p_lz;
7946
7947 RedrawingDisabled = 0;
7948 p_lz = FALSE;
7949 if (eap->forceit)
7950 status_redraw_all();
7951 else
7952 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007953 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 RedrawingDisabled = r;
7955 p_lz = p;
7956 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957}
7958
Bram Moolenaare12bab32019-01-08 22:02:56 +01007959/*
7960 * ":redrawtabline": force redraw of the tabline
7961 */
7962 static void
7963ex_redrawtabline(exarg_T *eap UNUSED)
7964{
7965 int r = RedrawingDisabled;
7966 int p = p_lz;
7967
7968 RedrawingDisabled = 0;
7969 p_lz = FALSE;
7970
7971 draw_tabline();
7972
7973 RedrawingDisabled = r;
7974 p_lz = p;
7975 out_flush();
7976}
7977
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007979close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980{
7981 if (redir_fd != NULL)
7982 {
7983 fclose(redir_fd);
7984 redir_fd = NULL;
7985 }
7986#ifdef FEAT_EVAL
7987 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007988 if (redir_vname)
7989 {
7990 var_redir_stop();
7991 redir_vname = 0;
7992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993#endif
7994}
7995
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007996#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007997 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007998vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007999{
8000 if (vim_mkdir(name, prot) != 0)
8001 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008002 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008003 return FAIL;
8004 }
8005 return OK;
8006}
8007#endif
8008
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009/*
8010 * Open a file for writing for an Ex command, with some checks.
8011 * Return file descriptor, or NULL on failure.
8012 */
8013 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008014open_exfile(
8015 char_u *fname,
8016 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008017 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018{
8019 FILE *fd;
8020
8021#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008022 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 if (mch_isdir(fname))
8024 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008025 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 return NULL;
8027 }
8028#endif
8029 if (!forceit && *mode != 'a' && vim_fexists(fname))
8030 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008031 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 return NULL;
8033 }
8034
8035 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008036 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037
8038 return fd;
8039}
8040
8041/*
8042 * ":mark" and ":k".
8043 */
8044 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008045ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046{
8047 pos_T pos;
8048
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008049 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008050 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008051 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02008052 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 else
8054 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008055 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 curwin->w_cursor.lnum = eap->line2;
8057 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008058 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008059 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008060 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 }
8062}
8063
8064/*
8065 * Update w_topline, w_leftcol and the cursor position.
8066 */
8067 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008068update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008070 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 update_topline();
8072 if (!curwin->w_p_wrap)
8073 validate_cursor();
8074 update_curswant();
8075}
8076
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008078 * Save the current State and go to Normal mode.
8079 * Return TRUE if the typeahead could be saved.
8080 */
8081 int
8082save_current_state(save_state_T *sst)
8083{
8084 sst->save_msg_scroll = msg_scroll;
8085 sst->save_restart_edit = restart_edit;
8086 sst->save_msg_didout = msg_didout;
8087 sst->save_State = State;
8088 sst->save_insertmode = p_im;
8089 sst->save_finish_op = finish_op;
8090 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008091 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008092
Bram Moolenaar4324d872020-12-01 20:12:24 +01008093 msg_scroll = FALSE; // no msg scrolling in Normal mode
8094 restart_edit = 0; // don't go to Insert mode
8095 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008096
Bram Moolenaara452b802020-12-01 21:47:59 +01008097 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008098 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008099
8100 /*
8101 * Save the current typeahead. This is required to allow using ":normal"
8102 * from an event handler and makes sure we don't hang when the argument
8103 * ends with half a command.
8104 */
8105 save_typeahead(&sst->tabuf);
8106 return sst->tabuf.typebuf_valid;
8107}
8108
8109 void
8110restore_current_state(save_state_T *sst)
8111{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008112 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008113 restore_typeahead(&sst->tabuf);
8114
8115 msg_scroll = sst->save_msg_scroll;
8116 restart_edit = sst->save_restart_edit;
8117 p_im = sst->save_insertmode;
8118 finish_op = sst->save_finish_op;
8119 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008120 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008121 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008122 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008123
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008124 // Restore the state (needed when called from a function executed for
8125 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008126 State = sst->save_State;
8127#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008128 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008129#endif
8130}
8131
8132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 * ":normal[!] {commands}": Execute normal mode commands.
8134 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008135 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008136ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008138 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 char_u *arg = NULL;
8140 int l;
8141 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008143 if (ex_normal_lock > 0)
8144 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008145 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008146 return;
8147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 if (ex_normal_busy >= p_mmd)
8149 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008150 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 return;
8152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 /*
8155 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8156 * this for the K_SPECIAL leading byte, otherwise special keys will not
8157 * work.
8158 */
8159 if (has_mbyte)
8160 {
8161 int len = 0;
8162
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008163 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 for (p = eap->arg; *p != NUL; ++p)
8165 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008166#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008167 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008169#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008170 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008171 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008172#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 )
8176 len += 2;
8177 }
8178 if (len > 0)
8179 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008180 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 if (arg != NULL)
8182 {
8183 len = 0;
8184 for (p = eap->arg; *p != NUL; ++p)
8185 {
8186 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008187#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 if (*p == CSI)
8189 {
8190 arg[len++] = KS_EXTRA;
8191 arg[len++] = (int)KE_CSI;
8192 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008193#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008194 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 {
8196 arg[len++] = *++p;
8197 if (*p == K_SPECIAL)
8198 {
8199 arg[len++] = KS_SPECIAL;
8200 arg[len++] = KE_FILLER;
8201 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008202#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 else if (*p == CSI)
8204 {
8205 arg[len++] = KS_EXTRA;
8206 arg[len++] = (int)KE_CSI;
8207 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 }
8210 arg[len] = NUL;
8211 }
8212 }
8213 }
8214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008216 ++ex_normal_busy;
8217 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {
8219 /*
8220 * Repeat the :normal command for each line in the range. When no
8221 * range given, execute it just once, without positioning the cursor
8222 * first.
8223 */
8224 do
8225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 if (eap->addr_count != 0)
8227 {
8228 curwin->w_cursor.lnum = eap->line1++;
8229 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008230 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 }
8232
Bram Moolenaar13505972019-01-24 15:04:48 +01008233 exec_normal_cmd(arg != NULL
8234 ? arg
8235 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 }
8237 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8238 }
8239
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008240 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 update_topline_cursor();
8242
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008243 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008245 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008246#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008247 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008248#endif
8249
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251}
8252
8253/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008254 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 */
8256 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008257ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008259 if (eap->forceit)
8260 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008261 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008262 if (!curwin->w_cursor.lnum)
8263 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008264 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008265 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008266#ifdef FEAT_TERMINAL
8267 // Ignore this when running in an active terminal.
8268 if (term_job_running(curbuf->b_term))
8269 return;
8270#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008271
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008272 // Ignore the command when already in Insert mode. Inserting an
8273 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008274 if (State & INSERT)
8275 return;
8276
Bram Moolenaar64969662005-12-14 21:59:55 +00008277 if (eap->cmdidx == CMD_startinsert)
8278 restart_edit = 'a';
8279 else if (eap->cmdidx == CMD_startreplace)
8280 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008282 restart_edit = 'V';
8283
8284 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008286 if (eap->cmdidx == CMD_startinsert)
8287 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008288 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008290
8291 if (VIsual_active)
8292 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293}
8294
8295/*
8296 * ":stopinsert"
8297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008299ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300{
8301 restart_edit = 0;
8302 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008303 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008306/*
8307 * Execute normal mode command "cmd".
8308 * "remap" can be REMAP_NONE or REMAP_YES.
8309 */
8310 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008311exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008312{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008313 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008314 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008315 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008316}
Bram Moolenaar25281632016-01-21 23:32:32 +01008317
Bram Moolenaar25281632016-01-21 23:32:32 +01008318/*
8319 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008320 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008321 */
8322 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008323exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008324{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008325 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008326 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008327
Bram Moolenaar905dd902019-04-07 14:21:47 +02008328 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8329 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008330 clear_oparg(&oa);
8331 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008332 while ((!stuff_empty()
8333 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008334 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008335 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008336 {
8337 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008338#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008339 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008340 && oa.op_type == OP_NOP && oa.regname == NUL
8341 && !VIsual_active)
8342 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008343 // If terminal_loop() returns OK we got a key that is handled
8344 // in Normal model. With FAIL we first need to position the
8345 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008346 if (terminal_loop(TRUE) == OK)
8347 normal_cmd(&oa, TRUE);
8348 }
8349 else
8350#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008351 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008352 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008353 }
8354}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008355
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356#ifdef FEAT_FIND_ID
8357 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008358ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359{
8360 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8361 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8362 (linenr_T)1, (linenr_T)MAXLNUM);
8363}
8364
Bram Moolenaar4033c552017-09-16 20:54:51 +02008365#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366/*
8367 * ":psearch"
8368 */
8369 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008370ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371{
8372 g_do_tagpreview = p_pvh;
8373 ex_findpat(eap);
8374 g_do_tagpreview = 0;
8375}
8376#endif
8377
8378 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008379ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380{
8381 int whole = TRUE;
8382 long n;
8383 char_u *p;
8384 int action;
8385
8386 switch (cmdnames[eap->cmdidx].cmd_name[2])
8387 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008388 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8390 action = ACTION_GOTO;
8391 else
8392 action = ACTION_SHOW;
8393 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008394 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 action = ACTION_SHOW_ALL;
8396 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008397 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 action = ACTION_GOTO;
8399 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008400 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 action = ACTION_SPLIT;
8402 break;
8403 }
8404
8405 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008406 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 {
8408 n = getdigits(&eap->arg);
8409 eap->arg = skipwhite(eap->arg);
8410 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008411 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 {
8413 whole = FALSE;
8414 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008415 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 if (*p)
8417 {
8418 *p++ = NUL;
8419 p = skipwhite(p);
8420
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008421 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008422 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008423 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 else
8425 eap->nextcmd = check_nextcmd(p);
8426 }
8427 }
8428 if (!eap->skip)
8429 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8430 whole, !eap->forceit,
8431 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8432 n, action, eap->line1, eap->line2);
8433}
8434#endif
8435
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436
Bram Moolenaar4033c552017-09-16 20:54:51 +02008437#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438/*
8439 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8440 */
8441 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008442ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008444 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8446}
8447
8448/*
8449 * ":pedit"
8450 */
8451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008452ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453{
8454 win_T *curwin_save = curwin;
8455
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008456 if (ERROR_IF_ANY_POPUP_WINDOW)
8457 return;
8458
Bram Moolenaar026587b2019-08-17 15:08:00 +02008459 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008461 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008462
Bram Moolenaar026587b2019-08-17 15:08:00 +02008463 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008465
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 if (curwin != curwin_save && win_valid(curwin_save))
8467 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008468 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 validate_cursor();
8470 redraw_later(VALID);
8471 win_enter(curwin_save, TRUE);
8472 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008473# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008474 else if (WIN_IS_POPUP(curwin))
8475 {
8476 // can't keep focus in popup window
8477 win_enter(firstwin, TRUE);
8478 }
8479# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 g_do_tagpreview = 0;
8481}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483
8484/*
8485 * ":stag", ":stselect" and ":stjump".
8486 */
8487 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008488ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489{
8490 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008491 postponed_split_flags = cmdmod.cmod_split;
8492 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8494 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008495 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497
8498/*
8499 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8500 */
8501 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008502ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503{
8504 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8505}
8506
8507 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008508ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509{
8510 int cmd;
8511
8512 switch (name[1])
8513 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008514 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008516 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008518 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008520 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008522 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008524 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008526 case 'f': // ":tfirst"
8527 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008529 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008531 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008533 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008535 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 return;
8537 }
8538#endif
8539 cmd = DT_TAG;
8540 break;
8541 }
8542
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008543 if (name[0] == 'l')
8544 {
8545#ifndef FEAT_QUICKFIX
8546 ex_ni(eap);
8547 return;
8548#else
8549 cmd = DT_LTAG;
8550#endif
8551 }
8552
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8554 eap->forceit, TRUE);
8555}
8556
8557/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008558 * Check "str" for starting with a special cmdline variable.
8559 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8560 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8561 */
8562 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008563find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008564{
8565 int len;
8566 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008567 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008568 "%",
8569#define SPEC_PERC 0
8570 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008571#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008572 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008573#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008574 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008575#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008576 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008577#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008578 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008579#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008580 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008581#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008582 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008583#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008584 "<stack>", // call stack
8585#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008586 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008587#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008588 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008589#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008590 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008591#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008592 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008593#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008594 "<SID>", // script ID: <SNR>123_
8595#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008596#ifdef FEAT_CLIENTSERVER
8597 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008598# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008599#endif
8600 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008601
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008602 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008603 {
8604 len = (int)STRLEN(spec_str[i]);
8605 if (STRNCMP(src, spec_str[i], len) == 0)
8606 {
8607 *usedlen = len;
8608 return i;
8609 }
8610 }
8611 return -1;
8612}
8613
8614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 * Evaluate cmdline variables.
8616 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008617 * change "%" to curbuf->b_ffname
8618 * "#" to curwin->w_alt_fnum
8619 * "%%" to curwin->w_alt_fnum in Vim9 script
8620 * "<cword>" to word under the cursor
8621 * "<cWORD>" to WORD under the cursor
8622 * "<cexpr>" to C-expression under the cursor
8623 * "<cfile>" to path name under the cursor
8624 * "<sfile>" to sourced file name
8625 * "<stack>" to call stack
8626 * "<slnum>" to sourced file line number
8627 * "<afile>" to file name for autocommand
8628 * "<abuf>" to buffer number for autocommand
8629 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 *
8631 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8632 * "" for error without a message) and NULL is returned.
8633 * Returns an allocated string if a valid match was found.
8634 * Returns NULL if no match was found. "usedlen" then still contains the
8635 * number of characters to skip.
8636 */
8637 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008638eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008639 char_u *src, // pointer into commandline
8640 char_u *srcstart, // beginning of valid memory for src
8641 int *usedlen, // characters after src that are used
8642 linenr_T *lnump, // line number for :e command, or NULL
8643 char **errormsg, // pointer to error message
8644 int *escaped) // return value has escaped white space (can
8645 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646{
8647 int i;
8648 char_u *s;
8649 char_u *result;
8650 char_u *resultbuf = NULL;
8651 int resultlen;
8652 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008653 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008655 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658
8659 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008660 if (escaped != NULL)
8661 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662
8663 /*
8664 * Check if there is something to do.
8665 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008666 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008667 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 {
8669 *usedlen = 1;
8670 return NULL;
8671 }
8672
8673 /*
8674 * Skip when preceded with a backslash "\%" and "\#".
8675 * Note: In "\\%" the % is also not recognized!
8676 */
8677 if (src > srcstart && src[-1] == '\\')
8678 {
8679 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008680 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 return NULL;
8682 }
8683
8684 /*
8685 * word or WORD under cursor
8686 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008687 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8688 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008690 resultlen = find_ident_under_cursor(&result,
8691 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8692 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8693 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 if (resultlen == 0)
8695 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008696 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 return NULL;
8698 }
8699 }
8700
8701 /*
8702 * '#': Alternate file name
8703 * '%': Current file name
8704 * File name under the cursor
8705 * File name for autocommand
8706 * and following modifiers
8707 */
8708 else
8709 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008710 int off = 0;
8711
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 switch (spec_idx)
8713 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008714 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008715#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008716 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008719 // '%': current file
8720 if (curbuf->b_fname == NULL)
8721 {
8722 result = (char_u *)"";
8723 valid = 0; // Must have ":p:h" to be valid
8724 }
8725 else
8726 {
8727 result = curbuf->b_fname;
8728 tilde_file = STRCMP(result, "~") == 0;
8729 }
8730 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008732#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008733 // "%%" alternate file
8734 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008735#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01008736 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008737 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008738 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008740 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 result = arg_all();
8742 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008743 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008744 if (escaped != NULL)
8745 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 break;
8748 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008749 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008750 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008751 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008753 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008754 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008755 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008756 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008758 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008760 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00008761 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008762 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008763 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008764 return NULL;
8765 }
8766#ifdef FEAT_EVAL
8767 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8768 (long)i);
8769 if (result == NULL)
8770 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008771 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008772 return NULL;
8773 }
8774#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008775 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 }
8779 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008780 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008781 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
8782 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008783 buf = buflist_findnr(i);
8784 if (buf == NULL)
8785 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008786 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008787 return NULL;
8788 }
8789 if (lnump != NULL)
8790 *lnump = ECMD_LAST;
8791 if (buf->b_fname == NULL)
8792 {
8793 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008794 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008795 }
8796 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008797 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008798 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008799 tilde_file = STRCMP(result, "~") == 0;
8800 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 break;
8803
8804#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008805 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008806 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 if (result == NULL)
8808 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008809 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 return NULL;
8811 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008812 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 break;
8814#endif
8815
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008816 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008818 if (result != NULL && !autocmd_fname_full)
8819 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008820 // Still need to turn the fname into a full path. It is
8821 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008822 autocmd_fname_full = TRUE;
8823 result = FullName_save(autocmd_fname, FALSE);
8824 vim_free(autocmd_fname);
8825 autocmd_fname = result;
8826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 if (result == NULL)
8828 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008829 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 return NULL;
8831 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008832 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 break;
8834
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008835 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 if (autocmd_bufnr <= 0)
8837 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008838 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 return NULL;
8840 }
8841 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8842 result = strbuf;
8843 break;
8844
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008845 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 result = autocmd_match;
8847 if (result == NULL)
8848 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008849 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 return NULL;
8851 }
8852 break;
8853
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008854 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008855 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008856 result = estack_sfile(spec_idx == SPEC_SFILE
8857 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 if (result == NULL)
8859 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008860 *errormsg = spec_idx == SPEC_SFILE
8861 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8862 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 return NULL;
8864 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008865 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008867
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008868 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008869 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008870 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008871 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008872 return NULL;
8873 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008874 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008875 result = strbuf;
8876 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008877
8878#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008879 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008880 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008881 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008882 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008883 return NULL;
8884 }
8885 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008886 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008887 result = strbuf;
8888 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008889
Bram Moolenaar90944302020-08-01 20:45:11 +02008890 case SPEC_SID:
8891 if (current_sctx.sc_sid <= 0)
8892 {
8893 *errormsg = _(e_usingsid);
8894 return NULL;
8895 }
8896 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
8897 result = strbuf;
8898 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02008899#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02008900
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008901#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008902 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008903 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8904 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 result = strbuf;
8906 break;
8907#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008908
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008909 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008910 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008911 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 }
8913
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008914 resultlen = (int)STRLEN(result); // length of new string
8915 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 {
8917 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 resultlen = (int)(s - result);
8920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 else if (!skip_mod)
8922 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008923 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 &resultlen);
8925 if (result == NULL)
8926 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008927 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 return NULL;
8929 }
8930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 }
8932
8933 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8934 {
8935 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008936 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008937 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008939 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 result = NULL;
8941 }
8942 else
8943 result = vim_strnsave(result, resultlen);
8944 vim_free(resultbuf);
8945 return result;
8946}
8947
8948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 * Expand the <sfile> string in "arg".
8950 *
8951 * Returns an allocated string, or NULL for any error.
8952 */
8953 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008954expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008956 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 int len;
8958 char_u *result;
8959 char_u *newres;
8960 char_u *repl;
8961 int srclen;
8962 char_u *p;
8963
8964 result = vim_strsave(arg);
8965 if (result == NULL)
8966 return NULL;
8967
8968 for (p = result; *p; )
8969 {
8970 if (STRNCMP(p, "<sfile>", 7) != 0)
8971 ++p;
8972 else
8973 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008974 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008975 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 if (errormsg != NULL)
8977 {
8978 if (*errormsg)
8979 emsg(errormsg);
8980 vim_free(result);
8981 return NULL;
8982 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008983 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 {
8985 p += srclen;
8986 continue;
8987 }
8988 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8989 newres = alloc(len);
8990 if (newres == NULL)
8991 {
8992 vim_free(repl);
8993 vim_free(result);
8994 return NULL;
8995 }
8996 mch_memmove(newres, result, (size_t)(p - result));
8997 STRCPY(newres + (p - result), repl);
8998 len = (int)STRLEN(newres);
8999 STRCAT(newres, p + srclen);
9000 vim_free(repl);
9001 vim_free(result);
9002 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009003 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 }
9005 }
9006
9007 return result;
9008}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009011/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009012 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009013 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009014 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009016dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 if (fname == NULL)
9019 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009020 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021}
9022#endif
9023
9024/*
9025 * ":behave {mswin,xterm}"
9026 */
9027 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009028ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029{
9030 if (STRCMP(eap->arg, "mswin") == 0)
9031 {
9032 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9033 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9034 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9035 set_option_value((char_u *)"keymodel", 0L,
9036 (char_u *)"startsel,stopsel", 0);
9037 }
9038 else if (STRCMP(eap->arg, "xterm") == 0)
9039 {
9040 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9041 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9042 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9043 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9044 }
9045 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009046 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047}
9048
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049static int filetype_detect = FALSE;
9050static int filetype_plugin = FALSE;
9051static int filetype_indent = FALSE;
9052
9053/*
9054 * ":filetype [plugin] [indent] {on,off,detect}"
9055 * on: Load the filetype.vim file to install autocommands for file types.
9056 * off: Load the ftoff.vim file to remove all autocommands for file types.
9057 * plugin on: load filetype.vim and ftplugin.vim
9058 * plugin off: load ftplugof.vim
9059 * indent on: load filetype.vim and indent.vim
9060 * indent off: load indoff.vim
9061 */
9062 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009063ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064{
9065 char_u *arg = eap->arg;
9066 int plugin = FALSE;
9067 int indent = FALSE;
9068
9069 if (*eap->arg == NUL)
9070 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009071 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009072 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 filetype_detect ? "ON" : "OFF",
9074 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9075 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9076 return;
9077 }
9078
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009079 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 for (;;)
9081 {
9082 if (STRNCMP(arg, "plugin", 6) == 0)
9083 {
9084 plugin = TRUE;
9085 arg = skipwhite(arg + 6);
9086 continue;
9087 }
9088 if (STRNCMP(arg, "indent", 6) == 0)
9089 {
9090 indent = TRUE;
9091 arg = skipwhite(arg + 6);
9092 continue;
9093 }
9094 break;
9095 }
9096 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9097 {
9098 if (*arg == 'o' || !filetype_detect)
9099 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009100 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 filetype_detect = TRUE;
9102 if (plugin)
9103 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009104 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 filetype_plugin = TRUE;
9106 }
9107 if (indent)
9108 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009109 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 filetype_indent = TRUE;
9111 }
9112 }
9113 if (*arg == 'd')
9114 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009115 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009116 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 }
9118 }
9119 else if (STRCMP(arg, "off") == 0)
9120 {
9121 if (plugin || indent)
9122 {
9123 if (plugin)
9124 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009125 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 filetype_plugin = FALSE;
9127 }
9128 if (indent)
9129 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009130 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 filetype_indent = FALSE;
9132 }
9133 }
9134 else
9135 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009136 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 filetype_detect = FALSE;
9138 }
9139 }
9140 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009141 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142}
9143
9144/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009145 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 */
9147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009148ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149{
9150 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009151 {
9152 char_u *arg = eap->arg;
9153
9154 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9155 arg += 9;
9156
9157 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9158 if (arg != eap->arg)
9159 did_filetype = FALSE;
9160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009164ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165{
9166#ifdef FEAT_DIGRAPHS
9167 if (*eap->arg != NUL)
9168 putdigraph(eap->arg);
9169 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009170 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009172 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173#endif
9174}
9175
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009176#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9177 void
9178set_no_hlsearch(int flag)
9179{
9180 no_hlsearch = flag;
9181# ifdef FEAT_EVAL
9182 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9183# endif
9184}
9185
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186/*
9187 * ":nohlsearch"
9188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009190ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009192 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009193 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195#endif
9196
9197#ifdef FEAT_CRYPT
9198/*
9199 * ":X": Get crypt key
9200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009202ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009204 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009205 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206}
9207#endif
9208
9209#ifdef FEAT_FOLDING
9210 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009211ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212{
9213 if (foldManualAllowed(TRUE))
9214 foldCreate(eap->line1, eap->line2);
9215}
9216
9217 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009218ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219{
9220 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9221 eap->forceit, FALSE);
9222}
9223
9224 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009225ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226{
9227 linenr_T lnum;
9228
Bram Moolenaar4facea32019-10-12 20:17:40 +02009229# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009230 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009231# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009232
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009233 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9235 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9236 ml_setmarked(lnum);
9237
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009238 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009240 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009241# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009242 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009243# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244}
9245#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009246
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009247#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009248/*
9249 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9250 * instead of a quickfix command.
9251 */
9252 int
9253is_loclist_cmd(int cmdidx)
9254{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009255 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009256 return FALSE;
9257 return cmdnames[cmdidx].cmd_name[0] == 'l';
9258}
9259#endif
9260
Bram Moolenaar4facea32019-10-12 20:17:40 +02009261#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009262 int
9263get_pressedreturn(void)
9264{
9265 return ex_pressedreturn;
9266}
9267
9268 void
9269set_pressedreturn(int val)
9270{
9271 ex_pressedreturn = val;
9272}
9273#endif