blob: 5e9eeb78fec89ed1cfad9b7be0b799a8fbedf5a6 [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_def ex_ni
278# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200279# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100280# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281# define ex_echo ex_ni
282# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283# define ex_else ex_ni
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200284# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200285# define ex_endfunction ex_ni
286# define ex_endif ex_ni
287# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200289# define ex_eval ex_ni
290# define ex_execute ex_ni
291# define ex_finally ex_ni
292# define ex_finish ex_ni
293# define ex_function ex_ni
294# define ex_if ex_ni
295# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200296# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200297# define ex_lockvar ex_ni
298# define ex_oldfiles ex_ni
299# define ex_options ex_ni
300# define ex_packadd ex_ni
301# define ex_packloadall ex_ni
302# define ex_return ex_ni
303# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304# define ex_throw ex_ni
305# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000307# define ex_unlockvar ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100308# define ex_vim9script ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200309# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100310# define ex_import ex_ni
311# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200313#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314# define ex_loadview ex_ni
315#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200316#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317# define ex_viminfo ex_ni
318#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100319static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100320static void ex_filetype(exarg_T *eap);
321static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000323# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324# define ex_diffpatch ex_ni
325# define ex_diffgetput ex_ni
326# define ex_diffsplit ex_ni
327# define ex_diffthis ex_ni
328# define ex_diffupdate ex_ni
329#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100330static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100332static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333#else
334# define ex_nohlsearch ex_ni
335# define ex_match ex_ni
336#endif
337#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100338static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339#else
340# define ex_X ex_ni
341#endif
342#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100343static void ex_fold(exarg_T *eap);
344static void ex_foldopen(exarg_T *eap);
345static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346#else
347# define ex_fold ex_ni
348# define ex_foldopen ex_ni
349# define ex_folddo ex_ni
350#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100351#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352# define ex_language ex_ni
353#endif
354#ifndef FEAT_SIGNS
355# define ex_sign ex_ni
356#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000357#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200358# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000359# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200360# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363#ifndef FEAT_JUMPLIST
364# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200365# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366# define ex_changes ex_ni
367#endif
368
Bram Moolenaar05159a02005-02-26 23:04:13 +0000369#ifndef FEAT_PROFILE
370# define ex_profile ex_ni
371#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200372#ifndef FEAT_TERMINAL
373# define ex_terminal ex_ni
374#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200375#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
376# define ex_xrestore ex_ni
377#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100378#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200379# define ex_popupclear ex_ni
380#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000381
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382/*
383 * Declare cmdnames[].
384 */
385#define DO_DECLARE_EXCMD
386#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200387#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100388
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389static char_u dollar_command[2] = {'$', 0};
390
391
392#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100393// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394typedef struct
395{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100396 char_u *line; // command line
397 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398} wcmd_T;
399
400/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000401 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000403 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000405struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100407 garray_T *lines_gap; // growarray with line info
408 int current_line; // last read line from growarray
409 int repeating; // TRUE when looping a second time
410 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200411 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 void *cookie;
413};
414
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200415static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100416static int store_loop_line(garray_T *gap, char_u *line);
417static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000418
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100419// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000420struct dbg_stuff
421{
422 int trylevel;
423 int force_abort;
424 except_T *caught_stack;
425 char_u *vv_exception;
426 char_u *vv_throwpoint;
427 int did_emsg;
428 int got_int;
429 int did_throw;
430 int need_rethrow;
431 int check_cstack;
432 except_T *current_exception;
433};
434
Bram Moolenaared203462004-06-16 11:19:22 +0000435 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100436save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000437{
438 dsp->trylevel = trylevel; trylevel = 0;
439 dsp->force_abort = force_abort; force_abort = FALSE;
440 dsp->caught_stack = caught_stack; caught_stack = NULL;
441 dsp->vv_exception = v_exception(NULL);
442 dsp->vv_throwpoint = v_throwpoint(NULL);
443
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100444 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000445 dsp->did_emsg = did_emsg; did_emsg = FALSE;
446 dsp->got_int = got_int; got_int = FALSE;
447 dsp->did_throw = did_throw; did_throw = FALSE;
448 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
449 dsp->check_cstack = check_cstack; check_cstack = FALSE;
450 dsp->current_exception = current_exception; current_exception = NULL;
451}
452
453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100454restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000455{
456 suppress_errthrow = FALSE;
457 trylevel = dsp->trylevel;
458 force_abort = dsp->force_abort;
459 caught_stack = dsp->caught_stack;
460 (void)v_exception(dsp->vv_exception);
461 (void)v_throwpoint(dsp->vv_throwpoint);
462 did_emsg = dsp->did_emsg;
463 got_int = dsp->got_int;
464 did_throw = dsp->did_throw;
465 need_rethrow = dsp->need_rethrow;
466 check_cstack = dsp->check_cstack;
467 current_exception = dsp->current_exception;
468}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469#endif
470
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471/*
472 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
473 * command is given.
474 */
475 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100476do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100477 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478{
479 int save_msg_scroll;
480 int prev_msg_row;
481 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100482 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000483
484 if (improved)
485 exmode_active = EXMODE_VIM;
486 else
487 exmode_active = EXMODE_NORMAL;
488 State = NORMAL;
489
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100490 // When using ":global /pat/ visual" and then "Q" we return to continue
491 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000492 if (global_busy)
493 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494
495 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100496 ++RedrawingDisabled; // don't redisplay the window
497 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100499 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 ++hold_gui_events;
501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502
Bram Moolenaar32526b32019-01-19 17:43:09 +0100503 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 while (exmode_active)
505 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100506 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000507 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
508 {
509 exmode_active = FALSE;
510 break;
511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 msg_scroll = TRUE;
513 need_wait_return = FALSE;
514 ex_pressedreturn = FALSE;
515 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100516 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 prev_msg_row = msg_row;
518 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 if (improved)
520 {
521 cmdline_row = msg_row;
522 do_cmdline(NULL, getexline, NULL, 0);
523 }
524 else
525 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
526 lines_left = Rows - 1;
527
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100529 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000531 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100532 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000533 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000535 if (ex_pressedreturn)
536 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100537 // go up one line, to overwrite the ":<CR>" line, so the
538 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000539 msg_row = prev_msg_row;
540 if (prev_msg_row == Rows - 1)
541 msg_row--;
542 }
543 msg_col = 0;
544 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
545 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100548 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 {
550 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100551 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000552 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100553 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 }
556
557#ifdef FEAT_GUI
558 --hold_gui_events;
559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 --RedrawingDisabled;
561 --no_wait_return;
562 update_screen(CLEAR);
563 need_wait_return = FALSE;
564 msg_scroll = save_msg_scroll;
565}
566
567/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200568 * Print the executed command for when 'verbose' is set.
569 * When "lnum" is 0 only print the command.
570 */
571 static void
572msg_verbose_cmd(linenr_T lnum, char_u *cmd)
573{
574 ++no_wait_return;
575 verbose_enter_scroll();
576
577 if (lnum == 0)
578 smsg(_("Executing: %s"), cmd);
579 else
580 smsg(_("line %ld: %s"), (long)lnum, cmd);
581 if (msg_silent == 0)
582 msg_puts("\n"); // don't overwrite this
583
584 verbose_leave_scroll();
585 --no_wait_return;
586}
587
588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 * Execute a simple command line. Used for translated commands like "*".
590 */
591 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100592do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593{
594 return do_cmdline(cmd, NULL, NULL,
595 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
596}
597
598/*
599 * do_cmdline(): execute one Ex command line
600 *
601 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100602 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 * 2. Split up in parts separated with '|'.
604 *
605 * This function can be called recursively!
606 *
607 * flags:
608 * DOCMD_VERBOSE - The command will be included in the error message.
609 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100610 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 * DOCMD_KEYTYPED - Don't reset KeyTyped.
612 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
613 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
614 *
615 * return FAIL if cmdline could not be executed, OK otherwise
616 */
617 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100618do_cmdline(
619 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200620 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100621 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100622 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100624 char_u *next_cmdline; // next cmd to execute
625 char_u *cmdline_copy = NULL; // copy of cmd line
626 int used_getline = FALSE; // used "fgetline" to obtain command
627 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100629 int count = 0; // line number count
630 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 int retval = OK;
632#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100633 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100634 garray_T lines_ga; // keep lines for ":while"/":for"
635 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200636 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100637 char_u *fname = NULL; // function or script name
638 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
639 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
640 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200642 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200643 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100645 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200646 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000648 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000650 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100652# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653# define cmd_cookie cookie
654#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100655 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200656#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100657 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100659 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
660 // location for storing error messages to be converted to an exception.
661 // This ensures that the do_errthrow() call in do_one_cmd() does not
662 // combine the messages stored by an earlier invocation of do_one_cmd()
663 // with the command name of the later one. This would happen when
664 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 saved_msg_list = msg_list;
666 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667#endif
668
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100669 // It's possible to create an endless loop with ":execute", catch that
670 // here. The value of 200 allows nested function calls, ":source", etc.
671 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100672 if (call_depth >= 200
673#ifdef FEAT_EVAL
674 && call_depth >= p_mfd
675#endif
676 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100678 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100680 // When converting to an exception, we do not include the command name
681 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100682 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 msg_list = saved_msg_list;
684#endif
685 return FAIL;
686 }
687 ++call_depth;
688
689#ifdef FEAT_EVAL
690 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000691 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 cstack.cs_trylevel = 0;
693 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000694 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
696
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100697 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100699 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100700 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000701 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 ++ex_nesting_level;
703
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100704 // Get the function or script name and the address where the next breakpoint
705 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000706 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {
708 fname = func_name(real_cookie);
709 breakpoint = func_breakpoint(real_cookie);
710 dbg_tick = func_dbg_tick(real_cookie);
711 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100712 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100714 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 breakpoint = source_breakpoint(real_cookie);
716 dbg_tick = source_dbg_tick(real_cookie);
717 }
718
719 /*
720 * Initialize "force_abort" and "suppress_errthrow" at the top level.
721 */
722 if (!recursive)
723 {
724 force_abort = FALSE;
725 suppress_errthrow = FALSE;
726 }
727
728 /*
729 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000730 * exception handling (used when debugging). Otherwise clear it to avoid
731 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000733 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000734 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000735 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200736 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
738 initial_trylevel = trylevel;
739
740 /*
741 * "did_throw" will be set to TRUE when an exception is being thrown.
742 */
743 did_throw = FALSE;
744#endif
745 /*
746 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000747 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 * If force_abort is set, we cancel everything.
749 */
750 did_emsg = FALSE;
751
752 /*
753 * KeyTyped is only set when calling vgetc(). Reset it here when not
754 * calling vgetc() (sourced command lines).
755 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100756 if (!(flags & DOCMD_KEYTYPED)
757 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 KeyTyped = FALSE;
759
760 /*
761 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000762 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 * - for multiple commands on one line, separated with '|'
764 * - when repeating until there are no more lines (for ":source")
765 */
766 next_cmdline = cmdline;
767 do
768 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000769#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100770 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000771#endif
772
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100773 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 if (next_cmdline == NULL
775#ifdef FEAT_EVAL
776 && !force_abort
777 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000778 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779#endif
780 )
781 did_emsg = FALSE;
782
783 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000784 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100785 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 * 3. If a line is given: Make a copy, so we can mess with it.
787 */
788
789#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100790 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000791 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100793 // Each '|' separated command is stored separately in lines_ga, to
794 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100795 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100797 // Check if a function has returned or, unless it has an unclosed
798 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000799 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000801# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000802 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000803 func_line_end(real_cookie);
804# endif
805 if (func_has_ended(real_cookie))
806 {
807 retval = FAIL;
808 break;
809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000811#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000812 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100813 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000814 script_line_end();
815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100817 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100818 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 {
820 retval = FAIL;
821 break;
822 }
823
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100824 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 if (breakpoint != NULL && dbg_tick != NULL
826 && *dbg_tick != debug_tick)
827 {
828 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100829 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100830 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 *dbg_tick = debug_tick;
832 }
833
834 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100835 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100837 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100839 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100841 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100842 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100844 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100845 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 *dbg_tick = debug_tick;
847 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000848# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000849 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000850 {
851 if (getline_is_func)
852 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100853 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000854 script_line_start();
855 }
856# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858#endif
859
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100860 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 if (next_cmdline == NULL)
862 {
863 /*
864 * Need to set msg_didout for the first line after an ":if",
865 * otherwise the ":if" will be overwritten.
866 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100867 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100869 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870#ifdef FEAT_EVAL
871 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
872#else
873 0
874#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200875 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100877 // Don't call wait_return for aborted command line. The NULL
878 // returned for the end of a sourced file or executed function
879 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 if (KeyTyped && !(flags & DOCMD_REPEAT))
881 need_wait_return = FALSE;
882 retval = FAIL;
883 break;
884 }
885 used_getline = TRUE;
886
887 /*
888 * Keep the first typed line. Clear it when more lines are typed.
889 */
890 if (flags & DOCMD_KEEPLINE)
891 {
892 vim_free(repeat_cmdline);
893 if (count == 0)
894 repeat_cmdline = vim_strsave(next_cmdline);
895 else
896 repeat_cmdline = NULL;
897 }
898 }
899
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100900 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 else if (cmdline_copy == NULL)
902 {
903 next_cmdline = vim_strsave(next_cmdline);
904 if (next_cmdline == NULL)
905 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100906 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 retval = FAIL;
908 break;
909 }
910 }
911 cmdline_copy = next_cmdline;
912
913#ifdef FEAT_EVAL
914 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200915 * Inside a while/for loop, and when the command looks like a ":while"
916 * or ":for", the line is stored, because we may need it later when
917 * looping.
918 *
919 * When there is a '|' and another command, it is stored separately,
920 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000921 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200922 *
923 * Pass a different "fgetline" function to do_one_cmd() below,
924 * that it stores lines in or reads them from "lines_ga". Makes it
925 * possible to define a function inside a while/for loop and handles
926 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200928 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200930 cmd_getline = get_loop_line;
931 cmd_cookie = (void *)&cmd_loop_cookie;
932 cmd_loop_cookie.lines_gap = &lines_ga;
933 cmd_loop_cookie.current_line = current_line;
934 cmd_loop_cookie.getline = fgetline;
935 cmd_loop_cookie.cookie = cookie;
936 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
937
938 // Save the current line when encountering it the first time.
939 if (current_line == lines_ga.ga_len
940 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 {
942 retval = FAIL;
943 break;
944 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200945 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200947 else
948 {
949 cmd_getline = fgetline;
950 cmd_cookie = cookie;
951 }
952
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 did_endif = FALSE;
954#endif
955
956 if (count++ == 0)
957 {
958 /*
959 * All output from the commands is put below each other, without
960 * waiting for a return. Don't do this when executing commands
961 * from a script or when being called recursive (e.g. for ":e
962 * +command file").
963 */
964 if (!(flags & DOCMD_NOWAIT) && !recursive)
965 {
966 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100967 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100969 msg_scroll = TRUE; // put messages below each other
970 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 ++RedrawingDisabled;
972 did_inc = TRUE;
973 }
974 }
975
Bram Moolenaar823654b2020-05-29 23:03:09 +0200976 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100977 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978
979 /*
980 * 2. Execute one '|' separated command.
981 * do_one_cmd() will return NULL if there is no trailing '|'.
982 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
983 */
984 ++recursive;
985 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
986#ifdef FEAT_EVAL
987 &cstack,
988#endif
989 cmd_getline, cmd_cookie);
990 --recursive;
991
992#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000993 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100994 // Use "current_line" from "cmd_loop_cookie", it may have been
995 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000996 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997#endif
998
999 if (next_cmdline == NULL)
1000 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001001 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001002
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 /*
1004 * If the command was typed, remember it for the ':' register.
1005 * Do this AFTER executing the command to make :@: work.
1006 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001007 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 && new_last_cmdline != NULL)
1009 {
1010 vim_free(last_cmdline);
1011 last_cmdline = new_last_cmdline;
1012 new_last_cmdline = NULL;
1013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 }
1015 else
1016 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001017 // need to copy the command after the '|' to cmdline_copy, for the
1018 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001019 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 next_cmdline = cmdline_copy;
1021 }
1022
1023
1024#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001025 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001027 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 && !func_has_abort(real_cookie))
1029 did_emsg = FALSE;
1030
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001031 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 {
1033 ++current_line;
1034
1035 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001036 * An ":endwhile", ":endfor" and ":continue" is handled here.
1037 * If we were executing commands, jump back to the ":while" or
1038 * ":for".
1039 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001041 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001043 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001045 // Jump back to the matching ":while" or ":for". Be careful
1046 // not to use a cs_line[] from an entry that isn't a ":while"
1047 // or ":for": It would make "current_line" invalid and can
1048 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 if (!did_emsg && !got_int && !did_throw
1050 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001051 && (cstack.cs_flags[cstack.cs_idx]
1052 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 && cstack.cs_line[cstack.cs_idx] >= 0
1054 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1055 {
1056 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001057 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001058 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001059 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001061 // Check for the next breakpoint at or after the ":while"
1062 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 if (breakpoint != NULL)
1064 {
1065 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001066 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 fname,
1068 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1069 *dbg_tick = debug_tick;
1070 }
1071 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001072 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001074 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001076 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1077 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 }
1079 }
1080
1081 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001084 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001086 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001087 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 }
1089 }
1090
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001091 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001092 if (breakpoint != NULL && has_watchexpr())
1093 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001094 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001095 *dbg_tick = debug_tick;
1096 }
1097
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 /*
1099 * When not inside any ":while" loop, clear remembered lines.
1100 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001101 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 {
1103 if (lines_ga.ga_len > 0)
1104 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001105 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1107 free_cmdlines(&lines_ga);
1108 }
1109 current_line = 0;
1110 }
1111
1112 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001113 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1114 * being restored at the ":endtry". Reset them here and set the
1115 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1116 * This includes the case where a missing ":endif", ":endwhile" or
1117 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001119 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001121 cstack.cs_lflags &= ~CSL_HAD_FINA;
1122 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1123 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 did_throw ? (void *)current_exception : NULL);
1125 did_emsg = got_int = did_throw = FALSE;
1126 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1127 }
1128
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001129 // Update global "trylevel" for recursive calls to do_cmdline() from
1130 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 trylevel = initial_trylevel + cstack.cs_trylevel;
1132
1133 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001134 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 * files) is aborted because of an error, an interrupt, or an uncaught
1136 * exception, cancel everything. If it is left normally, reset
1137 * force_abort to get the non-EH compatible abortion behavior for
1138 * the rest of the script.
1139 */
1140 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1141 force_abort = FALSE;
1142
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001143 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001145#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146
1147 }
1148 /*
1149 * Continue executing command lines when:
1150 * - no CTRL-C typed, no aborting error, no exception thrown or try
1151 * conditionals need to be checked for executing finally clauses or
1152 * catching an interrupt exception
1153 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001154 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 * looping for ":source" command or function call.
1156 */
1157 while (!((got_int
1158#ifdef FEAT_EVAL
1159 || (did_emsg && force_abort) || did_throw
1160#endif
1161 )
1162#ifdef FEAT_EVAL
1163 && cstack.cs_trylevel == 0
1164#endif
1165 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001166 && !(did_emsg
1167#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001168 // Keep going when inside try/catch, so that the error can be
1169 // deal with, except when it is a syntax error, it may cause
1170 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001171 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1172#endif
1173 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001174 && (getline_equal(fgetline, cookie, getexmodeline)
1175 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 && (next_cmdline != NULL
1177#ifdef FEAT_EVAL
1178 || cstack.cs_idx >= 0
1179#endif
1180 || (flags & DOCMD_REPEAT)));
1181
1182 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001183 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184#ifdef FEAT_EVAL
1185 free_cmdlines(&lines_ga);
1186 ga_clear(&lines_ga);
1187
1188 if (cstack.cs_idx >= 0)
1189 {
1190 /*
1191 * If a sourced file or executed function ran to its end, report the
1192 * unclosed conditional.
1193 */
1194 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001195 && ((getline_equal(fgetline, cookie, getsourceline)
1196 && !source_finished(fgetline, cookie))
1197 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 && !func_has_ended(real_cookie))))
1199 {
1200 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001201 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001203 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001204 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001205 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001207 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 }
1209
1210 /*
1211 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1212 * ":endtry" in a sourced file or executed function. If the try
1213 * conditional is in its finally clause, ignore anything pending.
1214 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001215 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 */
1217 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001218 {
1219 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1220
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001221 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001222 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001223 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1224 &cstack.cs_looplevel);
1225 }
1226 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 trylevel = initial_trylevel;
1228 }
1229
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001230 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1231 // lack was reported above and the error message is to be converted to an
1232 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001233 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 ? (char_u *)"endfunction" : (char_u *)NULL);
1235
1236 if (trylevel == 0)
1237 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001238 // Just in case did_throw got set but current_exception wasn't.
1239 if (current_exception == NULL)
1240 did_throw = FALSE;
1241
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 /*
1243 * When an exception is being thrown out of the outermost try
1244 * conditional, discard the uncaught exception, disable the conversion
1245 * of interrupts or errors to exceptions, and ensure that no more
1246 * commands are executed.
1247 */
1248 if (did_throw)
1249 {
1250 void *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001251 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252
1253 /*
1254 * If the uncaught exception is a user exception, report it as an
1255 * error. If it is an error exception, display the saved error
1256 * message now. For an interrupt exception, do nothing; the
1257 * interrupt message is given elsewhere.
1258 */
1259 switch (current_exception->type)
1260 {
1261 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001262 vim_snprintf((char *)IObuff, IOSIZE,
1263 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 current_exception->value);
1265 p = vim_strsave(IObuff);
1266 break;
1267 case ET_ERROR:
1268 messages = current_exception->messages;
1269 current_exception->messages = NULL;
1270 break;
1271 case ET_INTERRUPT:
1272 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 }
1274
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001275 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1276 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001277 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 current_exception->throw_name = NULL;
1279
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001280 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 suppress_errthrow = TRUE;
1282 force_abort = TRUE;
1283
1284 if (messages != NULL)
1285 {
1286 do
1287 {
1288 next = messages->next;
1289 emsg(messages->msg);
1290 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001291 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 vim_free(messages);
1293 messages = next;
1294 }
1295 while (messages != NULL);
1296 }
1297 else if (p != NULL)
1298 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001299 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 vim_free(p);
1301 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001302 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001303 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001304 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 }
1306
1307 /*
1308 * On an interrupt or an aborting error not converted to an exception,
1309 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001310 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 * when force_abort is set and did_emsg unset in case of an interrupt
1312 * from a finally clause after an error.
1313 */
1314 else if (got_int || (did_emsg && force_abort))
1315 suppress_errthrow = TRUE;
1316 }
1317
1318 /*
1319 * The current cstack will be freed when do_cmdline() returns. An uncaught
1320 * exception will have to be rethrown in the previous cstack. If a function
1321 * has just returned or a script file was just finished and the previous
1322 * cstack belongs to the same function or, respectively, script file, it
1323 * will have to be checked for finally clauses to be executed due to the
1324 * ":return" or ":finish". This is done in do_one_cmd().
1325 */
1326 if (did_throw)
1327 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001328 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001330 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 && ex_nesting_level > func_level(real_cookie) + 1))
1332 {
1333 if (!did_throw)
1334 check_cstack = TRUE;
1335 }
1336 else
1337 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001338 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001339 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 --ex_nesting_level;
1341 /*
1342 * Go to debug mode when returning from a function in which we are
1343 * single-stepping.
1344 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001345 if ((getline_equal(fgetline, cookie, getsourceline)
1346 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001348 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 ? (char_u *)_("End of sourced file")
1350 : (char_u *)_("End of function"));
1351 }
1352
1353 /*
1354 * Restore the exception environment (done after returning from the
1355 * debugger).
1356 */
1357 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001358 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359
1360 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001361
1362 // Cleanup if "cs_emsg_silent_list" remains.
1363 if (cstack.cs_emsg_silent_list != NULL)
1364 {
1365 eslist_T *elem, *temp;
1366
1367 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1368 {
1369 temp = elem->next;
1370 vim_free(elem);
1371 }
1372 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001373#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374
1375 /*
1376 * If there was too much output to fit on the command line, ask the user to
1377 * hit return before redrawing the screen. With the ":global" command we do
1378 * this only once after the command is finished.
1379 */
1380 if (did_inc)
1381 {
1382 --RedrawingDisabled;
1383 --no_wait_return;
1384 msg_scroll = FALSE;
1385
1386 /*
1387 * When just finished an ":if"-":else" which was typed, no need to
1388 * wait for hit-return. Also for an error situation.
1389 */
1390 if (retval == FAIL
1391#ifdef FEAT_EVAL
1392 || (did_endif && KeyTyped && !did_emsg)
1393#endif
1394 )
1395 {
1396 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001397 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 }
1399 else if (need_wait_return)
1400 {
1401 /*
1402 * The msg_start() above clears msg_didout. The wait_return we do
1403 * here should not overwrite the command that may be shown before
1404 * doing that.
1405 */
1406 msg_didout |= msg_didout_before_start;
1407 wait_return(FALSE);
1408 }
1409 }
1410
Bram Moolenaar2a942252012-11-28 23:03:07 +01001411#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001412 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001413#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 /*
1415 * Reset if_level, in case a sourced script file contains more ":if" than
1416 * ":endif" (could be ":if x | foo | endif").
1417 */
1418 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001419#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001420
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 --call_depth;
1422 return retval;
1423}
1424
1425#ifdef FEAT_EVAL
1426/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001427 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 */
1429 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001430get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001432 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 wcmd_T *wp;
1434 char_u *line;
1435
1436 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1437 {
1438 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001439 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001441 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001443 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001445 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001446 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 ++cp->current_line;
1448
1449 return line;
1450 }
1451
1452 KeyTyped = FALSE;
1453 ++cp->current_line;
1454 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001455 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 return vim_strsave(wp->line);
1457}
1458
1459/*
1460 * Store a line in "gap" so that a ":while" loop can execute it again.
1461 */
1462 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001463store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
1465 if (ga_grow(gap, 1) == FAIL)
1466 return FAIL;
1467 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001468 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 return OK;
1471}
1472
1473/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001474 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 */
1476 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001477free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478{
1479 while (gap->ga_len > 0)
1480 {
1481 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1482 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 }
1484}
1485#endif
1486
1487/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001488 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1489 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001492getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001493 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001494 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001495 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496{
1497#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001498 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001499 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001501 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1502 // function that's originally used to obtain the lines. This may be
1503 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001504 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001505 cp = (struct loop_cookie *)cookie;
1506 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 {
1508 gp = cp->getline;
1509 cp = cp->cookie;
1510 }
1511 return gp == func;
1512#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001513 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514#endif
1515}
1516
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001518 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 * getline function. Otherwise return "cookie".
1520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001522getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001523 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001524 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525{
Bram Moolenaar13505972019-01-24 15:04:48 +01001526#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001527 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001528 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001530 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1531 // cookie that's originally used to obtain the lines. This may be nested
1532 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001533 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001534 cp = (struct loop_cookie *)cookie;
1535 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 {
1537 gp = cp->getline;
1538 cp = cp->cookie;
1539 }
1540 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001541#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001544}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545
Bram Moolenaard5053d02020-06-28 15:51:16 +02001546#if defined(FEAT_EVAL) || defined(PROT)
1547/*
1548 * Get the next line source line without advancing.
1549 */
1550 char_u *
1551getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001552 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001553 void *cookie) // argument for fgetline()
1554{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001555 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001556 struct loop_cookie *cp;
1557 wcmd_T *wp;
1558
1559 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1560 // cookie that's originally used to obtain the lines. This may be nested
1561 // several levels.
1562 gp = fgetline;
1563 cp = (struct loop_cookie *)cookie;
1564 while (gp == get_loop_line)
1565 {
1566 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1567 {
1568 // executing lines a second time, use the stored copy
1569 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1570 return wp->line;
1571 }
1572 gp = cp->getline;
1573 cp = cp->cookie;
1574 }
1575 if (gp == getsourceline)
1576 return source_nextline(cp);
1577 return NULL;
1578}
1579#endif
1580
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001581
1582/*
1583 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1584 * ":bwipeout", etc.
1585 * Returns the buffer number.
1586 */
1587 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001588compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001589{
1590 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001591 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001592 int count = offset;
1593
1594 buf = firstbuf;
1595 while (buf->b_next != NULL && buf->b_fnum < lnum)
1596 buf = buf->b_next;
1597 while (count != 0)
1598 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001599 count += (offset < 0) ? 1 : -1;
1600 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1601 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001602 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001603 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001604 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001605 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001606 while (buf->b_ml.ml_mfp == NULL)
1607 {
1608 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1609 if (nextbuf == NULL)
1610 break;
1611 buf = nextbuf;
1612 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001613 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001614 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001615 if (addr_type == ADDR_LOADED_BUFFERS)
1616 while (buf->b_ml.ml_mfp == NULL)
1617 {
1618 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1619 if (nextbuf == NULL)
1620 break;
1621 buf = nextbuf;
1622 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001623 return buf->b_fnum;
1624}
1625
Bram Moolenaarb7316892019-05-01 18:08:42 +02001626/*
1627 * Return the window number of "win".
1628 * When "win" is NULL return the number of windows.
1629 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001630 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001631current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001632{
1633 win_T *wp;
1634 int nr = 0;
1635
Bram Moolenaar29323592016-07-24 22:04:11 +02001636 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001637 {
1638 ++nr;
1639 if (wp == win)
1640 break;
1641 }
1642 return nr;
1643}
1644
1645 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001646current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001647{
1648 tabpage_T *tp;
1649 int nr = 0;
1650
Bram Moolenaar29323592016-07-24 22:04:11 +02001651 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001652 {
1653 ++nr;
1654 if (tp == tab)
1655 break;
1656 }
1657 return nr;
1658}
1659
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001660 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001661comment_start(char_u *p, int starts_with_colon UNUSED)
1662{
1663#ifdef FEAT_EVAL
1664 if (in_vim9script())
1665 return p[0] == '#' && p[1] != '{' && !starts_with_colon;
1666#endif
1667 return *p == '"';
1668}
1669
Bram Moolenaarf240e182014-11-27 18:33:02 +01001670# define CURRENT_WIN_NR current_win_nr(curwin)
1671# define LAST_WIN_NR current_win_nr(NULL)
1672# define CURRENT_TAB_NR current_tab_nr(curtab)
1673# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001674
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675/*
1676 * Execute one Ex command.
1677 *
1678 * If 'sourcing' is TRUE, the command will be included in the error message.
1679 *
1680 * 1. skip comment lines and leading space
1681 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001682 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001683 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001684 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001685 * 6. parse arguments
1686 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001688 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 *
1690 * This function may be called recursively!
1691 */
1692#if (_MSC_VER == 1200)
1693/*
Bram Moolenaared203462004-06-16 11:19:22 +00001694 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001696 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697#endif
1698 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001699do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001700 char_u **cmdlinep,
1701 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001703 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001705 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001706 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001708 char_u *p;
1709 linenr_T lnum;
1710 long n;
1711 char *errormsg = NULL; // error message
1712 char_u *after_modifier = NULL;
1713 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001714 cmdmod_T save_cmdmod;
1715 int save_reg_executing = reg_executing;
1716 int ni; // set when Not Implemented
1717 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001718 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001719#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001720 int may_have_range;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001721 int vim9script = in_vim9script();
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723
Bram Moolenaara80faa82020-04-12 19:37:17 +02001724 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 ea.line1 = 1;
1726 ea.line2 = 1;
1727#ifdef FEAT_EVAL
1728 ++ex_nesting_level;
1729#endif
1730
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001731 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 if (quitmore
1733#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001734 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001735 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001736#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001737 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001738 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 --quitmore;
1740
1741 /*
1742 * Reset browse, confirm, etc.. They are restored when returning, for
1743 * recursive calls.
1744 */
1745 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001747 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001748 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1749 goto doend;
1750
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001751/*
1752 * 1. Skip comment lines and leading white space and colons.
1753 * 2. Handle command modifiers.
1754 */
1755 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001757 ea.cmdlinep = cmdlinep;
1758 ea.getline = fgetline;
1759 ea.cookie = cookie;
1760#ifdef FEAT_EVAL
1761 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001762 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001764 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001765 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001766 apply_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001768 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
1770#ifdef FEAT_EVAL
1771 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1772 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1773#else
1774 ea.skip = (if_level > 0);
1775#endif
1776
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001778 * 3. Skip over the range to find the command. Let "p" point to after it.
1779 *
1780 * We need the command to know what kind of range it uses.
1781 */
1782 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001783#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001784 // In Vim9 script a colon is required before the range.
1785 may_have_range = !vim9script || starts_with_colon;
1786 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001787#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001788 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789
1790#ifdef FEAT_EVAL
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001791 if (vim9script && !starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001792 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001793 if (ea.cmd == cmd + 1 && *cmd == '$')
1794 // should be "$VAR = val"
1795 --ea.cmd;
1796 else if (ea.cmd > cmd)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001797 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001798 emsg(_(e_colon_required_before_a_range));
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001799 goto doend;
1800 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001802 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 else
1804#endif
1805 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001806
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001807#ifdef FEAT_EVAL
1808# ifdef FEAT_PROFILE
1809 // Count this line for profiling if skip is TRUE.
1810 if (do_profiling == PROF_YES
1811 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1812 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1813 {
1814 int skip = did_emsg || got_int || did_throw;
1815
1816 if (ea.cmdidx == CMD_catch)
1817 skip = !skip && !(cstack->cs_idx >= 0
1818 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1819 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1820 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1821 skip = skip || !(cstack->cs_idx >= 0
1822 && !(cstack->cs_flags[cstack->cs_idx]
1823 & (CSF_ACTIVE | CSF_TRUE)));
1824 else if (ea.cmdidx == CMD_finally)
1825 skip = FALSE;
1826 else if (ea.cmdidx != CMD_endif
1827 && ea.cmdidx != CMD_endfor
1828 && ea.cmdidx != CMD_endtry
1829 && ea.cmdidx != CMD_endwhile)
1830 skip = ea.skip;
1831
1832 if (!skip)
1833 {
1834 if (getline_equal(fgetline, cookie, get_func_line))
1835 func_line_exec(getline_cookie(fgetline, cookie));
1836 else if (getline_equal(fgetline, cookie, getsourceline))
1837 script_line_exec();
1838 }
1839 }
1840# endif
1841
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001842 // May go to debug mode. If this happens and the ">quit" debug command is
1843 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001844 dbg_check_breakpoint(&ea);
1845 if (!ea.skip && got_int)
1846 {
1847 ea.skip = TRUE;
1848 (void)do_intthrow(cstack);
1849 }
1850#endif
1851
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001852/*
1853 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 *
1855 * where 'addr' is:
1856 *
1857 * % (entire file)
1858 * $ [+-NUM]
1859 * 'x [+-NUM] (where x denotes a currently defined mark)
1860 * . [+-NUM]
1861 * [+-NUM]..
1862 * NUM
1863 *
1864 * The ea.cmd pointer is updated to point to the first character following the
1865 * range spec. If an initial address is found, but no second, the upper bound
1866 * is equal to the lower.
1867 */
1868
Bram Moolenaar25190db2019-05-04 15:05:28 +02001869 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001870 if (!IS_USER_CMDIDX(ea.cmdidx))
1871 {
1872 if (ea.cmdidx != CMD_SIZE)
1873 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1874 else
1875 ea.addr_type = ADDR_LINES;
1876
Bram Moolenaar25190db2019-05-04 15:05:28 +02001877 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001878 if (ea.cmdidx == CMD_wincmd && p != NULL)
1879 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001880#ifdef FEAT_QUICKFIX
1881 // :.cc in quickfix window uses line number
1882 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1883 ea.addr_type = ADDR_OTHER;
1884#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001885 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001886
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001887 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001888#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001889 if (!may_have_range)
1890 ea.line1 = ea.line2 = default_address(&ea);
1891 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001892#endif
1893 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1894 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001897 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 */
1899
1900 /*
1901 * Skip ':' and any white space
1902 */
1903 ea.cmd = skipwhite(ea.cmd);
1904 while (*ea.cmd == ':')
1905 ea.cmd = skipwhite(ea.cmd + 1);
1906
1907 /*
1908 * If we got a line, but no command, then go to the line.
1909 * If we find a '|' or '\n' we set ea.nextcmd.
1910 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001911 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1912 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 {
1914 /*
1915 * strange vi behaviour:
1916 * ":3" jumps to line 3
1917 * ":3|..." prints line 3
1918 * ":|" prints current line
1919 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001920 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001922 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 {
1924 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001925 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 if ((errormsg = invalid_range(&ea)) == NULL)
1927 {
1928 correct_range(&ea);
1929 ex_print(&ea);
1930 }
1931 }
1932 else if (ea.addr_count != 0)
1933 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001934 if (ea.line2 > curbuf->b_ml.ml_line_count)
1935 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001936 // With '-' in 'cpoptions' a line number past the file is an
1937 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001938 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1939 ea.line2 = -1;
1940 else
1941 ea.line2 = curbuf->b_ml.ml_line_count;
1942 }
1943
1944 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001945 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 else
1947 {
1948 if (ea.line2 == 0)
1949 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 else
1951 curwin->w_cursor.lnum = ea.line2;
1952 beginline(BL_SOL | BL_FIX);
1953 }
1954 }
1955 goto doend;
1956 }
1957
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001958 // If this looks like an undefined user command and there are CmdUndefined
1959 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001960 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1961 && ASCII_ISUPPER(*ea.cmd)
1962 && has_cmdundefined())
1963 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001964 int ret;
1965
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001966 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001967 while (ASCII_ISALNUM(*p))
1968 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001969 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001970 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1971 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001972 // If the autocommands did something and didn't cause an error, try
1973 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001974 p = (ret
1975#ifdef FEAT_EVAL
1976 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001977#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001979 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001980
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 if (p == NULL)
1982 {
1983 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001984 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 goto doend;
1986 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001987 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001988 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1989 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 {
1991 errormsg = uc_fun_cmd();
1992 goto doend;
1993 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001994
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 if (ea.cmdidx == CMD_SIZE)
1996 {
1997 if (!ea.skip)
1998 {
1999 STRCPY(IObuff, _("E492: Not an editor command"));
2000 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002001 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002002 // If the modifier was parsed OK the error must be in the
2003 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002004 if (after_modifier != NULL)
2005 append_command(after_modifier);
2006 else
2007 append_command(*cmdlinep);
2008 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002009 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002010 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 }
2012 goto doend;
2013 }
2014
Bram Moolenaar958636c2014-10-21 20:01:58 +02002015 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2016 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002017#ifdef HAVE_EX_SCRIPT_NI
2018 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2019#endif
2020 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
2022#ifndef FEAT_EVAL
2023 /*
2024 * When the expression evaluation is disabled, recognize the ":if" and
2025 * ":endif" commands and ignore everything in between it.
2026 */
2027 if (ea.cmdidx == CMD_if)
2028 ++if_level;
2029 if (if_level)
2030 {
2031 if (ea.cmdidx == CMD_endif)
2032 --if_level;
2033 goto doend;
2034 }
2035
2036#endif
2037
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002038 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002039 if (*p == '!' && ea.cmdidx != CMD_substitute
2040 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {
2042 ++p;
2043 ea.forceit = TRUE;
2044 }
2045 else
2046 ea.forceit = FALSE;
2047
2048/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002049 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002051 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002052 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053
2054 if (!ea.skip)
2055 {
2056#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002057 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002059 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002060 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 goto doend;
2062 }
2063#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002064 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002065 {
2066 errormsg = _("E981: Command not allowed in rvim");
2067 goto doend;
2068 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002069 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002071 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002072 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 goto doend;
2074 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002075
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002076 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002078#ifdef FEAT_CMDWIN
2079 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2080 {
2081 // Command not allowed in the command line window
2082 errormsg = _(e_cmdwin);
2083 goto doend;
2084 }
2085#endif
2086 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2087 {
2088 // Command not allowed when text is locked
2089 errormsg = _(get_text_locked_msg());
2090 goto doend;
2091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002093
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002094 // Disallow editing another buffer when "curbuf_lock" is set.
2095 // Do allow ":checktime" (it is postponed).
2096 // Do allow ":edit" (check for an argument later).
2097 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002098 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002099 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002100 && ea.cmdidx != CMD_edit
2101 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002102 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002103 && curbuf_locked())
2104 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002106 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002108 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002109 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 goto doend;
2111 }
2112 }
2113
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002114 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002116 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 goto doend;
2118 }
2119
2120 /*
2121 * Don't complain about the range if it is not used
2122 * (could happen if line_count is accidentally set to 0).
2123 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002124 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 {
2126 /*
2127 * If the range is backwards, ask for confirmation and, if given, swap
2128 * ea.line1 & ea.line2 so it's forwards again.
2129 * When global command is busy, don't ask, will fail below.
2130 */
2131 if (!global_busy && ea.line1 > ea.line2)
2132 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002133 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002135 if (sourcing || exmode_active)
2136 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002137 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002138 goto doend;
2139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 if (ask_yesno((char_u *)
2141 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002142 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 }
2144 lnum = ea.line1;
2145 ea.line1 = ea.line2;
2146 ea.line2 = lnum;
2147 }
2148 if ((errormsg = invalid_range(&ea)) != NULL)
2149 goto doend;
2150 }
2151
Bram Moolenaarb7316892019-05-01 18:08:42 +02002152 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2153 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 ea.line2 = 1;
2155
2156 correct_range(&ea);
2157
2158#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002159 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002160 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002162 // Put the first line at the start of a closed fold, put the last line
2163 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 (void)hasFolding(ea.line1, &ea.line1, NULL);
2165 (void)hasFolding(ea.line2, NULL, &ea.line2);
2166 }
2167#endif
2168
2169#ifdef FEAT_QUICKFIX
2170 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002171 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 * option here, so things like % get expanded.
2173 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002174 p = replace_makeprg(&ea, p, cmdlinep);
2175 if (p == NULL)
2176 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177#endif
2178
2179 /*
2180 * Skip to start of argument.
2181 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2182 */
2183 if (ea.cmdidx == CMD_bang)
2184 ea.arg = p;
2185 else
2186 ea.arg = skipwhite(p);
2187
Bram Moolenaar379fb762018-08-30 15:58:28 +02002188 // ":file" cannot be run with an argument when "curbuf_lock" is set
2189 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2190 goto doend;
2191
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 /*
2193 * Check for "++opt=val" argument.
2194 * Must be first, allow ":w ++enc=utf8 !cmd"
2195 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002196 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2198 if (getargopt(&ea) == FAIL && !ni)
2199 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002200 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 goto doend;
2202 }
2203
2204 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2205 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002206 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002208 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002210 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 goto doend;
2212 }
2213 ea.arg = skipwhite(ea.arg + 1);
2214 ea.append = TRUE;
2215 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002216 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 {
2218 ++ea.arg;
2219 ea.usefilter = TRUE;
2220 }
2221 }
2222
2223 if (ea.cmdidx == CMD_read)
2224 {
2225 if (ea.forceit)
2226 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002227 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 ea.forceit = FALSE;
2229 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002230 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 {
2232 ++ea.arg;
2233 ea.usefilter = TRUE;
2234 }
2235 }
2236
2237 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2238 {
2239 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002240 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 {
2242 ++ea.arg;
2243 ++ea.amount;
2244 }
2245 ea.arg = skipwhite(ea.arg);
2246 }
2247
2248 /*
2249 * Check for "+command" argument, before checking for next command.
2250 * Don't do this for ":read !cmd" and ":write !cmd".
2251 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002252 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2254
2255 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002256 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 * Don't do this for ":read !cmd" and ":write !cmd".
2258 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002259 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 separate_nextcmd(&ea);
2261
2262 /*
2263 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002264 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2265 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002267 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002268 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002269 || ea.cmdidx == CMD_global
2270 || ea.cmdidx == CMD_vglobal
2271 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
2273 for (p = ea.arg; *p; ++p)
2274 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002275 // Remove one backslash before a newline, so that it's possible to
2276 // pass a newline to the shell and also a newline that is preceded
2277 // with a backslash. This makes it impossible to end a shell
2278 // command in a backslash, but that doesn't appear useful.
2279 // Halving the number of backslashes is incompatible with previous
2280 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002282 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 else if (*p == '\n')
2284 {
2285 ea.nextcmd = p + 1;
2286 *p = NUL;
2287 break;
2288 }
2289 }
2290 }
2291
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002292 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002293 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002295 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002296 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002298 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002299 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002300 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002302#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002303 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002304 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002306 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002307 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 }
2309#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002310 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2311 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002312 {
2313 ea.regname = *ea.arg++;
2314#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002315 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002316 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2317 {
2318 set_expr_line(vim_strsave(ea.arg));
2319 ea.arg += STRLEN(ea.arg);
2320 }
2321#endif
2322 ea.arg = skipwhite(ea.arg);
2323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 }
2325
2326 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002327 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 * count, it's a buffer name.
2329 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002330 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2331 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002332 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 {
2334 n = getdigits(&ea.arg);
2335 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002336 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002338 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 goto doend;
2340 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002341 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 {
2343 ea.line2 = n;
2344 if (ea.addr_count == 0)
2345 ea.addr_count = 1;
2346 }
2347 else
2348 {
2349 ea.line1 = ea.line2;
2350 ea.line2 += n - 1;
2351 ++ea.addr_count;
2352 /*
2353 * Be vi compatible: no error message for out of range.
2354 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002355 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 ea.line2 = curbuf->b_ml.ml_line_count;
2357 }
2358 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002359
2360 /*
2361 * Check for flags: 'l', 'p' and '#'.
2362 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002363 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002364 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002365 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2366 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002368 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002369 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 goto doend;
2371 }
2372
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002373 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002375 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 goto doend;
2377 }
2378
2379#ifdef FEAT_EVAL
2380 /*
2381 * Skip the command when it's not going to be executed.
2382 * The commands like :if, :endif, etc. always need to be executed.
2383 * Also make an exception for commands that handle a trailing command
2384 * themselves.
2385 */
2386 if (ea.skip)
2387 {
2388 switch (ea.cmdidx)
2389 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002390 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 case CMD_while:
2392 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002393 case CMD_for:
2394 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 case CMD_if:
2396 case CMD_elseif:
2397 case CMD_else:
2398 case CMD_endif:
2399 case CMD_try:
2400 case CMD_catch:
2401 case CMD_finally:
2402 case CMD_endtry:
2403 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002404 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 break;
2406
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002407 // Commands that handle '|' themselves. Check: A command should
2408 // either have the EX_TRLBAR flag, appear in this list or appear in
2409 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 case CMD_aboveleft:
2411 case CMD_and:
2412 case CMD_belowright:
2413 case CMD_botright:
2414 case CMD_browse:
2415 case CMD_call:
2416 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002417 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 case CMD_delfunction:
2419 case CMD_djump:
2420 case CMD_dlist:
2421 case CMD_dsearch:
2422 case CMD_dsplit:
2423 case CMD_echo:
2424 case CMD_echoerr:
2425 case CMD_echomsg:
2426 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002427 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002429 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002430 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 case CMD_help:
2432 case CMD_hide:
2433 case CMD_ijump:
2434 case CMD_ilist:
2435 case CMD_isearch:
2436 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002437 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 case CMD_keepjumps:
2439 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002440 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 case CMD_leftabove:
2442 case CMD_let:
2443 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002444 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002445 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002447 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002448 case CMD_noautocmd:
2449 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 case CMD_perl:
2451 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002452 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002453 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002454 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 case CMD_return:
2456 case CMD_rightbelow:
2457 case CMD_ruby:
2458 case CMD_silent:
2459 case CMD_smagic:
2460 case CMD_snomagic:
2461 case CMD_substitute:
2462 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002463 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 case CMD_tcl:
2465 case CMD_throw:
2466 case CMD_tilde:
2467 case CMD_topleft:
2468 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002469 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002470 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 case CMD_verbose:
2472 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002473 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 break;
2475
2476 default: goto doend;
2477 }
2478 }
2479#endif
2480
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002481 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 {
2483 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2484 goto doend;
2485 }
2486
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 /*
2488 * Accept buffer name. Cannot be used at the same time with a buffer
2489 * number. Don't do this for a user command.
2490 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002491 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002492 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 {
2494 /*
2495 * :bdelete, :bwipeout and :bunload take several arguments, separated
2496 * by spaces: find next space (skipping over escaped characters).
2497 * The others take one argument: ignore trailing spaces.
2498 */
2499 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2500 || ea.cmdidx == CMD_bunload)
2501 p = skiptowhite_esc(ea.arg);
2502 else
2503 {
2504 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002505 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 --p;
2507 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002508 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002509 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002510 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 goto doend;
2512 ea.addr_count = 1;
2513 ea.arg = skipwhite(p);
2514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002516 // The :try command saves the emsg_silent flag, reset it here when
2517 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002518 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002519 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002520 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002521 if (emsg_silent < 0)
2522 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002523 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002524 }
2525
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002527 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
Bram Moolenaar958636c2014-10-21 20:01:58 +02002530 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 {
2532 /*
2533 * Execute a user-defined command.
2534 */
2535 do_ucmd(&ea);
2536 }
2537 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 {
2539 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002540 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 */
2542 ea.errmsg = NULL;
2543 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2544 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002545 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 }
2547
2548#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002550 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2551 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002552 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 /*
2555 * If the command just executed called do_cmdline(), any throw or ":return"
2556 * or ":finish" encountered there must also check the cstack of the still
2557 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2558 * exception, or reanimate a returned function or finished script file and
2559 * return or finish it again.
2560 */
2561 if (need_rethrow)
2562 do_throw(cstack);
2563 else if (check_cstack)
2564 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002565 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002567 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 && current_func_returned())
2569 do_return(&ea, TRUE, FALSE, NULL);
2570 }
2571 need_rethrow = check_cstack = FALSE;
2572#endif
2573
2574doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002575 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002576 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002578 curwin->w_cursor.col = 0;
2579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580
2581 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2582 {
2583 if (sourcing)
2584 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002585 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 {
2587 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002588 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002590 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 }
2592 emsg(errormsg);
2593 }
2594#ifdef FEAT_EVAL
2595 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002596 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2597 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598#endif
2599
Bram Moolenaare1004402020-10-24 20:49:43 +02002600 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002602 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002604 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 ea.nextcmd = NULL;
2606
2607#ifdef FEAT_EVAL
2608 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002609 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610#endif
2611
2612 return ea.nextcmd;
2613}
2614#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002615 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616#endif
2617
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002618static char ex_error_buf[MSG_BUF_LEN];
2619
2620/*
2621 * Return an error message with argument included.
2622 * Uses a static buffer, only the last error will be kept.
2623 * "msg" will be translated, caller should use N_().
2624 */
2625 char *
2626ex_errmsg(char *msg, char_u *arg)
2627{
2628 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2629 return ex_error_buf;
2630}
2631
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002633 * Parse and skip over command modifiers:
2634 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002635 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002636 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002637 * When "skip_only" is TRUE the global variables are not changed, except for
2638 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002639 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2640 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002641 * Call apply_cmdmod() to get the side effects of the modifiers:
2642 * - Increment "sandbox" for ":sandbox"
2643 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002644 * - set msg_silent for ":silent"
2645 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002646 * Return FAIL when the command is not to be executed.
2647 * May set "errormsg" to an error message.
2648 */
2649 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002650parse_command_modifiers(
2651 exarg_T *eap,
2652 char **errormsg,
2653 cmdmod_T *cmod,
2654 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002655{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002656 char_u *p;
2657 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002658
Bram Moolenaare1004402020-10-24 20:49:43 +02002659 CLEAR_POINTER(cmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002660
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002661 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002662 for (;;)
2663 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002664 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002665 {
2666 if (*eap->cmd == ':')
2667 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002668 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002669 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002670
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002671 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002672 if (*eap->cmd == NUL && exmode_active
2673 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2674 || getline_equal(eap->getline, eap->cookie, getexline))
2675 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2676 {
2677 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002678 if (!skip_only)
2679 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002680 }
2681
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002682 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002683 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002684 return FAIL;
2685 if (*eap->cmd == NUL)
2686 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002687 if (!skip_only)
2688 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002689 return FAIL;
2690 }
2691
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002692 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002693 switch (*p)
2694 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002695 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002696 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2697 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002698 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002699 continue;
2700
2701 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2702 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002703 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002704 continue;
2705 }
2706 if (checkforcmd(&eap->cmd, "browse", 3))
2707 {
2708#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002709 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002710#endif
2711 continue;
2712 }
2713 if (!checkforcmd(&eap->cmd, "botright", 2))
2714 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002715 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002716 continue;
2717
2718 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2719 break;
2720#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002721 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002722#endif
2723 continue;
2724
2725 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2726 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002727 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002728 continue;
2729 }
2730 if (checkforcmd(&eap->cmd, "keepalt", 5))
2731 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002732 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002733 continue;
2734 }
2735 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2736 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002737 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002738 continue;
2739 }
2740 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2741 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002742 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002743 continue;
2744
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002745 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002746 {
2747 char_u *reg_pat;
2748
2749 if (!checkforcmd(&p, "filter", 4)
2750 || *p == NUL || ends_excmd(*p))
2751 break;
2752 if (*p == '!')
2753 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002754 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002755 p = skipwhite(p + 1);
2756 if (*p == NUL || ends_excmd(*p))
2757 break;
2758 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002759#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002760 // Avoid that "filter(arg)" is recognized.
2761 if (in_vim9script() && !VIM_ISWHITE(*p))
2762 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002763#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002764 if (skip_only)
2765 p = skip_vimgrep_pat(p, NULL, NULL);
2766 else
2767 // NOTE: This puts a NUL after the pattern.
2768 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002769 if (p == NULL || *p == NUL)
2770 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002771 if (!skip_only)
2772 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002773 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002774 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002775 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002776 break;
2777 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002778 eap->cmd = p;
2779 continue;
2780 }
2781
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002782 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002783 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2784 || *p == NUL || ends_excmd(*p))
2785 break;
2786 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002787 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002788 continue;
2789
2790 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2791 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002792 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002793 continue;
2794 }
2795
2796 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2797 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002798 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002799 continue;
2800
2801 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2802 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002803 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002804 continue;
2805 }
2806 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2807 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002808 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002809 continue;
2810
2811 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2812 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002813 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002814 continue;
2815
2816 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2817 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002818 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02002819 continue;
2820 }
2821 if (!checkforcmd(&eap->cmd, "silent", 3))
2822 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002823 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002824 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2825 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002826 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002827 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02002828 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002829 }
2830 continue;
2831
2832 case 't': if (checkforcmd(&p, "tab", 3))
2833 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002834 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002835 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002836 long tabnr = get_address(eap, &eap->cmd,
2837 ADDR_TABS, eap->skip,
2838 skip_only, FALSE, 1);
2839 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02002840 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01002841 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002842 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002843 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2844 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002845 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002846 return FAIL;
2847 }
Bram Moolenaare1004402020-10-24 20:49:43 +02002848 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002849 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002850 }
2851 eap->cmd = p;
2852 continue;
2853 }
2854 if (!checkforcmd(&eap->cmd, "topleft", 2))
2855 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002856 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02002857 continue;
2858
2859 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2860 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002861 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002862 continue;
2863
2864 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2865 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002866 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002867 continue;
2868 }
2869 if (!checkforcmd(&p, "verbose", 4))
2870 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002871 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02002872 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002873 else
Bram Moolenaare1004402020-10-24 20:49:43 +02002874 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002875 eap->cmd = p;
2876 continue;
2877 }
2878 break;
2879 }
2880
2881 return OK;
2882}
2883
2884/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002885 * Apply the command modifiers. Saves current state in "cmdmod", call
2886 * undo_cmdmod() later.
2887 */
2888 void
2889apply_cmdmod(cmdmod_T *cmod)
2890{
2891#ifdef HAVE_SANDBOX
2892 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
2893 {
2894 ++sandbox;
2895 cmod->cmod_did_sandbox = TRUE;
2896 }
2897#endif
2898 if (cmod->cmod_verbose > 0)
2899 {
2900 if (cmod->cmod_verbose_save == 0)
2901 cmod->cmod_verbose_save = p_verbose + 1;
2902 p_verbose = cmod->cmod_verbose;
2903 }
2904
2905 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
2906 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02002907 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002908 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02002909 cmod->cmod_save_msg_scroll = msg_scroll;
2910 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002911 if (cmod->cmod_flags & CMOD_SILENT)
2912 ++msg_silent;
2913 if (cmod->cmod_flags & CMOD_UNSILENT)
2914 msg_silent = 0;
2915
2916 if (cmod->cmod_flags & CMOD_ERRSILENT)
2917 {
2918 ++emsg_silent;
2919 ++cmod->cmod_did_esilent;
2920 }
2921
Bram Moolenaare1004402020-10-24 20:49:43 +02002922 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002923 {
2924 // Set 'eventignore' to "all".
2925 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02002926 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002927 set_string_option_direct((char_u *)"ei", -1,
2928 (char_u *)"all", OPT_FREE, SID_NONE);
2929 }
2930}
2931
2932/*
Bram Moolenaare1004402020-10-24 20:49:43 +02002933 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002934 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002935 void
Bram Moolenaare1004402020-10-24 20:49:43 +02002936undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002937{
Bram Moolenaare1004402020-10-24 20:49:43 +02002938 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002939 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002940 p_verbose = cmod->cmod_verbose_save - 1;
2941 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002942 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002943
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002944#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02002945 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002946 {
2947 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02002948 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002949 }
2950#endif
2951
Bram Moolenaare1004402020-10-24 20:49:43 +02002952 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002953 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002954 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02002955 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
2956 OPT_FREE, SID_NONE);
2957 free_string_option(cmod->cmod_save_ei);
2958 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002959 }
2960
Bram Moolenaare1004402020-10-24 20:49:43 +02002961 if (cmod->cmod_filter_regmatch.regprog != NULL)
2962 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002963
Bram Moolenaare1004402020-10-24 20:49:43 +02002964 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002965 {
2966 // messages could be enabled for a serious error, need to check if the
2967 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02002968 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
2969 msg_silent = cmod->cmod_save_msg_silent - 1;
2970 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002971 if (emsg_silent < 0)
2972 emsg_silent = 0;
2973 // Restore msg_scroll, it's set by file I/O commands, even when no
2974 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02002975 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002976
2977 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2978 // somewhere in the line. Put it back in the first column.
2979 if (redirecting())
2980 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002981
Bram Moolenaare1004402020-10-24 20:49:43 +02002982 cmod->cmod_save_msg_silent = 0;
2983 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002984 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002985}
2986
2987/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002988 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002989 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002990 * Return FAIL and set "errormsg" or return OK.
2991 */
2992 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002993parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002994{
2995 int address_count = 1;
2996 linenr_T lnum;
2997
2998 // Repeat for all ',' or ';' separated addresses.
2999 for (;;)
3000 {
3001 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003002 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003003 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003004 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003005 eap->addr_count == 0, address_count++);
3006 if (eap->cmd == NULL) // error detected
3007 return FAIL;
3008 if (lnum == MAXLNUM)
3009 {
3010 if (*eap->cmd == '%') // '%' - all lines
3011 {
3012 ++eap->cmd;
3013 switch (eap->addr_type)
3014 {
3015 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003016 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003017 eap->line1 = 1;
3018 eap->line2 = curbuf->b_ml.ml_line_count;
3019 break;
3020 case ADDR_LOADED_BUFFERS:
3021 {
3022 buf_T *buf = firstbuf;
3023
3024 while (buf->b_next != NULL
3025 && buf->b_ml.ml_mfp == NULL)
3026 buf = buf->b_next;
3027 eap->line1 = buf->b_fnum;
3028 buf = lastbuf;
3029 while (buf->b_prev != NULL
3030 && buf->b_ml.ml_mfp == NULL)
3031 buf = buf->b_prev;
3032 eap->line2 = buf->b_fnum;
3033 break;
3034 }
3035 case ADDR_BUFFERS:
3036 eap->line1 = firstbuf->b_fnum;
3037 eap->line2 = lastbuf->b_fnum;
3038 break;
3039 case ADDR_WINDOWS:
3040 case ADDR_TABS:
3041 if (IS_USER_CMDIDX(eap->cmdidx))
3042 {
3043 eap->line1 = 1;
3044 eap->line2 = eap->addr_type == ADDR_WINDOWS
3045 ? LAST_WIN_NR : LAST_TAB_NR;
3046 }
3047 else
3048 {
3049 // there is no Vim command which uses '%' and
3050 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003051 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003052 return FAIL;
3053 }
3054 break;
3055 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003056 case ADDR_UNSIGNED:
3057 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003058 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003059 return FAIL;
3060 case ADDR_ARGUMENTS:
3061 if (ARGCOUNT == 0)
3062 eap->line1 = eap->line2 = 0;
3063 else
3064 {
3065 eap->line1 = 1;
3066 eap->line2 = ARGCOUNT;
3067 }
3068 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003069 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003070#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003071 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003072 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003073 if (eap->line2 == 0)
3074 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003075#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003076 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003077 case ADDR_NONE:
3078 // Will give an error later if a range is found.
3079 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003080 }
3081 ++eap->addr_count;
3082 }
3083 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3084 {
3085 pos_T *fp;
3086
3087 // '*' - visual area
3088 if (eap->addr_type != ADDR_LINES)
3089 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003090 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003091 return FAIL;
3092 }
3093
3094 ++eap->cmd;
3095 if (!eap->skip)
3096 {
3097 fp = getmark('<', FALSE);
3098 if (check_mark(fp) == FAIL)
3099 return FAIL;
3100 eap->line1 = fp->lnum;
3101 fp = getmark('>', FALSE);
3102 if (check_mark(fp) == FAIL)
3103 return FAIL;
3104 eap->line2 = fp->lnum;
3105 ++eap->addr_count;
3106 }
3107 }
3108 }
3109 else
3110 eap->line2 = lnum;
3111 eap->addr_count++;
3112
3113 if (*eap->cmd == ';')
3114 {
3115 if (!eap->skip)
3116 {
3117 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003118 // Don't leave the cursor on an illegal line or column, but do
3119 // accept zero as address, so 0;/PATTERN/ works correctly.
3120 if (eap->line2 > 0)
3121 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003122 }
3123 }
3124 else if (*eap->cmd != ',')
3125 break;
3126 ++eap->cmd;
3127 }
3128
3129 // One address given: set start and end lines.
3130 if (eap->addr_count == 1)
3131 {
3132 eap->line1 = eap->line2;
3133 // ... but only implicit: really no address given
3134 if (lnum == MAXLNUM)
3135 eap->addr_count = 0;
3136 }
3137 return OK;
3138}
3139
3140/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003141 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 * If there is a match advance "pp" to the argument and return TRUE.
3143 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003144 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003145checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003146 char_u **pp, // start of command
3147 char *cmd, // name of command
3148 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149{
3150 int i;
3151
3152 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003153 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 break;
Bram Moolenaar7a66a172020-10-16 19:56:12 +02003155 if (i >= len && !isalpha((*pp)[i]) && (*pp)[i] != '_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 {
3157 *pp = skipwhite(*pp + i);
3158 return TRUE;
3159 }
3160 return FALSE;
3161}
3162
3163/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003164 * Append "cmd" to the error message in IObuff.
3165 * Takes care of limiting the length and handling 0xa0, which would be
3166 * invisible otherwise.
3167 */
3168 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003169append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003170{
3171 char_u *s = cmd;
3172 char_u *d;
3173
3174 STRCAT(IObuff, ": ");
3175 d = IObuff + STRLEN(IObuff);
3176 while (*s != NUL && d - IObuff < IOSIZE - 7)
3177 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003178 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003179 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003180 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003181 STRCPY(d, "<a0>");
3182 d += 4;
3183 }
3184 else
3185 MB_COPY_CHAR(s, d);
3186 }
3187 *d = NUL;
3188}
3189
3190/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003191 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3192 * the name. Otherwise just return "start".
3193 */
3194 char_u *
3195skip_option_env_lead(char_u *start)
3196{
3197 char_u *name = start;
3198
3199 if (*start == '&')
3200 {
3201 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3202 name += 3;
3203 else
3204 name += 1;
3205 }
3206 else if (*start == '$')
3207 name += 1;
3208 return name;
3209}
3210
3211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003213 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003214 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003215 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 *
3217 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3218 * assignment without "let". Sets eap->cmdidx to the command while returning
3219 * "eap->cmd".
3220 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 * Returns NULL for an ambiguous user command.
3222 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 char_u *
3224find_ex_command(
3225 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003226 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003227 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003228 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229{
3230 int len;
3231 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003232 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234#ifdef FEAT_EVAL
3235 /*
3236 * Recognize a Vim9 script function/method call and assignment:
3237 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3238 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003239 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003240 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003242 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243
Bram Moolenaar83144542020-08-02 20:40:43 +02003244 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003245 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003247 int oplen;
3248 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003250 if (
3251 // "(..." is an expression.
3252 // "funcname(" is always a function call.
3253 *p == '('
3254 || (p == eap->cmd
3255 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003256 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003257 *eap->cmd == '{'
3258 // "'string'->func()" is an expression.
3259 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003260 // '"string"->func()' is an expression.
3261 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003262 // "g:varname" is an expression.
3263 || eap->cmd[1] == ':'
3264 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003265 // "varname->func()" is an expression.
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003266 : (*p == '-' && p[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003267 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003268 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3269 {
3270 // "{" by itself is the start of a block.
3271 eap->cmdidx = CMD_block;
3272 return eap->cmd + 1;
3273 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003274 eap->cmdidx = CMD_eval;
3275 return eap->cmd;
3276 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003277
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003278 if (p != eap->cmd && (
3279 // "varname[]" is an expression.
3280 *p == '['
3281 // "varname.key" is an expression.
3282 || (*p == '.' && ASCII_ISALPHA(p[1]))))
3283 {
3284 char_u *after = p;
3285
3286 // When followed by "=" or "+=" then it is an assignment.
3287 ++emsg_silent;
Bram Moolenaar683581e2020-10-22 21:22:58 +02003288 if (skip_expr(&after, NULL) == OK
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003289 && (*after == '='
3290 || (*after != NUL && after[1] == '=')))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003291 eap->cmdidx = CMD_var;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003292 else
3293 eap->cmdidx = CMD_eval;
3294 --emsg_silent;
3295 return eap->cmd;
3296 }
3297
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003298 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3299 // an assignment.
3300 // If there is no line break inside the "[...]" then "p" is
3301 // advanced to after the "]" by to_name_const_end(): check if a "="
3302 // follows.
3303 // If "[...]" has a line break "p" still points at the "[" and it
3304 // can't be an assignment.
3305 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003306 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003307 p = to_name_const_end(eap->cmd);
3308 if (p == eap->cmd || *skipwhite(p) != '=')
3309 {
3310 eap->cmdidx = CMD_eval;
3311 return eap->cmd;
3312 }
3313 if (p > eap->cmd && *skipwhite(p) == '=')
3314 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003315 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003316 return eap->cmd;
3317 }
3318 }
3319
3320 // Recognize an assignment if we recognize the variable name:
3321 // "g:var = expr"
3322 // "var = expr" where "var" is a local var name.
Bram Moolenaar83144542020-08-02 20:40:43 +02003323 if (*eap->cmd == '@')
3324 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003325 oplen = assignment_len(skipwhite(p), &heredoc);
3326 if (oplen > 0)
3327 {
3328 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3329 || *eap->cmd == '&'
3330 || *eap->cmd == '$'
3331 || *eap->cmd == '@'
3332 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
3333 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003334 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003335 return eap->cmd;
3336 }
3337 }
3338
3339 // Recognize using a type for a w:, b:, t: or g: variable:
3340 // "w:varname: number = 123".
3341 if (eap->cmd[1] == ':' && *p == ':')
3342 {
3343 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003344 return eap->cmd;
3345 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003346 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347 }
3348#endif
3349
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 /*
3351 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003352 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 * - the 'k' command can directly be followed by any character.
3354 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003355 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003357 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 */
3359 p = eap->cmd;
3360 if (*p == 'k')
3361 {
3362 eap->cmdidx = CMD_k;
3363 ++p;
3364 }
3365 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003366 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3367 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 || p[1] == 'g'
3369 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3370 || p[1] == 'I'
3371 || (p[1] == 'r' && p[2] != 'e')))
3372 {
3373 eap->cmdidx = CMD_substitute;
3374 ++p;
3375 }
3376 else
3377 {
3378 while (ASCII_ISALPHA(*p))
3379 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003380 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003381 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003383 while (ASCII_ISALNUM(*p))
3384 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 }
3386 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3387 {
3388 // include "9" for "vim9script"
3389 ++p;
3390 while (ASCII_ISALPHA(*p))
3391 ++p;
3392 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003393
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003394 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003395 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 ++p;
3397 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003398 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3399 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003400 // Check for ":dl", ":dell", etc. to ":deletel": that's
3401 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003402 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003403 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003404 break;
3405 if (i == len - 1)
3406 {
3407 --len;
3408 if (p[-1] == 'l')
3409 eap->flags |= EXFLAG_LIST;
3410 else
3411 eap->flags |= EXFLAG_PRINT;
3412 }
3413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003415 if (ASCII_ISLOWER(eap->cmd[0]))
3416 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003417 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003418 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003419
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003420 if (command_count != (int)CMD_SIZE)
3421 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003422 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003423 getout(1);
3424 }
3425
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003426 // Use a precomputed index for fast look-up in cmdnames[]
3427 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003428 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3429 if (ASCII_ISLOWER(c2))
3430 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3431 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003432 else if (ASCII_ISUPPER(eap->cmd[0]))
3433 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003435 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
3437 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3438 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3439 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3440 (size_t)len) == 0)
3441 {
3442#ifdef FEAT_EVAL
3443 if (full != NULL
3444 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3445 *full = TRUE;
3446#endif
3447 break;
3448 }
3449
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003450 // Look for a user defined command as a last resort. Let ":Print" be
3451 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003452 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3453 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003455 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 while (ASCII_ISALNUM(*p))
3457 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003458 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003460 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 eap->cmdidx = CMD_SIZE;
3462 }
3463
Bram Moolenaar373863e2020-09-26 17:20:53 +02003464 // ":fina" means ":finally" for backwards compatibility.
3465 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3466 eap->cmdidx = CMD_finally;
3467
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 return p;
3469}
3470
3471#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003472static struct cmdmod
3473{
3474 char *name;
3475 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003476 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003477} cmdmods[] = {
3478 {"aboveleft", 3, FALSE},
3479 {"belowright", 3, FALSE},
3480 {"botright", 2, FALSE},
3481 {"browse", 3, FALSE},
3482 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003483 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003484 {"hide", 3, FALSE},
3485 {"keepalt", 5, FALSE},
3486 {"keepjumps", 5, FALSE},
3487 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003488 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003489 {"leftabove", 5, FALSE},
3490 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003491 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003492 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003493 {"rightbelow", 6, FALSE},
3494 {"sandbox", 3, FALSE},
3495 {"silent", 3, FALSE},
3496 {"tab", 3, TRUE},
3497 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003498 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003499 {"verbose", 4, TRUE},
3500 {"vertical", 4, FALSE},
3501};
3502
3503/*
3504 * Return length of a command modifier (including optional count).
3505 * Return zero when it's not a modifier.
3506 */
3507 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003508modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003509{
3510 int i, j;
3511 char_u *p = cmd;
3512
3513 if (VIM_ISDIGIT(*cmd))
3514 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003515 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003516 {
3517 for (j = 0; p[j] != NUL; ++j)
3518 if (p[j] != cmdmods[i].name[j])
3519 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003520 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003521 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003522 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003523 }
3524 return 0;
3525}
3526
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527/*
3528 * Return > 0 if an Ex command "name" exists.
3529 * Return 2 if there is an exact match.
3530 * Return 3 if there is an ambiguous match.
3531 */
3532 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003533cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534{
3535 exarg_T ea;
3536 int full = FALSE;
3537 int i;
3538 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003539 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003541 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003542 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 {
3544 for (j = 0; name[j] != NUL; ++j)
3545 if (name[j] != cmdmods[i].name[j])
3546 break;
3547 if (name[j] == NUL && j >= cmdmods[i].minlen)
3548 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3549 }
3550
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003551 // Check built-in commands and user defined commands.
3552 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003553 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003556 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003558 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3559 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003560 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003561 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3563}
3564#endif
3565
Bram Moolenaard0190392019-08-23 21:17:35 +02003566 cmdidx_T
3567excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568{
Bram Moolenaard0190392019-08-23 21:17:35 +02003569 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570
Bram Moolenaard0190392019-08-23 21:17:35 +02003571 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3572 idx = (cmdidx_T)((int)idx + 1))
3573 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 break;
3575
Bram Moolenaard0190392019-08-23 21:17:35 +02003576 return idx;
3577}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578
Bram Moolenaard0190392019-08-23 21:17:35 +02003579 long
3580excmd_get_argt(cmdidx_T idx)
3581{
3582 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583}
3584
3585/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003586 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 *
3588 * Backslashed delimiters after / or ? will be skipped, and commands will
3589 * not be expanded between /'s and ?'s or after "'".
3590 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003591 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 * Returns the "cmd" pointer advanced to beyond the range.
3593 */
3594 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003595skip_range(
3596 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003597 int skip_star, // skip "*" used for Visual range
3598 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003600 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003602 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003604 if (*cmd == '\\')
3605 {
3606 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3607 ++cmd;
3608 else
3609 break;
3610 }
3611 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 {
3613 if (*++cmd == NUL && ctx != NULL)
3614 *ctx = EXPAND_NOTHING;
3615 }
3616 else if (*cmd == '/' || *cmd == '?')
3617 {
3618 delim = *cmd++;
3619 while (*cmd != NUL && *cmd != delim)
3620 if (*cmd++ == '\\' && *cmd != NUL)
3621 ++cmd;
3622 if (*cmd == NUL && ctx != NULL)
3623 *ctx = EXPAND_NOTHING;
3624 }
3625 if (*cmd != NUL)
3626 ++cmd;
3627 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003628
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003629 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003630 while (*cmd == ':')
3631 cmd = skipwhite(cmd + 1);
3632
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003633 // Skip "*" used for Visual range.
3634 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3635 cmd = skipwhite(cmd + 1);
3636
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 return cmd;
3638}
3639
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003640 static void
3641addr_error(cmd_addr_T addr_type)
3642{
3643 if (addr_type == ADDR_NONE)
3644 emsg(_(e_norange));
3645 else
3646 emsg(_(e_invrange));
3647}
3648
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003650 * Return the default address for an address type.
3651 */
3652 static linenr_T
3653default_address(exarg_T *eap)
3654{
3655 linenr_T lnum = 0;
3656
3657 switch (eap->addr_type)
3658 {
3659 case ADDR_LINES:
3660 case ADDR_OTHER:
3661 // Default is the cursor line number. Avoid using an invalid
3662 // line number though.
3663 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3664 lnum = curbuf->b_ml.ml_line_count;
3665 else
3666 lnum = curwin->w_cursor.lnum;
3667 break;
3668 case ADDR_WINDOWS:
3669 lnum = CURRENT_WIN_NR;
3670 break;
3671 case ADDR_ARGUMENTS:
3672 lnum = curwin->w_arg_idx + 1;
3673 if (lnum > ARGCOUNT)
3674 lnum = ARGCOUNT;
3675 break;
3676 case ADDR_LOADED_BUFFERS:
3677 case ADDR_BUFFERS:
3678 lnum = curbuf->b_fnum;
3679 break;
3680 case ADDR_TABS:
3681 lnum = CURRENT_TAB_NR;
3682 break;
3683 case ADDR_TABS_RELATIVE:
3684 case ADDR_UNSIGNED:
3685 lnum = 1;
3686 break;
3687 case ADDR_QUICKFIX:
3688#ifdef FEAT_QUICKFIX
3689 lnum = qf_get_cur_idx(eap);
3690#endif
3691 break;
3692 case ADDR_QUICKFIX_VALID:
3693#ifdef FEAT_QUICKFIX
3694 lnum = qf_get_cur_valid_idx(eap);
3695#endif
3696 break;
3697 case ADDR_NONE:
3698 // Will give an error later if a range is found.
3699 break;
3700 }
3701 return lnum;
3702}
3703
3704/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003705 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 *
3707 * Set ptr to the next character after the part that was interpreted.
3708 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003709 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 *
3711 * Return MAXLNUM when no Ex address was found.
3712 */
3713 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003714get_address(
3715 exarg_T *eap UNUSED,
3716 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003717 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003718 int skip, // only skip the address, don't use it
3719 int silent, // no errors or side effects
3720 int to_other_file, // flag: may jump to other file
3721 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722{
3723 int c;
3724 int i;
3725 long n;
3726 char_u *cmd;
3727 pos_T pos;
3728 pos_T *fp;
3729 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003730 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731
3732 cmd = skipwhite(*ptr);
3733 lnum = MAXLNUM;
3734 do
3735 {
3736 switch (*cmd)
3737 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003738 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003739 ++cmd;
3740 switch (addr_type)
3741 {
3742 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003743 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 lnum = curwin->w_cursor.lnum;
3745 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003746 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003747 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003748 break;
3749 case ADDR_ARGUMENTS:
3750 lnum = curwin->w_arg_idx + 1;
3751 break;
3752 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003753 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003754 lnum = curbuf->b_fnum;
3755 break;
3756 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003757 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003758 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003759 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003760 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003761 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003762 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003763 cmd = NULL;
3764 goto error;
3765 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003766 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003767#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003768 lnum = qf_get_cur_idx(eap);
3769#endif
3770 break;
3771 case ADDR_QUICKFIX_VALID:
3772#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003773 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003774#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003775 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003776 }
3777 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003779 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003780 ++cmd;
3781 switch (addr_type)
3782 {
3783 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003784 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 lnum = curbuf->b_ml.ml_line_count;
3786 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003787 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003788 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003789 break;
3790 case ADDR_ARGUMENTS:
3791 lnum = ARGCOUNT;
3792 break;
3793 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003794 buf = lastbuf;
3795 while (buf->b_ml.ml_mfp == NULL)
3796 {
3797 if (buf->b_prev == NULL)
3798 break;
3799 buf = buf->b_prev;
3800 }
3801 lnum = buf->b_fnum;
3802 break;
3803 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003804 lnum = lastbuf->b_fnum;
3805 break;
3806 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003807 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003808 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003809 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003810 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003811 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003812 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003813 cmd = NULL;
3814 goto error;
3815 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003816 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003817#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003818 lnum = qf_get_size(eap);
3819 if (lnum == 0)
3820 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003821#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003822 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003823 case ADDR_QUICKFIX_VALID:
3824#ifdef FEAT_QUICKFIX
3825 lnum = qf_get_valid_size(eap);
3826 if (lnum == 0)
3827 lnum = 1;
3828#endif
3829 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003830 }
3831 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003833 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003834 if (*++cmd == NUL)
3835 {
3836 cmd = NULL;
3837 goto error;
3838 }
3839 if (addr_type != ADDR_LINES)
3840 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003841 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003842 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003843 goto error;
3844 }
3845 if (skip)
3846 ++cmd;
3847 else
3848 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003849 // Only accept a mark in another file when it is
3850 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003851 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3852 ++cmd;
3853 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003854 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003855 lnum = curwin->w_cursor.lnum;
3856 else
3857 {
3858 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
3860 cmd = NULL;
3861 goto error;
3862 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003863 lnum = fp->lnum;
3864 }
3865 }
3866 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867
3868 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003869 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003870 c = *cmd++;
3871 if (addr_type != ADDR_LINES)
3872 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003873 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003874 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003875 goto error;
3876 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003877 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003878 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003879 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003880 if (*cmd == c)
3881 ++cmd;
3882 }
3883 else
3884 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003885 int flags;
3886
3887 pos = curwin->w_cursor; // save curwin->w_cursor
3888
3889 // When '/' or '?' follows another address, start from
3890 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003891 if (lnum != MAXLNUM)
3892 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003893
3894 // Start a forward search at the end of the line (unless
3895 // before the first line).
3896 // Start a backward search at the start of the line.
3897 // This makes sure we never match in the current
3898 // line, and can match anywhere in the
3899 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003900 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003901 curwin->w_cursor.col = MAXCOL;
3902 else
3903 curwin->w_cursor.col = 0;
3904 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003905 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003906 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003907 {
3908 curwin->w_cursor = pos;
3909 cmd = NULL;
3910 goto error;
3911 }
3912 lnum = curwin->w_cursor.lnum;
3913 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003914 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003915 cmd += searchcmdlen;
3916 }
3917 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003919 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003920 ++cmd;
3921 if (addr_type != ADDR_LINES)
3922 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003923 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003924 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003925 goto error;
3926 }
3927 if (*cmd == '&')
3928 i = RE_SUBST;
3929 else if (*cmd == '?' || *cmd == '/')
3930 i = RE_SEARCH;
3931 else
3932 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003933 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003934 cmd = NULL;
3935 goto error;
3936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003938 if (!skip)
3939 {
3940 /*
3941 * When search follows another address, start from
3942 * there.
3943 */
3944 if (lnum != MAXLNUM)
3945 pos.lnum = lnum;
3946 else
3947 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003949 /*
3950 * Start the search just like for the above
3951 * do_search().
3952 */
3953 if (*cmd != '?')
3954 pos.col = MAXCOL;
3955 else
3956 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003957 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003958 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003959 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003960 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003961 lnum = pos.lnum;
3962 else
3963 {
3964 cmd = NULL;
3965 goto error;
3966 }
3967 }
3968 ++cmd;
3969 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970
3971 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003972 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003973 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 }
3975
3976 for (;;)
3977 {
3978 cmd = skipwhite(cmd);
3979 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3980 break;
3981
3982 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003983 {
3984 switch (addr_type)
3985 {
3986 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003987 case ADDR_OTHER:
3988 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003989 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003990 break;
3991 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003992 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003993 break;
3994 case ADDR_ARGUMENTS:
3995 lnum = curwin->w_arg_idx + 1;
3996 break;
3997 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003998 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003999 lnum = curbuf->b_fnum;
4000 break;
4001 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004002 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004003 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004004 case ADDR_TABS_RELATIVE:
4005 lnum = 1;
4006 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004007 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004008#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004009 lnum = qf_get_cur_idx(eap);
4010#endif
4011 break;
4012 case ADDR_QUICKFIX_VALID:
4013#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004014 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004015#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004016 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004017 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004018 case ADDR_UNSIGNED:
4019 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004020 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004021 }
4022 }
4023
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004025 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 else
4027 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004028 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 n = 1;
4030 else
4031 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004032
4033 if (addr_type == ADDR_TABS_RELATIVE)
4034 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004035 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004036 cmd = NULL;
4037 goto error;
4038 }
4039 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004040 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004041 lnum = compute_buffer_local_count(
4042 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004044 {
4045#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004046 // Relative line addressing, need to adjust for folded lines
4047 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004048 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4049 && address_count >= 2)
4050 (void)hasFolding(lnum, NULL, &lnum);
4051#endif
4052 if (i == '-')
4053 lnum -= n;
4054 else
4055 lnum += n;
4056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 }
4058 } while (*cmd == '/' || *cmd == '?');
4059
4060error:
4061 *ptr = cmd;
4062 return lnum;
4063}
4064
4065/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004066 * Set eap->line1 and eap->line2 to the whole range.
4067 * Used for commands with the EX_DFLALL flag and no range given.
4068 */
4069 static void
4070address_default_all(exarg_T *eap)
4071{
4072 eap->line1 = 1;
4073 switch (eap->addr_type)
4074 {
4075 case ADDR_LINES:
4076 case ADDR_OTHER:
4077 eap->line2 = curbuf->b_ml.ml_line_count;
4078 break;
4079 case ADDR_LOADED_BUFFERS:
4080 {
4081 buf_T *buf = firstbuf;
4082
4083 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4084 buf = buf->b_next;
4085 eap->line1 = buf->b_fnum;
4086 buf = lastbuf;
4087 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4088 buf = buf->b_prev;
4089 eap->line2 = buf->b_fnum;
4090 }
4091 break;
4092 case ADDR_BUFFERS:
4093 eap->line1 = firstbuf->b_fnum;
4094 eap->line2 = lastbuf->b_fnum;
4095 break;
4096 case ADDR_WINDOWS:
4097 eap->line2 = LAST_WIN_NR;
4098 break;
4099 case ADDR_TABS:
4100 eap->line2 = LAST_TAB_NR;
4101 break;
4102 case ADDR_TABS_RELATIVE:
4103 eap->line2 = 1;
4104 break;
4105 case ADDR_ARGUMENTS:
4106 if (ARGCOUNT == 0)
4107 eap->line1 = eap->line2 = 0;
4108 else
4109 eap->line2 = ARGCOUNT;
4110 break;
4111 case ADDR_QUICKFIX_VALID:
4112#ifdef FEAT_QUICKFIX
4113 eap->line2 = qf_get_valid_size(eap);
4114 if (eap->line2 == 0)
4115 eap->line2 = 1;
4116#endif
4117 break;
4118 case ADDR_NONE:
4119 case ADDR_UNSIGNED:
4120 case ADDR_QUICKFIX:
4121 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4122 break;
4123 }
4124}
4125
4126
4127/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004128 * Get flags from an Ex command argument.
4129 */
4130 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004131get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004132{
4133 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4134 {
4135 if (*eap->arg == 'l')
4136 eap->flags |= EXFLAG_LIST;
4137 else if (*eap->arg == 'p')
4138 eap->flags |= EXFLAG_PRINT;
4139 else
4140 eap->flags |= EXFLAG_NR;
4141 eap->arg = skipwhite(eap->arg + 1);
4142 }
4143}
4144
4145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 * Function called for command which is Not Implemented. NI!
4147 */
4148 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004149ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150{
4151 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004152 eap->errmsg =
4153 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154}
4155
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004156#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157/*
4158 * Function called for script command which is Not Implemented. NI!
4159 * Skips over ":perl <<EOF" constructs.
4160 */
4161 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004162ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163{
4164 if (!eap->skip)
4165 ex_ni(eap);
4166 else
4167 vim_free(script_get(eap, eap->arg));
4168}
4169#endif
4170
4171/*
4172 * Check range in Ex command for validity.
4173 * Return NULL when valid, error message when invalid.
4174 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004175 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004176invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004178 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004179
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 if ( eap->line1 < 0
4181 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004182 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004183 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004184
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004185 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004186 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004187 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004188 {
4189 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004190 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004191#ifdef FEAT_DIFF
4192 + (eap->cmdidx == CMD_diffget)
4193#endif
4194 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004195 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004196 break;
4197 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004198 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004199 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004200 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004201 break;
4202 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004203 // Only a boundary check, not whether the buffers actually
4204 // exist.
4205 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004206 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004207 break;
4208 case ADDR_LOADED_BUFFERS:
4209 buf = firstbuf;
4210 while (buf->b_ml.ml_mfp == NULL)
4211 {
4212 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004213 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004214 buf = buf->b_next;
4215 }
4216 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004217 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004218 buf = lastbuf;
4219 while (buf->b_ml.ml_mfp == NULL)
4220 {
4221 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004222 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004223 buf = buf->b_prev;
4224 }
4225 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004226 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004227 break;
4228 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004229 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004230 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004231 break;
4232 case ADDR_TABS:
4233 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004234 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004235 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004236 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004237 case ADDR_OTHER:
4238 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004239 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004240 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004241#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004242 // No error for value that is too big, will use the last entry.
4243 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004244 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004245#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004246 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004247 case ADDR_QUICKFIX_VALID:
4248#ifdef FEAT_QUICKFIX
4249 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4250 || eap->line2 < 0)
4251 return _(e_invrange);
4252#endif
4253 break;
4254 case ADDR_UNSIGNED:
4255 if (eap->line2 < 0)
4256 return _(e_invrange);
4257 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004258 case ADDR_NONE:
4259 // Will give an error elsewhere.
4260 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004261 }
4262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 return NULL;
4264}
4265
4266/*
4267 * Correct the range for zero line number, if required.
4268 */
4269 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004270correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004272 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 {
4274 if (eap->line1 == 0)
4275 eap->line1 = 1;
4276 if (eap->line2 == 0)
4277 eap->line2 = 1;
4278 }
4279}
4280
Bram Moolenaar748bf032005-02-02 23:04:36 +00004281#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004282/*
4283 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4284 * pattern. Otherwise return eap->arg.
4285 */
4286 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004287skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004288{
4289 char_u *p = eap->arg;
4290
Bram Moolenaara37420f2006-02-04 22:37:47 +00004291 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4292 || eap->cmdidx == CMD_vimgrepadd
4293 || eap->cmdidx == CMD_lvimgrepadd
4294 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004295 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004296 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004297 if (p == NULL)
4298 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004299 }
4300 return p;
4301}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004302
4303/*
4304 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4305 * in the command line, so that things like % get expanded.
4306 */
4307 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004308replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004309{
4310 char_u *new_cmdline;
4311 char_u *program;
4312 char_u *pos;
4313 char_u *ptr;
4314 int len;
4315 int i;
4316
4317 /*
4318 * Don't do it when ":vimgrep" is used for ":grep".
4319 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004320 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4321 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4322 || eap->cmdidx == CMD_grepadd
4323 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004324 && !grep_internal(eap->cmdidx))
4325 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004326 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4327 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004328 {
4329 if (*curbuf->b_p_gp == NUL)
4330 program = p_gp;
4331 else
4332 program = curbuf->b_p_gp;
4333 }
4334 else
4335 {
4336 if (*curbuf->b_p_mp == NUL)
4337 program = p_mp;
4338 else
4339 program = curbuf->b_p_mp;
4340 }
4341
4342 p = skipwhite(p);
4343
4344 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4345 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004346 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004347 i = 1;
4348 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4349 ++i;
4350 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004351 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004352 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004353 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004354 ptr = new_cmdline;
4355 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4356 {
4357 i = (int)(pos - program);
4358 STRNCPY(ptr, program, i);
4359 STRCPY(ptr += i, p);
4360 ptr += len;
4361 program = pos + 2;
4362 }
4363 STRCPY(ptr, program);
4364 }
4365 else
4366 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004367 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004368 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004369 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004370 STRCPY(new_cmdline, program);
4371 STRCAT(new_cmdline, " ");
4372 STRCAT(new_cmdline, p);
4373 }
4374 msg_make(p);
4375
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004376 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004377 vim_free(*cmdlinep);
4378 *cmdlinep = new_cmdline;
4379 p = new_cmdline;
4380 }
4381 return p;
4382}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004383#endif
4384
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385/*
4386 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004387 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 * Return FAIL for failure, OK otherwise.
4389 */
4390 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004391expand_filename(
4392 exarg_T *eap,
4393 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004394 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004396 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 char_u *repl;
4398 int srclen;
4399 char_u *p;
4400 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004401 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
Bram Moolenaar748bf032005-02-02 23:04:36 +00004403#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004404 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004405 p = skip_grep_pat(eap);
4406#else
4407 p = eap->arg;
4408#endif
4409
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 /*
4411 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4412 * the file name contains a wildcard it should not cause expanding.
4413 * (it will be expanded anyway if there is a wildcard before replacing).
4414 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004415 has_wildcards = mch_has_wildcard(p);
4416 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004418#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004419 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004420 if (p[0] == '`' && p[1] == '=')
4421 {
4422 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004423 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004424 if (*p == '`')
4425 ++p;
4426 continue;
4427 }
4428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 /*
4430 * Quick check if this cannot be the start of a special string.
4431 * Also removes backslash before '%', '#' and '<'.
4432 */
4433 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4434 {
4435 ++p;
4436 continue;
4437 }
4438
4439 /*
4440 * Try to find a match at this position.
4441 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004442 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4443 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004444 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004446 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 {
4448 p += srclen;
4449 continue;
4450 }
4451
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004452 // Wildcards won't be expanded below, the replacement is taken
4453 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004454 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4455 {
4456 char_u *l = repl;
4457
4458 repl = expand_env_save(repl);
4459 vim_free(l);
4460 }
4461
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004462 // Need to escape white space et al. with a backslash.
4463 // Don't do this for:
4464 // - replacement that already has been escaped: "##"
4465 // - shell commands (may have to use quotes instead).
4466 // - non-unix systems when there is a single argument (spaces don't
4467 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004469 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 && eap->cmdidx != CMD_grep
4472 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004473 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004474 && eap->cmdidx != CMD_lgrep
4475 && eap->cmdidx != CMD_lgrepadd
4476 && eap->cmdidx != CMD_lmake
4477 && eap->cmdidx != CMD_make
4478 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004480 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481#endif
4482 )
4483 {
4484 char_u *l;
4485#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004486 // Don't escape a backslash here, because rem_backslash() doesn't
4487 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 static char_u *nobslash = (char_u *)" \t\"|";
4489# define ESCAPE_CHARS nobslash
4490#else
4491# define ESCAPE_CHARS escape_chars
4492#endif
4493
4494 for (l = repl; *l; ++l)
4495 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4496 {
4497 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4498 if (l != NULL)
4499 {
4500 vim_free(repl);
4501 repl = l;
4502 }
4503 break;
4504 }
4505 }
4506
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004507 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004508 if ((eap->usefilter || eap->cmdidx == CMD_bang
4509 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004510 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 {
4512 char_u *l;
4513
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004514 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 if (l != NULL)
4516 {
4517 vim_free(repl);
4518 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 }
4520 }
4521
4522 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4523 vim_free(repl);
4524 if (p == NULL)
4525 return FAIL;
4526 }
4527
4528 /*
4529 * One file argument: Expand wildcards.
4530 * Don't do this with ":r !command" or ":w !command".
4531 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004532 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 {
4534 /*
4535 * May do this twice:
4536 * 1. Replace environment variables.
4537 * 2. Replace any other wildcards, remove backslashes.
4538 */
4539 for (n = 1; n <= 2; ++n)
4540 {
4541 if (n == 2)
4542 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 /*
4544 * Halve the number of backslashes (this is Vi compatible).
4545 * For Unix and OS/2, when wildcards are expanded, this is
4546 * done by ExpandOne() below.
4547 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004548#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 if (!has_wildcards)
4550#endif
4551 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 }
4553
4554 if (has_wildcards)
4555 {
4556 if (n == 1)
4557 {
4558 /*
4559 * First loop: May expand environment variables. This
4560 * can be done much faster with expand_env() than with
4561 * something else (e.g., calling a shell).
4562 * After expanding environment variables, check again
4563 * if there are still wildcards present.
4564 */
4565 if (vim_strchr(eap->arg, '$') != NULL
4566 || vim_strchr(eap->arg, '~') != NULL)
4567 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004568 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004569 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 has_wildcards = mch_has_wildcard(NameBuff);
4571 p = NameBuff;
4572 }
4573 else
4574 p = NULL;
4575 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004576 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 {
4578 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004579 int options = WILD_LIST_NOTFOUND
4580 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581
4582 ExpandInit(&xpc);
4583 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004584 if (p_wic)
4585 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004587 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 if (p == NULL)
4589 return FAIL;
4590 }
4591 if (p != NULL)
4592 {
4593 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4594 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004595 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 vim_free(p);
4597 }
4598 }
4599 }
4600 }
4601 return OK;
4602}
4603
4604/*
4605 * Replace part of the command line, keeping eap->cmd, eap->arg and
4606 * eap->nextcmd correct.
4607 * "src" points to the part that is to be replaced, of length "srclen".
4608 * "repl" is the replacement string.
4609 * Returns a pointer to the character after the replaced string.
4610 * Returns NULL for failure.
4611 */
4612 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004613repl_cmdline(
4614 exarg_T *eap,
4615 char_u *src,
4616 int srclen,
4617 char_u *repl,
4618 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619{
4620 int len;
4621 int i;
4622 char_u *new_cmdline;
4623
4624 /*
4625 * The new command line is build in new_cmdline[].
4626 * First allocate it.
4627 * Careful: a "+cmd" argument may have been NUL terminated.
4628 */
4629 len = (int)STRLEN(repl);
4630 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004631 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004632 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004633 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004634 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635
4636 /*
4637 * Copy the stuff before the expanded part.
4638 * Copy the expanded stuff.
4639 * Copy what came after the expanded part.
4640 * Copy the next commands, if there are any.
4641 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004642 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004644
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004646 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004648 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004650 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 {
4652 i = (int)STRLEN(new_cmdline) + 1;
4653 STRCPY(new_cmdline + i, eap->nextcmd);
4654 eap->nextcmd = new_cmdline + i;
4655 }
4656 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4657 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4658 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4659 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4660 vim_free(*cmdlinep);
4661 *cmdlinep = new_cmdline;
4662
4663 return src;
4664}
4665
4666/*
4667 * Check for '|' to separate commands and '"' to start comments.
4668 */
4669 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004670separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671{
4672 char_u *p;
4673
Bram Moolenaar86b68352004-12-27 21:59:20 +00004674#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004675 p = skip_grep_pat(eap);
4676#else
4677 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004678#endif
4679
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004680 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 {
4682 if (*p == Ctrl_V)
4683 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004684 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004685 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004687 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004688 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004689 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 break;
4691 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004692
4693#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004694 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004695 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004696 {
4697 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004698 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004699 }
4700#endif
4701
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004702 // Check for '"': start of comment or '|': next command
4703 // :@" and :*" do not start a comment!
4704 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004705 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004706#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004707 && !in_vim9script()
4708#endif
4709 && !(eap->argt & EX_NOTRLCOM)
4710 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4711 || p != eap->arg)
4712 && (eap->cmdidx != CMD_redir
4713 || p != eap->arg + 1 || p[-1] != '@'))
4714#ifdef FEAT_EVAL
4715 || (*p == '#'
4716 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004717 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004718 && p[1] != '{'
4719 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 || *p == '|' || *p == '\n')
4722 {
4723 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004724 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 * AND 'b' is present in 'cpoptions'.
4726 */
4727 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004728 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004730 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 --p;
4732 }
4733 else
4734 {
4735 eap->nextcmd = check_nextcmd(p);
4736 *p = NUL;
4737 break;
4738 }
4739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004741
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004742 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 del_trailing_spaces(eap->arg);
4744}
4745
4746/*
4747 * get + command from ex argument
4748 */
4749 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004750getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751{
4752 char_u *arg = *argp;
4753 char_u *command = NULL;
4754
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004755 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 {
4757 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004758 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 command = dollar_command;
4760 else
4761 {
4762 command = arg;
4763 arg = skip_cmd_arg(command, TRUE);
4764 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004765 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 }
4767
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004768 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 *argp = arg;
4770 }
4771 return command;
4772}
4773
4774/*
4775 * Find end of "+command" argument. Skip over "\ " and "\\".
4776 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004777 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004778skip_cmd_arg(
4779 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004780 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781{
4782 while (*p && !vim_isspace(*p))
4783 {
4784 if (*p == '\\' && p[1] != NUL)
4785 {
4786 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004787 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 else
4789 ++p;
4790 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004791 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 }
4793 return p;
4794}
4795
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004796 int
4797get_bad_opt(char_u *p, exarg_T *eap)
4798{
4799 if (STRICMP(p, "keep") == 0)
4800 eap->bad_char = BAD_KEEP;
4801 else if (STRICMP(p, "drop") == 0)
4802 eap->bad_char = BAD_DROP;
4803 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4804 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004805 else
4806 return FAIL;
4807 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004808}
4809
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810/*
4811 * Get "++opt=arg" argument.
4812 * Return FAIL or OK.
4813 */
4814 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004815getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816{
4817 char_u *arg = eap->arg + 2;
4818 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004819 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004822 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4824 {
4825 if (*arg == 'n')
4826 {
4827 arg += 2;
4828 eap->force_bin = FORCE_NOBIN;
4829 }
4830 else
4831 eap->force_bin = FORCE_BIN;
4832 if (!checkforcmd(&arg, "binary", 3))
4833 return FAIL;
4834 eap->arg = skipwhite(arg);
4835 return OK;
4836 }
4837
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004838 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004839 if (STRNCMP(arg, "edit", 4) == 0)
4840 {
4841 eap->read_edit = TRUE;
4842 eap->arg = skipwhite(arg + 4);
4843 return OK;
4844 }
4845
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 if (STRNCMP(arg, "ff", 2) == 0)
4847 {
4848 arg += 2;
4849 pp = &eap->force_ff;
4850 }
4851 else if (STRNCMP(arg, "fileformat", 10) == 0)
4852 {
4853 arg += 10;
4854 pp = &eap->force_ff;
4855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 else if (STRNCMP(arg, "enc", 3) == 0)
4857 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004858 if (STRNCMP(arg, "encoding", 8) == 0)
4859 arg += 8;
4860 else
4861 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 pp = &eap->force_enc;
4863 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004864 else if (STRNCMP(arg, "bad", 3) == 0)
4865 {
4866 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004867 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869
4870 if (pp == NULL || *arg != '=')
4871 return FAIL;
4872
4873 ++arg;
4874 *pp = (int)(arg - eap->cmd);
4875 arg = skip_cmd_arg(arg, FALSE);
4876 eap->arg = skipwhite(arg);
4877 *arg = NUL;
4878
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 if (pp == &eap->force_ff)
4880 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4882 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004883 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004885 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004887 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4889 *p = TOLOWER_ASC(*p);
4890 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004891 else
4892 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004893 // Check ++bad= argument. Must be a single-byte character, "keep" or
4894 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004895 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004896 return FAIL;
4897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898
4899 return OK;
4900}
4901
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004903ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904{
4905 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004906 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 * directory for security reasons.
4908 */
4909 if (secure)
4910 {
4911 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004912 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 }
4914 else if (eap->cmdidx == CMD_autocmd)
4915 do_autocmd(eap->arg, eap->forceit);
4916 else
4917 do_augroup(eap->arg, eap->forceit);
4918}
4919
4920/*
4921 * ":doautocmd": Apply the automatic commands to the current buffer.
4922 */
4923 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004924ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004926 char_u *arg = eap->arg;
4927 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004928 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004929
Bram Moolenaar1610d052016-06-09 22:53:01 +02004930 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004931 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004932 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004933 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936/*
4937 * :[N]bunload[!] [N] [bufname] unload buffer
4938 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4939 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4940 */
4941 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004942ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004944 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004945 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 eap->errmsg = do_bufdel(
4947 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4948 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4949 : DOBUF_UNLOAD, eap->arg,
4950 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4951}
4952
4953/*
4954 * :[N]buffer [N] to buffer N
4955 * :[N]sbuffer [N] to buffer N
4956 */
4957 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004958ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004960 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004961 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004963 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 else
4965 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004966 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4968 else
4969 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004970 if (eap->do_ecmd_cmd != NULL)
4971 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 }
4973}
4974
4975/*
4976 * :[N]bmodified [N] to next mod. buffer
4977 * :[N]sbmodified [N] to next mod. buffer
4978 */
4979 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004980ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981{
4982 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004983 if (eap->do_ecmd_cmd != NULL)
4984 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985}
4986
4987/*
4988 * :[N]bnext [N] to next buffer
4989 * :[N]sbnext [N] split and to next buffer
4990 */
4991 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004992ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004994 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004995 return;
4996
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004998 if (eap->do_ecmd_cmd != NULL)
4999 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000}
5001
5002/*
5003 * :[N]bNext [N] to previous buffer
5004 * :[N]bprevious [N] to previous buffer
5005 * :[N]sbNext [N] split and to previous buffer
5006 * :[N]sbprevious [N] split and to previous buffer
5007 */
5008 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005009ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005011 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005012 return;
5013
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005015 if (eap->do_ecmd_cmd != NULL)
5016 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017}
5018
5019/*
5020 * :brewind to first buffer
5021 * :bfirst to first buffer
5022 * :sbrewind split and to first buffer
5023 * :sbfirst split and to first buffer
5024 */
5025 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005026ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005028 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005029 return;
5030
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005032 if (eap->do_ecmd_cmd != NULL)
5033 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034}
5035
5036/*
5037 * :blast to last buffer
5038 * :sblast split and to last buffer
5039 */
5040 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005041ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005043 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005044 return;
5045
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005047 if (eap->do_ecmd_cmd != NULL)
5048 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050
Bram Moolenaar7a092242020-04-16 22:10:49 +02005051/*
5052 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02005053 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005056ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005058 int comment_char = '"';
5059
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005060#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005061 if (in_vim9script())
5062 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005063#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005064 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005065}
5066
5067/*
5068 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5069 * to "cmd_start" or has a white space character before it.
5070 */
5071 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005072ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005073{
5074 int c = *cmd;
5075
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005076 if (c == NUL || c == '|' || c == '\n')
5077 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005078#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005079 if (in_vim9script())
5080 return c == '#' && cmd[1] != '{'
5081 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005082#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005083 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084}
5085
5086#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5087 || defined(PROTO)
5088/*
5089 * Return the next command, after the first '|' or '\n'.
5090 * Return NULL if not found.
5091 */
5092 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005093find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094{
5095 while (*p != '|' && *p != '\n')
5096 {
5097 if (*p == NUL)
5098 return NULL;
5099 ++p;
5100 }
5101 return (p + 1);
5102}
5103#endif
5104
5105/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005106 * Check if *p is a separator between Ex commands, skipping over white space.
5107 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 */
5109 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005110check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005112 char_u *s = skipwhite(p);
5113
5114 if (*s == '|' || *s == '\n')
5115 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 else
5117 return NULL;
5118}
5119
5120/*
5121 * - if there are more files to edit
5122 * - and this is the last window
5123 * - and forceit not used
5124 * - and not repeated twice on a row
5125 * return FAIL and give error message if 'message' TRUE
5126 * return OK otherwise
5127 */
5128 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005129check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005130 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005131 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132{
5133 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5134
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005135 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005136 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
5138 if (message)
5139 {
5140#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005141 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5142 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005144 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005146 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5147 NGETTEXT("%d more file to edit. Quit anyway?",
5148 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5150 return OK;
5151 return FAIL;
5152 }
5153#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005154 semsg(NGETTEXT("E173: %d more file to edit",
5155 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005156 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 }
5158 return FAIL;
5159 }
5160 return OK;
5161}
5162
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163/*
5164 * Function given to ExpandGeneric() to obtain the list of command names.
5165 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005167get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168{
5169 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 return cmdnames[idx].cmd_name;
5172}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005175ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176{
Bram Moolenaare6850792010-05-14 15:28:44 +02005177 if (*eap->arg == NUL)
5178 {
5179#ifdef FEAT_EVAL
5180 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5181 char_u *p = NULL;
5182
5183 if (expr != NULL)
5184 {
5185 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005186 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005187 --emsg_off;
5188 vim_free(expr);
5189 }
5190 if (p != NULL)
5191 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005192 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005193 vim_free(p);
5194 }
5195 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005196 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005197#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005198 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005199#endif
5200 }
5201 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005202 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005203
5204#ifdef FEAT_VTP
5205 else if (has_vtp_working())
5206 {
5207 // background color change requires clear + redraw
5208 update_screen(CLEAR);
5209 redrawcmd();
5210 }
5211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212}
5213
5214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005215ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216{
5217 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005218 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 do_highlight(eap->arg, eap->forceit, FALSE);
5220}
5221
5222
5223/*
5224 * Call this function if we thought we were going to exit, but we won't
5225 * (because of an error). May need to restore the terminal mode.
5226 */
5227 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005228not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229{
5230 exiting = FALSE;
5231 settmode(TMODE_RAW);
5232}
5233
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005234 static int
5235before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5236{
5237 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5238
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005239 // Bail out when autocommands closed the window.
5240 // Refuse to quit when the buffer in the last window is being closed (can
5241 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005242 if (!win_valid(wp)
5243 || curbuf_locked()
5244 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5245 return TRUE;
5246
5247 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5248 {
5249 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005250 // Refuse to quit when locked or when the window was closed or the
5251 // buffer in the last window is being closed (can only happen in
5252 // autocommands).
5253 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005254 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5255 return TRUE;
5256 }
5257
5258 return FALSE;
5259}
5260
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005262 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005263 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005264 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005266 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005267ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005269 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005270
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271#ifdef FEAT_CMDWIN
5272 if (cmdwin_type != 0)
5273 {
5274 cmdwin_result = Ctrl_C;
5275 return;
5276 }
5277#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005278 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005279 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005280 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005281 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005282 return;
5283 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005284 if (eap->addr_count > 0)
5285 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005286 int wnr = eap->line2;
5287
5288 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5289 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005290 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005291 }
5292 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005293 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005294
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005295 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005296 if (curbuf_locked())
5297 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005298
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005299 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005300 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005301 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302
5303#ifdef FEAT_NETBEANS_INTG
5304 netbeansForcedQuit = eap->forceit;
5305#endif
5306
5307 /*
5308 * If there are more files or windows we won't exit.
5309 */
5310 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5311 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005312 if ((!buf_hide(wp->w_buffer)
5313 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005314 | (eap->forceit ? CCGD_FORCEIT : 0)
5315 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005317 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 {
5319 not_exiting();
5320 }
5321 else
5322 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005323 // quit last window
5324 // Note: only_one_window() returns true, even so a help window is
5325 // still open. In that case only quit, if no address has been
5326 // specified. Example:
5327 // :h|wincmd w|1q - don't quit
5328 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005329 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005331 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005332#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005334#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005335 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005336 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
5338}
5339
5340/*
5341 * ":cquit".
5342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005344ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005346 // this does not always pass on the exit code to the Manx compiler. why?
5347 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348}
5349
5350/*
5351 * ":qall": try to quit all windows
5352 */
5353 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005354ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355{
5356# ifdef FEAT_CMDWIN
5357 if (cmdwin_type != 0)
5358 {
5359 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005360 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 else
5362 cmdwin_result = K_XF2;
5363 return;
5364 }
5365# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005366
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005367 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005368 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005369 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005370 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005371 return;
5372 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005373
5374 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005375 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005376
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005378 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 getout(0);
5380 not_exiting();
5381}
5382
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383/*
5384 * ":close": close current window, unless it is the last one
5385 */
5386 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005387ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005389 win_T *win;
5390 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005391#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005393 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005395#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005396 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005397 {
5398 if (eap->addr_count == 0)
5399 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005400 else
5401 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005402 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005403 {
5404 winnr++;
5405 if (winnr == eap->line2)
5406 break;
5407 }
5408 if (win == NULL)
5409 win = lastwin;
5410 ex_win_close(eap->forceit, win, NULL);
5411 }
5412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413}
5414
Bram Moolenaar4033c552017-09-16 20:54:51 +02005415#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005416/*
5417 * ":pclose": Close any preview window.
5418 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005420ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005421{
5422 win_T *win;
5423
Bram Moolenaar79648732019-07-18 21:43:07 +02005424 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005425 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005426 if (win->w_p_pvw)
5427 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005428 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005429 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005430 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005431# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005432 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005433 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005434# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005435}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005436#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005437
Bram Moolenaarf740b292006-02-16 22:11:02 +00005438/*
5439 * Close window "win" and take care of handling closing the last window for a
5440 * modified buffer.
5441 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005443ex_win_close(
5444 int forceit,
5445 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005446 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447{
5448 int need_hide;
5449 buf_T *buf = win->w_buffer;
5450
Bram Moolenaarcf844172020-06-26 19:44:06 +02005451 // Never close the autocommand window.
5452 if (win == aucmd_win)
5453 {
5454 emsg(_(e_autocmd_close));
5455 return;
5456 }
5457
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005459 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005461#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005462 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005464 bufref_T bufref;
5465
5466 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005468 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 return;
5470 need_hide = FALSE;
5471 }
5472 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005475 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 return;
5477 }
5478 }
5479
Bram Moolenaar4033c552017-09-16 20:54:51 +02005480#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005482#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005483
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005484 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005485 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005486 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005487 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005488 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489}
5490
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005492 * Handle the argument for a tabpage related ex command.
5493 * Returns a tabpage number.
5494 * When an error is encountered then eap->errmsg is set.
5495 */
5496 static int
5497get_tabpage_arg(exarg_T *eap)
5498{
5499 int tab_number;
5500 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5501
5502 if (eap->arg && *eap->arg != NUL)
5503 {
5504 char_u *p = eap->arg;
5505 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005506 int relative = 0; // argument +N/-N means: go to N places to the
5507 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005508
5509 if (*p == '-')
5510 {
5511 relative = -1;
5512 p++;
5513 }
5514 else if (*p == '+')
5515 {
5516 relative = 1;
5517 p++;
5518 }
5519
5520 p_save = p;
5521 tab_number = getdigits(&p);
5522
5523 if (relative == 0)
5524 {
5525 if (STRCMP(p, "$") == 0)
5526 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005527 else if (STRCMP(p, "#") == 0)
5528 if (valid_tabpage(lastused_tabpage))
5529 tab_number = tabpage_index(lastused_tabpage);
5530 else
5531 {
5532 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5533 tab_number = 0;
5534 goto theend;
5535 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005536 else if (p == p_save || *p_save == '-' || *p != NUL
5537 || tab_number > LAST_TAB_NR)
5538 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005539 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005540 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005541 goto theend;
5542 }
5543 }
5544 else
5545 {
5546 if (*p_save == NUL)
5547 tab_number = 1;
5548 else if (p == p_save || *p_save == '-' || *p != NUL
5549 || tab_number == 0)
5550 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005551 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005552 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005553 goto theend;
5554 }
5555 tab_number = tab_number * relative + tabpage_index(curtab);
5556 if (!unaccept_arg0 && relative == -1)
5557 --tab_number;
5558 }
5559 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005560 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005561 }
5562 else if (eap->addr_count > 0)
5563 {
5564 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005565 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005566 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005567 tab_number = 0;
5568 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005569 else
5570 {
5571 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005572 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005573 {
5574 --tab_number;
5575 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005576 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005577 }
5578 }
5579 }
5580 else
5581 {
5582 switch (eap->cmdidx)
5583 {
5584 case CMD_tabnext:
5585 tab_number = tabpage_index(curtab) + 1;
5586 if (tab_number > LAST_TAB_NR)
5587 tab_number = 1;
5588 break;
5589 case CMD_tabmove:
5590 tab_number = LAST_TAB_NR;
5591 break;
5592 default:
5593 tab_number = tabpage_index(curtab);
5594 }
5595 }
5596
5597theend:
5598 return tab_number;
5599}
5600
5601/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005602 * ":tabclose": close current tab page, unless it is the last one.
5603 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 */
5605 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005606ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005608 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005609 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005610
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005611# ifdef FEAT_CMDWIN
5612 if (cmdwin_type != 0)
5613 cmdwin_result = K_IGNORE;
5614 else
5615# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005616 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005617 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005618 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005620 tab_number = get_tabpage_arg(eap);
5621 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005622 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005623 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005624 if (tp == NULL)
5625 {
5626 beep_flush();
5627 return;
5628 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005629 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005630 {
5631 tabpage_close_other(tp, eap->forceit);
5632 return;
5633 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005634 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005635 tabpage_close(eap->forceit);
5636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005638}
5639
5640/*
5641 * ":tabonly": close all tab pages except the current one
5642 */
5643 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005644ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005645{
5646 tabpage_T *tp;
5647 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005648 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005649
5650# ifdef FEAT_CMDWIN
5651 if (cmdwin_type != 0)
5652 cmdwin_result = K_IGNORE;
5653 else
5654# endif
5655 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005656 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005657 else
5658 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005659 tab_number = get_tabpage_arg(eap);
5660 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005661 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005662 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005663 // Repeat this up to a 1000 times, because autocommands may
5664 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005665 for (done = 0; done < 1000; ++done)
5666 {
5667 FOR_ALL_TABPAGES(tp)
5668 if (tp->tp_topframe != topframe)
5669 {
5670 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005671 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005672 if (valid_tabpage(tp))
5673 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005674 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005675 break;
5676 }
5677 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005678 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005679 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005680 }
5681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683
5684/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005685 * Close the current tab page.
5686 */
5687 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005688tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005689{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005690 // First close all the windows but the current one. If that worked then
5691 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005692 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005693 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005694 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005695 ex_win_close(forceit, curwin, NULL);
5696# ifdef FEAT_GUI
5697 need_mouse_correct = TRUE;
5698# endif
5699}
5700
5701/*
5702 * Close tab page "tp", which is not the current tab page.
5703 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005704 * Also takes care of the tab pages line disappearing when closing the
5705 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005706 */
5707 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005708tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005709{
5710 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005711 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005712 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005713
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005714 // Limit to 1000 windows, autocommands may add a window while we close
5715 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005716 while (++done < 1000)
5717 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005718 wp = tp->tp_firstwin;
5719 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005720
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005721 // Autocommands may delete the tab page under our fingers and we may
5722 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005723 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005724 break;
5725 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005726
Bram Moolenaar29323592016-07-24 22:04:11 +02005727 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005728
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005729 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005730 if (h != tabline_height())
5731 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005732}
5733
5734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 * ":only".
5736 */
5737 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005738ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005740 win_T *wp;
5741 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742# ifdef FEAT_GUI
5743 need_mouse_correct = TRUE;
5744# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005745 if (eap->addr_count > 0)
5746 {
5747 wnr = eap->line2;
5748 for (wp = firstwin; --wnr > 0; )
5749 {
5750 if (wp->w_next == NULL)
5751 break;
5752 else
5753 wp = wp->w_next;
5754 }
5755 win_goto(wp);
5756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 close_others(TRUE, eap->forceit);
5758}
5759
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005761ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005763 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005764 if (!eap->skip)
5765 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005766#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005767 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005768#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005769 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005770 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005771 else
5772 {
5773 int winnr = 0;
5774 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005775
Bram Moolenaar2256c992016-11-15 21:17:07 +01005776 FOR_ALL_WINDOWS(win)
5777 {
5778 winnr++;
5779 if (winnr == eap->line2)
5780 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005781 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005782 if (win == NULL)
5783 win = lastwin;
5784 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 }
5787}
5788
5789/*
5790 * ":stop" and ":suspend": Suspend Vim.
5791 */
5792 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005793ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794{
5795 /*
5796 * Disallow suspending for "rvim".
5797 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005798 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 {
5800 if (!eap->forceit)
5801 autowrite_all();
5802 windgoto((int)Rows - 1, 0);
5803 out_char('\n');
5804 out_flush();
5805 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005806 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005808 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005810 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811#ifdef FEAT_TITLE
5812 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005813 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814#endif
5815 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005816 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005818 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 }
5820}
5821
5822/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005823 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 */
5825 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005826ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827{
Bram Moolenaar461f2122020-07-28 20:25:47 +02005828#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02005829 if (not_in_vim9(eap) == FAIL)
5830 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02005831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832#ifdef FEAT_CMDWIN
5833 if (cmdwin_type != 0)
5834 {
5835 cmdwin_result = Ctrl_C;
5836 return;
5837 }
5838#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005839 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005840 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005841 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005842 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005843 return;
5844 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005845
5846 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005847 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848
5849 /*
5850 * if more files or windows we won't exit
5851 */
5852 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5853 exiting = TRUE;
5854 if ( ((eap->cmdidx == CMD_wq
5855 || curbufIsChanged())
5856 && do_write(eap) == FAIL)
5857 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005858 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 {
5860 not_exiting();
5861 }
5862 else
5863 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005864 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005866 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867# ifdef FEAT_GUI
5868 need_mouse_correct = TRUE;
5869# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005870 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005871 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 }
5873}
5874
5875/*
5876 * ":print", ":list", ":number".
5877 */
5878 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005879ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005881 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005882 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005883 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005885 for ( ;!got_int; ui_breakcheck())
5886 {
5887 print_line(eap->line1,
5888 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5889 || (eap->flags & EXFLAG_NR)),
5890 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5891 if (++eap->line1 > eap->line2)
5892 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005893 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005894 }
5895 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005896 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005897 curwin->w_cursor.lnum = eap->line2;
5898 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 }
5900
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902}
5903
5904#ifdef FEAT_BYTEOFF
5905 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005906ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907{
5908 goto_byte(eap->line2);
5909}
5910#endif
5911
5912/*
5913 * ":shell".
5914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005916ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917{
5918 do_shell(NULL, 0);
5919}
5920
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005921#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005923static int drop_busy = FALSE;
5924static int drop_filec;
5925static char_u **drop_filev = NULL;
5926static int drop_split;
5927static void (*drop_callback)(void *);
5928static void *drop_cookie;
5929
5930 static void
5931handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932{
5933 exarg_T ea;
5934 int save_msg_scroll = msg_scroll;
5935
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005936 // Setting the argument list may cause screen updates and being called
5937 // recursively. Avoid that by setting drop_busy.
5938 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005940 // Check whether the current buffer is changed. If so, we will need
5941 // to split the current window or data could be lost.
5942 // We don't need to check if the 'hidden' option is set, as in this
5943 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005944 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 {
5946 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005947 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 --emsg_off;
5949 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005950 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 if (win_split(0, 0) == FAIL)
5953 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005954 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005956 // When splitting the window, create a new alist. Otherwise the
5957 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 alist_unlink(curwin->w_alist);
5959 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 }
5961
5962 /*
5963 * Set up the new argument list.
5964 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005965 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966
5967 /*
5968 * Move to the first file.
5969 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005970 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005971 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 ea.cmd = (char_u *)"next";
5973 do_argfile(&ea, 0);
5974
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005975 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5976 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 need_start_insertmode = FALSE;
5978
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005979 // Restore msg_scroll, otherwise a following command may cause scrolling
5980 // unexpectedly. The screen will be redrawn by the caller, thus
5981 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005983
5984 if (drop_callback != NULL)
5985 drop_callback(drop_cookie);
5986
5987 drop_filev = NULL;
5988 drop_busy = FALSE;
5989}
5990
5991/*
5992 * Handle a file drop. The code is here because a drop is *nearly* like an
5993 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005994 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005995 * code is very similar to :args and hence needs access to a lot of the static
5996 * functions in this file.
5997 *
5998 * The "filev" list must have been allocated using alloc(), as should each item
5999 * in the list. This function takes over responsibility for freeing the "filev"
6000 * list.
6001 */
6002 void
6003handle_drop(
6004 int filec, // the number of files dropped
6005 char_u **filev, // the list of files dropped
6006 int split, // force splitting the window
6007 void (*callback)(void *), // to be called after setting the argument
6008 // list
6009 void *cookie) // argument for "callback" (allocated)
6010{
6011 // Cannot handle recursive drops, finish the pending one.
6012 if (drop_busy)
6013 {
6014 FreeWild(filec, filev);
6015 vim_free(cookie);
6016 return;
6017 }
6018
6019 // When calling handle_drop() more than once in a row we only use the last
6020 // one.
6021 if (drop_filev != NULL)
6022 {
6023 FreeWild(drop_filec, drop_filev);
6024 vim_free(drop_cookie);
6025 }
6026
6027 drop_filec = filec;
6028 drop_filev = filev;
6029 drop_split = split;
6030 drop_callback = callback;
6031 drop_cookie = cookie;
6032
6033 // Postpone this when:
6034 // - editing the command line
6035 // - not possible to change the current buffer
6036 // - updating the screen
6037 // As it may change buffers and window structures that are in use and cause
6038 // freed memory to be used.
6039 if (text_locked() || curbuf_locked() || updating_screen)
6040 return;
6041
6042 handle_drop_internal();
6043}
6044
6045/*
6046 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6047 * reset: Handle a postponed drop.
6048 */
6049 void
6050handle_any_postponed_drop(void)
6051{
6052 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006053 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006054 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055}
6056#endif
6057
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 * ":preserve".
6060 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006062ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006064 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 ml_preserve(curbuf, TRUE);
6066}
6067
6068/*
6069 * ":recover".
6070 */
6071 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006072ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006074 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006076 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6077 | CCGD_MULTWIN
6078 | (eap->forceit ? CCGD_FORCEIT : 0)
6079 | CCGD_EXCMD)
6080
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 && (*eap->arg == NUL
6082 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006083 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 recoverymode = FALSE;
6085}
6086
6087/*
6088 * Command modifier used in a wrong way.
6089 */
6090 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006091ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006093 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094}
6095
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096/*
6097 * :sview [+command] file split window with new file, read-only
6098 * :split [[+command] file] split window with current or new file
6099 * :vsplit [[+command] file] split window vertically with current or new file
6100 * :new [[+command] file] split window with no or new file
6101 * :vnew [[+command] file] split vertically window with no or new file
6102 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006103 *
6104 * :tabedit open new Tab page with empty window
6105 * :tabedit [+command] file open new Tab page and edit "file"
6106 * :tabnew [[+command] file] just like :tabedit
6107 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 */
6109 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006110ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006112 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006113#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006115#endif
6116#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006117 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006118 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006119#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006120 int use_tab = eap->cmdidx == CMD_tabedit
6121 || eap->cmdidx == CMD_tabfind
6122 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006124 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006125 return;
6126
Bram Moolenaar4033c552017-09-16 20:54:51 +02006127#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130
Bram Moolenaar4033c552017-09-16 20:54:51 +02006131#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006132 // A ":split" in the quickfix window works like ":new". Don't want two
6133 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006134 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 {
6136 if (eap->cmdidx == CMD_split)
6137 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 if (eap->cmdidx == CMD_vsplit)
6139 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142
Bram Moolenaar4033c552017-09-16 20:54:51 +02006143#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006144 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 {
6146 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6147 FNAME_MESS, TRUE, curbuf->b_ffname);
6148 if (fname == NULL)
6149 goto theend;
6150 eap->arg = fname;
6151 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006152# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006153 else
6154# endif
6155#endif
6156#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006157 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 && eap->cmdidx != CMD_new)
6160 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006161 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006162# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006163 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006164# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006165 au_has_group((char_u *)"FileExplorer"))
6166 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006167 // No browsing supported but we do have the file explorer:
6168 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006169 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006170 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006171 }
6172 else
6173 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006174 fname = do_browse(0, (char_u *)(use_tab
6175 ? _("Edit File in new tab page")
6176 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006178 if (fname == NULL)
6179 goto theend;
6180 eap->arg = fname;
6181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006183 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006186 /*
6187 * Either open new tab page or split the window.
6188 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006189 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006190 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006191 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006192 : eap->addr_count == 0 ? 0
6193 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006194 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006195 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006196
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006197 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006198 if (curwin != old_curwin
6199 && win_valid(old_curwin)
6200 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006201 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006202 old_curwin->w_alt_fnum = curbuf->b_fnum;
6203 }
6204 }
6205 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6207 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006208 // Reset 'scrollbind' when editing another file, but keep it when
6209 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006210 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006211 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 else
6213 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 do_exedit(eap, old_curwin);
6215 }
6216
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006217# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006218 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006219# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006221# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222theend:
6223 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006224# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006226
6227/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006228 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006229 */
6230 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006231tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006232{
6233 exarg_T ea;
6234
Bram Moolenaara80faa82020-04-12 19:37:17 +02006235 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006236 ea.cmdidx = CMD_tabnew;
6237 ea.cmd = (char_u *)"tabn";
6238 ea.arg = (char_u *)"";
6239 ex_splitview(&ea);
6240}
6241
6242/*
6243 * :tabnext command
6244 */
6245 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006246ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006247{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006248 int tab_number;
6249
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006250 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006251 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006252 switch (eap->cmdidx)
6253 {
6254 case CMD_tabfirst:
6255 case CMD_tabrewind:
6256 goto_tabpage(1);
6257 break;
6258 case CMD_tablast:
6259 goto_tabpage(9999);
6260 break;
6261 case CMD_tabprevious:
6262 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006263 if (eap->arg && *eap->arg != NUL)
6264 {
6265 char_u *p = eap->arg;
6266 char_u *p_save = p;
6267
6268 tab_number = getdigits(&p);
6269 if (p == p_save || *p_save == '-' || *p != NUL
6270 || tab_number == 0)
6271 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006272 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006273 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006274 return;
6275 }
6276 }
6277 else
6278 {
6279 if (eap->addr_count == 0)
6280 tab_number = 1;
6281 else
6282 {
6283 tab_number = eap->line2;
6284 if (tab_number < 1)
6285 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006286 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006287 return;
6288 }
6289 }
6290 }
6291 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006292 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006293 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006294 tab_number = get_tabpage_arg(eap);
6295 if (eap->errmsg == NULL)
6296 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006297 break;
6298 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006299}
6300
6301/*
6302 * :tabmove command
6303 */
6304 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006305ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006306{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006307 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006308
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006309 tab_number = get_tabpage_arg(eap);
6310 if (eap->errmsg == NULL)
6311 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006312}
6313
6314/*
6315 * :tabs command: List tabs and their contents.
6316 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006317 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006318ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006319{
6320 tabpage_T *tp;
6321 win_T *wp;
6322 int tabcount = 1;
6323
6324 msg_start();
6325 msg_scroll = TRUE;
6326 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6327 {
6328 msg_putchar('\n');
6329 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006330 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006331 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006332 ui_breakcheck();
6333
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006334 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006335 wp = firstwin;
6336 else
6337 wp = tp->tp_firstwin;
6338 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6339 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006340 msg_putchar('\n');
6341 msg_putchar(wp == curwin ? '>' : ' ');
6342 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006343 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6344 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006345 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006346 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006347 else
6348 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6349 IObuff, IOSIZE, TRUE);
6350 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006351 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006352 ui_breakcheck();
6353 }
6354 }
6355}
6356
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357/*
6358 * ":mode": Set screen mode.
6359 * If no argument given, just get the screen size and redraw.
6360 */
6361 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006362ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363{
6364 if (*eap->arg == NUL)
6365 shell_resized();
6366 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006367 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368}
6369
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370/*
6371 * ":resize".
6372 * set, increment or decrement current window height
6373 */
6374 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006375ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376{
6377 int n;
6378 win_T *wp = curwin;
6379
6380 if (eap->addr_count > 0)
6381 {
6382 n = eap->line2;
6383 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6384 ;
6385 }
6386
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006387# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006389# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006391 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 {
6393 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006394 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006395 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 n = 9999;
6397 win_setwidth_win((int)n, wp);
6398 }
6399 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 {
6401 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006402 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006403 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 n = 9999;
6405 win_setheight_win((int)n, wp);
6406 }
6407}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408
6409/*
6410 * ":find [+command] <file>" command.
6411 */
6412 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006413ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414{
6415#ifdef FEAT_SEARCHPATH
6416 char_u *fname;
6417 int count;
6418
6419 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6420 TRUE, curbuf->b_ffname);
6421 if (eap->addr_count > 0)
6422 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006423 // Repeat finding the file "count" times. This matters when it
6424 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 count = eap->line2;
6426 while (fname != NULL && --count > 0)
6427 {
6428 vim_free(fname);
6429 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6430 FALSE, curbuf->b_ffname);
6431 }
6432 }
6433
6434 if (fname != NULL)
6435 {
6436 eap->arg = fname;
6437#endif
6438 do_exedit(eap, NULL);
6439#ifdef FEAT_SEARCHPATH
6440 vim_free(fname);
6441 }
6442#endif
6443}
6444
6445/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006446 * ":open" simulation: for now just work like ":visual".
6447 */
6448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006449ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006450{
6451 regmatch_T regmatch;
6452 char_u *p;
6453
6454 curwin->w_cursor.lnum = eap->line2;
6455 beginline(BL_SOL | BL_FIX);
6456 if (*eap->arg == '/')
6457 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006458 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006459 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006460 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006461 *p = NUL;
6462 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6463 if (regmatch.regprog != NULL)
6464 {
6465 regmatch.rm_ic = p_ic;
6466 p = ml_get_curline();
6467 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006468 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006469 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006470 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006471 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006472 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006473 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006474 eap->arg += STRLEN(eap->arg);
6475 }
6476 check_cursor();
6477
6478 eap->cmdidx = CMD_visual;
6479 do_exedit(eap, NULL);
6480}
6481
6482/*
6483 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 */
6485 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006486ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487{
6488 do_exedit(eap, NULL);
6489}
6490
6491/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006492 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006495do_exedit(
6496 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006497 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498{
6499 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006501 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006503 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6504 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006505 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 /*
6507 * ":vi" command ends Ex mode.
6508 */
6509 if (exmode_active && (eap->cmdidx == CMD_visual
6510 || eap->cmdidx == CMD_view))
6511 {
6512 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006513 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006515 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006516 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006517 if (global_busy)
6518 {
6519 int rd = RedrawingDisabled;
6520 int nwr = no_wait_return;
6521 int ms = msg_scroll;
6522#ifdef FEAT_GUI
6523 int he = hold_gui_events;
6524#endif
6525
6526 if (eap->nextcmd != NULL)
6527 {
6528 stuffReadbuff(eap->nextcmd);
6529 eap->nextcmd = NULL;
6530 }
6531
6532 if (exmode_was != EXMODE_VIM)
6533 settmode(TMODE_RAW);
6534 RedrawingDisabled = 0;
6535 no_wait_return = 0;
6536 need_wait_return = FALSE;
6537 msg_scroll = 0;
6538#ifdef FEAT_GUI
6539 hold_gui_events = 0;
6540#endif
6541 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006542 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006543
6544 main_loop(FALSE, TRUE);
6545
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006546 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006547 RedrawingDisabled = rd;
6548 no_wait_return = nwr;
6549 msg_scroll = ms;
6550#ifdef FEAT_GUI
6551 hold_gui_events = he;
6552#endif
6553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 }
6557
6558 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006559 || eap->cmdidx == CMD_tabnew
6560 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006561 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006563 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 setpcmark();
6565 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006566 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6567 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006569 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 || *eap->arg != NUL
6571#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006572 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573#endif
6574 )
6575 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006576 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6577 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006578 if (*eap->arg != NUL && curbuf_locked())
6579 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006580
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 n = readonlymode;
6582 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6583 readonlymode = TRUE;
6584 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006585 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6586 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 setpcmark();
6588 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6589 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006590 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6592 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6593 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006594 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006596 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006597 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006599 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006601 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 if (old_curwin != NULL)
6603 {
6604 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006605 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006607#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006608 cleanup_T cs;
6609
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006610 // Reset the error/interrupt/exception state here so that
6611 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006612 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006613#endif
6614#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006616#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006617 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006618
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006619#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006620 // Restore the error/interrupt/exception state if not
6621 // discarded by a new aborting error, interrupt, or
6622 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006623 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 }
6626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 }
6628 else if (readonlymode && curbuf->b_nwindows == 1)
6629 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006630 // When editing an already visited buffer, 'readonly' won't be set
6631 // but the previous value is kept. With ":view" and ":sview" we
6632 // want the file to be readonly, except when another window is
6633 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 curbuf->b_p_ro = TRUE;
6635 }
6636 readonlymode = n;
6637 }
6638 else
6639 {
6640 if (eap->do_ecmd_cmd != NULL)
6641 do_cmdline_cmd(eap->do_ecmd_cmd);
6642#ifdef FEAT_TITLE
6643 n = curwin->w_arg_idx_invalid;
6644#endif
6645 check_arg_idx(curwin);
6646#ifdef FEAT_TITLE
6647 if (n != curwin->w_arg_idx_invalid)
6648 maketitle();
6649#endif
6650 }
6651
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 /*
6653 * if ":split file" worked, set alternate file name in old window to new
6654 * file
6655 */
6656 if (old_curwin != NULL
6657 && *eap->arg != NUL
6658 && curwin != old_curwin
6659 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006660 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006661 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663
6664 ex_no_reprint = TRUE;
6665}
6666
6667#ifndef FEAT_GUI
6668/*
6669 * ":gui" and ":gvim" when there is no GUI.
6670 */
6671 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006672ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006674 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675}
6676#endif
6677
Bram Moolenaar4f974752019-02-17 17:44:42 +01006678#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006680ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681{
6682 gui_make_tearoff(eap->arg);
6683}
6684#endif
6685
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006686#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6687 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006689ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006691# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6692 if (gui.in_use)
6693 gui_make_popup(eap->arg, eap->forceit);
6694# ifdef FEAT_TERM_POPUP_MENU
6695 else
6696# endif
6697# endif
6698# ifdef FEAT_TERM_POPUP_MENU
6699 pum_make_popup(eap->arg, eap->forceit);
6700# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701}
6702#endif
6703
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006705ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706{
6707 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006708 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006710 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711}
6712
6713/*
6714 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6715 * offset.
6716 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006719ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006722 win_T *save_curwin = curwin;
6723 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 long topline;
6725 long y;
6726 linenr_T old_linenr = curwin->w_cursor.lnum;
6727
6728 setpcmark();
6729
6730 /*
6731 * determine max topline
6732 */
6733 if (curwin->w_p_scb)
6734 {
6735 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006736 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 {
6738 if (wp->w_p_scb && wp->w_buffer)
6739 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006740 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 if (topline > y)
6742 topline = y;
6743 }
6744 }
6745 if (topline < 1)
6746 topline = 1;
6747 }
6748 else
6749 {
6750 topline = 1;
6751 }
6752
6753
6754 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006755 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006757 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 {
6759 if (curwin->w_p_scb)
6760 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006761 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 y = topline - curwin->w_topline;
6763 if (y > 0)
6764 scrollup(y, TRUE);
6765 else
6766 scrolldown(-y, TRUE);
6767 curwin->w_scbind_pos = topline;
6768 redraw_later(VALID);
6769 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 }
6772 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006773 curwin = save_curwin;
6774 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 if (curwin->w_p_scb)
6776 {
6777 did_syncbind = TRUE;
6778 checkpcmark();
6779 if (old_linenr != curwin->w_cursor.lnum)
6780 {
6781 char_u ctrl_o[2];
6782
6783 ctrl_o[0] = Ctrl_O;
6784 ctrl_o[1] = 0;
6785 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6786 }
6787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788}
6789
6790
6791 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006792ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006794 int i;
6795 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6796 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006798 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 do_bang(1, eap, FALSE, FALSE, TRUE);
6800 else
6801 {
6802 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6803 return;
6804
6805#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006806 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {
6808 char_u *browseFile;
6809
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006810 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 NULL, NULL, NULL, curbuf);
6812 if (browseFile != NULL)
6813 {
6814 i = readfile(browseFile, NULL,
6815 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6816 vim_free(browseFile);
6817 }
6818 else
6819 i = OK;
6820 }
6821 else
6822#endif
6823 if (*eap->arg == NUL)
6824 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006825 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 return;
6827 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6828 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6829 }
6830 else
6831 {
6832 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6833 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6834 i = readfile(eap->arg, NULL,
6835 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6836
6837 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006838 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006840#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 if (!aborting())
6842#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006843 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 }
6845 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006846 {
6847 if (empty && exmode_active)
6848 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006849 // Delete the empty line that remains. Historically ex does
6850 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006851 if (eap->line2 == 0)
6852 lnum = curbuf->b_ml.ml_line_count;
6853 else
6854 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006855 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006856 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006857 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006858 if (curwin->w_cursor.lnum > 1
6859 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006860 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006861 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006862 }
6863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 }
6867}
6868
Bram Moolenaarea408852005-06-25 22:49:46 +00006869static char_u *prev_dir = NULL;
6870
6871#if defined(EXITFREE) || defined(PROTO)
6872 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006873free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006874{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006875 VIM_CLEAR(prev_dir);
6876 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006877}
6878#endif
6879
Bram Moolenaarf4258302013-06-02 18:20:17 +02006880/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006881 * Get the previous directory for the given chdir scope.
6882 */
6883 static char_u *
6884get_prevdir(cdscope_T scope)
6885{
6886 if (scope == CDSCOPE_WINDOW)
6887 return curwin->w_prevdir;
6888 else if (scope == CDSCOPE_TABPAGE)
6889 return curtab->tp_prevdir;
6890 return prev_dir;
6891}
6892
6893/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006894 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006895 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6896 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006897 */
6898 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006899post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006900{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006901 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006902 // Clear tab local directory for both :cd and :tcd
6903 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006904 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006905 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006906 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006907 char_u *pdir = get_prevdir(scope);
6908
6909 // If still in the global directory, need to remember current
6910 // directory as the global directory.
6911 if (globaldir == NULL && pdir != NULL)
6912 globaldir = vim_strsave(pdir);
6913
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006914 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006915 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006916 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006917 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006918 curtab->tp_localdir = vim_strsave(NameBuff);
6919 else
6920 curwin->w_localdir = vim_strsave(NameBuff);
6921 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006922 }
6923 else
6924 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006925 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006926 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006927 }
6928
6929 shorten_fnames(TRUE);
6930}
6931
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006932/*
6933 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006934 * chdir() function.
6935 * scope == CDSCOPE_WINDOW: changes the window-local directory
6936 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6937 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006938 * Returns TRUE if the directory is successfully changed.
6939 */
6940 int
6941changedir_func(
6942 char_u *new_dir,
6943 int forceit,
6944 cdscope_T scope)
6945{
6946 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006947 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006948 int dir_differs;
6949 int retval = FALSE;
6950
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006951 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006952 return FALSE;
6953
6954 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6955 {
6956 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6957 return FALSE;
6958 }
6959
6960 // ":cd -": Change to previous directory
6961 if (STRCMP(new_dir, "-") == 0)
6962 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006963 pdir = get_prevdir(scope);
6964 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006965 {
6966 emsg(_("E186: No previous directory"));
6967 return FALSE;
6968 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006969 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006970 }
6971
Bram Moolenaar002bc792020-06-05 22:33:42 +02006972 // Free the previous directory
6973 tofree = get_prevdir(scope);
6974
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006975 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006976 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006977 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006978 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006979 pdir = NULL;
6980 if (scope == CDSCOPE_WINDOW)
6981 curwin->w_prevdir = pdir;
6982 else if (scope == CDSCOPE_TABPAGE)
6983 curtab->tp_prevdir = pdir;
6984 else
6985 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006986
6987#if defined(UNIX) || defined(VMS)
6988 // for UNIX ":cd" means: go to home directory
6989 if (*new_dir == NUL)
6990 {
6991 // use NameBuff for home directory name
6992# ifdef VMS
6993 char_u *p;
6994
6995 p = mch_getenv((char_u *)"SYS$LOGIN");
6996 if (p == NULL || *p == NUL) // empty is the same as not set
6997 NameBuff[0] = NUL;
6998 else
6999 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7000# else
7001 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7002# endif
7003 new_dir = NameBuff;
7004 }
7005#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02007006 dir_differs = new_dir == NULL || pdir == NULL
7007 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007008 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7009 emsg(_(e_failed));
7010 else
7011 {
7012 char_u *acmd_fname;
7013
7014 post_chdir(scope);
7015
7016 if (dir_differs)
7017 {
7018 if (scope == CDSCOPE_WINDOW)
7019 acmd_fname = (char_u *)"window";
7020 else if (scope == CDSCOPE_TABPAGE)
7021 acmd_fname = (char_u *)"tabpage";
7022 else
7023 acmd_fname = (char_u *)"global";
7024 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7025 curbuf);
7026 }
7027 retval = TRUE;
7028 }
7029 vim_free(tofree);
7030
7031 return retval;
7032}
Bram Moolenaarea408852005-06-25 22:49:46 +00007033
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007035 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007037 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007038ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007040 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041
7042 new_dir = eap->arg;
7043#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007044 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 if (*new_dir == NUL)
7046 ex_pwd(NULL);
7047 else
7048#endif
7049 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007050 cdscope_T scope = CDSCOPE_GLOBAL;
7051
7052 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7053 scope = CDSCOPE_WINDOW;
7054 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7055 scope = CDSCOPE_TABPAGE;
7056
7057 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007058 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007059 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007060 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 ex_pwd(eap);
7062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 }
7064}
7065
7066/*
7067 * ":pwd".
7068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007070ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071{
7072 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7073 {
7074#ifdef BACKSLASH_IN_FILENAME
7075 slash_adjust(NameBuff);
7076#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007077 if (p_verbose > 0)
7078 {
7079 char *context = "global";
7080
7081 if (curwin->w_localdir != NULL)
7082 context = "window";
7083 else if (curtab->tp_localdir != NULL)
7084 context = "tabpage";
7085 smsg("[%s] %s", context, (char *)NameBuff);
7086 }
7087 else
7088 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 }
7090 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007091 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092}
7093
7094/*
7095 * ":=".
7096 */
7097 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007098ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007100 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007101 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102}
7103
7104 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007105ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007107 int n;
7108 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109
7110 if (cursor_valid())
7111 {
7112 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7113 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007114 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007116
7117 len = eap->line2;
7118 switch (*eap->arg)
7119 {
7120 case 'm': break;
7121 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007122 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007123 }
7124 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125}
7126
7127/*
7128 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7129 */
7130 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007131do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132{
Bram Moolenaar52953192019-08-16 10:27:13 +02007133 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007134 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007135# ifdef ELAPSED_FUNC
7136 elapsed_T start_tv;
7137
7138 // Remember at what time we started, so that we know how much longer we
7139 // should wait after waiting for a bit.
7140 ELAPSED_INIT(start_tv);
7141# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142
7143 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007144 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007145 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007147 wait_now = msec - done > 1000L ? 1000L : msec - done;
7148#ifdef FEAT_TIMERS
7149 {
7150 long due_time = check_due_timer();
7151
7152 if (due_time > 0 && due_time < wait_now)
7153 wait_now = due_time;
7154 }
7155#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007156#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007157 if (has_any_channel() && wait_now > 20L)
7158 wait_now = 20L;
7159#endif
7160#ifdef FEAT_SOUND
7161 if (has_any_sound_callback() && wait_now > 20L)
7162 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007163#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007164 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007165
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007166#ifdef FEAT_JOB_CHANNEL
7167 if (has_any_channel())
7168 ui_breakcheck_force(TRUE);
7169 else
7170#endif
7171 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007172#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007173 // Process the netbeans and clientserver messages that may have been
7174 // received in the call to ui_breakcheck() when the GUI is in use. This
7175 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007176 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007177#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007178
7179# ifdef ELAPSED_FUNC
7180 // actual time passed
7181 done = ELAPSED_FUNC(start_tv);
7182# else
7183 // guestimate time passed (will actually be more)
7184 done += wait_now;
7185# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007187
7188 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7189 // input buffer, otherwise a following call to input() fails.
7190 if (got_int)
7191 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192}
7193
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194/*
7195 * ":winsize" command (obsolete).
7196 */
7197 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007198ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199{
7200 int w, h;
7201 char_u *arg = eap->arg;
7202 char_u *p;
7203
7204 w = getdigits(&arg);
7205 arg = skipwhite(arg);
7206 p = arg;
7207 h = getdigits(&arg);
7208 if (*p != NUL && *arg == NUL)
7209 set_shellsize(w, h, TRUE);
7210 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007211 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212}
7213
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007215ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216{
7217 int xchar = NUL;
7218 char_u *p;
7219
7220 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7221 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007222 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 if (eap->arg[1] == NUL)
7224 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007225 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 return;
7227 }
7228 xchar = eap->arg[1];
7229 p = eap->arg + 2;
7230 }
7231 else
7232 p = eap->arg + 1;
7233
7234 eap->nextcmd = check_nextcmd(p);
7235 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007236 if (*p != NUL && *p != (
7237#ifdef FEAT_EVAL
7238 in_vim9script() ? '#' :
7239#endif
7240 '"')
7241 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007242 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007243 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007245 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007246 postponed_split_flags = cmdmod.cmod_split;
7247 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7249 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007250 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 }
7252}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253
Bram Moolenaar843ee412004-06-30 16:16:41 +00007254#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255/*
7256 * ":winpos".
7257 */
7258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007259ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260{
7261 int x, y;
7262 char_u *arg = eap->arg;
7263 char_u *p;
7264
7265 if (*arg == NUL)
7266 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007267# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007268# ifdef VIMDLL
7269 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7270 mch_get_winpos(&x, &y) != FAIL)
7271# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007273# else
7274 if (mch_get_winpos(&x, &y) != FAIL)
7275# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 {
7277 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007278 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 }
7280 else
7281# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007282 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 }
7284 else
7285 {
7286 x = getdigits(&arg);
7287 arg = skipwhite(arg);
7288 p = arg;
7289 y = getdigits(&arg);
7290 if (*p == NUL || *arg != NUL)
7291 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007292 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 return;
7294 }
7295# ifdef FEAT_GUI
7296 if (gui.in_use)
7297 gui_mch_set_winpos(x, y);
7298 else if (gui.starting)
7299 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007300 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 gui_win_x = x;
7302 gui_win_y = y;
7303 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007304# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 else
7306# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007307# endif
7308# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007309 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310# endif
7311# ifdef HAVE_TGETENT
7312 if (*T_CWP)
7313 term_set_winpos(x, y);
7314# endif
7315 }
7316}
7317#endif
7318
7319/*
7320 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7321 */
7322 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007323ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324{
7325 oparg_T oa;
7326
7327 clear_oparg(&oa);
7328 oa.regname = eap->regname;
7329 oa.start.lnum = eap->line1;
7330 oa.end.lnum = eap->line2;
7331 oa.line_count = eap->line2 - eap->line1 + 1;
7332 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007334 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 {
7336 setpcmark();
7337 curwin->w_cursor.lnum = eap->line1;
7338 beginline(BL_SOL | BL_FIX);
7339 }
7340
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007341 if (VIsual_active)
7342 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007343
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 switch (eap->cmdidx)
7345 {
7346 case CMD_delete:
7347 oa.op_type = OP_DELETE;
7348 op_delete(&oa);
7349 break;
7350
7351 case CMD_yank:
7352 oa.op_type = OP_YANK;
7353 (void)op_yank(&oa, FALSE, TRUE);
7354 break;
7355
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007356 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007357 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007359 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7360#else
7361 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007363 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 oa.op_type = OP_RSHIFT;
7365 else
7366 oa.op_type = OP_LSHIFT;
7367 op_shift(&oa, FALSE, eap->amount);
7368 break;
7369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007371 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372}
7373
7374/*
7375 * ":put".
7376 */
7377 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007378ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007380 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 if (eap->line2 == 0)
7382 {
7383 eap->line2 = 1;
7384 eap->forceit = TRUE;
7385 }
7386 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007387 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007388 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389}
7390
7391/*
7392 * Handle ":copy" and ":move".
7393 */
7394 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007395ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396{
7397 long n;
7398
Bram Moolenaar491799b2020-08-01 19:23:43 +02007399#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007400 if (not_in_vim9(eap) == FAIL)
7401 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007402#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007403 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007404 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 {
7406 eap->nextcmd = NULL;
7407 return;
7408 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007409 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410
7411 /*
7412 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7413 */
7414 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7415 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007416 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 return;
7418 }
7419
7420 if (eap->cmdidx == CMD_move)
7421 {
7422 if (do_move(eap->line1, eap->line2, n) == FAIL)
7423 return;
7424 }
7425 else
7426 ex_copy(eap->line1, eap->line2, n);
7427 u_clearline();
7428 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007429 ex_may_print(eap);
7430}
7431
7432/*
7433 * Print the current line if flags were given to the Ex command.
7434 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007435 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007436ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007437{
7438 if (eap->flags != 0)
7439 {
7440 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7441 (eap->flags & EXFLAG_LIST));
7442 ex_no_reprint = TRUE;
7443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444}
7445
7446/*
7447 * ":smagic" and ":snomagic".
7448 */
7449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007450ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451{
7452 int magic_save = p_magic;
7453
7454 p_magic = (eap->cmdidx == CMD_smagic);
Bram Moolenaar66e00142020-08-12 21:58:12 +02007455 ex_substitute(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 p_magic = magic_save;
7457}
7458
7459/*
7460 * ":join".
7461 */
7462 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007463ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464{
7465 curwin->w_cursor.lnum = eap->line1;
7466 if (eap->line1 == eap->line2)
7467 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007468 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 return;
7470 if (eap->line2 == curbuf->b_ml.ml_line_count)
7471 {
7472 beep_flush();
7473 return;
7474 }
7475 ++eap->line2;
7476 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007477 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007479 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480}
7481
7482/*
7483 * ":[addr]@r" or ":[addr]*r": execute register
7484 */
7485 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007486ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487{
7488 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007489 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490
7491 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007492 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493
7494#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007495 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496#endif
7497
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007498 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 c = *eap->arg;
7500 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7501 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007502 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007503 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7504 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 else
7509 {
7510 int save_efr = exec_from_reg;
7511
7512 exec_from_reg = TRUE;
7513
7514 /*
7515 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007516 * Continue until the stuff buffer is empty and all added characters
7517 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007519 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7521
7522 exec_from_reg = save_efr;
7523 }
7524}
7525
7526/*
7527 * ":!".
7528 */
7529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007530ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531{
7532 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7533}
7534
7535/*
7536 * ":undo".
7537 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007539ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007541 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007542 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007543 else
7544 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545}
7546
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007547#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007549ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007550{
7551 char_u hash[UNDO_HASH_SIZE];
7552
7553 u_compute_hash(hash);
7554 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7555}
7556
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007557 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007558ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007559{
7560 char_u hash[UNDO_HASH_SIZE];
7561
7562 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007563 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007564}
7565#endif
7566
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567/*
7568 * ":redo".
7569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007571ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572{
7573 u_redo(1);
7574}
7575
7576/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007577 * ":earlier" and ":later".
7578 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007579 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007580ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007581{
7582 long count = 0;
7583 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007584 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007585 char_u *p = eap->arg;
7586
7587 if (*p == NUL)
7588 count = 1;
7589 else if (isdigit(*p))
7590 {
7591 count = getdigits(&p);
7592 switch (*p)
7593 {
7594 case 's': ++p; sec = TRUE; break;
7595 case 'm': ++p; sec = TRUE; count *= 60; break;
7596 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007597 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7598 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007599 }
7600 }
7601
7602 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007603 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007604 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007605 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7606 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007607}
7608
7609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 * ":redir": start/stop redirection.
7611 */
7612 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007613ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614{
7615 char *mode;
7616 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007617 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618
Bram Moolenaarba768492016-07-08 15:32:54 +02007619#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007620 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007621 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007622 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007623 return;
7624 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007625#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007626
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 if (STRICMP(eap->arg, "END") == 0)
7628 close_redir();
7629 else
7630 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007631 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007633 ++arg;
7634 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007636 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 mode = "a";
7638 }
7639 else
7640 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007641 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642
7643 close_redir();
7644
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007645 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007646 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 if (fname == NULL)
7648 return;
7649#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007650 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {
7652 char_u *browseFile;
7653
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007654 browseFile = do_browse(BROWSE_SAVE,
7655 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007656 fname, NULL, NULL,
7657 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007659 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 vim_free(fname);
7661 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007662 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 }
7664#endif
7665
7666 redir_fd = open_exfile(fname, eap->forceit, mode);
7667 vim_free(fname);
7668 }
7669#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007670 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007672 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007674 ++arg;
7675 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007677 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007678 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007680 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007682 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007683 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007684 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007685 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007687 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007688 if (*arg == '>')
7689 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007690 // Make register empty when not using @A-@Z and the
7691 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007692 if (*arg == NUL && !isupper(redir_reg))
7693 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007695 }
7696 if (*arg != NUL)
7697 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007698 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007699 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007700 }
7701 }
7702 else if (*arg == '=' && arg[1] == '>')
7703 {
7704 int append;
7705
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007706 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007707 close_redir();
7708 arg += 2;
7709
7710 if (*arg == '>')
7711 {
7712 ++arg;
7713 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 }
7715 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007716 append = FALSE;
7717
7718 if (var_redir_start(skipwhite(arg), append) == OK)
7719 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 }
7721#endif
7722
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007723 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007726 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007728
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007729 // Make sure redirection is not off. Can happen for cmdline completion
7730 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007731 if (redir_fd != NULL
7732#ifdef FEAT_EVAL
7733 || redir_reg || redir_vname
7734#endif
7735 )
7736 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737}
7738
7739/*
7740 * ":redraw": force redraw
7741 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007742 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007743ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744{
7745 int r = RedrawingDisabled;
7746 int p = p_lz;
7747
7748 RedrawingDisabled = 0;
7749 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007750 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007752 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753#ifdef FEAT_TITLE
7754 if (need_maketitle)
7755 maketitle();
7756#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007757#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7758# ifdef VIMDLL
7759 if (!gui.in_use)
7760# endif
7761 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 RedrawingDisabled = r;
7764 p_lz = p;
7765
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007766 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 msg_didout = FALSE;
7768 msg_col = 0;
7769
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007770 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007771 need_wait_return = FALSE;
7772
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 out_flush();
7774}
7775
7776/*
7777 * ":redrawstatus": force redraw of status line(s)
7778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007780ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 int r = RedrawingDisabled;
7783 int p = p_lz;
7784
7785 RedrawingDisabled = 0;
7786 p_lz = FALSE;
7787 if (eap->forceit)
7788 status_redraw_all();
7789 else
7790 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007791 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 RedrawingDisabled = r;
7793 p_lz = p;
7794 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795}
7796
Bram Moolenaare12bab32019-01-08 22:02:56 +01007797/*
7798 * ":redrawtabline": force redraw of the tabline
7799 */
7800 static void
7801ex_redrawtabline(exarg_T *eap UNUSED)
7802{
7803 int r = RedrawingDisabled;
7804 int p = p_lz;
7805
7806 RedrawingDisabled = 0;
7807 p_lz = FALSE;
7808
7809 draw_tabline();
7810
7811 RedrawingDisabled = r;
7812 p_lz = p;
7813 out_flush();
7814}
7815
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007817close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818{
7819 if (redir_fd != NULL)
7820 {
7821 fclose(redir_fd);
7822 redir_fd = NULL;
7823 }
7824#ifdef FEAT_EVAL
7825 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007826 if (redir_vname)
7827 {
7828 var_redir_stop();
7829 redir_vname = 0;
7830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831#endif
7832}
7833
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007834#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007835 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007836vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007837{
7838 if (vim_mkdir(name, prot) != 0)
7839 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007840 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007841 return FAIL;
7842 }
7843 return OK;
7844}
7845#endif
7846
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847/*
7848 * Open a file for writing for an Ex command, with some checks.
7849 * Return file descriptor, or NULL on failure.
7850 */
7851 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007852open_exfile(
7853 char_u *fname,
7854 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007855 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856{
7857 FILE *fd;
7858
7859#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007860 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 if (mch_isdir(fname))
7862 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007863 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 return NULL;
7865 }
7866#endif
7867 if (!forceit && *mode != 'a' && vim_fexists(fname))
7868 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007869 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 return NULL;
7871 }
7872
7873 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007874 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875
7876 return fd;
7877}
7878
7879/*
7880 * ":mark" and ":k".
7881 */
7882 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007883ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884{
7885 pos_T pos;
7886
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007887 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007888 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007889 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02007890 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 else
7892 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007893 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 curwin->w_cursor.lnum = eap->line2;
7895 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007896 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007897 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007898 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 }
7900}
7901
7902/*
7903 * Update w_topline, w_leftcol and the cursor position.
7904 */
7905 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007906update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007908 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 update_topline();
7910 if (!curwin->w_p_wrap)
7911 validate_cursor();
7912 update_curswant();
7913}
7914
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007916 * Save the current State and go to Normal mode.
7917 * Return TRUE if the typeahead could be saved.
7918 */
7919 int
7920save_current_state(save_state_T *sst)
7921{
7922 sst->save_msg_scroll = msg_scroll;
7923 sst->save_restart_edit = restart_edit;
7924 sst->save_msg_didout = msg_didout;
7925 sst->save_State = State;
7926 sst->save_insertmode = p_im;
7927 sst->save_finish_op = finish_op;
7928 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007929 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007930
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007931 msg_scroll = FALSE; // no msg scrolling in Normal mode
7932 restart_edit = 0; // don't go to Insert mode
7933 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007934
7935 /*
7936 * Save the current typeahead. This is required to allow using ":normal"
7937 * from an event handler and makes sure we don't hang when the argument
7938 * ends with half a command.
7939 */
7940 save_typeahead(&sst->tabuf);
7941 return sst->tabuf.typebuf_valid;
7942}
7943
7944 void
7945restore_current_state(save_state_T *sst)
7946{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007947 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007948 restore_typeahead(&sst->tabuf);
7949
7950 msg_scroll = sst->save_msg_scroll;
7951 restart_edit = sst->save_restart_edit;
7952 p_im = sst->save_insertmode;
7953 finish_op = sst->save_finish_op;
7954 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007955 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007956 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007957
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007958 // Restore the state (needed when called from a function executed for
7959 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007960 State = sst->save_State;
7961#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007962 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007963#endif
7964}
7965
7966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 * ":normal[!] {commands}": Execute normal mode commands.
7968 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007969 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007970ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007972 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 char_u *arg = NULL;
7974 int l;
7975 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007977 if (ex_normal_lock > 0)
7978 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007979 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007980 return;
7981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 if (ex_normal_busy >= p_mmd)
7983 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007984 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 return;
7986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 /*
7989 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7990 * this for the K_SPECIAL leading byte, otherwise special keys will not
7991 * work.
7992 */
7993 if (has_mbyte)
7994 {
7995 int len = 0;
7996
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007997 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 for (p = eap->arg; *p != NUL; ++p)
7999 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008000#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008001 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008003#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008004 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008005 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008006#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 )
8010 len += 2;
8011 }
8012 if (len > 0)
8013 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008014 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 if (arg != NULL)
8016 {
8017 len = 0;
8018 for (p = eap->arg; *p != NUL; ++p)
8019 {
8020 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008021#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 if (*p == CSI)
8023 {
8024 arg[len++] = KS_EXTRA;
8025 arg[len++] = (int)KE_CSI;
8026 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008027#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008028 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 {
8030 arg[len++] = *++p;
8031 if (*p == K_SPECIAL)
8032 {
8033 arg[len++] = KS_SPECIAL;
8034 arg[len++] = KE_FILLER;
8035 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008036#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 else if (*p == CSI)
8038 {
8039 arg[len++] = KS_EXTRA;
8040 arg[len++] = (int)KE_CSI;
8041 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 }
8044 arg[len] = NUL;
8045 }
8046 }
8047 }
8048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008050 ++ex_normal_busy;
8051 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 {
8053 /*
8054 * Repeat the :normal command for each line in the range. When no
8055 * range given, execute it just once, without positioning the cursor
8056 * first.
8057 */
8058 do
8059 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 if (eap->addr_count != 0)
8061 {
8062 curwin->w_cursor.lnum = eap->line1++;
8063 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008064 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 }
8066
Bram Moolenaar13505972019-01-24 15:04:48 +01008067 exec_normal_cmd(arg != NULL
8068 ? arg
8069 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 }
8071 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8072 }
8073
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008074 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 update_topline_cursor();
8076
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008077 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008079 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008080#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008081 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008082#endif
8083
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085}
8086
8087/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008088 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 */
8090 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008091ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008093 if (eap->forceit)
8094 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008095 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008096 if (!curwin->w_cursor.lnum)
8097 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008098 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008099 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008100#ifdef FEAT_TERMINAL
8101 // Ignore this when running in an active terminal.
8102 if (term_job_running(curbuf->b_term))
8103 return;
8104#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008105
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008106 // Ignore the command when already in Insert mode. Inserting an
8107 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008108 if (State & INSERT)
8109 return;
8110
Bram Moolenaar64969662005-12-14 21:59:55 +00008111 if (eap->cmdidx == CMD_startinsert)
8112 restart_edit = 'a';
8113 else if (eap->cmdidx == CMD_startreplace)
8114 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008116 restart_edit = 'V';
8117
8118 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008120 if (eap->cmdidx == CMD_startinsert)
8121 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008122 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 }
8124}
8125
8126/*
8127 * ":stopinsert"
8128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008130ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131{
8132 restart_edit = 0;
8133 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008134 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008137/*
8138 * Execute normal mode command "cmd".
8139 * "remap" can be REMAP_NONE or REMAP_YES.
8140 */
8141 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008142exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008143{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008144 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008145 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008146 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008147}
Bram Moolenaar25281632016-01-21 23:32:32 +01008148
Bram Moolenaar25281632016-01-21 23:32:32 +01008149/*
8150 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008151 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008152 */
8153 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008154exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008155{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008156 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008157 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008158
Bram Moolenaar905dd902019-04-07 14:21:47 +02008159 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8160 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008161 clear_oparg(&oa);
8162 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008163 while ((!stuff_empty()
8164 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008165 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008166 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008167 {
8168 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008169#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008170 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008171 && oa.op_type == OP_NOP && oa.regname == NUL
8172 && !VIsual_active)
8173 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008174 // If terminal_loop() returns OK we got a key that is handled
8175 // in Normal model. With FAIL we first need to position the
8176 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008177 if (terminal_loop(TRUE) == OK)
8178 normal_cmd(&oa, TRUE);
8179 }
8180 else
8181#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008182 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008183 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008184 }
8185}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008186
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187#ifdef FEAT_FIND_ID
8188 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008189ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190{
8191 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8192 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8193 (linenr_T)1, (linenr_T)MAXLNUM);
8194}
8195
Bram Moolenaar4033c552017-09-16 20:54:51 +02008196#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197/*
8198 * ":psearch"
8199 */
8200 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008201ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202{
8203 g_do_tagpreview = p_pvh;
8204 ex_findpat(eap);
8205 g_do_tagpreview = 0;
8206}
8207#endif
8208
8209 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008210ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211{
8212 int whole = TRUE;
8213 long n;
8214 char_u *p;
8215 int action;
8216
8217 switch (cmdnames[eap->cmdidx].cmd_name[2])
8218 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008219 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8221 action = ACTION_GOTO;
8222 else
8223 action = ACTION_SHOW;
8224 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008225 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 action = ACTION_SHOW_ALL;
8227 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008228 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 action = ACTION_GOTO;
8230 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008231 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 action = ACTION_SPLIT;
8233 break;
8234 }
8235
8236 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008237 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 {
8239 n = getdigits(&eap->arg);
8240 eap->arg = skipwhite(eap->arg);
8241 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008242 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {
8244 whole = FALSE;
8245 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008246 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 if (*p)
8248 {
8249 *p++ = NUL;
8250 p = skipwhite(p);
8251
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008252 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008253 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008254 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 else
8256 eap->nextcmd = check_nextcmd(p);
8257 }
8258 }
8259 if (!eap->skip)
8260 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8261 whole, !eap->forceit,
8262 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8263 n, action, eap->line1, eap->line2);
8264}
8265#endif
8266
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267
Bram Moolenaar4033c552017-09-16 20:54:51 +02008268#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269/*
8270 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8271 */
8272 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008273ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008275 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8277}
8278
8279/*
8280 * ":pedit"
8281 */
8282 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008283ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284{
8285 win_T *curwin_save = curwin;
8286
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008287 if (ERROR_IF_ANY_POPUP_WINDOW)
8288 return;
8289
Bram Moolenaar026587b2019-08-17 15:08:00 +02008290 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008292 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008293
Bram Moolenaar026587b2019-08-17 15:08:00 +02008294 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008296
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 if (curwin != curwin_save && win_valid(curwin_save))
8298 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008299 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 validate_cursor();
8301 redraw_later(VALID);
8302 win_enter(curwin_save, TRUE);
8303 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008304# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008305 else if (WIN_IS_POPUP(curwin))
8306 {
8307 // can't keep focus in popup window
8308 win_enter(firstwin, TRUE);
8309 }
8310# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 g_do_tagpreview = 0;
8312}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314
8315/*
8316 * ":stag", ":stselect" and ":stjump".
8317 */
8318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008319ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320{
8321 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008322 postponed_split_flags = cmdmod.cmod_split;
8323 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8325 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008326 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328
8329/*
8330 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8331 */
8332 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008333ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334{
8335 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8336}
8337
8338 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008339ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340{
8341 int cmd;
8342
8343 switch (name[1])
8344 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008345 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008347 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008349 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008351 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008353 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008355 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008357 case 'f': // ":tfirst"
8358 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008360 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008362 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008364 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008366 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 return;
8368 }
8369#endif
8370 cmd = DT_TAG;
8371 break;
8372 }
8373
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008374 if (name[0] == 'l')
8375 {
8376#ifndef FEAT_QUICKFIX
8377 ex_ni(eap);
8378 return;
8379#else
8380 cmd = DT_LTAG;
8381#endif
8382 }
8383
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8385 eap->forceit, TRUE);
8386}
8387
8388/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008389 * Check "str" for starting with a special cmdline variable.
8390 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8391 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8392 */
8393 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008394find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008395{
8396 int len;
8397 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008398 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008399 "%",
8400#define SPEC_PERC 0
8401 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008402#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008403 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008404#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008405 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008406#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008407 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008408#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008409 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008410#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008411 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008412#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008413 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008414#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008415 "<stack>", // call stack
8416#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008417 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008418#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008419 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008420#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008421 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008422#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008423 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008424#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008425 "<SID>", // script ID: <SNR>123_
8426#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008427#ifdef FEAT_CLIENTSERVER
8428 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008429# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008430#endif
8431 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008432
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008433 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008434 {
8435 len = (int)STRLEN(spec_str[i]);
8436 if (STRNCMP(src, spec_str[i], len) == 0)
8437 {
8438 *usedlen = len;
8439 return i;
8440 }
8441 }
8442 return -1;
8443}
8444
8445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 * Evaluate cmdline variables.
8447 *
8448 * change '%' to curbuf->b_ffname
8449 * '#' to curwin->w_altfile
8450 * '<cword>' to word under the cursor
8451 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008452 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 * '<cfile>' to path name under the cursor
8454 * '<sfile>' to sourced file name
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008455 * '<stack>' to call stack
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008456 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 * '<afile>' to file name for autocommand
8458 * '<abuf>' to buffer number for autocommand
8459 * '<amatch>' to matching name for autocommand
8460 *
8461 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8462 * "" for error without a message) and NULL is returned.
8463 * Returns an allocated string if a valid match was found.
8464 * Returns NULL if no match was found. "usedlen" then still contains the
8465 * number of characters to skip.
8466 */
8467 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008468eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008469 char_u *src, // pointer into commandline
8470 char_u *srcstart, // beginning of valid memory for src
8471 int *usedlen, // characters after src that are used
8472 linenr_T *lnump, // line number for :e command, or NULL
8473 char **errormsg, // pointer to error message
8474 int *escaped) // return value has escaped white space (can
8475 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476{
8477 int i;
8478 char_u *s;
8479 char_u *result;
8480 char_u *resultbuf = NULL;
8481 int resultlen;
8482 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008483 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008485 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488
8489 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008490 if (escaped != NULL)
8491 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492
8493 /*
8494 * Check if there is something to do.
8495 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008496 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008497 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 {
8499 *usedlen = 1;
8500 return NULL;
8501 }
8502
8503 /*
8504 * Skip when preceded with a backslash "\%" and "\#".
8505 * Note: In "\\%" the % is also not recognized!
8506 */
8507 if (src > srcstart && src[-1] == '\\')
8508 {
8509 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008510 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 return NULL;
8512 }
8513
8514 /*
8515 * word or WORD under cursor
8516 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008517 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8518 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008520 resultlen = find_ident_under_cursor(&result,
8521 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8522 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8523 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 if (resultlen == 0)
8525 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008526 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 return NULL;
8528 }
8529 }
8530
8531 /*
8532 * '#': Alternate file name
8533 * '%': Current file name
8534 * File name under the cursor
8535 * File name for autocommand
8536 * and following modifiers
8537 */
8538 else
8539 {
8540 switch (spec_idx)
8541 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008542 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 if (curbuf->b_fname == NULL)
8544 {
8545 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008546 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 }
8548 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008549 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008551 tilde_file = STRCMP(result, "~") == 0;
8552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 break;
8554
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008555 case SPEC_HASH: // '#' or "#99": alternate file
8556 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 {
8558 result = arg_all();
8559 resultbuf = result;
8560 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008561 if (escaped != NULL)
8562 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 break;
8565 }
8566 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008567 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008568 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008570 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008571 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008572 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008573 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008575 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008577 if (*usedlen < 2)
8578 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008579 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008580 *usedlen = 1;
8581 return NULL;
8582 }
8583#ifdef FEAT_EVAL
8584 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8585 (long)i);
8586 if (result == NULL)
8587 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008588 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008589 return NULL;
8590 }
8591#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008592 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 }
8596 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008597 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008598 if (i == 0 && src[1] == '<' && *usedlen > 1)
8599 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008600 buf = buflist_findnr(i);
8601 if (buf == NULL)
8602 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008603 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008604 return NULL;
8605 }
8606 if (lnump != NULL)
8607 *lnump = ECMD_LAST;
8608 if (buf->b_fname == NULL)
8609 {
8610 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008611 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008612 }
8613 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008614 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008615 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008616 tilde_file = STRCMP(result, "~") == 0;
8617 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 break;
8620
8621#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008622 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008623 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 if (result == NULL)
8625 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008626 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 return NULL;
8628 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008629 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 break;
8631#endif
8632
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008633 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008635 if (result != NULL && !autocmd_fname_full)
8636 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008637 // Still need to turn the fname into a full path. It is
8638 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008639 autocmd_fname_full = TRUE;
8640 result = FullName_save(autocmd_fname, FALSE);
8641 vim_free(autocmd_fname);
8642 autocmd_fname = result;
8643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 if (result == NULL)
8645 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008646 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 return NULL;
8648 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008649 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 break;
8651
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008652 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 if (autocmd_bufnr <= 0)
8654 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008655 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 return NULL;
8657 }
8658 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8659 result = strbuf;
8660 break;
8661
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008662 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 result = autocmd_match;
8664 if (result == NULL)
8665 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008666 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 return NULL;
8668 }
8669 break;
8670
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008671 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008672 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008673 result = estack_sfile(spec_idx == SPEC_SFILE
8674 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 if (result == NULL)
8676 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008677 *errormsg = spec_idx == SPEC_SFILE
8678 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8679 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 return NULL;
8681 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008682 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008684
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008685 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008686 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008687 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008688 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008689 return NULL;
8690 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008691 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008692 result = strbuf;
8693 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008694
8695#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008696 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008697 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008698 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008699 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008700 return NULL;
8701 }
8702 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008703 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008704 result = strbuf;
8705 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008706
Bram Moolenaar90944302020-08-01 20:45:11 +02008707 case SPEC_SID:
8708 if (current_sctx.sc_sid <= 0)
8709 {
8710 *errormsg = _(e_usingsid);
8711 return NULL;
8712 }
8713 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
8714 result = strbuf;
8715 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02008716#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02008717
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008718#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008719 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008720 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8721 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 result = strbuf;
8723 break;
8724#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008725
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008726 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008727 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008728 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 }
8730
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008731 resultlen = (int)STRLEN(result); // length of new string
8732 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 {
8734 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 resultlen = (int)(s - result);
8737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 else if (!skip_mod)
8739 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008740 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 &resultlen);
8742 if (result == NULL)
8743 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008744 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 return NULL;
8746 }
8747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 }
8749
8750 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8751 {
8752 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008753 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008754 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008756 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 result = NULL;
8758 }
8759 else
8760 result = vim_strnsave(result, resultlen);
8761 vim_free(resultbuf);
8762 return result;
8763}
8764
8765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 * Expand the <sfile> string in "arg".
8767 *
8768 * Returns an allocated string, or NULL for any error.
8769 */
8770 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008771expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008773 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 int len;
8775 char_u *result;
8776 char_u *newres;
8777 char_u *repl;
8778 int srclen;
8779 char_u *p;
8780
8781 result = vim_strsave(arg);
8782 if (result == NULL)
8783 return NULL;
8784
8785 for (p = result; *p; )
8786 {
8787 if (STRNCMP(p, "<sfile>", 7) != 0)
8788 ++p;
8789 else
8790 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008791 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008792 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 if (errormsg != NULL)
8794 {
8795 if (*errormsg)
8796 emsg(errormsg);
8797 vim_free(result);
8798 return NULL;
8799 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008800 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 {
8802 p += srclen;
8803 continue;
8804 }
8805 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8806 newres = alloc(len);
8807 if (newres == NULL)
8808 {
8809 vim_free(repl);
8810 vim_free(result);
8811 return NULL;
8812 }
8813 mch_memmove(newres, result, (size_t)(p - result));
8814 STRCPY(newres + (p - result), repl);
8815 len = (int)STRLEN(newres);
8816 STRCAT(newres, p + srclen);
8817 vim_free(repl);
8818 vim_free(result);
8819 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008820 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 }
8822 }
8823
8824 return result;
8825}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008828/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008829 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008830 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008833dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 if (fname == NULL)
8836 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008837 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838}
8839#endif
8840
8841/*
8842 * ":behave {mswin,xterm}"
8843 */
8844 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008845ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846{
8847 if (STRCMP(eap->arg, "mswin") == 0)
8848 {
8849 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8850 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8851 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8852 set_option_value((char_u *)"keymodel", 0L,
8853 (char_u *)"startsel,stopsel", 0);
8854 }
8855 else if (STRCMP(eap->arg, "xterm") == 0)
8856 {
8857 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8858 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8859 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8860 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8861 }
8862 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008863 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864}
8865
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866static int filetype_detect = FALSE;
8867static int filetype_plugin = FALSE;
8868static int filetype_indent = FALSE;
8869
8870/*
8871 * ":filetype [plugin] [indent] {on,off,detect}"
8872 * on: Load the filetype.vim file to install autocommands for file types.
8873 * off: Load the ftoff.vim file to remove all autocommands for file types.
8874 * plugin on: load filetype.vim and ftplugin.vim
8875 * plugin off: load ftplugof.vim
8876 * indent on: load filetype.vim and indent.vim
8877 * indent off: load indoff.vim
8878 */
8879 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008880ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881{
8882 char_u *arg = eap->arg;
8883 int plugin = FALSE;
8884 int indent = FALSE;
8885
8886 if (*eap->arg == NUL)
8887 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008888 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008889 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 filetype_detect ? "ON" : "OFF",
8891 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8892 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8893 return;
8894 }
8895
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008896 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 for (;;)
8898 {
8899 if (STRNCMP(arg, "plugin", 6) == 0)
8900 {
8901 plugin = TRUE;
8902 arg = skipwhite(arg + 6);
8903 continue;
8904 }
8905 if (STRNCMP(arg, "indent", 6) == 0)
8906 {
8907 indent = TRUE;
8908 arg = skipwhite(arg + 6);
8909 continue;
8910 }
8911 break;
8912 }
8913 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8914 {
8915 if (*arg == 'o' || !filetype_detect)
8916 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008917 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 filetype_detect = TRUE;
8919 if (plugin)
8920 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008921 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 filetype_plugin = TRUE;
8923 }
8924 if (indent)
8925 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008926 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 filetype_indent = TRUE;
8928 }
8929 }
8930 if (*arg == 'd')
8931 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008932 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008933 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 }
8935 }
8936 else if (STRCMP(arg, "off") == 0)
8937 {
8938 if (plugin || indent)
8939 {
8940 if (plugin)
8941 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008942 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 filetype_plugin = FALSE;
8944 }
8945 if (indent)
8946 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008947 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 filetype_indent = FALSE;
8949 }
8950 }
8951 else
8952 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008953 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 filetype_detect = FALSE;
8955 }
8956 }
8957 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008958 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959}
8960
8961/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008962 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 */
8964 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008965ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966{
8967 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008968 {
8969 char_u *arg = eap->arg;
8970
8971 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8972 arg += 9;
8973
8974 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8975 if (arg != eap->arg)
8976 did_filetype = FALSE;
8977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008981ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982{
8983#ifdef FEAT_DIGRAPHS
8984 if (*eap->arg != NUL)
8985 putdigraph(eap->arg);
8986 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008987 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008989 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990#endif
8991}
8992
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008993#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8994 void
8995set_no_hlsearch(int flag)
8996{
8997 no_hlsearch = flag;
8998# ifdef FEAT_EVAL
8999 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9000# endif
9001}
9002
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003/*
9004 * ":nohlsearch"
9005 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009007ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009009 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009010 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012#endif
9013
9014#ifdef FEAT_CRYPT
9015/*
9016 * ":X": Get crypt key
9017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009019ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009021 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009022 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023}
9024#endif
9025
9026#ifdef FEAT_FOLDING
9027 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009028ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029{
9030 if (foldManualAllowed(TRUE))
9031 foldCreate(eap->line1, eap->line2);
9032}
9033
9034 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009035ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036{
9037 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9038 eap->forceit, FALSE);
9039}
9040
9041 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009042ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043{
9044 linenr_T lnum;
9045
Bram Moolenaar4facea32019-10-12 20:17:40 +02009046# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009047 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009048# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009049
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009050 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9052 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9053 ml_setmarked(lnum);
9054
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009055 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009057 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009058# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009059 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061}
9062#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009063
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009064#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009065/*
9066 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9067 * instead of a quickfix command.
9068 */
9069 int
9070is_loclist_cmd(int cmdidx)
9071{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009072 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009073 return FALSE;
9074 return cmdnames[cmdidx].cmd_name[0] == 'l';
9075}
9076#endif
9077
Bram Moolenaar4facea32019-10-12 20:17:40 +02009078#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009079 int
9080get_pressedreturn(void)
9081{
9082 return ex_pressedreturn;
9083}
9084
9085 void
9086set_pressedreturn(int val)
9087{
9088 ex_pressedreturn = val;
9089}
9090#endif