blob: 1e1b8e3a461245c27e1cb35f2e5370bcd0f0bf24 [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
1714 int save_msg_scroll = msg_scroll;
1715 cmdmod_T save_cmdmod;
1716 int save_reg_executing = reg_executing;
1717 int ni; // set when Not Implemented
1718 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001719 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001720#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001721 int may_have_range;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001722 int vim9script = in_vim9script();
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724
Bram Moolenaara80faa82020-04-12 19:37:17 +02001725 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 ea.line1 = 1;
1727 ea.line2 = 1;
1728#ifdef FEAT_EVAL
1729 ++ex_nesting_level;
1730#endif
1731
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001732 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 if (quitmore
1734#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001735 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001736 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001737#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001738 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001739 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 --quitmore;
1741
1742 /*
1743 * Reset browse, confirm, etc.. They are restored when returning, for
1744 * recursive calls.
1745 */
1746 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001748 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001749 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1750 goto doend;
1751
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001752/*
1753 * 1. Skip comment lines and leading white space and colons.
1754 * 2. Handle command modifiers.
1755 */
1756 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001758 ea.cmdlinep = cmdlinep;
1759 ea.getline = fgetline;
1760 ea.cookie = cookie;
1761#ifdef FEAT_EVAL
1762 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001763 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001765 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001766 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001767 apply_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001769 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770
1771#ifdef FEAT_EVAL
1772 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1773 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1774#else
1775 ea.skip = (if_level > 0);
1776#endif
1777
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001779 * 3. Skip over the range to find the command. Let "p" point to after it.
1780 *
1781 * We need the command to know what kind of range it uses.
1782 */
1783 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001784#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001785 // In Vim9 script a colon is required before the range.
1786 may_have_range = !vim9script || starts_with_colon;
1787 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001788#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001789 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001790
1791#ifdef FEAT_EVAL
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001792 if (vim9script && !starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001793 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001794 if (ea.cmd == cmd + 1 && *cmd == '$')
1795 // should be "$VAR = val"
1796 --ea.cmd;
1797 else if (ea.cmd > cmd)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001798 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001799 emsg(_(e_colon_required_before_a_range));
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001800 goto doend;
1801 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001803 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 else
1805#endif
1806 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001807
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001808#ifdef FEAT_EVAL
1809# ifdef FEAT_PROFILE
1810 // Count this line for profiling if skip is TRUE.
1811 if (do_profiling == PROF_YES
1812 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1813 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1814 {
1815 int skip = did_emsg || got_int || did_throw;
1816
1817 if (ea.cmdidx == CMD_catch)
1818 skip = !skip && !(cstack->cs_idx >= 0
1819 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1820 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1821 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1822 skip = skip || !(cstack->cs_idx >= 0
1823 && !(cstack->cs_flags[cstack->cs_idx]
1824 & (CSF_ACTIVE | CSF_TRUE)));
1825 else if (ea.cmdidx == CMD_finally)
1826 skip = FALSE;
1827 else if (ea.cmdidx != CMD_endif
1828 && ea.cmdidx != CMD_endfor
1829 && ea.cmdidx != CMD_endtry
1830 && ea.cmdidx != CMD_endwhile)
1831 skip = ea.skip;
1832
1833 if (!skip)
1834 {
1835 if (getline_equal(fgetline, cookie, get_func_line))
1836 func_line_exec(getline_cookie(fgetline, cookie));
1837 else if (getline_equal(fgetline, cookie, getsourceline))
1838 script_line_exec();
1839 }
1840 }
1841# endif
1842
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001843 // May go to debug mode. If this happens and the ">quit" debug command is
1844 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001845 dbg_check_breakpoint(&ea);
1846 if (!ea.skip && got_int)
1847 {
1848 ea.skip = TRUE;
1849 (void)do_intthrow(cstack);
1850 }
1851#endif
1852
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001853/*
1854 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 *
1856 * where 'addr' is:
1857 *
1858 * % (entire file)
1859 * $ [+-NUM]
1860 * 'x [+-NUM] (where x denotes a currently defined mark)
1861 * . [+-NUM]
1862 * [+-NUM]..
1863 * NUM
1864 *
1865 * The ea.cmd pointer is updated to point to the first character following the
1866 * range spec. If an initial address is found, but no second, the upper bound
1867 * is equal to the lower.
1868 */
1869
Bram Moolenaar25190db2019-05-04 15:05:28 +02001870 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001871 if (!IS_USER_CMDIDX(ea.cmdidx))
1872 {
1873 if (ea.cmdidx != CMD_SIZE)
1874 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1875 else
1876 ea.addr_type = ADDR_LINES;
1877
Bram Moolenaar25190db2019-05-04 15:05:28 +02001878 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001879 if (ea.cmdidx == CMD_wincmd && p != NULL)
1880 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001881#ifdef FEAT_QUICKFIX
1882 // :.cc in quickfix window uses line number
1883 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1884 ea.addr_type = ADDR_OTHER;
1885#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001886 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001887
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001888 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001889#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001890 if (!may_have_range)
1891 ea.line1 = ea.line2 = default_address(&ea);
1892 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001893#endif
1894 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1895 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001898 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 */
1900
1901 /*
1902 * Skip ':' and any white space
1903 */
1904 ea.cmd = skipwhite(ea.cmd);
1905 while (*ea.cmd == ':')
1906 ea.cmd = skipwhite(ea.cmd + 1);
1907
1908 /*
1909 * If we got a line, but no command, then go to the line.
1910 * If we find a '|' or '\n' we set ea.nextcmd.
1911 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001912 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1913 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 {
1915 /*
1916 * strange vi behaviour:
1917 * ":3" jumps to line 3
1918 * ":3|..." prints line 3
1919 * ":|" prints current line
1920 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001921 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001923 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 {
1925 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001926 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 if ((errormsg = invalid_range(&ea)) == NULL)
1928 {
1929 correct_range(&ea);
1930 ex_print(&ea);
1931 }
1932 }
1933 else if (ea.addr_count != 0)
1934 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001935 if (ea.line2 > curbuf->b_ml.ml_line_count)
1936 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001937 // With '-' in 'cpoptions' a line number past the file is an
1938 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001939 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1940 ea.line2 = -1;
1941 else
1942 ea.line2 = curbuf->b_ml.ml_line_count;
1943 }
1944
1945 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001946 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 else
1948 {
1949 if (ea.line2 == 0)
1950 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 else
1952 curwin->w_cursor.lnum = ea.line2;
1953 beginline(BL_SOL | BL_FIX);
1954 }
1955 }
1956 goto doend;
1957 }
1958
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001959 // If this looks like an undefined user command and there are CmdUndefined
1960 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001961 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1962 && ASCII_ISUPPER(*ea.cmd)
1963 && has_cmdundefined())
1964 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001965 int ret;
1966
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001967 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001968 while (ASCII_ISALNUM(*p))
1969 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001970 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001971 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1972 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001973 // If the autocommands did something and didn't cause an error, try
1974 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001975 p = (ret
1976#ifdef FEAT_EVAL
1977 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001978#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001980 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001981
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 if (p == NULL)
1983 {
1984 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001985 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 goto doend;
1987 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001988 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001989 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1990 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 {
1992 errormsg = uc_fun_cmd();
1993 goto doend;
1994 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001995
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 if (ea.cmdidx == CMD_SIZE)
1997 {
1998 if (!ea.skip)
1999 {
2000 STRCPY(IObuff, _("E492: Not an editor command"));
2001 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002002 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002003 // If the modifier was parsed OK the error must be in the
2004 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002005 if (after_modifier != NULL)
2006 append_command(after_modifier);
2007 else
2008 append_command(*cmdlinep);
2009 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002010 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002011 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 }
2013 goto doend;
2014 }
2015
Bram Moolenaar958636c2014-10-21 20:01:58 +02002016 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2017 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002018#ifdef HAVE_EX_SCRIPT_NI
2019 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2020#endif
2021 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022
2023#ifndef FEAT_EVAL
2024 /*
2025 * When the expression evaluation is disabled, recognize the ":if" and
2026 * ":endif" commands and ignore everything in between it.
2027 */
2028 if (ea.cmdidx == CMD_if)
2029 ++if_level;
2030 if (if_level)
2031 {
2032 if (ea.cmdidx == CMD_endif)
2033 --if_level;
2034 goto doend;
2035 }
2036
2037#endif
2038
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002039 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002040 if (*p == '!' && ea.cmdidx != CMD_substitute
2041 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 {
2043 ++p;
2044 ea.forceit = TRUE;
2045 }
2046 else
2047 ea.forceit = FALSE;
2048
2049/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002050 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002052 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002053 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054
2055 if (!ea.skip)
2056 {
2057#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002058 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002060 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002061 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 goto doend;
2063 }
2064#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002065 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002066 {
2067 errormsg = _("E981: Command not allowed in rvim");
2068 goto doend;
2069 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002070 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002072 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002073 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 goto doend;
2075 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002076
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002077 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002079#ifdef FEAT_CMDWIN
2080 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2081 {
2082 // Command not allowed in the command line window
2083 errormsg = _(e_cmdwin);
2084 goto doend;
2085 }
2086#endif
2087 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2088 {
2089 // Command not allowed when text is locked
2090 errormsg = _(get_text_locked_msg());
2091 goto doend;
2092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002094
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002095 // Disallow editing another buffer when "curbuf_lock" is set.
2096 // Do allow ":checktime" (it is postponed).
2097 // Do allow ":edit" (check for an argument later).
2098 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002099 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002100 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002101 && ea.cmdidx != CMD_edit
2102 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002103 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002104 && curbuf_locked())
2105 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002107 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002109 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002110 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 goto doend;
2112 }
2113 }
2114
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002115 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002117 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 goto doend;
2119 }
2120
2121 /*
2122 * Don't complain about the range if it is not used
2123 * (could happen if line_count is accidentally set to 0).
2124 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002125 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 {
2127 /*
2128 * If the range is backwards, ask for confirmation and, if given, swap
2129 * ea.line1 & ea.line2 so it's forwards again.
2130 * When global command is busy, don't ask, will fail below.
2131 */
2132 if (!global_busy && ea.line1 > ea.line2)
2133 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002134 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002136 if (sourcing || exmode_active)
2137 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002138 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002139 goto doend;
2140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 if (ask_yesno((char_u *)
2142 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002143 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 }
2145 lnum = ea.line1;
2146 ea.line1 = ea.line2;
2147 ea.line2 = lnum;
2148 }
2149 if ((errormsg = invalid_range(&ea)) != NULL)
2150 goto doend;
2151 }
2152
Bram Moolenaarb7316892019-05-01 18:08:42 +02002153 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2154 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 ea.line2 = 1;
2156
2157 correct_range(&ea);
2158
2159#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002160 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002161 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002163 // Put the first line at the start of a closed fold, put the last line
2164 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 (void)hasFolding(ea.line1, &ea.line1, NULL);
2166 (void)hasFolding(ea.line2, NULL, &ea.line2);
2167 }
2168#endif
2169
2170#ifdef FEAT_QUICKFIX
2171 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002172 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 * option here, so things like % get expanded.
2174 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002175 p = replace_makeprg(&ea, p, cmdlinep);
2176 if (p == NULL)
2177 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178#endif
2179
2180 /*
2181 * Skip to start of argument.
2182 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2183 */
2184 if (ea.cmdidx == CMD_bang)
2185 ea.arg = p;
2186 else
2187 ea.arg = skipwhite(p);
2188
Bram Moolenaar379fb762018-08-30 15:58:28 +02002189 // ":file" cannot be run with an argument when "curbuf_lock" is set
2190 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2191 goto doend;
2192
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 /*
2194 * Check for "++opt=val" argument.
2195 * Must be first, allow ":w ++enc=utf8 !cmd"
2196 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002197 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2199 if (getargopt(&ea) == FAIL && !ni)
2200 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002201 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 goto doend;
2203 }
2204
2205 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2206 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002207 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002209 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002211 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 goto doend;
2213 }
2214 ea.arg = skipwhite(ea.arg + 1);
2215 ea.append = TRUE;
2216 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002217 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 {
2219 ++ea.arg;
2220 ea.usefilter = TRUE;
2221 }
2222 }
2223
2224 if (ea.cmdidx == CMD_read)
2225 {
2226 if (ea.forceit)
2227 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002228 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 ea.forceit = FALSE;
2230 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002231 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 {
2233 ++ea.arg;
2234 ea.usefilter = TRUE;
2235 }
2236 }
2237
2238 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2239 {
2240 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002241 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 {
2243 ++ea.arg;
2244 ++ea.amount;
2245 }
2246 ea.arg = skipwhite(ea.arg);
2247 }
2248
2249 /*
2250 * Check for "+command" argument, before checking for next command.
2251 * Don't do this for ":read !cmd" and ":write !cmd".
2252 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002253 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2255
2256 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002257 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 * Don't do this for ":read !cmd" and ":write !cmd".
2259 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002260 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 separate_nextcmd(&ea);
2262
2263 /*
2264 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002265 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2266 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002268 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002269 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002270 || ea.cmdidx == CMD_global
2271 || ea.cmdidx == CMD_vglobal
2272 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
2274 for (p = ea.arg; *p; ++p)
2275 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002276 // Remove one backslash before a newline, so that it's possible to
2277 // pass a newline to the shell and also a newline that is preceded
2278 // with a backslash. This makes it impossible to end a shell
2279 // command in a backslash, but that doesn't appear useful.
2280 // Halving the number of backslashes is incompatible with previous
2281 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002283 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 else if (*p == '\n')
2285 {
2286 ea.nextcmd = p + 1;
2287 *p = NUL;
2288 break;
2289 }
2290 }
2291 }
2292
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002293 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002294 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002296 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002297 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002299 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002300 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002301 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002303#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002304 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002305 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002307 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002308 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 }
2310#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002311 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2312 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002313 {
2314 ea.regname = *ea.arg++;
2315#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002316 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002317 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2318 {
2319 set_expr_line(vim_strsave(ea.arg));
2320 ea.arg += STRLEN(ea.arg);
2321 }
2322#endif
2323 ea.arg = skipwhite(ea.arg);
2324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 }
2326
2327 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002328 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 * count, it's a buffer name.
2330 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002331 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2332 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002333 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 {
2335 n = getdigits(&ea.arg);
2336 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002337 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002339 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 goto doend;
2341 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002342 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
2344 ea.line2 = n;
2345 if (ea.addr_count == 0)
2346 ea.addr_count = 1;
2347 }
2348 else
2349 {
2350 ea.line1 = ea.line2;
2351 ea.line2 += n - 1;
2352 ++ea.addr_count;
2353 /*
2354 * Be vi compatible: no error message for out of range.
2355 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002356 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 ea.line2 = curbuf->b_ml.ml_line_count;
2358 }
2359 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002360
2361 /*
2362 * Check for flags: 'l', 'p' and '#'.
2363 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002364 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002365 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002366 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2367 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002369 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002370 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 goto doend;
2372 }
2373
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002374 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002376 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 goto doend;
2378 }
2379
2380#ifdef FEAT_EVAL
2381 /*
2382 * Skip the command when it's not going to be executed.
2383 * The commands like :if, :endif, etc. always need to be executed.
2384 * Also make an exception for commands that handle a trailing command
2385 * themselves.
2386 */
2387 if (ea.skip)
2388 {
2389 switch (ea.cmdidx)
2390 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002391 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 case CMD_while:
2393 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002394 case CMD_for:
2395 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 case CMD_if:
2397 case CMD_elseif:
2398 case CMD_else:
2399 case CMD_endif:
2400 case CMD_try:
2401 case CMD_catch:
2402 case CMD_finally:
2403 case CMD_endtry:
2404 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002405 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 break;
2407
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002408 // Commands that handle '|' themselves. Check: A command should
2409 // either have the EX_TRLBAR flag, appear in this list or appear in
2410 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 case CMD_aboveleft:
2412 case CMD_and:
2413 case CMD_belowright:
2414 case CMD_botright:
2415 case CMD_browse:
2416 case CMD_call:
2417 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002418 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 case CMD_delfunction:
2420 case CMD_djump:
2421 case CMD_dlist:
2422 case CMD_dsearch:
2423 case CMD_dsplit:
2424 case CMD_echo:
2425 case CMD_echoerr:
2426 case CMD_echomsg:
2427 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002428 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002430 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002431 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 case CMD_help:
2433 case CMD_hide:
2434 case CMD_ijump:
2435 case CMD_ilist:
2436 case CMD_isearch:
2437 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002438 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 case CMD_keepjumps:
2440 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002441 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 case CMD_leftabove:
2443 case CMD_let:
2444 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002445 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002446 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002448 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002449 case CMD_noautocmd:
2450 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 case CMD_perl:
2452 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002453 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002454 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002455 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 case CMD_return:
2457 case CMD_rightbelow:
2458 case CMD_ruby:
2459 case CMD_silent:
2460 case CMD_smagic:
2461 case CMD_snomagic:
2462 case CMD_substitute:
2463 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002464 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 case CMD_tcl:
2466 case CMD_throw:
2467 case CMD_tilde:
2468 case CMD_topleft:
2469 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002470 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002471 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 case CMD_verbose:
2473 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002474 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 break;
2476
2477 default: goto doend;
2478 }
2479 }
2480#endif
2481
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002482 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 {
2484 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2485 goto doend;
2486 }
2487
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 /*
2489 * Accept buffer name. Cannot be used at the same time with a buffer
2490 * number. Don't do this for a user command.
2491 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002492 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002493 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 {
2495 /*
2496 * :bdelete, :bwipeout and :bunload take several arguments, separated
2497 * by spaces: find next space (skipping over escaped characters).
2498 * The others take one argument: ignore trailing spaces.
2499 */
2500 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2501 || ea.cmdidx == CMD_bunload)
2502 p = skiptowhite_esc(ea.arg);
2503 else
2504 {
2505 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002506 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 --p;
2508 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002509 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002510 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002511 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 goto doend;
2513 ea.addr_count = 1;
2514 ea.arg = skipwhite(p);
2515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002517 // The :try command saves the emsg_silent flag, reset it here when
2518 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002519 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002520 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002521 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002522 if (emsg_silent < 0)
2523 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002524 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002525 }
2526
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002528 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
Bram Moolenaar958636c2014-10-21 20:01:58 +02002531 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 {
2533 /*
2534 * Execute a user-defined command.
2535 */
2536 do_ucmd(&ea);
2537 }
2538 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 {
2540 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002541 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 */
2543 ea.errmsg = NULL;
2544 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2545 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002546 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 }
2548
2549#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002551 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2552 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002553 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 /*
2556 * If the command just executed called do_cmdline(), any throw or ":return"
2557 * or ":finish" encountered there must also check the cstack of the still
2558 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2559 * exception, or reanimate a returned function or finished script file and
2560 * return or finish it again.
2561 */
2562 if (need_rethrow)
2563 do_throw(cstack);
2564 else if (check_cstack)
2565 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002566 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002568 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 && current_func_returned())
2570 do_return(&ea, TRUE, FALSE, NULL);
2571 }
2572 need_rethrow = check_cstack = FALSE;
2573#endif
2574
2575doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002576 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002577 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002579 curwin->w_cursor.col = 0;
2580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581
2582 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2583 {
2584 if (sourcing)
2585 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002586 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 {
2588 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002589 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002591 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 }
2593 emsg(errormsg);
2594 }
2595#ifdef FEAT_EVAL
2596 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002597 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2598 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599#endif
2600
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002601 undo_cmdmod(save_msg_scroll);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002603 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002605 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 ea.nextcmd = NULL;
2607
2608#ifdef FEAT_EVAL
2609 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002610 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611#endif
2612
2613 return ea.nextcmd;
2614}
2615#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002616 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617#endif
2618
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002619static char ex_error_buf[MSG_BUF_LEN];
2620
2621/*
2622 * Return an error message with argument included.
2623 * Uses a static buffer, only the last error will be kept.
2624 * "msg" will be translated, caller should use N_().
2625 */
2626 char *
2627ex_errmsg(char *msg, char_u *arg)
2628{
2629 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2630 return ex_error_buf;
2631}
2632
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002634 * Parse and skip over command modifiers:
2635 * - update eap->cmd
2636 * - store flags in "cmdmod".
2637 * - Set ex_pressedreturn for an empty command line.
2638 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002639 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002640 * When "skip_only" is TRUE the global variables are not changed, except for
2641 * "cmdmod".
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002642 * Call apply_cmdmod() to get the side effects of the modifiers:
2643 * - Increment "sandbox" for ":sandbox"
2644 * - set p_verbose for ":verbose"
Bram Moolenaareffed932018-08-14 13:38:17 +02002645 * Return FAIL when the command is not to be executed.
2646 * May set "errormsg" to an error message.
2647 */
2648 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002649parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002650{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002651 char_u *p;
2652 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002653
Bram Moolenaara80faa82020-04-12 19:37:17 +02002654 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002655
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002656 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002657 for (;;)
2658 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002659 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002660 {
2661 if (*eap->cmd == ':')
2662 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002663 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002664 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002665
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002666 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002667 if (*eap->cmd == NUL && exmode_active
2668 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2669 || getline_equal(eap->getline, eap->cookie, getexline))
2670 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2671 {
2672 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002673 if (!skip_only)
2674 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002675 }
2676
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002677 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002678 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002679 return FAIL;
2680 if (*eap->cmd == NUL)
2681 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002682 if (!skip_only)
2683 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002684 return FAIL;
2685 }
2686
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002687 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002688 switch (*p)
2689 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002690 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002691 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2692 break;
2693 cmdmod.split |= WSP_ABOVE;
2694 continue;
2695
2696 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2697 {
2698 cmdmod.split |= WSP_BELOW;
2699 continue;
2700 }
2701 if (checkforcmd(&eap->cmd, "browse", 3))
2702 {
2703#ifdef FEAT_BROWSE_CMD
2704 cmdmod.browse = TRUE;
2705#endif
2706 continue;
2707 }
2708 if (!checkforcmd(&eap->cmd, "botright", 2))
2709 break;
2710 cmdmod.split |= WSP_BOT;
2711 continue;
2712
2713 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2714 break;
2715#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2716 cmdmod.confirm = TRUE;
2717#endif
2718 continue;
2719
2720 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2721 {
2722 cmdmod.keepmarks = TRUE;
2723 continue;
2724 }
2725 if (checkforcmd(&eap->cmd, "keepalt", 5))
2726 {
2727 cmdmod.keepalt = TRUE;
2728 continue;
2729 }
2730 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2731 {
2732 cmdmod.keeppatterns = TRUE;
2733 continue;
2734 }
2735 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2736 break;
2737 cmdmod.keepjumps = TRUE;
2738 continue;
2739
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002740 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002741 {
2742 char_u *reg_pat;
2743
2744 if (!checkforcmd(&p, "filter", 4)
2745 || *p == NUL || ends_excmd(*p))
2746 break;
2747 if (*p == '!')
2748 {
2749 cmdmod.filter_force = TRUE;
2750 p = skipwhite(p + 1);
2751 if (*p == NUL || ends_excmd(*p))
2752 break;
2753 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002754#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002755 // Avoid that "filter(arg)" is recognized.
2756 if (in_vim9script() && !VIM_ISWHITE(*p))
2757 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002758#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002759 if (skip_only)
2760 p = skip_vimgrep_pat(p, NULL, NULL);
2761 else
2762 // NOTE: This puts a NUL after the pattern.
2763 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002764 if (p == NULL || *p == NUL)
2765 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002766 if (!skip_only)
2767 {
2768 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002769 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002770 if (cmdmod.filter_regmatch.regprog == NULL)
2771 break;
2772 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002773 eap->cmd = p;
2774 continue;
2775 }
2776
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002777 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002778 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2779 || *p == NUL || ends_excmd(*p))
2780 break;
2781 eap->cmd = p;
2782 cmdmod.hide = TRUE;
2783 continue;
2784
2785 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2786 {
2787 cmdmod.lockmarks = TRUE;
2788 continue;
2789 }
2790
2791 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2792 break;
2793 cmdmod.split |= WSP_ABOVE;
2794 continue;
2795
2796 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2797 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002798 cmdmod.cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002799 continue;
2800 }
2801 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2802 break;
2803 cmdmod.noswapfile = TRUE;
2804 continue;
2805
2806 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2807 break;
2808 cmdmod.split |= WSP_BELOW;
2809 continue;
2810
2811 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2812 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002813 cmdmod.cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02002814 continue;
2815 }
2816 if (!checkforcmd(&eap->cmd, "silent", 3))
2817 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002818 cmdmod.cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002819 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2820 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002821 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002822 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002823 cmdmod.cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002824 }
2825 continue;
2826
2827 case 't': if (checkforcmd(&p, "tab", 3))
2828 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002829 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002830 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002831 long tabnr = get_address(eap, &eap->cmd,
2832 ADDR_TABS, eap->skip,
2833 skip_only, FALSE, 1);
2834 if (tabnr == MAXLNUM)
2835 cmdmod.tab = tabpage_index(curtab) + 1;
2836 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002837 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002838 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2839 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002840 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002841 return FAIL;
2842 }
2843 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002844 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002845 }
2846 eap->cmd = p;
2847 continue;
2848 }
2849 if (!checkforcmd(&eap->cmd, "topleft", 2))
2850 break;
2851 cmdmod.split |= WSP_TOP;
2852 continue;
2853
2854 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2855 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002856 cmdmod.cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002857 continue;
2858
2859 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2860 {
2861 cmdmod.split |= WSP_VERT;
2862 continue;
2863 }
2864 if (!checkforcmd(&p, "verbose", 4))
2865 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002866 if (vim_isdigit(*eap->cmd))
2867 cmdmod.cmod_verbose = atoi((char *)eap->cmd);
2868 else
2869 cmdmod.cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002870 eap->cmd = p;
2871 continue;
2872 }
2873 break;
2874 }
2875
2876 return OK;
2877}
2878
2879/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002880 * Apply the command modifiers. Saves current state in "cmdmod", call
2881 * undo_cmdmod() later.
2882 */
2883 void
2884apply_cmdmod(cmdmod_T *cmod)
2885{
2886#ifdef HAVE_SANDBOX
2887 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
2888 {
2889 ++sandbox;
2890 cmod->cmod_did_sandbox = TRUE;
2891 }
2892#endif
2893 if (cmod->cmod_verbose > 0)
2894 {
2895 if (cmod->cmod_verbose_save == 0)
2896 cmod->cmod_verbose_save = p_verbose + 1;
2897 p_verbose = cmod->cmod_verbose;
2898 }
2899
2900 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
2901 && cmod->cmod_save_msg_silent == 0)
2902 cmod->cmod_save_msg_silent = msg_silent + 1;
2903 if (cmod->cmod_flags & CMOD_SILENT)
2904 ++msg_silent;
2905 if (cmod->cmod_flags & CMOD_UNSILENT)
2906 msg_silent = 0;
2907
2908 if (cmod->cmod_flags & CMOD_ERRSILENT)
2909 {
2910 ++emsg_silent;
2911 ++cmod->cmod_did_esilent;
2912 }
2913
2914 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmdmod.cmod_save_ei == NULL)
2915 {
2916 // Set 'eventignore' to "all".
2917 // First save the existing option value for restoring it later.
2918 cmdmod.cmod_save_ei = vim_strsave(p_ei);
2919 set_string_option_direct((char_u *)"ei", -1,
2920 (char_u *)"all", OPT_FREE, SID_NONE);
2921 }
2922}
2923
2924/*
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002925 * Undo and free contents of "cmdmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002926 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002927 void
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002928undo_cmdmod(int save_msg_scroll)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002929{
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002930 if (cmdmod.cmod_verbose_save > 0)
2931 {
2932 p_verbose = cmdmod.cmod_verbose_save - 1;
2933 cmdmod.cmod_verbose_save = 0;
2934 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002935
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002936#ifdef HAVE_SANDBOX
2937 if (cmdmod.cmod_did_sandbox)
2938 {
2939 --sandbox;
2940 cmdmod.cmod_did_sandbox = FALSE;
2941 }
2942#endif
2943
2944 if (cmdmod.cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002945 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002946 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002947 set_string_option_direct((char_u *)"ei", -1, cmdmod.cmod_save_ei,
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002948 OPT_FREE, SID_NONE);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002949 free_string_option(cmdmod.cmod_save_ei);
2950 cmdmod.cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002951 }
2952
2953 if (cmdmod.filter_regmatch.regprog != NULL)
2954 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002955
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002956 if (cmdmod.cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002957 {
2958 // messages could be enabled for a serious error, need to check if the
2959 // counters don't become negative
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002960 if (!did_emsg || msg_silent > cmdmod.cmod_save_msg_silent - 1)
2961 msg_silent = cmdmod.cmod_save_msg_silent - 1;
2962 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002963 if (emsg_silent < 0)
2964 emsg_silent = 0;
2965 // Restore msg_scroll, it's set by file I/O commands, even when no
2966 // message is actually displayed.
2967 msg_scroll = save_msg_scroll;
2968
2969 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2970 // somewhere in the line. Put it back in the first column.
2971 if (redirecting())
2972 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002973
2974 cmdmod.cmod_save_msg_silent = 0;
2975 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002976 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002977}
2978
2979/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002980 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002981 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002982 * Return FAIL and set "errormsg" or return OK.
2983 */
2984 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002985parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002986{
2987 int address_count = 1;
2988 linenr_T lnum;
2989
2990 // Repeat for all ',' or ';' separated addresses.
2991 for (;;)
2992 {
2993 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002994 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002995 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002996 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002997 eap->addr_count == 0, address_count++);
2998 if (eap->cmd == NULL) // error detected
2999 return FAIL;
3000 if (lnum == MAXLNUM)
3001 {
3002 if (*eap->cmd == '%') // '%' - all lines
3003 {
3004 ++eap->cmd;
3005 switch (eap->addr_type)
3006 {
3007 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003008 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003009 eap->line1 = 1;
3010 eap->line2 = curbuf->b_ml.ml_line_count;
3011 break;
3012 case ADDR_LOADED_BUFFERS:
3013 {
3014 buf_T *buf = firstbuf;
3015
3016 while (buf->b_next != NULL
3017 && buf->b_ml.ml_mfp == NULL)
3018 buf = buf->b_next;
3019 eap->line1 = buf->b_fnum;
3020 buf = lastbuf;
3021 while (buf->b_prev != NULL
3022 && buf->b_ml.ml_mfp == NULL)
3023 buf = buf->b_prev;
3024 eap->line2 = buf->b_fnum;
3025 break;
3026 }
3027 case ADDR_BUFFERS:
3028 eap->line1 = firstbuf->b_fnum;
3029 eap->line2 = lastbuf->b_fnum;
3030 break;
3031 case ADDR_WINDOWS:
3032 case ADDR_TABS:
3033 if (IS_USER_CMDIDX(eap->cmdidx))
3034 {
3035 eap->line1 = 1;
3036 eap->line2 = eap->addr_type == ADDR_WINDOWS
3037 ? LAST_WIN_NR : LAST_TAB_NR;
3038 }
3039 else
3040 {
3041 // there is no Vim command which uses '%' and
3042 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003043 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003044 return FAIL;
3045 }
3046 break;
3047 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003048 case ADDR_UNSIGNED:
3049 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003050 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003051 return FAIL;
3052 case ADDR_ARGUMENTS:
3053 if (ARGCOUNT == 0)
3054 eap->line1 = eap->line2 = 0;
3055 else
3056 {
3057 eap->line1 = 1;
3058 eap->line2 = ARGCOUNT;
3059 }
3060 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003061 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003062#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003063 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003064 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003065 if (eap->line2 == 0)
3066 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003067#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003068 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003069 case ADDR_NONE:
3070 // Will give an error later if a range is found.
3071 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003072 }
3073 ++eap->addr_count;
3074 }
3075 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3076 {
3077 pos_T *fp;
3078
3079 // '*' - visual area
3080 if (eap->addr_type != ADDR_LINES)
3081 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003082 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003083 return FAIL;
3084 }
3085
3086 ++eap->cmd;
3087 if (!eap->skip)
3088 {
3089 fp = getmark('<', FALSE);
3090 if (check_mark(fp) == FAIL)
3091 return FAIL;
3092 eap->line1 = fp->lnum;
3093 fp = getmark('>', FALSE);
3094 if (check_mark(fp) == FAIL)
3095 return FAIL;
3096 eap->line2 = fp->lnum;
3097 ++eap->addr_count;
3098 }
3099 }
3100 }
3101 else
3102 eap->line2 = lnum;
3103 eap->addr_count++;
3104
3105 if (*eap->cmd == ';')
3106 {
3107 if (!eap->skip)
3108 {
3109 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003110 // Don't leave the cursor on an illegal line or column, but do
3111 // accept zero as address, so 0;/PATTERN/ works correctly.
3112 if (eap->line2 > 0)
3113 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003114 }
3115 }
3116 else if (*eap->cmd != ',')
3117 break;
3118 ++eap->cmd;
3119 }
3120
3121 // One address given: set start and end lines.
3122 if (eap->addr_count == 1)
3123 {
3124 eap->line1 = eap->line2;
3125 // ... but only implicit: really no address given
3126 if (lnum == MAXLNUM)
3127 eap->addr_count = 0;
3128 }
3129 return OK;
3130}
3131
3132/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003133 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 * If there is a match advance "pp" to the argument and return TRUE.
3135 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003136 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003137checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003138 char_u **pp, // start of command
3139 char *cmd, // name of command
3140 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141{
3142 int i;
3143
3144 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003145 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 break;
Bram Moolenaar7a66a172020-10-16 19:56:12 +02003147 if (i >= len && !isalpha((*pp)[i]) && (*pp)[i] != '_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 {
3149 *pp = skipwhite(*pp + i);
3150 return TRUE;
3151 }
3152 return FALSE;
3153}
3154
3155/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003156 * Append "cmd" to the error message in IObuff.
3157 * Takes care of limiting the length and handling 0xa0, which would be
3158 * invisible otherwise.
3159 */
3160 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003161append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003162{
3163 char_u *s = cmd;
3164 char_u *d;
3165
3166 STRCAT(IObuff, ": ");
3167 d = IObuff + STRLEN(IObuff);
3168 while (*s != NUL && d - IObuff < IOSIZE - 7)
3169 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003170 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003171 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003172 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003173 STRCPY(d, "<a0>");
3174 d += 4;
3175 }
3176 else
3177 MB_COPY_CHAR(s, d);
3178 }
3179 *d = NUL;
3180}
3181
3182/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003183 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3184 * the name. Otherwise just return "start".
3185 */
3186 char_u *
3187skip_option_env_lead(char_u *start)
3188{
3189 char_u *name = start;
3190
3191 if (*start == '&')
3192 {
3193 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3194 name += 3;
3195 else
3196 name += 1;
3197 }
3198 else if (*start == '$')
3199 name += 1;
3200 return name;
3201}
3202
3203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003205 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003206 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003207 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 *
3209 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3210 * assignment without "let". Sets eap->cmdidx to the command while returning
3211 * "eap->cmd".
3212 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 * Returns NULL for an ambiguous user command.
3214 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 char_u *
3216find_ex_command(
3217 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003218 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003219 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003220 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
3222 int len;
3223 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003224 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226#ifdef FEAT_EVAL
3227 /*
3228 * Recognize a Vim9 script function/method call and assignment:
3229 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3230 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003231 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003232 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003234 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235
Bram Moolenaar83144542020-08-02 20:40:43 +02003236 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003237 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003239 int oplen;
3240 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003242 if (
3243 // "(..." is an expression.
3244 // "funcname(" is always a function call.
3245 *p == '('
3246 || (p == eap->cmd
3247 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003248 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003249 *eap->cmd == '{'
3250 // "'string'->func()" is an expression.
3251 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003252 // '"string"->func()' is an expression.
3253 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003254 // "g:varname" is an expression.
3255 || eap->cmd[1] == ':'
3256 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003257 // "varname->func()" is an expression.
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003258 : (*p == '-' && p[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003259 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003260 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3261 {
3262 // "{" by itself is the start of a block.
3263 eap->cmdidx = CMD_block;
3264 return eap->cmd + 1;
3265 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003266 eap->cmdidx = CMD_eval;
3267 return eap->cmd;
3268 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003269
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003270 if (p != eap->cmd && (
3271 // "varname[]" is an expression.
3272 *p == '['
3273 // "varname.key" is an expression.
3274 || (*p == '.' && ASCII_ISALPHA(p[1]))))
3275 {
3276 char_u *after = p;
3277
3278 // When followed by "=" or "+=" then it is an assignment.
3279 ++emsg_silent;
Bram Moolenaar683581e2020-10-22 21:22:58 +02003280 if (skip_expr(&after, NULL) == OK
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003281 && (*after == '='
3282 || (*after != NUL && after[1] == '=')))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003283 eap->cmdidx = CMD_var;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003284 else
3285 eap->cmdidx = CMD_eval;
3286 --emsg_silent;
3287 return eap->cmd;
3288 }
3289
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003290 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3291 // an assignment.
3292 // If there is no line break inside the "[...]" then "p" is
3293 // advanced to after the "]" by to_name_const_end(): check if a "="
3294 // follows.
3295 // If "[...]" has a line break "p" still points at the "[" and it
3296 // can't be an assignment.
3297 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003298 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003299 p = to_name_const_end(eap->cmd);
3300 if (p == eap->cmd || *skipwhite(p) != '=')
3301 {
3302 eap->cmdidx = CMD_eval;
3303 return eap->cmd;
3304 }
3305 if (p > eap->cmd && *skipwhite(p) == '=')
3306 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003307 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003308 return eap->cmd;
3309 }
3310 }
3311
3312 // Recognize an assignment if we recognize the variable name:
3313 // "g:var = expr"
3314 // "var = expr" where "var" is a local var name.
Bram Moolenaar83144542020-08-02 20:40:43 +02003315 if (*eap->cmd == '@')
3316 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003317 oplen = assignment_len(skipwhite(p), &heredoc);
3318 if (oplen > 0)
3319 {
3320 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3321 || *eap->cmd == '&'
3322 || *eap->cmd == '$'
3323 || *eap->cmd == '@'
3324 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
3325 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003326 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003327 return eap->cmd;
3328 }
3329 }
3330
3331 // Recognize using a type for a w:, b:, t: or g: variable:
3332 // "w:varname: number = 123".
3333 if (eap->cmd[1] == ':' && *p == ':')
3334 {
3335 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003336 return eap->cmd;
3337 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003338 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 }
3340#endif
3341
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 /*
3343 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003344 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 * - the 'k' command can directly be followed by any character.
3346 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003347 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003349 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 */
3351 p = eap->cmd;
3352 if (*p == 'k')
3353 {
3354 eap->cmdidx = CMD_k;
3355 ++p;
3356 }
3357 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003358 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3359 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 || p[1] == 'g'
3361 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3362 || p[1] == 'I'
3363 || (p[1] == 'r' && p[2] != 'e')))
3364 {
3365 eap->cmdidx = CMD_substitute;
3366 ++p;
3367 }
3368 else
3369 {
3370 while (ASCII_ISALPHA(*p))
3371 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003372 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003373 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003375 while (ASCII_ISALNUM(*p))
3376 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377 }
3378 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3379 {
3380 // include "9" for "vim9script"
3381 ++p;
3382 while (ASCII_ISALPHA(*p))
3383 ++p;
3384 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003385
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003386 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003387 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 ++p;
3389 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003390 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3391 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003392 // Check for ":dl", ":dell", etc. to ":deletel": that's
3393 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003394 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003395 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003396 break;
3397 if (i == len - 1)
3398 {
3399 --len;
3400 if (p[-1] == 'l')
3401 eap->flags |= EXFLAG_LIST;
3402 else
3403 eap->flags |= EXFLAG_PRINT;
3404 }
3405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003407 if (ASCII_ISLOWER(eap->cmd[0]))
3408 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003409 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003410 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003411
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003412 if (command_count != (int)CMD_SIZE)
3413 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003414 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003415 getout(1);
3416 }
3417
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003418 // Use a precomputed index for fast look-up in cmdnames[]
3419 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003420 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3421 if (ASCII_ISLOWER(c2))
3422 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3423 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003424 else if (ASCII_ISUPPER(eap->cmd[0]))
3425 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003427 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428
3429 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3430 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3431 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3432 (size_t)len) == 0)
3433 {
3434#ifdef FEAT_EVAL
3435 if (full != NULL
3436 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3437 *full = TRUE;
3438#endif
3439 break;
3440 }
3441
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003442 // Look for a user defined command as a last resort. Let ":Print" be
3443 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003444 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3445 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003447 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 while (ASCII_ISALNUM(*p))
3449 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003450 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003452 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 eap->cmdidx = CMD_SIZE;
3454 }
3455
Bram Moolenaar373863e2020-09-26 17:20:53 +02003456 // ":fina" means ":finally" for backwards compatibility.
3457 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3458 eap->cmdidx = CMD_finally;
3459
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 return p;
3461}
3462
3463#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003464static struct cmdmod
3465{
3466 char *name;
3467 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003468 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003469} cmdmods[] = {
3470 {"aboveleft", 3, FALSE},
3471 {"belowright", 3, FALSE},
3472 {"botright", 2, FALSE},
3473 {"browse", 3, FALSE},
3474 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003475 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003476 {"hide", 3, FALSE},
3477 {"keepalt", 5, FALSE},
3478 {"keepjumps", 5, FALSE},
3479 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003480 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003481 {"leftabove", 5, FALSE},
3482 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003483 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003484 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003485 {"rightbelow", 6, FALSE},
3486 {"sandbox", 3, FALSE},
3487 {"silent", 3, FALSE},
3488 {"tab", 3, TRUE},
3489 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003490 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003491 {"verbose", 4, TRUE},
3492 {"vertical", 4, FALSE},
3493};
3494
3495/*
3496 * Return length of a command modifier (including optional count).
3497 * Return zero when it's not a modifier.
3498 */
3499 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003500modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003501{
3502 int i, j;
3503 char_u *p = cmd;
3504
3505 if (VIM_ISDIGIT(*cmd))
3506 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003507 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003508 {
3509 for (j = 0; p[j] != NUL; ++j)
3510 if (p[j] != cmdmods[i].name[j])
3511 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003512 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003513 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003514 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003515 }
3516 return 0;
3517}
3518
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519/*
3520 * Return > 0 if an Ex command "name" exists.
3521 * Return 2 if there is an exact match.
3522 * Return 3 if there is an ambiguous match.
3523 */
3524 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003525cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526{
3527 exarg_T ea;
3528 int full = FALSE;
3529 int i;
3530 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003531 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003533 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003534 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 {
3536 for (j = 0; name[j] != NUL; ++j)
3537 if (name[j] != cmdmods[i].name[j])
3538 break;
3539 if (name[j] == NUL && j >= cmdmods[i].minlen)
3540 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3541 }
3542
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003543 // Check built-in commands and user defined commands.
3544 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003545 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003548 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003550 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3551 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003552 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003553 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3555}
3556#endif
3557
Bram Moolenaard0190392019-08-23 21:17:35 +02003558 cmdidx_T
3559excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560{
Bram Moolenaard0190392019-08-23 21:17:35 +02003561 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562
Bram Moolenaard0190392019-08-23 21:17:35 +02003563 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3564 idx = (cmdidx_T)((int)idx + 1))
3565 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 break;
3567
Bram Moolenaard0190392019-08-23 21:17:35 +02003568 return idx;
3569}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570
Bram Moolenaard0190392019-08-23 21:17:35 +02003571 long
3572excmd_get_argt(cmdidx_T idx)
3573{
3574 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575}
3576
3577/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003578 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 *
3580 * Backslashed delimiters after / or ? will be skipped, and commands will
3581 * not be expanded between /'s and ?'s or after "'".
3582 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003583 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 * Returns the "cmd" pointer advanced to beyond the range.
3585 */
3586 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003587skip_range(
3588 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003589 int skip_star, // skip "*" used for Visual range
3590 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003592 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003594 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003596 if (*cmd == '\\')
3597 {
3598 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3599 ++cmd;
3600 else
3601 break;
3602 }
3603 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 {
3605 if (*++cmd == NUL && ctx != NULL)
3606 *ctx = EXPAND_NOTHING;
3607 }
3608 else if (*cmd == '/' || *cmd == '?')
3609 {
3610 delim = *cmd++;
3611 while (*cmd != NUL && *cmd != delim)
3612 if (*cmd++ == '\\' && *cmd != NUL)
3613 ++cmd;
3614 if (*cmd == NUL && ctx != NULL)
3615 *ctx = EXPAND_NOTHING;
3616 }
3617 if (*cmd != NUL)
3618 ++cmd;
3619 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003620
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003621 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003622 while (*cmd == ':')
3623 cmd = skipwhite(cmd + 1);
3624
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003625 // Skip "*" used for Visual range.
3626 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3627 cmd = skipwhite(cmd + 1);
3628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 return cmd;
3630}
3631
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003632 static void
3633addr_error(cmd_addr_T addr_type)
3634{
3635 if (addr_type == ADDR_NONE)
3636 emsg(_(e_norange));
3637 else
3638 emsg(_(e_invrange));
3639}
3640
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003642 * Return the default address for an address type.
3643 */
3644 static linenr_T
3645default_address(exarg_T *eap)
3646{
3647 linenr_T lnum = 0;
3648
3649 switch (eap->addr_type)
3650 {
3651 case ADDR_LINES:
3652 case ADDR_OTHER:
3653 // Default is the cursor line number. Avoid using an invalid
3654 // line number though.
3655 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3656 lnum = curbuf->b_ml.ml_line_count;
3657 else
3658 lnum = curwin->w_cursor.lnum;
3659 break;
3660 case ADDR_WINDOWS:
3661 lnum = CURRENT_WIN_NR;
3662 break;
3663 case ADDR_ARGUMENTS:
3664 lnum = curwin->w_arg_idx + 1;
3665 if (lnum > ARGCOUNT)
3666 lnum = ARGCOUNT;
3667 break;
3668 case ADDR_LOADED_BUFFERS:
3669 case ADDR_BUFFERS:
3670 lnum = curbuf->b_fnum;
3671 break;
3672 case ADDR_TABS:
3673 lnum = CURRENT_TAB_NR;
3674 break;
3675 case ADDR_TABS_RELATIVE:
3676 case ADDR_UNSIGNED:
3677 lnum = 1;
3678 break;
3679 case ADDR_QUICKFIX:
3680#ifdef FEAT_QUICKFIX
3681 lnum = qf_get_cur_idx(eap);
3682#endif
3683 break;
3684 case ADDR_QUICKFIX_VALID:
3685#ifdef FEAT_QUICKFIX
3686 lnum = qf_get_cur_valid_idx(eap);
3687#endif
3688 break;
3689 case ADDR_NONE:
3690 // Will give an error later if a range is found.
3691 break;
3692 }
3693 return lnum;
3694}
3695
3696/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003697 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 *
3699 * Set ptr to the next character after the part that was interpreted.
3700 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003701 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 *
3703 * Return MAXLNUM when no Ex address was found.
3704 */
3705 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003706get_address(
3707 exarg_T *eap UNUSED,
3708 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003709 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003710 int skip, // only skip the address, don't use it
3711 int silent, // no errors or side effects
3712 int to_other_file, // flag: may jump to other file
3713 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714{
3715 int c;
3716 int i;
3717 long n;
3718 char_u *cmd;
3719 pos_T pos;
3720 pos_T *fp;
3721 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003722 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723
3724 cmd = skipwhite(*ptr);
3725 lnum = MAXLNUM;
3726 do
3727 {
3728 switch (*cmd)
3729 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003730 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003731 ++cmd;
3732 switch (addr_type)
3733 {
3734 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003735 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 lnum = curwin->w_cursor.lnum;
3737 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003738 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003739 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003740 break;
3741 case ADDR_ARGUMENTS:
3742 lnum = curwin->w_arg_idx + 1;
3743 break;
3744 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003745 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003746 lnum = curbuf->b_fnum;
3747 break;
3748 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003749 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003750 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003751 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003752 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003753 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003754 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003755 cmd = NULL;
3756 goto error;
3757 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003758 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003759#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003760 lnum = qf_get_cur_idx(eap);
3761#endif
3762 break;
3763 case ADDR_QUICKFIX_VALID:
3764#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003765 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003766#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003767 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003768 }
3769 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003771 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003772 ++cmd;
3773 switch (addr_type)
3774 {
3775 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003776 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 lnum = curbuf->b_ml.ml_line_count;
3778 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003779 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003780 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003781 break;
3782 case ADDR_ARGUMENTS:
3783 lnum = ARGCOUNT;
3784 break;
3785 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003786 buf = lastbuf;
3787 while (buf->b_ml.ml_mfp == NULL)
3788 {
3789 if (buf->b_prev == NULL)
3790 break;
3791 buf = buf->b_prev;
3792 }
3793 lnum = buf->b_fnum;
3794 break;
3795 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003796 lnum = lastbuf->b_fnum;
3797 break;
3798 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003799 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003800 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003801 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003802 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003803 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003804 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003805 cmd = NULL;
3806 goto error;
3807 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003808 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003809#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003810 lnum = qf_get_size(eap);
3811 if (lnum == 0)
3812 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003813#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003814 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003815 case ADDR_QUICKFIX_VALID:
3816#ifdef FEAT_QUICKFIX
3817 lnum = qf_get_valid_size(eap);
3818 if (lnum == 0)
3819 lnum = 1;
3820#endif
3821 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003822 }
3823 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003825 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003826 if (*++cmd == NUL)
3827 {
3828 cmd = NULL;
3829 goto error;
3830 }
3831 if (addr_type != ADDR_LINES)
3832 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003833 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003834 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003835 goto error;
3836 }
3837 if (skip)
3838 ++cmd;
3839 else
3840 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003841 // Only accept a mark in another file when it is
3842 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003843 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3844 ++cmd;
3845 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003846 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003847 lnum = curwin->w_cursor.lnum;
3848 else
3849 {
3850 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 {
3852 cmd = NULL;
3853 goto error;
3854 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003855 lnum = fp->lnum;
3856 }
3857 }
3858 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859
3860 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003861 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003862 c = *cmd++;
3863 if (addr_type != ADDR_LINES)
3864 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003865 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003866 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003867 goto error;
3868 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003869 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003870 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003871 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003872 if (*cmd == c)
3873 ++cmd;
3874 }
3875 else
3876 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003877 int flags;
3878
3879 pos = curwin->w_cursor; // save curwin->w_cursor
3880
3881 // When '/' or '?' follows another address, start from
3882 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003883 if (lnum != MAXLNUM)
3884 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003885
3886 // Start a forward search at the end of the line (unless
3887 // before the first line).
3888 // Start a backward search at the start of the line.
3889 // This makes sure we never match in the current
3890 // line, and can match anywhere in the
3891 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003892 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003893 curwin->w_cursor.col = MAXCOL;
3894 else
3895 curwin->w_cursor.col = 0;
3896 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003897 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003898 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003899 {
3900 curwin->w_cursor = pos;
3901 cmd = NULL;
3902 goto error;
3903 }
3904 lnum = curwin->w_cursor.lnum;
3905 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003906 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003907 cmd += searchcmdlen;
3908 }
3909 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003911 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003912 ++cmd;
3913 if (addr_type != ADDR_LINES)
3914 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003915 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003916 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003917 goto error;
3918 }
3919 if (*cmd == '&')
3920 i = RE_SUBST;
3921 else if (*cmd == '?' || *cmd == '/')
3922 i = RE_SEARCH;
3923 else
3924 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003925 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003926 cmd = NULL;
3927 goto error;
3928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003930 if (!skip)
3931 {
3932 /*
3933 * When search follows another address, start from
3934 * there.
3935 */
3936 if (lnum != MAXLNUM)
3937 pos.lnum = lnum;
3938 else
3939 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003941 /*
3942 * Start the search just like for the above
3943 * do_search().
3944 */
3945 if (*cmd != '?')
3946 pos.col = MAXCOL;
3947 else
3948 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003949 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003950 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003951 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003952 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003953 lnum = pos.lnum;
3954 else
3955 {
3956 cmd = NULL;
3957 goto error;
3958 }
3959 }
3960 ++cmd;
3961 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962
3963 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003964 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003965 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 }
3967
3968 for (;;)
3969 {
3970 cmd = skipwhite(cmd);
3971 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3972 break;
3973
3974 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003975 {
3976 switch (addr_type)
3977 {
3978 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003979 case ADDR_OTHER:
3980 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003981 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003982 break;
3983 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003984 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003985 break;
3986 case ADDR_ARGUMENTS:
3987 lnum = curwin->w_arg_idx + 1;
3988 break;
3989 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003990 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003991 lnum = curbuf->b_fnum;
3992 break;
3993 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003994 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003995 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003996 case ADDR_TABS_RELATIVE:
3997 lnum = 1;
3998 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003999 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004000#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004001 lnum = qf_get_cur_idx(eap);
4002#endif
4003 break;
4004 case ADDR_QUICKFIX_VALID:
4005#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004006 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004007#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004008 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004009 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004010 case ADDR_UNSIGNED:
4011 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004012 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004013 }
4014 }
4015
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004017 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 else
4019 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004020 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 n = 1;
4022 else
4023 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004024
4025 if (addr_type == ADDR_TABS_RELATIVE)
4026 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004027 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004028 cmd = NULL;
4029 goto error;
4030 }
4031 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004032 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004033 lnum = compute_buffer_local_count(
4034 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004036 {
4037#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004038 // Relative line addressing, need to adjust for folded lines
4039 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004040 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4041 && address_count >= 2)
4042 (void)hasFolding(lnum, NULL, &lnum);
4043#endif
4044 if (i == '-')
4045 lnum -= n;
4046 else
4047 lnum += n;
4048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
4050 } while (*cmd == '/' || *cmd == '?');
4051
4052error:
4053 *ptr = cmd;
4054 return lnum;
4055}
4056
4057/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004058 * Set eap->line1 and eap->line2 to the whole range.
4059 * Used for commands with the EX_DFLALL flag and no range given.
4060 */
4061 static void
4062address_default_all(exarg_T *eap)
4063{
4064 eap->line1 = 1;
4065 switch (eap->addr_type)
4066 {
4067 case ADDR_LINES:
4068 case ADDR_OTHER:
4069 eap->line2 = curbuf->b_ml.ml_line_count;
4070 break;
4071 case ADDR_LOADED_BUFFERS:
4072 {
4073 buf_T *buf = firstbuf;
4074
4075 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4076 buf = buf->b_next;
4077 eap->line1 = buf->b_fnum;
4078 buf = lastbuf;
4079 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4080 buf = buf->b_prev;
4081 eap->line2 = buf->b_fnum;
4082 }
4083 break;
4084 case ADDR_BUFFERS:
4085 eap->line1 = firstbuf->b_fnum;
4086 eap->line2 = lastbuf->b_fnum;
4087 break;
4088 case ADDR_WINDOWS:
4089 eap->line2 = LAST_WIN_NR;
4090 break;
4091 case ADDR_TABS:
4092 eap->line2 = LAST_TAB_NR;
4093 break;
4094 case ADDR_TABS_RELATIVE:
4095 eap->line2 = 1;
4096 break;
4097 case ADDR_ARGUMENTS:
4098 if (ARGCOUNT == 0)
4099 eap->line1 = eap->line2 = 0;
4100 else
4101 eap->line2 = ARGCOUNT;
4102 break;
4103 case ADDR_QUICKFIX_VALID:
4104#ifdef FEAT_QUICKFIX
4105 eap->line2 = qf_get_valid_size(eap);
4106 if (eap->line2 == 0)
4107 eap->line2 = 1;
4108#endif
4109 break;
4110 case ADDR_NONE:
4111 case ADDR_UNSIGNED:
4112 case ADDR_QUICKFIX:
4113 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4114 break;
4115 }
4116}
4117
4118
4119/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004120 * Get flags from an Ex command argument.
4121 */
4122 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004123get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004124{
4125 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4126 {
4127 if (*eap->arg == 'l')
4128 eap->flags |= EXFLAG_LIST;
4129 else if (*eap->arg == 'p')
4130 eap->flags |= EXFLAG_PRINT;
4131 else
4132 eap->flags |= EXFLAG_NR;
4133 eap->arg = skipwhite(eap->arg + 1);
4134 }
4135}
4136
4137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 * Function called for command which is Not Implemented. NI!
4139 */
4140 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004141ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142{
4143 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004144 eap->errmsg =
4145 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146}
4147
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004148#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149/*
4150 * Function called for script command which is Not Implemented. NI!
4151 * Skips over ":perl <<EOF" constructs.
4152 */
4153 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004154ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155{
4156 if (!eap->skip)
4157 ex_ni(eap);
4158 else
4159 vim_free(script_get(eap, eap->arg));
4160}
4161#endif
4162
4163/*
4164 * Check range in Ex command for validity.
4165 * Return NULL when valid, error message when invalid.
4166 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004167 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004168invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004170 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004171
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 if ( eap->line1 < 0
4173 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004174 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004175 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004176
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004177 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004178 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004179 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004180 {
4181 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004182 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004183#ifdef FEAT_DIFF
4184 + (eap->cmdidx == CMD_diffget)
4185#endif
4186 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004187 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004188 break;
4189 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004190 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004191 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004192 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004193 break;
4194 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004195 // Only a boundary check, not whether the buffers actually
4196 // exist.
4197 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004198 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004199 break;
4200 case ADDR_LOADED_BUFFERS:
4201 buf = firstbuf;
4202 while (buf->b_ml.ml_mfp == NULL)
4203 {
4204 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004205 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004206 buf = buf->b_next;
4207 }
4208 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004209 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004210 buf = lastbuf;
4211 while (buf->b_ml.ml_mfp == NULL)
4212 {
4213 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004214 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004215 buf = buf->b_prev;
4216 }
4217 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004218 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004219 break;
4220 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004221 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004222 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004223 break;
4224 case ADDR_TABS:
4225 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004226 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004227 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004228 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004229 case ADDR_OTHER:
4230 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004231 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004232 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004233#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004234 // No error for value that is too big, will use the last entry.
4235 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004236 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004237#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004238 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004239 case ADDR_QUICKFIX_VALID:
4240#ifdef FEAT_QUICKFIX
4241 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4242 || eap->line2 < 0)
4243 return _(e_invrange);
4244#endif
4245 break;
4246 case ADDR_UNSIGNED:
4247 if (eap->line2 < 0)
4248 return _(e_invrange);
4249 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004250 case ADDR_NONE:
4251 // Will give an error elsewhere.
4252 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004253 }
4254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 return NULL;
4256}
4257
4258/*
4259 * Correct the range for zero line number, if required.
4260 */
4261 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004262correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004264 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 {
4266 if (eap->line1 == 0)
4267 eap->line1 = 1;
4268 if (eap->line2 == 0)
4269 eap->line2 = 1;
4270 }
4271}
4272
Bram Moolenaar748bf032005-02-02 23:04:36 +00004273#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004274/*
4275 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4276 * pattern. Otherwise return eap->arg.
4277 */
4278 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004279skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004280{
4281 char_u *p = eap->arg;
4282
Bram Moolenaara37420f2006-02-04 22:37:47 +00004283 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4284 || eap->cmdidx == CMD_vimgrepadd
4285 || eap->cmdidx == CMD_lvimgrepadd
4286 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004287 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004288 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004289 if (p == NULL)
4290 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004291 }
4292 return p;
4293}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004294
4295/*
4296 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4297 * in the command line, so that things like % get expanded.
4298 */
4299 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004300replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004301{
4302 char_u *new_cmdline;
4303 char_u *program;
4304 char_u *pos;
4305 char_u *ptr;
4306 int len;
4307 int i;
4308
4309 /*
4310 * Don't do it when ":vimgrep" is used for ":grep".
4311 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004312 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4313 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4314 || eap->cmdidx == CMD_grepadd
4315 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004316 && !grep_internal(eap->cmdidx))
4317 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004318 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4319 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004320 {
4321 if (*curbuf->b_p_gp == NUL)
4322 program = p_gp;
4323 else
4324 program = curbuf->b_p_gp;
4325 }
4326 else
4327 {
4328 if (*curbuf->b_p_mp == NUL)
4329 program = p_mp;
4330 else
4331 program = curbuf->b_p_mp;
4332 }
4333
4334 p = skipwhite(p);
4335
4336 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4337 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004338 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004339 i = 1;
4340 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4341 ++i;
4342 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004343 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004344 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004345 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004346 ptr = new_cmdline;
4347 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4348 {
4349 i = (int)(pos - program);
4350 STRNCPY(ptr, program, i);
4351 STRCPY(ptr += i, p);
4352 ptr += len;
4353 program = pos + 2;
4354 }
4355 STRCPY(ptr, program);
4356 }
4357 else
4358 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004359 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004360 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004361 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004362 STRCPY(new_cmdline, program);
4363 STRCAT(new_cmdline, " ");
4364 STRCAT(new_cmdline, p);
4365 }
4366 msg_make(p);
4367
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004368 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004369 vim_free(*cmdlinep);
4370 *cmdlinep = new_cmdline;
4371 p = new_cmdline;
4372 }
4373 return p;
4374}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004375#endif
4376
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377/*
4378 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004379 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 * Return FAIL for failure, OK otherwise.
4381 */
4382 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004383expand_filename(
4384 exarg_T *eap,
4385 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004386 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004388 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 char_u *repl;
4390 int srclen;
4391 char_u *p;
4392 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004393 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394
Bram Moolenaar748bf032005-02-02 23:04:36 +00004395#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004396 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004397 p = skip_grep_pat(eap);
4398#else
4399 p = eap->arg;
4400#endif
4401
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 /*
4403 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4404 * the file name contains a wildcard it should not cause expanding.
4405 * (it will be expanded anyway if there is a wildcard before replacing).
4406 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004407 has_wildcards = mch_has_wildcard(p);
4408 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004410#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004411 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004412 if (p[0] == '`' && p[1] == '=')
4413 {
4414 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004415 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004416 if (*p == '`')
4417 ++p;
4418 continue;
4419 }
4420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 /*
4422 * Quick check if this cannot be the start of a special string.
4423 * Also removes backslash before '%', '#' and '<'.
4424 */
4425 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4426 {
4427 ++p;
4428 continue;
4429 }
4430
4431 /*
4432 * Try to find a match at this position.
4433 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004434 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4435 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004436 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004438 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 {
4440 p += srclen;
4441 continue;
4442 }
4443
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004444 // Wildcards won't be expanded below, the replacement is taken
4445 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004446 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4447 {
4448 char_u *l = repl;
4449
4450 repl = expand_env_save(repl);
4451 vim_free(l);
4452 }
4453
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004454 // Need to escape white space et al. with a backslash.
4455 // Don't do this for:
4456 // - replacement that already has been escaped: "##"
4457 // - shell commands (may have to use quotes instead).
4458 // - non-unix systems when there is a single argument (spaces don't
4459 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004461 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 && eap->cmdidx != CMD_grep
4464 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004465 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004466 && eap->cmdidx != CMD_lgrep
4467 && eap->cmdidx != CMD_lgrepadd
4468 && eap->cmdidx != CMD_lmake
4469 && eap->cmdidx != CMD_make
4470 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004472 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473#endif
4474 )
4475 {
4476 char_u *l;
4477#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004478 // Don't escape a backslash here, because rem_backslash() doesn't
4479 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 static char_u *nobslash = (char_u *)" \t\"|";
4481# define ESCAPE_CHARS nobslash
4482#else
4483# define ESCAPE_CHARS escape_chars
4484#endif
4485
4486 for (l = repl; *l; ++l)
4487 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4488 {
4489 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4490 if (l != NULL)
4491 {
4492 vim_free(repl);
4493 repl = l;
4494 }
4495 break;
4496 }
4497 }
4498
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004499 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004500 if ((eap->usefilter || eap->cmdidx == CMD_bang
4501 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004502 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 {
4504 char_u *l;
4505
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004506 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 if (l != NULL)
4508 {
4509 vim_free(repl);
4510 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 }
4512 }
4513
4514 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4515 vim_free(repl);
4516 if (p == NULL)
4517 return FAIL;
4518 }
4519
4520 /*
4521 * One file argument: Expand wildcards.
4522 * Don't do this with ":r !command" or ":w !command".
4523 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004524 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 {
4526 /*
4527 * May do this twice:
4528 * 1. Replace environment variables.
4529 * 2. Replace any other wildcards, remove backslashes.
4530 */
4531 for (n = 1; n <= 2; ++n)
4532 {
4533 if (n == 2)
4534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 /*
4536 * Halve the number of backslashes (this is Vi compatible).
4537 * For Unix and OS/2, when wildcards are expanded, this is
4538 * done by ExpandOne() below.
4539 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004540#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 if (!has_wildcards)
4542#endif
4543 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 }
4545
4546 if (has_wildcards)
4547 {
4548 if (n == 1)
4549 {
4550 /*
4551 * First loop: May expand environment variables. This
4552 * can be done much faster with expand_env() than with
4553 * something else (e.g., calling a shell).
4554 * After expanding environment variables, check again
4555 * if there are still wildcards present.
4556 */
4557 if (vim_strchr(eap->arg, '$') != NULL
4558 || vim_strchr(eap->arg, '~') != NULL)
4559 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004560 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004561 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 has_wildcards = mch_has_wildcard(NameBuff);
4563 p = NameBuff;
4564 }
4565 else
4566 p = NULL;
4567 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004568 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 {
4570 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004571 int options = WILD_LIST_NOTFOUND
4572 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573
4574 ExpandInit(&xpc);
4575 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004576 if (p_wic)
4577 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004579 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 if (p == NULL)
4581 return FAIL;
4582 }
4583 if (p != NULL)
4584 {
4585 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4586 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004587 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 vim_free(p);
4589 }
4590 }
4591 }
4592 }
4593 return OK;
4594}
4595
4596/*
4597 * Replace part of the command line, keeping eap->cmd, eap->arg and
4598 * eap->nextcmd correct.
4599 * "src" points to the part that is to be replaced, of length "srclen".
4600 * "repl" is the replacement string.
4601 * Returns a pointer to the character after the replaced string.
4602 * Returns NULL for failure.
4603 */
4604 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004605repl_cmdline(
4606 exarg_T *eap,
4607 char_u *src,
4608 int srclen,
4609 char_u *repl,
4610 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611{
4612 int len;
4613 int i;
4614 char_u *new_cmdline;
4615
4616 /*
4617 * The new command line is build in new_cmdline[].
4618 * First allocate it.
4619 * Careful: a "+cmd" argument may have been NUL terminated.
4620 */
4621 len = (int)STRLEN(repl);
4622 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004623 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004624 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004625 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004626 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627
4628 /*
4629 * Copy the stuff before the expanded part.
4630 * Copy the expanded stuff.
4631 * Copy what came after the expanded part.
4632 * Copy the next commands, if there are any.
4633 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004634 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004636
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004638 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004640 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004642 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 {
4644 i = (int)STRLEN(new_cmdline) + 1;
4645 STRCPY(new_cmdline + i, eap->nextcmd);
4646 eap->nextcmd = new_cmdline + i;
4647 }
4648 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4649 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4650 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4651 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4652 vim_free(*cmdlinep);
4653 *cmdlinep = new_cmdline;
4654
4655 return src;
4656}
4657
4658/*
4659 * Check for '|' to separate commands and '"' to start comments.
4660 */
4661 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004662separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663{
4664 char_u *p;
4665
Bram Moolenaar86b68352004-12-27 21:59:20 +00004666#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004667 p = skip_grep_pat(eap);
4668#else
4669 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004670#endif
4671
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004672 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 {
4674 if (*p == Ctrl_V)
4675 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004676 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004677 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004679 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004680 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004681 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 break;
4683 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004684
4685#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004686 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004687 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004688 {
4689 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004690 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004691 }
4692#endif
4693
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004694 // Check for '"': start of comment or '|': next command
4695 // :@" and :*" do not start a comment!
4696 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004697 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004698#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004699 && !in_vim9script()
4700#endif
4701 && !(eap->argt & EX_NOTRLCOM)
4702 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4703 || p != eap->arg)
4704 && (eap->cmdidx != CMD_redir
4705 || p != eap->arg + 1 || p[-1] != '@'))
4706#ifdef FEAT_EVAL
4707 || (*p == '#'
4708 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004709 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004710 && p[1] != '{'
4711 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 || *p == '|' || *p == '\n')
4714 {
4715 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004716 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 * AND 'b' is present in 'cpoptions'.
4718 */
4719 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004720 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004722 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 --p;
4724 }
4725 else
4726 {
4727 eap->nextcmd = check_nextcmd(p);
4728 *p = NUL;
4729 break;
4730 }
4731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004733
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004734 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 del_trailing_spaces(eap->arg);
4736}
4737
4738/*
4739 * get + command from ex argument
4740 */
4741 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004742getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743{
4744 char_u *arg = *argp;
4745 char_u *command = NULL;
4746
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004747 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 {
4749 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004750 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 command = dollar_command;
4752 else
4753 {
4754 command = arg;
4755 arg = skip_cmd_arg(command, TRUE);
4756 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004757 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 }
4759
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004760 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 *argp = arg;
4762 }
4763 return command;
4764}
4765
4766/*
4767 * Find end of "+command" argument. Skip over "\ " and "\\".
4768 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004769 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004770skip_cmd_arg(
4771 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004772 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773{
4774 while (*p && !vim_isspace(*p))
4775 {
4776 if (*p == '\\' && p[1] != NUL)
4777 {
4778 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004779 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 else
4781 ++p;
4782 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004783 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 }
4785 return p;
4786}
4787
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004788 int
4789get_bad_opt(char_u *p, exarg_T *eap)
4790{
4791 if (STRICMP(p, "keep") == 0)
4792 eap->bad_char = BAD_KEEP;
4793 else if (STRICMP(p, "drop") == 0)
4794 eap->bad_char = BAD_DROP;
4795 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4796 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004797 else
4798 return FAIL;
4799 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004800}
4801
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802/*
4803 * Get "++opt=arg" argument.
4804 * Return FAIL or OK.
4805 */
4806 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004807getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808{
4809 char_u *arg = eap->arg + 2;
4810 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004811 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004814 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4816 {
4817 if (*arg == 'n')
4818 {
4819 arg += 2;
4820 eap->force_bin = FORCE_NOBIN;
4821 }
4822 else
4823 eap->force_bin = FORCE_BIN;
4824 if (!checkforcmd(&arg, "binary", 3))
4825 return FAIL;
4826 eap->arg = skipwhite(arg);
4827 return OK;
4828 }
4829
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004830 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004831 if (STRNCMP(arg, "edit", 4) == 0)
4832 {
4833 eap->read_edit = TRUE;
4834 eap->arg = skipwhite(arg + 4);
4835 return OK;
4836 }
4837
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 if (STRNCMP(arg, "ff", 2) == 0)
4839 {
4840 arg += 2;
4841 pp = &eap->force_ff;
4842 }
4843 else if (STRNCMP(arg, "fileformat", 10) == 0)
4844 {
4845 arg += 10;
4846 pp = &eap->force_ff;
4847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 else if (STRNCMP(arg, "enc", 3) == 0)
4849 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004850 if (STRNCMP(arg, "encoding", 8) == 0)
4851 arg += 8;
4852 else
4853 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 pp = &eap->force_enc;
4855 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004856 else if (STRNCMP(arg, "bad", 3) == 0)
4857 {
4858 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004859 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861
4862 if (pp == NULL || *arg != '=')
4863 return FAIL;
4864
4865 ++arg;
4866 *pp = (int)(arg - eap->cmd);
4867 arg = skip_cmd_arg(arg, FALSE);
4868 eap->arg = skipwhite(arg);
4869 *arg = NUL;
4870
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 if (pp == &eap->force_ff)
4872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4874 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004875 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004877 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004879 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4881 *p = TOLOWER_ASC(*p);
4882 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004883 else
4884 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004885 // Check ++bad= argument. Must be a single-byte character, "keep" or
4886 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004887 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004888 return FAIL;
4889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890
4891 return OK;
4892}
4893
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004895ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896{
4897 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004898 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 * directory for security reasons.
4900 */
4901 if (secure)
4902 {
4903 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004904 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 }
4906 else if (eap->cmdidx == CMD_autocmd)
4907 do_autocmd(eap->arg, eap->forceit);
4908 else
4909 do_augroup(eap->arg, eap->forceit);
4910}
4911
4912/*
4913 * ":doautocmd": Apply the automatic commands to the current buffer.
4914 */
4915 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004916ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004918 char_u *arg = eap->arg;
4919 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004920 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004921
Bram Moolenaar1610d052016-06-09 22:53:01 +02004922 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004923 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004924 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004925 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928/*
4929 * :[N]bunload[!] [N] [bufname] unload buffer
4930 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4931 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4932 */
4933 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004934ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004936 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004937 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 eap->errmsg = do_bufdel(
4939 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4940 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4941 : DOBUF_UNLOAD, eap->arg,
4942 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4943}
4944
4945/*
4946 * :[N]buffer [N] to buffer N
4947 * :[N]sbuffer [N] to buffer N
4948 */
4949 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004950ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004952 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004953 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004955 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 else
4957 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004958 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4960 else
4961 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004962 if (eap->do_ecmd_cmd != NULL)
4963 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 }
4965}
4966
4967/*
4968 * :[N]bmodified [N] to next mod. buffer
4969 * :[N]sbmodified [N] to next mod. buffer
4970 */
4971 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004972ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973{
4974 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004975 if (eap->do_ecmd_cmd != NULL)
4976 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977}
4978
4979/*
4980 * :[N]bnext [N] to next buffer
4981 * :[N]sbnext [N] split and to next buffer
4982 */
4983 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004984ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004986 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004987 return;
4988
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004990 if (eap->do_ecmd_cmd != NULL)
4991 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992}
4993
4994/*
4995 * :[N]bNext [N] to previous buffer
4996 * :[N]bprevious [N] to previous buffer
4997 * :[N]sbNext [N] split and to previous buffer
4998 * :[N]sbprevious [N] split and to previous buffer
4999 */
5000 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005001ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005003 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005004 return;
5005
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005007 if (eap->do_ecmd_cmd != NULL)
5008 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009}
5010
5011/*
5012 * :brewind to first buffer
5013 * :bfirst to first buffer
5014 * :sbrewind split and to first buffer
5015 * :sbfirst split and to first buffer
5016 */
5017 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005018ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005020 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005021 return;
5022
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005024 if (eap->do_ecmd_cmd != NULL)
5025 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026}
5027
5028/*
5029 * :blast to last buffer
5030 * :sblast split and to last buffer
5031 */
5032 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005033ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005035 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005036 return;
5037
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005039 if (eap->do_ecmd_cmd != NULL)
5040 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042
Bram Moolenaar7a092242020-04-16 22:10:49 +02005043/*
5044 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02005045 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005048ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005050 int comment_char = '"';
5051
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005052#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005053 if (in_vim9script())
5054 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005055#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005056 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005057}
5058
5059/*
5060 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5061 * to "cmd_start" or has a white space character before it.
5062 */
5063 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005064ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005065{
5066 int c = *cmd;
5067
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005068 if (c == NUL || c == '|' || c == '\n')
5069 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005070#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005071 if (in_vim9script())
5072 return c == '#' && cmd[1] != '{'
5073 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005074#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005075 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076}
5077
5078#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5079 || defined(PROTO)
5080/*
5081 * Return the next command, after the first '|' or '\n'.
5082 * Return NULL if not found.
5083 */
5084 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005085find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086{
5087 while (*p != '|' && *p != '\n')
5088 {
5089 if (*p == NUL)
5090 return NULL;
5091 ++p;
5092 }
5093 return (p + 1);
5094}
5095#endif
5096
5097/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005098 * Check if *p is a separator between Ex commands, skipping over white space.
5099 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 */
5101 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005102check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005104 char_u *s = skipwhite(p);
5105
5106 if (*s == '|' || *s == '\n')
5107 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 else
5109 return NULL;
5110}
5111
5112/*
5113 * - if there are more files to edit
5114 * - and this is the last window
5115 * - and forceit not used
5116 * - and not repeated twice on a row
5117 * return FAIL and give error message if 'message' TRUE
5118 * return OK otherwise
5119 */
5120 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005121check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005122 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005123 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124{
5125 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5126
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005127 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005128 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 {
5130 if (message)
5131 {
5132#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5133 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5134 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005135 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005137 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5138 NGETTEXT("%d more file to edit. Quit anyway?",
5139 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5141 return OK;
5142 return FAIL;
5143 }
5144#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005145 semsg(NGETTEXT("E173: %d more file to edit",
5146 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005147 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 }
5149 return FAIL;
5150 }
5151 return OK;
5152}
5153
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154/*
5155 * Function given to ExpandGeneric() to obtain the list of command names.
5156 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005158get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159{
5160 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 return cmdnames[idx].cmd_name;
5163}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005166ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167{
Bram Moolenaare6850792010-05-14 15:28:44 +02005168 if (*eap->arg == NUL)
5169 {
5170#ifdef FEAT_EVAL
5171 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5172 char_u *p = NULL;
5173
5174 if (expr != NULL)
5175 {
5176 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005177 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005178 --emsg_off;
5179 vim_free(expr);
5180 }
5181 if (p != NULL)
5182 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005183 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005184 vim_free(p);
5185 }
5186 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005187 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005188#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005189 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005190#endif
5191 }
5192 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005193 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005194
5195#ifdef FEAT_VTP
5196 else if (has_vtp_working())
5197 {
5198 // background color change requires clear + redraw
5199 update_screen(CLEAR);
5200 redrawcmd();
5201 }
5202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203}
5204
5205 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005206ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207{
5208 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005209 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 do_highlight(eap->arg, eap->forceit, FALSE);
5211}
5212
5213
5214/*
5215 * Call this function if we thought we were going to exit, but we won't
5216 * (because of an error). May need to restore the terminal mode.
5217 */
5218 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005219not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220{
5221 exiting = FALSE;
5222 settmode(TMODE_RAW);
5223}
5224
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005225 static int
5226before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5227{
5228 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5229
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005230 // Bail out when autocommands closed the window.
5231 // Refuse to quit when the buffer in the last window is being closed (can
5232 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005233 if (!win_valid(wp)
5234 || curbuf_locked()
5235 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5236 return TRUE;
5237
5238 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5239 {
5240 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005241 // Refuse to quit when locked or when the window was closed or the
5242 // buffer in the last window is being closed (can only happen in
5243 // autocommands).
5244 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005245 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5246 return TRUE;
5247 }
5248
5249 return FALSE;
5250}
5251
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005253 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005254 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005255 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005257 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005258ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005260 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005261
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262#ifdef FEAT_CMDWIN
5263 if (cmdwin_type != 0)
5264 {
5265 cmdwin_result = Ctrl_C;
5266 return;
5267 }
5268#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005269 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005270 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005271 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005272 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005273 return;
5274 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005275 if (eap->addr_count > 0)
5276 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005277 int wnr = eap->line2;
5278
5279 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5280 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005281 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005282 }
5283 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005284 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005285
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005286 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005287 if (curbuf_locked())
5288 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005289
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005290 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005291 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005292 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293
5294#ifdef FEAT_NETBEANS_INTG
5295 netbeansForcedQuit = eap->forceit;
5296#endif
5297
5298 /*
5299 * If there are more files or windows we won't exit.
5300 */
5301 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5302 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005303 if ((!buf_hide(wp->w_buffer)
5304 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005305 | (eap->forceit ? CCGD_FORCEIT : 0)
5306 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005308 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 {
5310 not_exiting();
5311 }
5312 else
5313 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005314 // quit last window
5315 // Note: only_one_window() returns true, even so a help window is
5316 // still open. In that case only quit, if no address has been
5317 // specified. Example:
5318 // :h|wincmd w|1q - don't quit
5319 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005320 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005322 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005323#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005325#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005326 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005327 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 }
5329}
5330
5331/*
5332 * ":cquit".
5333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005335ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005337 // this does not always pass on the exit code to the Manx compiler. why?
5338 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339}
5340
5341/*
5342 * ":qall": try to quit all windows
5343 */
5344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005345ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346{
5347# ifdef FEAT_CMDWIN
5348 if (cmdwin_type != 0)
5349 {
5350 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005351 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 else
5353 cmdwin_result = K_XF2;
5354 return;
5355 }
5356# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005357
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005358 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005359 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005360 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005361 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005362 return;
5363 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005364
5365 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005366 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005367
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005369 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 getout(0);
5371 not_exiting();
5372}
5373
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374/*
5375 * ":close": close current window, unless it is the last one
5376 */
5377 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005378ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005380 win_T *win;
5381 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005382#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005384 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005386#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005387 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005388 {
5389 if (eap->addr_count == 0)
5390 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005391 else
5392 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005393 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005394 {
5395 winnr++;
5396 if (winnr == eap->line2)
5397 break;
5398 }
5399 if (win == NULL)
5400 win = lastwin;
5401 ex_win_close(eap->forceit, win, NULL);
5402 }
5403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404}
5405
Bram Moolenaar4033c552017-09-16 20:54:51 +02005406#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005407/*
5408 * ":pclose": Close any preview window.
5409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005411ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005412{
5413 win_T *win;
5414
Bram Moolenaar79648732019-07-18 21:43:07 +02005415 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005416 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005417 if (win->w_p_pvw)
5418 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005419 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005420 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005421 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005422# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005423 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005424 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005425# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005426}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005427#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005428
Bram Moolenaarf740b292006-02-16 22:11:02 +00005429/*
5430 * Close window "win" and take care of handling closing the last window for a
5431 * modified buffer.
5432 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005433 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005434ex_win_close(
5435 int forceit,
5436 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005437 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438{
5439 int need_hide;
5440 buf_T *buf = win->w_buffer;
5441
Bram Moolenaarcf844172020-06-26 19:44:06 +02005442 // Never close the autocommand window.
5443 if (win == aucmd_win)
5444 {
5445 emsg(_(e_autocmd_close));
5446 return;
5447 }
5448
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005450 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005452#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 if ((p_confirm || cmdmod.confirm) && p_write)
5454 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005455 bufref_T bufref;
5456
5457 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005459 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 return;
5461 need_hide = FALSE;
5462 }
5463 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005466 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 return;
5468 }
5469 }
5470
Bram Moolenaar4033c552017-09-16 20:54:51 +02005471#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005473#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005474
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005475 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005476 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005477 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005478 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005479 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480}
5481
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005483 * Handle the argument for a tabpage related ex command.
5484 * Returns a tabpage number.
5485 * When an error is encountered then eap->errmsg is set.
5486 */
5487 static int
5488get_tabpage_arg(exarg_T *eap)
5489{
5490 int tab_number;
5491 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5492
5493 if (eap->arg && *eap->arg != NUL)
5494 {
5495 char_u *p = eap->arg;
5496 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005497 int relative = 0; // argument +N/-N means: go to N places to the
5498 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005499
5500 if (*p == '-')
5501 {
5502 relative = -1;
5503 p++;
5504 }
5505 else if (*p == '+')
5506 {
5507 relative = 1;
5508 p++;
5509 }
5510
5511 p_save = p;
5512 tab_number = getdigits(&p);
5513
5514 if (relative == 0)
5515 {
5516 if (STRCMP(p, "$") == 0)
5517 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005518 else if (STRCMP(p, "#") == 0)
5519 if (valid_tabpage(lastused_tabpage))
5520 tab_number = tabpage_index(lastused_tabpage);
5521 else
5522 {
5523 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5524 tab_number = 0;
5525 goto theend;
5526 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005527 else if (p == p_save || *p_save == '-' || *p != NUL
5528 || tab_number > LAST_TAB_NR)
5529 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005530 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005531 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005532 goto theend;
5533 }
5534 }
5535 else
5536 {
5537 if (*p_save == NUL)
5538 tab_number = 1;
5539 else if (p == p_save || *p_save == '-' || *p != NUL
5540 || tab_number == 0)
5541 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005542 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005543 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005544 goto theend;
5545 }
5546 tab_number = tab_number * relative + tabpage_index(curtab);
5547 if (!unaccept_arg0 && relative == -1)
5548 --tab_number;
5549 }
5550 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005551 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005552 }
5553 else if (eap->addr_count > 0)
5554 {
5555 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005556 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005557 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005558 tab_number = 0;
5559 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005560 else
5561 {
5562 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005563 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005564 {
5565 --tab_number;
5566 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005567 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005568 }
5569 }
5570 }
5571 else
5572 {
5573 switch (eap->cmdidx)
5574 {
5575 case CMD_tabnext:
5576 tab_number = tabpage_index(curtab) + 1;
5577 if (tab_number > LAST_TAB_NR)
5578 tab_number = 1;
5579 break;
5580 case CMD_tabmove:
5581 tab_number = LAST_TAB_NR;
5582 break;
5583 default:
5584 tab_number = tabpage_index(curtab);
5585 }
5586 }
5587
5588theend:
5589 return tab_number;
5590}
5591
5592/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005593 * ":tabclose": close current tab page, unless it is the last one.
5594 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 */
5596 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005597ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005599 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005600 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005601
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005602# ifdef FEAT_CMDWIN
5603 if (cmdwin_type != 0)
5604 cmdwin_result = K_IGNORE;
5605 else
5606# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005607 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005608 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005609 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005611 tab_number = get_tabpage_arg(eap);
5612 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005613 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005614 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005615 if (tp == NULL)
5616 {
5617 beep_flush();
5618 return;
5619 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005620 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005621 {
5622 tabpage_close_other(tp, eap->forceit);
5623 return;
5624 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005625 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005626 tabpage_close(eap->forceit);
5627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005629}
5630
5631/*
5632 * ":tabonly": close all tab pages except the current one
5633 */
5634 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005635ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005636{
5637 tabpage_T *tp;
5638 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005639 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005640
5641# ifdef FEAT_CMDWIN
5642 if (cmdwin_type != 0)
5643 cmdwin_result = K_IGNORE;
5644 else
5645# endif
5646 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005647 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005648 else
5649 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005650 tab_number = get_tabpage_arg(eap);
5651 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005652 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005653 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005654 // Repeat this up to a 1000 times, because autocommands may
5655 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005656 for (done = 0; done < 1000; ++done)
5657 {
5658 FOR_ALL_TABPAGES(tp)
5659 if (tp->tp_topframe != topframe)
5660 {
5661 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005662 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005663 if (valid_tabpage(tp))
5664 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005665 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005666 break;
5667 }
5668 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005669 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005670 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005671 }
5672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674
5675/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005676 * Close the current tab page.
5677 */
5678 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005679tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005680{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005681 // First close all the windows but the current one. If that worked then
5682 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005683 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005684 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005685 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005686 ex_win_close(forceit, curwin, NULL);
5687# ifdef FEAT_GUI
5688 need_mouse_correct = TRUE;
5689# endif
5690}
5691
5692/*
5693 * Close tab page "tp", which is not the current tab page.
5694 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005695 * Also takes care of the tab pages line disappearing when closing the
5696 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005697 */
5698 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005699tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005700{
5701 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005702 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005703 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005704
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005705 // Limit to 1000 windows, autocommands may add a window while we close
5706 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005707 while (++done < 1000)
5708 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005709 wp = tp->tp_firstwin;
5710 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005711
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005712 // Autocommands may delete the tab page under our fingers and we may
5713 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005714 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005715 break;
5716 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005717
Bram Moolenaar29323592016-07-24 22:04:11 +02005718 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005719
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005720 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005721 if (h != tabline_height())
5722 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005723}
5724
5725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 * ":only".
5727 */
5728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005729ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005731 win_T *wp;
5732 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733# ifdef FEAT_GUI
5734 need_mouse_correct = TRUE;
5735# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005736 if (eap->addr_count > 0)
5737 {
5738 wnr = eap->line2;
5739 for (wp = firstwin; --wnr > 0; )
5740 {
5741 if (wp->w_next == NULL)
5742 break;
5743 else
5744 wp = wp->w_next;
5745 }
5746 win_goto(wp);
5747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 close_others(TRUE, eap->forceit);
5749}
5750
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005752ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005754 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005755 if (!eap->skip)
5756 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005757#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005758 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005759#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005760 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005761 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005762 else
5763 {
5764 int winnr = 0;
5765 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005766
Bram Moolenaar2256c992016-11-15 21:17:07 +01005767 FOR_ALL_WINDOWS(win)
5768 {
5769 winnr++;
5770 if (winnr == eap->line2)
5771 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005772 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005773 if (win == NULL)
5774 win = lastwin;
5775 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 }
5778}
5779
5780/*
5781 * ":stop" and ":suspend": Suspend Vim.
5782 */
5783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005784ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785{
5786 /*
5787 * Disallow suspending for "rvim".
5788 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005789 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 {
5791 if (!eap->forceit)
5792 autowrite_all();
5793 windgoto((int)Rows - 1, 0);
5794 out_char('\n');
5795 out_flush();
5796 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005797 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005799 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005801 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802#ifdef FEAT_TITLE
5803 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005804 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805#endif
5806 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005807 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005809 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 }
5811}
5812
5813/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005814 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 */
5816 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005817ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818{
Bram Moolenaar461f2122020-07-28 20:25:47 +02005819#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02005820 if (not_in_vim9(eap) == FAIL)
5821 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02005822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823#ifdef FEAT_CMDWIN
5824 if (cmdwin_type != 0)
5825 {
5826 cmdwin_result = Ctrl_C;
5827 return;
5828 }
5829#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005830 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005831 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005832 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005833 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005834 return;
5835 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005836
5837 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005838 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839
5840 /*
5841 * if more files or windows we won't exit
5842 */
5843 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5844 exiting = TRUE;
5845 if ( ((eap->cmdidx == CMD_wq
5846 || curbufIsChanged())
5847 && do_write(eap) == FAIL)
5848 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005849 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 {
5851 not_exiting();
5852 }
5853 else
5854 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005855 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005857 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858# ifdef FEAT_GUI
5859 need_mouse_correct = TRUE;
5860# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005861 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005862 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 }
5864}
5865
5866/*
5867 * ":print", ":list", ":number".
5868 */
5869 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005870ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005872 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005873 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005874 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005876 for ( ;!got_int; ui_breakcheck())
5877 {
5878 print_line(eap->line1,
5879 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5880 || (eap->flags & EXFLAG_NR)),
5881 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5882 if (++eap->line1 > eap->line2)
5883 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005884 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005885 }
5886 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005887 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005888 curwin->w_cursor.lnum = eap->line2;
5889 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 }
5891
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893}
5894
5895#ifdef FEAT_BYTEOFF
5896 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005897ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898{
5899 goto_byte(eap->line2);
5900}
5901#endif
5902
5903/*
5904 * ":shell".
5905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005907ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908{
5909 do_shell(NULL, 0);
5910}
5911
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005912#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005914static int drop_busy = FALSE;
5915static int drop_filec;
5916static char_u **drop_filev = NULL;
5917static int drop_split;
5918static void (*drop_callback)(void *);
5919static void *drop_cookie;
5920
5921 static void
5922handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923{
5924 exarg_T ea;
5925 int save_msg_scroll = msg_scroll;
5926
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005927 // Setting the argument list may cause screen updates and being called
5928 // recursively. Avoid that by setting drop_busy.
5929 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005931 // Check whether the current buffer is changed. If so, we will need
5932 // to split the current window or data could be lost.
5933 // We don't need to check if the 'hidden' option is set, as in this
5934 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005935 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 {
5937 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005938 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 --emsg_off;
5940 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005941 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 if (win_split(0, 0) == FAIL)
5944 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005945 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005947 // When splitting the window, create a new alist. Otherwise the
5948 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 alist_unlink(curwin->w_alist);
5950 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 }
5952
5953 /*
5954 * Set up the new argument list.
5955 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005956 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957
5958 /*
5959 * Move to the first file.
5960 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005961 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005962 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 ea.cmd = (char_u *)"next";
5964 do_argfile(&ea, 0);
5965
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005966 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5967 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 need_start_insertmode = FALSE;
5969
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005970 // Restore msg_scroll, otherwise a following command may cause scrolling
5971 // unexpectedly. The screen will be redrawn by the caller, thus
5972 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005974
5975 if (drop_callback != NULL)
5976 drop_callback(drop_cookie);
5977
5978 drop_filev = NULL;
5979 drop_busy = FALSE;
5980}
5981
5982/*
5983 * Handle a file drop. The code is here because a drop is *nearly* like an
5984 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005985 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005986 * code is very similar to :args and hence needs access to a lot of the static
5987 * functions in this file.
5988 *
5989 * The "filev" list must have been allocated using alloc(), as should each item
5990 * in the list. This function takes over responsibility for freeing the "filev"
5991 * list.
5992 */
5993 void
5994handle_drop(
5995 int filec, // the number of files dropped
5996 char_u **filev, // the list of files dropped
5997 int split, // force splitting the window
5998 void (*callback)(void *), // to be called after setting the argument
5999 // list
6000 void *cookie) // argument for "callback" (allocated)
6001{
6002 // Cannot handle recursive drops, finish the pending one.
6003 if (drop_busy)
6004 {
6005 FreeWild(filec, filev);
6006 vim_free(cookie);
6007 return;
6008 }
6009
6010 // When calling handle_drop() more than once in a row we only use the last
6011 // one.
6012 if (drop_filev != NULL)
6013 {
6014 FreeWild(drop_filec, drop_filev);
6015 vim_free(drop_cookie);
6016 }
6017
6018 drop_filec = filec;
6019 drop_filev = filev;
6020 drop_split = split;
6021 drop_callback = callback;
6022 drop_cookie = cookie;
6023
6024 // Postpone this when:
6025 // - editing the command line
6026 // - not possible to change the current buffer
6027 // - updating the screen
6028 // As it may change buffers and window structures that are in use and cause
6029 // freed memory to be used.
6030 if (text_locked() || curbuf_locked() || updating_screen)
6031 return;
6032
6033 handle_drop_internal();
6034}
6035
6036/*
6037 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6038 * reset: Handle a postponed drop.
6039 */
6040 void
6041handle_any_postponed_drop(void)
6042{
6043 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006044 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006045 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046}
6047#endif
6048
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 * ":preserve".
6051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006053ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006055 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 ml_preserve(curbuf, TRUE);
6057}
6058
6059/*
6060 * ":recover".
6061 */
6062 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006063ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006065 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006067 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6068 | CCGD_MULTWIN
6069 | (eap->forceit ? CCGD_FORCEIT : 0)
6070 | CCGD_EXCMD)
6071
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 && (*eap->arg == NUL
6073 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006074 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 recoverymode = FALSE;
6076}
6077
6078/*
6079 * Command modifier used in a wrong way.
6080 */
6081 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006082ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006084 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085}
6086
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087/*
6088 * :sview [+command] file split window with new file, read-only
6089 * :split [[+command] file] split window with current or new file
6090 * :vsplit [[+command] file] split window vertically with current or new file
6091 * :new [[+command] file] split window with no or new file
6092 * :vnew [[+command] file] split vertically window with no or new file
6093 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006094 *
6095 * :tabedit open new Tab page with empty window
6096 * :tabedit [+command] file open new Tab page and edit "file"
6097 * :tabnew [[+command] file] just like :tabedit
6098 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 */
6100 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006101ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006103 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006104#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006106#endif
6107#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006108 char_u dot_path[] = ".";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006110#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006111 int use_tab = eap->cmdidx == CMD_tabedit
6112 || eap->cmdidx == CMD_tabfind
6113 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006115 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006116 return;
6117
Bram Moolenaar4033c552017-09-16 20:54:51 +02006118#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121
Bram Moolenaar4033c552017-09-16 20:54:51 +02006122#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006123 // A ":split" in the quickfix window works like ":new". Don't want two
6124 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006125 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 {
6127 if (eap->cmdidx == CMD_split)
6128 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 if (eap->cmdidx == CMD_vsplit)
6130 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133
Bram Moolenaar4033c552017-09-16 20:54:51 +02006134#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006135 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 {
6137 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6138 FNAME_MESS, TRUE, curbuf->b_ffname);
6139 if (fname == NULL)
6140 goto theend;
6141 eap->arg = fname;
6142 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006143# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006144 else
6145# endif
6146#endif
6147#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 && eap->cmdidx != CMD_new)
6151 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006152 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006153# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006154 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006155# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006156 au_has_group((char_u *)"FileExplorer"))
6157 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006158 // No browsing supported but we do have the file explorer:
6159 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006160 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006161 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006162 }
6163 else
6164 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006165 fname = do_browse(0, (char_u *)(use_tab
6166 ? _("Edit File in new tab page")
6167 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006169 if (fname == NULL)
6170 goto theend;
6171 eap->arg = fname;
6172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006174 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006177 /*
6178 * Either open new tab page or split the window.
6179 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006180 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006181 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006182 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6183 : eap->addr_count == 0 ? 0
6184 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006185 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006186 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006187
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006188 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006189 if (curwin != old_curwin
6190 && win_valid(old_curwin)
6191 && old_curwin->w_buffer != curbuf
6192 && !cmdmod.keepalt)
6193 old_curwin->w_alt_fnum = curbuf->b_fnum;
6194 }
6195 }
6196 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6198 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006199 // Reset 'scrollbind' when editing another file, but keep it when
6200 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006202# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006204# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006206 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 else
6208 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 do_exedit(eap, old_curwin);
6210 }
6211
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006212# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006214# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006216# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217theend:
6218 vim_free(fname);
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
6222/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006223 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006224 */
6225 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006226tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006227{
6228 exarg_T ea;
6229
Bram Moolenaara80faa82020-04-12 19:37:17 +02006230 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006231 ea.cmdidx = CMD_tabnew;
6232 ea.cmd = (char_u *)"tabn";
6233 ea.arg = (char_u *)"";
6234 ex_splitview(&ea);
6235}
6236
6237/*
6238 * :tabnext command
6239 */
6240 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006241ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006242{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006243 int tab_number;
6244
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006245 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006246 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006247 switch (eap->cmdidx)
6248 {
6249 case CMD_tabfirst:
6250 case CMD_tabrewind:
6251 goto_tabpage(1);
6252 break;
6253 case CMD_tablast:
6254 goto_tabpage(9999);
6255 break;
6256 case CMD_tabprevious:
6257 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006258 if (eap->arg && *eap->arg != NUL)
6259 {
6260 char_u *p = eap->arg;
6261 char_u *p_save = p;
6262
6263 tab_number = getdigits(&p);
6264 if (p == p_save || *p_save == '-' || *p != NUL
6265 || tab_number == 0)
6266 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006267 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006268 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006269 return;
6270 }
6271 }
6272 else
6273 {
6274 if (eap->addr_count == 0)
6275 tab_number = 1;
6276 else
6277 {
6278 tab_number = eap->line2;
6279 if (tab_number < 1)
6280 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006281 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006282 return;
6283 }
6284 }
6285 }
6286 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006287 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006288 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006289 tab_number = get_tabpage_arg(eap);
6290 if (eap->errmsg == NULL)
6291 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006292 break;
6293 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006294}
6295
6296/*
6297 * :tabmove command
6298 */
6299 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006300ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006301{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006302 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006303
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006304 tab_number = get_tabpage_arg(eap);
6305 if (eap->errmsg == NULL)
6306 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006307}
6308
6309/*
6310 * :tabs command: List tabs and their contents.
6311 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006312 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006313ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006314{
6315 tabpage_T *tp;
6316 win_T *wp;
6317 int tabcount = 1;
6318
6319 msg_start();
6320 msg_scroll = TRUE;
6321 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6322 {
6323 msg_putchar('\n');
6324 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006325 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006326 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006327 ui_breakcheck();
6328
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006329 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006330 wp = firstwin;
6331 else
6332 wp = tp->tp_firstwin;
6333 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6334 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006335 msg_putchar('\n');
6336 msg_putchar(wp == curwin ? '>' : ' ');
6337 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006338 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6339 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006340 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006341 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006342 else
6343 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6344 IObuff, IOSIZE, TRUE);
6345 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006346 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006347 ui_breakcheck();
6348 }
6349 }
6350}
6351
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352/*
6353 * ":mode": Set screen mode.
6354 * If no argument given, just get the screen size and redraw.
6355 */
6356 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006357ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
6359 if (*eap->arg == NUL)
6360 shell_resized();
6361 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006362 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363}
6364
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365/*
6366 * ":resize".
6367 * set, increment or decrement current window height
6368 */
6369 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006370ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371{
6372 int n;
6373 win_T *wp = curwin;
6374
6375 if (eap->addr_count > 0)
6376 {
6377 n = eap->line2;
6378 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6379 ;
6380 }
6381
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006382# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006384# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 if (cmdmod.split & WSP_VERT)
6387 {
6388 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006389 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006390 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 n = 9999;
6392 win_setwidth_win((int)n, wp);
6393 }
6394 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 {
6396 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006397 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006398 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 n = 9999;
6400 win_setheight_win((int)n, wp);
6401 }
6402}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403
6404/*
6405 * ":find [+command] <file>" command.
6406 */
6407 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006408ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409{
6410#ifdef FEAT_SEARCHPATH
6411 char_u *fname;
6412 int count;
6413
6414 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6415 TRUE, curbuf->b_ffname);
6416 if (eap->addr_count > 0)
6417 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006418 // Repeat finding the file "count" times. This matters when it
6419 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 count = eap->line2;
6421 while (fname != NULL && --count > 0)
6422 {
6423 vim_free(fname);
6424 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6425 FALSE, curbuf->b_ffname);
6426 }
6427 }
6428
6429 if (fname != NULL)
6430 {
6431 eap->arg = fname;
6432#endif
6433 do_exedit(eap, NULL);
6434#ifdef FEAT_SEARCHPATH
6435 vim_free(fname);
6436 }
6437#endif
6438}
6439
6440/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006441 * ":open" simulation: for now just work like ":visual".
6442 */
6443 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006444ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006445{
6446 regmatch_T regmatch;
6447 char_u *p;
6448
6449 curwin->w_cursor.lnum = eap->line2;
6450 beginline(BL_SOL | BL_FIX);
6451 if (*eap->arg == '/')
6452 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006453 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006454 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006455 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006456 *p = NUL;
6457 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6458 if (regmatch.regprog != NULL)
6459 {
6460 regmatch.rm_ic = p_ic;
6461 p = ml_get_curline();
6462 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006463 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006464 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006465 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006466 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006467 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006468 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006469 eap->arg += STRLEN(eap->arg);
6470 }
6471 check_cursor();
6472
6473 eap->cmdidx = CMD_visual;
6474 do_exedit(eap, NULL);
6475}
6476
6477/*
6478 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 */
6480 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006481ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482{
6483 do_exedit(eap, NULL);
6484}
6485
6486/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006487 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006490do_exedit(
6491 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006492 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493{
6494 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006496 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006498 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6499 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006500 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 /*
6502 * ":vi" command ends Ex mode.
6503 */
6504 if (exmode_active && (eap->cmdidx == CMD_visual
6505 || eap->cmdidx == CMD_view))
6506 {
6507 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006508 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006510 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006511 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006512 if (global_busy)
6513 {
6514 int rd = RedrawingDisabled;
6515 int nwr = no_wait_return;
6516 int ms = msg_scroll;
6517#ifdef FEAT_GUI
6518 int he = hold_gui_events;
6519#endif
6520
6521 if (eap->nextcmd != NULL)
6522 {
6523 stuffReadbuff(eap->nextcmd);
6524 eap->nextcmd = NULL;
6525 }
6526
6527 if (exmode_was != EXMODE_VIM)
6528 settmode(TMODE_RAW);
6529 RedrawingDisabled = 0;
6530 no_wait_return = 0;
6531 need_wait_return = FALSE;
6532 msg_scroll = 0;
6533#ifdef FEAT_GUI
6534 hold_gui_events = 0;
6535#endif
6536 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006537 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006538
6539 main_loop(FALSE, TRUE);
6540
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006541 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006542 RedrawingDisabled = rd;
6543 no_wait_return = nwr;
6544 msg_scroll = ms;
6545#ifdef FEAT_GUI
6546 hold_gui_events = he;
6547#endif
6548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 }
6552
6553 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006554 || eap->cmdidx == CMD_tabnew
6555 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006556 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006558 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 setpcmark();
6560 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006561 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6562 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006564 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 || *eap->arg != NUL
6566#ifdef FEAT_BROWSE
6567 || cmdmod.browse
6568#endif
6569 )
6570 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006571 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6572 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006573 if (*eap->arg != NUL && curbuf_locked())
6574 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006575
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 n = readonlymode;
6577 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6578 readonlymode = TRUE;
6579 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006580 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6581 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 setpcmark();
6583 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6584 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006585 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6587 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6588 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006589 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006591 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006592 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006594 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006596 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 if (old_curwin != NULL)
6598 {
6599 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006600 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006602#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006603 cleanup_T cs;
6604
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006605 // Reset the error/interrupt/exception state here so that
6606 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006607 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006608#endif
6609#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006611#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006612 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006613
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006614#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006615 // Restore the error/interrupt/exception state if not
6616 // discarded by a new aborting error, interrupt, or
6617 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006618 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 }
6621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 }
6623 else if (readonlymode && curbuf->b_nwindows == 1)
6624 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006625 // When editing an already visited buffer, 'readonly' won't be set
6626 // but the previous value is kept. With ":view" and ":sview" we
6627 // want the file to be readonly, except when another window is
6628 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 curbuf->b_p_ro = TRUE;
6630 }
6631 readonlymode = n;
6632 }
6633 else
6634 {
6635 if (eap->do_ecmd_cmd != NULL)
6636 do_cmdline_cmd(eap->do_ecmd_cmd);
6637#ifdef FEAT_TITLE
6638 n = curwin->w_arg_idx_invalid;
6639#endif
6640 check_arg_idx(curwin);
6641#ifdef FEAT_TITLE
6642 if (n != curwin->w_arg_idx_invalid)
6643 maketitle();
6644#endif
6645 }
6646
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 /*
6648 * if ":split file" worked, set alternate file name in old window to new
6649 * file
6650 */
6651 if (old_curwin != NULL
6652 && *eap->arg != NUL
6653 && curwin != old_curwin
6654 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006655 && old_curwin->w_buffer != curbuf
6656 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658
6659 ex_no_reprint = TRUE;
6660}
6661
6662#ifndef FEAT_GUI
6663/*
6664 * ":gui" and ":gvim" when there is no GUI.
6665 */
6666 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006667ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006669 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670}
6671#endif
6672
Bram Moolenaar4f974752019-02-17 17:44:42 +01006673#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006675ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676{
6677 gui_make_tearoff(eap->arg);
6678}
6679#endif
6680
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006681#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6682 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006684ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006686# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6687 if (gui.in_use)
6688 gui_make_popup(eap->arg, eap->forceit);
6689# ifdef FEAT_TERM_POPUP_MENU
6690 else
6691# endif
6692# endif
6693# ifdef FEAT_TERM_POPUP_MENU
6694 pum_make_popup(eap->arg, eap->forceit);
6695# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696}
6697#endif
6698
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006700ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701{
6702 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006703 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006705 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706}
6707
6708/*
6709 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6710 * offset.
6711 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006714ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006717 win_T *save_curwin = curwin;
6718 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 long topline;
6720 long y;
6721 linenr_T old_linenr = curwin->w_cursor.lnum;
6722
6723 setpcmark();
6724
6725 /*
6726 * determine max topline
6727 */
6728 if (curwin->w_p_scb)
6729 {
6730 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006731 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 {
6733 if (wp->w_p_scb && wp->w_buffer)
6734 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006735 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 if (topline > y)
6737 topline = y;
6738 }
6739 }
6740 if (topline < 1)
6741 topline = 1;
6742 }
6743 else
6744 {
6745 topline = 1;
6746 }
6747
6748
6749 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006750 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006752 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 {
6754 if (curwin->w_p_scb)
6755 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006756 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 y = topline - curwin->w_topline;
6758 if (y > 0)
6759 scrollup(y, TRUE);
6760 else
6761 scrolldown(-y, TRUE);
6762 curwin->w_scbind_pos = topline;
6763 redraw_later(VALID);
6764 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 }
6767 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006768 curwin = save_curwin;
6769 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 if (curwin->w_p_scb)
6771 {
6772 did_syncbind = TRUE;
6773 checkpcmark();
6774 if (old_linenr != curwin->w_cursor.lnum)
6775 {
6776 char_u ctrl_o[2];
6777
6778 ctrl_o[0] = Ctrl_O;
6779 ctrl_o[1] = 0;
6780 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6781 }
6782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783}
6784
6785
6786 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006787ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006789 int i;
6790 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6791 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006793 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 do_bang(1, eap, FALSE, FALSE, TRUE);
6795 else
6796 {
6797 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6798 return;
6799
6800#ifdef FEAT_BROWSE
6801 if (cmdmod.browse)
6802 {
6803 char_u *browseFile;
6804
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006805 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 NULL, NULL, NULL, curbuf);
6807 if (browseFile != NULL)
6808 {
6809 i = readfile(browseFile, NULL,
6810 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6811 vim_free(browseFile);
6812 }
6813 else
6814 i = OK;
6815 }
6816 else
6817#endif
6818 if (*eap->arg == NUL)
6819 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006820 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 return;
6822 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6823 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6824 }
6825 else
6826 {
6827 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6828 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6829 i = readfile(eap->arg, NULL,
6830 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6831
6832 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006833 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006835#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 if (!aborting())
6837#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006838 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 }
6840 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006841 {
6842 if (empty && exmode_active)
6843 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006844 // Delete the empty line that remains. Historically ex does
6845 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006846 if (eap->line2 == 0)
6847 lnum = curbuf->b_ml.ml_line_count;
6848 else
6849 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006850 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006851 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006852 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006853 if (curwin->w_cursor.lnum > 1
6854 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006855 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006856 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006857 }
6858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 }
6862}
6863
Bram Moolenaarea408852005-06-25 22:49:46 +00006864static char_u *prev_dir = NULL;
6865
6866#if defined(EXITFREE) || defined(PROTO)
6867 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006868free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006869{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006870 VIM_CLEAR(prev_dir);
6871 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006872}
6873#endif
6874
Bram Moolenaarf4258302013-06-02 18:20:17 +02006875/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006876 * Get the previous directory for the given chdir scope.
6877 */
6878 static char_u *
6879get_prevdir(cdscope_T scope)
6880{
6881 if (scope == CDSCOPE_WINDOW)
6882 return curwin->w_prevdir;
6883 else if (scope == CDSCOPE_TABPAGE)
6884 return curtab->tp_prevdir;
6885 return prev_dir;
6886}
6887
6888/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006889 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006890 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6891 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006892 */
6893 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006894post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006895{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006896 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006897 // Clear tab local directory for both :cd and :tcd
6898 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006899 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006900 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006901 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006902 char_u *pdir = get_prevdir(scope);
6903
6904 // If still in the global directory, need to remember current
6905 // directory as the global directory.
6906 if (globaldir == NULL && pdir != NULL)
6907 globaldir = vim_strsave(pdir);
6908
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006909 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006910 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006911 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006912 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006913 curtab->tp_localdir = vim_strsave(NameBuff);
6914 else
6915 curwin->w_localdir = vim_strsave(NameBuff);
6916 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006917 }
6918 else
6919 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006920 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006921 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006922 }
6923
6924 shorten_fnames(TRUE);
6925}
6926
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006927/*
6928 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006929 * chdir() function.
6930 * scope == CDSCOPE_WINDOW: changes the window-local directory
6931 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6932 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006933 * Returns TRUE if the directory is successfully changed.
6934 */
6935 int
6936changedir_func(
6937 char_u *new_dir,
6938 int forceit,
6939 cdscope_T scope)
6940{
6941 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006942 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006943 int dir_differs;
6944 int retval = FALSE;
6945
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006946 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006947 return FALSE;
6948
6949 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6950 {
6951 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6952 return FALSE;
6953 }
6954
6955 // ":cd -": Change to previous directory
6956 if (STRCMP(new_dir, "-") == 0)
6957 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006958 pdir = get_prevdir(scope);
6959 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006960 {
6961 emsg(_("E186: No previous directory"));
6962 return FALSE;
6963 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006964 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006965 }
6966
Bram Moolenaar002bc792020-06-05 22:33:42 +02006967 // Free the previous directory
6968 tofree = get_prevdir(scope);
6969
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006970 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006971 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006972 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006973 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006974 pdir = NULL;
6975 if (scope == CDSCOPE_WINDOW)
6976 curwin->w_prevdir = pdir;
6977 else if (scope == CDSCOPE_TABPAGE)
6978 curtab->tp_prevdir = pdir;
6979 else
6980 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006981
6982#if defined(UNIX) || defined(VMS)
6983 // for UNIX ":cd" means: go to home directory
6984 if (*new_dir == NUL)
6985 {
6986 // use NameBuff for home directory name
6987# ifdef VMS
6988 char_u *p;
6989
6990 p = mch_getenv((char_u *)"SYS$LOGIN");
6991 if (p == NULL || *p == NUL) // empty is the same as not set
6992 NameBuff[0] = NUL;
6993 else
6994 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6995# else
6996 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6997# endif
6998 new_dir = NameBuff;
6999 }
7000#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02007001 dir_differs = new_dir == NULL || pdir == NULL
7002 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007003 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7004 emsg(_(e_failed));
7005 else
7006 {
7007 char_u *acmd_fname;
7008
7009 post_chdir(scope);
7010
7011 if (dir_differs)
7012 {
7013 if (scope == CDSCOPE_WINDOW)
7014 acmd_fname = (char_u *)"window";
7015 else if (scope == CDSCOPE_TABPAGE)
7016 acmd_fname = (char_u *)"tabpage";
7017 else
7018 acmd_fname = (char_u *)"global";
7019 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7020 curbuf);
7021 }
7022 retval = TRUE;
7023 }
7024 vim_free(tofree);
7025
7026 return retval;
7027}
Bram Moolenaarea408852005-06-25 22:49:46 +00007028
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007030 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007032 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007033ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007035 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036
7037 new_dir = eap->arg;
7038#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007039 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 if (*new_dir == NUL)
7041 ex_pwd(NULL);
7042 else
7043#endif
7044 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007045 cdscope_T scope = CDSCOPE_GLOBAL;
7046
7047 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7048 scope = CDSCOPE_WINDOW;
7049 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7050 scope = CDSCOPE_TABPAGE;
7051
7052 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007053 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007054 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007055 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 ex_pwd(eap);
7057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 }
7059}
7060
7061/*
7062 * ":pwd".
7063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007065ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066{
7067 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7068 {
7069#ifdef BACKSLASH_IN_FILENAME
7070 slash_adjust(NameBuff);
7071#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007072 if (p_verbose > 0)
7073 {
7074 char *context = "global";
7075
7076 if (curwin->w_localdir != NULL)
7077 context = "window";
7078 else if (curtab->tp_localdir != NULL)
7079 context = "tabpage";
7080 smsg("[%s] %s", context, (char *)NameBuff);
7081 }
7082 else
7083 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 }
7085 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007086 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087}
7088
7089/*
7090 * ":=".
7091 */
7092 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007093ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007095 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007096 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097}
7098
7099 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007100ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007102 int n;
7103 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104
7105 if (cursor_valid())
7106 {
7107 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7108 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007109 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007111
7112 len = eap->line2;
7113 switch (*eap->arg)
7114 {
7115 case 'm': break;
7116 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007117 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007118 }
7119 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120}
7121
7122/*
7123 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7124 */
7125 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007126do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127{
Bram Moolenaar52953192019-08-16 10:27:13 +02007128 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007129 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007130# ifdef ELAPSED_FUNC
7131 elapsed_T start_tv;
7132
7133 // Remember at what time we started, so that we know how much longer we
7134 // should wait after waiting for a bit.
7135 ELAPSED_INIT(start_tv);
7136# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137
7138 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007139 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007140 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007142 wait_now = msec - done > 1000L ? 1000L : msec - done;
7143#ifdef FEAT_TIMERS
7144 {
7145 long due_time = check_due_timer();
7146
7147 if (due_time > 0 && due_time < wait_now)
7148 wait_now = due_time;
7149 }
7150#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007151#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007152 if (has_any_channel() && wait_now > 20L)
7153 wait_now = 20L;
7154#endif
7155#ifdef FEAT_SOUND
7156 if (has_any_sound_callback() && wait_now > 20L)
7157 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007158#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007159 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007160
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007161#ifdef FEAT_JOB_CHANNEL
7162 if (has_any_channel())
7163 ui_breakcheck_force(TRUE);
7164 else
7165#endif
7166 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007167#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007168 // Process the netbeans and clientserver messages that may have been
7169 // received in the call to ui_breakcheck() when the GUI is in use. This
7170 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007171 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007172#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007173
7174# ifdef ELAPSED_FUNC
7175 // actual time passed
7176 done = ELAPSED_FUNC(start_tv);
7177# else
7178 // guestimate time passed (will actually be more)
7179 done += wait_now;
7180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007182
7183 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7184 // input buffer, otherwise a following call to input() fails.
7185 if (got_int)
7186 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187}
7188
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189/*
7190 * ":winsize" command (obsolete).
7191 */
7192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007193ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194{
7195 int w, h;
7196 char_u *arg = eap->arg;
7197 char_u *p;
7198
7199 w = getdigits(&arg);
7200 arg = skipwhite(arg);
7201 p = arg;
7202 h = getdigits(&arg);
7203 if (*p != NUL && *arg == NUL)
7204 set_shellsize(w, h, TRUE);
7205 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007206 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207}
7208
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007210ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211{
7212 int xchar = NUL;
7213 char_u *p;
7214
7215 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7216 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007217 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 if (eap->arg[1] == NUL)
7219 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007220 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 return;
7222 }
7223 xchar = eap->arg[1];
7224 p = eap->arg + 2;
7225 }
7226 else
7227 p = eap->arg + 1;
7228
7229 eap->nextcmd = check_nextcmd(p);
7230 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007231 if (*p != NUL && *p != (
7232#ifdef FEAT_EVAL
7233 in_vim9script() ? '#' :
7234#endif
7235 '"')
7236 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007237 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007238 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007240 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007242 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7244 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007245 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 }
7247}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248
Bram Moolenaar843ee412004-06-30 16:16:41 +00007249#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250/*
7251 * ":winpos".
7252 */
7253 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007254ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255{
7256 int x, y;
7257 char_u *arg = eap->arg;
7258 char_u *p;
7259
7260 if (*arg == NUL)
7261 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007262# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007263# ifdef VIMDLL
7264 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7265 mch_get_winpos(&x, &y) != FAIL)
7266# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007268# else
7269 if (mch_get_winpos(&x, &y) != FAIL)
7270# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 {
7272 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007273 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 }
7275 else
7276# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007277 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 }
7279 else
7280 {
7281 x = getdigits(&arg);
7282 arg = skipwhite(arg);
7283 p = arg;
7284 y = getdigits(&arg);
7285 if (*p == NUL || *arg != NUL)
7286 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007287 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 return;
7289 }
7290# ifdef FEAT_GUI
7291 if (gui.in_use)
7292 gui_mch_set_winpos(x, y);
7293 else if (gui.starting)
7294 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007295 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 gui_win_x = x;
7297 gui_win_y = y;
7298 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007299# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 else
7301# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007302# endif
7303# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007304 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305# endif
7306# ifdef HAVE_TGETENT
7307 if (*T_CWP)
7308 term_set_winpos(x, y);
7309# endif
7310 }
7311}
7312#endif
7313
7314/*
7315 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7316 */
7317 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007318ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319{
7320 oparg_T oa;
7321
7322 clear_oparg(&oa);
7323 oa.regname = eap->regname;
7324 oa.start.lnum = eap->line1;
7325 oa.end.lnum = eap->line2;
7326 oa.line_count = eap->line2 - eap->line1 + 1;
7327 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007329 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 {
7331 setpcmark();
7332 curwin->w_cursor.lnum = eap->line1;
7333 beginline(BL_SOL | BL_FIX);
7334 }
7335
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007336 if (VIsual_active)
7337 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007338
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 switch (eap->cmdidx)
7340 {
7341 case CMD_delete:
7342 oa.op_type = OP_DELETE;
7343 op_delete(&oa);
7344 break;
7345
7346 case CMD_yank:
7347 oa.op_type = OP_YANK;
7348 (void)op_yank(&oa, FALSE, TRUE);
7349 break;
7350
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007351 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007352 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007354 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7355#else
7356 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007358 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 oa.op_type = OP_RSHIFT;
7360 else
7361 oa.op_type = OP_LSHIFT;
7362 op_shift(&oa, FALSE, eap->amount);
7363 break;
7364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007366 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367}
7368
7369/*
7370 * ":put".
7371 */
7372 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007373ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007375 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 if (eap->line2 == 0)
7377 {
7378 eap->line2 = 1;
7379 eap->forceit = TRUE;
7380 }
7381 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007382 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007383 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384}
7385
7386/*
7387 * Handle ":copy" and ":move".
7388 */
7389 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007390ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391{
7392 long n;
7393
Bram Moolenaar491799b2020-08-01 19:23:43 +02007394#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007395 if (not_in_vim9(eap) == FAIL)
7396 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007397#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007398 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007399 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 {
7401 eap->nextcmd = NULL;
7402 return;
7403 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007404 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405
7406 /*
7407 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7408 */
7409 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7410 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007411 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 return;
7413 }
7414
7415 if (eap->cmdidx == CMD_move)
7416 {
7417 if (do_move(eap->line1, eap->line2, n) == FAIL)
7418 return;
7419 }
7420 else
7421 ex_copy(eap->line1, eap->line2, n);
7422 u_clearline();
7423 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007424 ex_may_print(eap);
7425}
7426
7427/*
7428 * Print the current line if flags were given to the Ex command.
7429 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007430 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007431ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007432{
7433 if (eap->flags != 0)
7434 {
7435 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7436 (eap->flags & EXFLAG_LIST));
7437 ex_no_reprint = TRUE;
7438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439}
7440
7441/*
7442 * ":smagic" and ":snomagic".
7443 */
7444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007445ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446{
7447 int magic_save = p_magic;
7448
7449 p_magic = (eap->cmdidx == CMD_smagic);
Bram Moolenaar66e00142020-08-12 21:58:12 +02007450 ex_substitute(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 p_magic = magic_save;
7452}
7453
7454/*
7455 * ":join".
7456 */
7457 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007458ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459{
7460 curwin->w_cursor.lnum = eap->line1;
7461 if (eap->line1 == eap->line2)
7462 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007463 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 return;
7465 if (eap->line2 == curbuf->b_ml.ml_line_count)
7466 {
7467 beep_flush();
7468 return;
7469 }
7470 ++eap->line2;
7471 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007472 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007474 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475}
7476
7477/*
7478 * ":[addr]@r" or ":[addr]*r": execute register
7479 */
7480 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007481ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482{
7483 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007484 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485
7486 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007487 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488
7489#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007490 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491#endif
7492
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007493 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 c = *eap->arg;
7495 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7496 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007497 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007498 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7499 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007500 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 else
7504 {
7505 int save_efr = exec_from_reg;
7506
7507 exec_from_reg = TRUE;
7508
7509 /*
7510 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007511 * Continue until the stuff buffer is empty and all added characters
7512 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007514 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7516
7517 exec_from_reg = save_efr;
7518 }
7519}
7520
7521/*
7522 * ":!".
7523 */
7524 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007525ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526{
7527 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7528}
7529
7530/*
7531 * ":undo".
7532 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007534ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007536 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007537 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007538 else
7539 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540}
7541
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007542#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007544ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007545{
7546 char_u hash[UNDO_HASH_SIZE];
7547
7548 u_compute_hash(hash);
7549 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7550}
7551
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007552 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007553ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007554{
7555 char_u hash[UNDO_HASH_SIZE];
7556
7557 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007558 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007559}
7560#endif
7561
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562/*
7563 * ":redo".
7564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007566ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567{
7568 u_redo(1);
7569}
7570
7571/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007572 * ":earlier" and ":later".
7573 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007574 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007575ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007576{
7577 long count = 0;
7578 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007579 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007580 char_u *p = eap->arg;
7581
7582 if (*p == NUL)
7583 count = 1;
7584 else if (isdigit(*p))
7585 {
7586 count = getdigits(&p);
7587 switch (*p)
7588 {
7589 case 's': ++p; sec = TRUE; break;
7590 case 'm': ++p; sec = TRUE; count *= 60; break;
7591 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007592 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7593 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007594 }
7595 }
7596
7597 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007598 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007599 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007600 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7601 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007602}
7603
7604/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 * ":redir": start/stop redirection.
7606 */
7607 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007608ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609{
7610 char *mode;
7611 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007612 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613
Bram Moolenaarba768492016-07-08 15:32:54 +02007614#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007615 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007616 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007617 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007618 return;
7619 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007620#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007621
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 if (STRICMP(eap->arg, "END") == 0)
7623 close_redir();
7624 else
7625 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007626 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007628 ++arg;
7629 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007631 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 mode = "a";
7633 }
7634 else
7635 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007636 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637
7638 close_redir();
7639
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007640 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007641 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 if (fname == NULL)
7643 return;
7644#ifdef FEAT_BROWSE
7645 if (cmdmod.browse)
7646 {
7647 char_u *browseFile;
7648
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007649 browseFile = do_browse(BROWSE_SAVE,
7650 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007651 fname, NULL, NULL,
7652 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007654 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 vim_free(fname);
7656 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007657 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 }
7659#endif
7660
7661 redir_fd = open_exfile(fname, eap->forceit, mode);
7662 vim_free(fname);
7663 }
7664#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007665 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007667 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007669 ++arg;
7670 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007672 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007673 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007675 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007677 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007678 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007679 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007680 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007682 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007683 if (*arg == '>')
7684 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007685 // Make register empty when not using @A-@Z and the
7686 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007687 if (*arg == NUL && !isupper(redir_reg))
7688 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007690 }
7691 if (*arg != NUL)
7692 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007693 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007694 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007695 }
7696 }
7697 else if (*arg == '=' && arg[1] == '>')
7698 {
7699 int append;
7700
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007701 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007702 close_redir();
7703 arg += 2;
7704
7705 if (*arg == '>')
7706 {
7707 ++arg;
7708 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 }
7710 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007711 append = FALSE;
7712
7713 if (var_redir_start(skipwhite(arg), append) == OK)
7714 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 }
7716#endif
7717
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007718 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007721 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007723
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007724 // Make sure redirection is not off. Can happen for cmdline completion
7725 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007726 if (redir_fd != NULL
7727#ifdef FEAT_EVAL
7728 || redir_reg || redir_vname
7729#endif
7730 )
7731 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732}
7733
7734/*
7735 * ":redraw": force redraw
7736 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007737 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007738ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739{
7740 int r = RedrawingDisabled;
7741 int p = p_lz;
7742
7743 RedrawingDisabled = 0;
7744 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007745 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007747 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748#ifdef FEAT_TITLE
7749 if (need_maketitle)
7750 maketitle();
7751#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007752#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7753# ifdef VIMDLL
7754 if (!gui.in_use)
7755# endif
7756 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 RedrawingDisabled = r;
7759 p_lz = p;
7760
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007761 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 msg_didout = FALSE;
7763 msg_col = 0;
7764
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007765 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007766 need_wait_return = FALSE;
7767
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 out_flush();
7769}
7770
7771/*
7772 * ":redrawstatus": force redraw of status line(s)
7773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007775ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 int r = RedrawingDisabled;
7778 int p = p_lz;
7779
7780 RedrawingDisabled = 0;
7781 p_lz = FALSE;
7782 if (eap->forceit)
7783 status_redraw_all();
7784 else
7785 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007786 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 RedrawingDisabled = r;
7788 p_lz = p;
7789 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790}
7791
Bram Moolenaare12bab32019-01-08 22:02:56 +01007792/*
7793 * ":redrawtabline": force redraw of the tabline
7794 */
7795 static void
7796ex_redrawtabline(exarg_T *eap UNUSED)
7797{
7798 int r = RedrawingDisabled;
7799 int p = p_lz;
7800
7801 RedrawingDisabled = 0;
7802 p_lz = FALSE;
7803
7804 draw_tabline();
7805
7806 RedrawingDisabled = r;
7807 p_lz = p;
7808 out_flush();
7809}
7810
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007812close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813{
7814 if (redir_fd != NULL)
7815 {
7816 fclose(redir_fd);
7817 redir_fd = NULL;
7818 }
7819#ifdef FEAT_EVAL
7820 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007821 if (redir_vname)
7822 {
7823 var_redir_stop();
7824 redir_vname = 0;
7825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826#endif
7827}
7828
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007829#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007830 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007831vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007832{
7833 if (vim_mkdir(name, prot) != 0)
7834 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007835 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007836 return FAIL;
7837 }
7838 return OK;
7839}
7840#endif
7841
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842/*
7843 * Open a file for writing for an Ex command, with some checks.
7844 * Return file descriptor, or NULL on failure.
7845 */
7846 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007847open_exfile(
7848 char_u *fname,
7849 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007850 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851{
7852 FILE *fd;
7853
7854#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007855 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 if (mch_isdir(fname))
7857 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007858 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 return NULL;
7860 }
7861#endif
7862 if (!forceit && *mode != 'a' && vim_fexists(fname))
7863 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007864 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 return NULL;
7866 }
7867
7868 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007869 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870
7871 return fd;
7872}
7873
7874/*
7875 * ":mark" and ":k".
7876 */
7877 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007878ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879{
7880 pos_T pos;
7881
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007882 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007883 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007884 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02007885 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 else
7887 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007888 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 curwin->w_cursor.lnum = eap->line2;
7890 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007891 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007892 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007893 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 }
7895}
7896
7897/*
7898 * Update w_topline, w_leftcol and the cursor position.
7899 */
7900 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007901update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007903 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 update_topline();
7905 if (!curwin->w_p_wrap)
7906 validate_cursor();
7907 update_curswant();
7908}
7909
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007911 * Save the current State and go to Normal mode.
7912 * Return TRUE if the typeahead could be saved.
7913 */
7914 int
7915save_current_state(save_state_T *sst)
7916{
7917 sst->save_msg_scroll = msg_scroll;
7918 sst->save_restart_edit = restart_edit;
7919 sst->save_msg_didout = msg_didout;
7920 sst->save_State = State;
7921 sst->save_insertmode = p_im;
7922 sst->save_finish_op = finish_op;
7923 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007924 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007925
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007926 msg_scroll = FALSE; // no msg scrolling in Normal mode
7927 restart_edit = 0; // don't go to Insert mode
7928 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007929
7930 /*
7931 * Save the current typeahead. This is required to allow using ":normal"
7932 * from an event handler and makes sure we don't hang when the argument
7933 * ends with half a command.
7934 */
7935 save_typeahead(&sst->tabuf);
7936 return sst->tabuf.typebuf_valid;
7937}
7938
7939 void
7940restore_current_state(save_state_T *sst)
7941{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007942 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007943 restore_typeahead(&sst->tabuf);
7944
7945 msg_scroll = sst->save_msg_scroll;
7946 restart_edit = sst->save_restart_edit;
7947 p_im = sst->save_insertmode;
7948 finish_op = sst->save_finish_op;
7949 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007950 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007951 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007952
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007953 // Restore the state (needed when called from a function executed for
7954 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007955 State = sst->save_State;
7956#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007957 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007958#endif
7959}
7960
7961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 * ":normal[!] {commands}": Execute normal mode commands.
7963 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007964 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007965ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007967 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 char_u *arg = NULL;
7969 int l;
7970 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007972 if (ex_normal_lock > 0)
7973 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007974 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007975 return;
7976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 if (ex_normal_busy >= p_mmd)
7978 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007979 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 return;
7981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 /*
7984 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7985 * this for the K_SPECIAL leading byte, otherwise special keys will not
7986 * work.
7987 */
7988 if (has_mbyte)
7989 {
7990 int len = 0;
7991
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007992 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 for (p = eap->arg; *p != NUL; ++p)
7994 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007995#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007996 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007998#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007999 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008000 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008001#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 )
8005 len += 2;
8006 }
8007 if (len > 0)
8008 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008009 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 if (arg != NULL)
8011 {
8012 len = 0;
8013 for (p = eap->arg; *p != NUL; ++p)
8014 {
8015 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008016#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 if (*p == CSI)
8018 {
8019 arg[len++] = KS_EXTRA;
8020 arg[len++] = (int)KE_CSI;
8021 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008022#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008023 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 {
8025 arg[len++] = *++p;
8026 if (*p == K_SPECIAL)
8027 {
8028 arg[len++] = KS_SPECIAL;
8029 arg[len++] = KE_FILLER;
8030 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008031#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 else if (*p == CSI)
8033 {
8034 arg[len++] = KS_EXTRA;
8035 arg[len++] = (int)KE_CSI;
8036 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 }
8039 arg[len] = NUL;
8040 }
8041 }
8042 }
8043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008045 ++ex_normal_busy;
8046 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 {
8048 /*
8049 * Repeat the :normal command for each line in the range. When no
8050 * range given, execute it just once, without positioning the cursor
8051 * first.
8052 */
8053 do
8054 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 if (eap->addr_count != 0)
8056 {
8057 curwin->w_cursor.lnum = eap->line1++;
8058 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008059 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 }
8061
Bram Moolenaar13505972019-01-24 15:04:48 +01008062 exec_normal_cmd(arg != NULL
8063 ? arg
8064 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 }
8066 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8067 }
8068
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008069 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 update_topline_cursor();
8071
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008072 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008074 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008075#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008076 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008077#endif
8078
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080}
8081
8082/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008083 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 */
8085 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008086ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008088 if (eap->forceit)
8089 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008090 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008091 if (!curwin->w_cursor.lnum)
8092 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008093 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008094 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008095#ifdef FEAT_TERMINAL
8096 // Ignore this when running in an active terminal.
8097 if (term_job_running(curbuf->b_term))
8098 return;
8099#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008100
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008101 // Ignore the command when already in Insert mode. Inserting an
8102 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008103 if (State & INSERT)
8104 return;
8105
Bram Moolenaar64969662005-12-14 21:59:55 +00008106 if (eap->cmdidx == CMD_startinsert)
8107 restart_edit = 'a';
8108 else if (eap->cmdidx == CMD_startreplace)
8109 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008111 restart_edit = 'V';
8112
8113 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008115 if (eap->cmdidx == CMD_startinsert)
8116 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008117 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 }
8119}
8120
8121/*
8122 * ":stopinsert"
8123 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008125ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126{
8127 restart_edit = 0;
8128 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008129 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008132/*
8133 * Execute normal mode command "cmd".
8134 * "remap" can be REMAP_NONE or REMAP_YES.
8135 */
8136 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008137exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008138{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008139 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008140 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008141 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008142}
Bram Moolenaar25281632016-01-21 23:32:32 +01008143
Bram Moolenaar25281632016-01-21 23:32:32 +01008144/*
8145 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008146 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008147 */
8148 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008149exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008150{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008151 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008152 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008153
Bram Moolenaar905dd902019-04-07 14:21:47 +02008154 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8155 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008156 clear_oparg(&oa);
8157 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008158 while ((!stuff_empty()
8159 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008160 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008161 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008162 {
8163 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008164#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008165 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008166 && oa.op_type == OP_NOP && oa.regname == NUL
8167 && !VIsual_active)
8168 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008169 // If terminal_loop() returns OK we got a key that is handled
8170 // in Normal model. With FAIL we first need to position the
8171 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008172 if (terminal_loop(TRUE) == OK)
8173 normal_cmd(&oa, TRUE);
8174 }
8175 else
8176#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008177 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008178 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008179 }
8180}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008181
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182#ifdef FEAT_FIND_ID
8183 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008184ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185{
8186 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8187 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8188 (linenr_T)1, (linenr_T)MAXLNUM);
8189}
8190
Bram Moolenaar4033c552017-09-16 20:54:51 +02008191#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192/*
8193 * ":psearch"
8194 */
8195 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008196ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197{
8198 g_do_tagpreview = p_pvh;
8199 ex_findpat(eap);
8200 g_do_tagpreview = 0;
8201}
8202#endif
8203
8204 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008205ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206{
8207 int whole = TRUE;
8208 long n;
8209 char_u *p;
8210 int action;
8211
8212 switch (cmdnames[eap->cmdidx].cmd_name[2])
8213 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008214 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8216 action = ACTION_GOTO;
8217 else
8218 action = ACTION_SHOW;
8219 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008220 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 action = ACTION_SHOW_ALL;
8222 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008223 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 action = ACTION_GOTO;
8225 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008226 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 action = ACTION_SPLIT;
8228 break;
8229 }
8230
8231 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008232 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 {
8234 n = getdigits(&eap->arg);
8235 eap->arg = skipwhite(eap->arg);
8236 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008237 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 {
8239 whole = FALSE;
8240 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008241 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 if (*p)
8243 {
8244 *p++ = NUL;
8245 p = skipwhite(p);
8246
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008247 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008248 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008249 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 else
8251 eap->nextcmd = check_nextcmd(p);
8252 }
8253 }
8254 if (!eap->skip)
8255 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8256 whole, !eap->forceit,
8257 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8258 n, action, eap->line1, eap->line2);
8259}
8260#endif
8261
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262
Bram Moolenaar4033c552017-09-16 20:54:51 +02008263#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264/*
8265 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8266 */
8267 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008268ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008270 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8272}
8273
8274/*
8275 * ":pedit"
8276 */
8277 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008278ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279{
8280 win_T *curwin_save = curwin;
8281
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008282 if (ERROR_IF_ANY_POPUP_WINDOW)
8283 return;
8284
Bram Moolenaar026587b2019-08-17 15:08:00 +02008285 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008287 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008288
Bram Moolenaar026587b2019-08-17 15:08:00 +02008289 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008291
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 if (curwin != curwin_save && win_valid(curwin_save))
8293 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008294 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 validate_cursor();
8296 redraw_later(VALID);
8297 win_enter(curwin_save, TRUE);
8298 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008299# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008300 else if (WIN_IS_POPUP(curwin))
8301 {
8302 // can't keep focus in popup window
8303 win_enter(firstwin, TRUE);
8304 }
8305# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 g_do_tagpreview = 0;
8307}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309
8310/*
8311 * ":stag", ":stselect" and ":stjump".
8312 */
8313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008314ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315{
8316 postponed_split = -1;
8317 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008318 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8320 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008321 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323
8324/*
8325 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8326 */
8327 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008328ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329{
8330 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8331}
8332
8333 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008334ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335{
8336 int cmd;
8337
8338 switch (name[1])
8339 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008340 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008342 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008344 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008346 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008348 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008350 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008352 case 'f': // ":tfirst"
8353 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008355 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008357 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008359 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008361 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 return;
8363 }
8364#endif
8365 cmd = DT_TAG;
8366 break;
8367 }
8368
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008369 if (name[0] == 'l')
8370 {
8371#ifndef FEAT_QUICKFIX
8372 ex_ni(eap);
8373 return;
8374#else
8375 cmd = DT_LTAG;
8376#endif
8377 }
8378
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8380 eap->forceit, TRUE);
8381}
8382
8383/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008384 * Check "str" for starting with a special cmdline variable.
8385 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8386 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8387 */
8388 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008389find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008390{
8391 int len;
8392 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008393 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008394 "%",
8395#define SPEC_PERC 0
8396 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008397#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008398 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008399#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008400 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008401#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008402 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008403#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008404 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008405#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008406 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008407#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008408 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008409#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008410 "<stack>", // call stack
8411#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008412 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008413#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008414 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008415#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008416 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008417#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008418 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008419#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008420 "<SID>", // script ID: <SNR>123_
8421#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008422#ifdef FEAT_CLIENTSERVER
8423 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008424# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008425#endif
8426 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008427
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008428 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008429 {
8430 len = (int)STRLEN(spec_str[i]);
8431 if (STRNCMP(src, spec_str[i], len) == 0)
8432 {
8433 *usedlen = len;
8434 return i;
8435 }
8436 }
8437 return -1;
8438}
8439
8440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 * Evaluate cmdline variables.
8442 *
8443 * change '%' to curbuf->b_ffname
8444 * '#' to curwin->w_altfile
8445 * '<cword>' to word under the cursor
8446 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008447 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 * '<cfile>' to path name under the cursor
8449 * '<sfile>' to sourced file name
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008450 * '<stack>' to call stack
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008451 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 * '<afile>' to file name for autocommand
8453 * '<abuf>' to buffer number for autocommand
8454 * '<amatch>' to matching name for autocommand
8455 *
8456 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8457 * "" for error without a message) and NULL is returned.
8458 * Returns an allocated string if a valid match was found.
8459 * Returns NULL if no match was found. "usedlen" then still contains the
8460 * number of characters to skip.
8461 */
8462 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008463eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008464 char_u *src, // pointer into commandline
8465 char_u *srcstart, // beginning of valid memory for src
8466 int *usedlen, // characters after src that are used
8467 linenr_T *lnump, // line number for :e command, or NULL
8468 char **errormsg, // pointer to error message
8469 int *escaped) // return value has escaped white space (can
8470 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471{
8472 int i;
8473 char_u *s;
8474 char_u *result;
8475 char_u *resultbuf = NULL;
8476 int resultlen;
8477 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008478 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008480 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483
8484 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008485 if (escaped != NULL)
8486 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487
8488 /*
8489 * Check if there is something to do.
8490 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008491 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008492 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 {
8494 *usedlen = 1;
8495 return NULL;
8496 }
8497
8498 /*
8499 * Skip when preceded with a backslash "\%" and "\#".
8500 * Note: In "\\%" the % is also not recognized!
8501 */
8502 if (src > srcstart && src[-1] == '\\')
8503 {
8504 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008505 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 return NULL;
8507 }
8508
8509 /*
8510 * word or WORD under cursor
8511 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008512 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8513 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008515 resultlen = find_ident_under_cursor(&result,
8516 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8517 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8518 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 if (resultlen == 0)
8520 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008521 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 return NULL;
8523 }
8524 }
8525
8526 /*
8527 * '#': Alternate file name
8528 * '%': Current file name
8529 * File name under the cursor
8530 * File name for autocommand
8531 * and following modifiers
8532 */
8533 else
8534 {
8535 switch (spec_idx)
8536 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008537 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 if (curbuf->b_fname == NULL)
8539 {
8540 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008541 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 }
8543 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008544 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008546 tilde_file = STRCMP(result, "~") == 0;
8547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 break;
8549
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008550 case SPEC_HASH: // '#' or "#99": alternate file
8551 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 {
8553 result = arg_all();
8554 resultbuf = result;
8555 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008556 if (escaped != NULL)
8557 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 break;
8560 }
8561 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008562 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008563 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008565 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008566 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008567 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008568 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008570 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008572 if (*usedlen < 2)
8573 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008574 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008575 *usedlen = 1;
8576 return NULL;
8577 }
8578#ifdef FEAT_EVAL
8579 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8580 (long)i);
8581 if (result == NULL)
8582 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008583 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008584 return NULL;
8585 }
8586#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008587 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 }
8591 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008592 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008593 if (i == 0 && src[1] == '<' && *usedlen > 1)
8594 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008595 buf = buflist_findnr(i);
8596 if (buf == NULL)
8597 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008598 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008599 return NULL;
8600 }
8601 if (lnump != NULL)
8602 *lnump = ECMD_LAST;
8603 if (buf->b_fname == NULL)
8604 {
8605 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008606 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008607 }
8608 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008609 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008610 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008611 tilde_file = STRCMP(result, "~") == 0;
8612 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 break;
8615
8616#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008617 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008618 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 if (result == NULL)
8620 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008621 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 return NULL;
8623 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008624 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 break;
8626#endif
8627
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008628 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008630 if (result != NULL && !autocmd_fname_full)
8631 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008632 // Still need to turn the fname into a full path. It is
8633 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008634 autocmd_fname_full = TRUE;
8635 result = FullName_save(autocmd_fname, FALSE);
8636 vim_free(autocmd_fname);
8637 autocmd_fname = result;
8638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 if (result == NULL)
8640 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008641 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 return NULL;
8643 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008644 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 break;
8646
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008647 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 if (autocmd_bufnr <= 0)
8649 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008650 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 return NULL;
8652 }
8653 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8654 result = strbuf;
8655 break;
8656
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008657 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 result = autocmd_match;
8659 if (result == NULL)
8660 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008661 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 return NULL;
8663 }
8664 break;
8665
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008666 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008667 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008668 result = estack_sfile(spec_idx == SPEC_SFILE
8669 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 if (result == NULL)
8671 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008672 *errormsg = spec_idx == SPEC_SFILE
8673 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8674 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 return NULL;
8676 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008677 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008679
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008680 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008681 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008682 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008683 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008684 return NULL;
8685 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008686 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008687 result = strbuf;
8688 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008689
8690#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008691 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008692 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008693 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008694 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008695 return NULL;
8696 }
8697 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008698 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008699 result = strbuf;
8700 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008701
Bram Moolenaar90944302020-08-01 20:45:11 +02008702 case SPEC_SID:
8703 if (current_sctx.sc_sid <= 0)
8704 {
8705 *errormsg = _(e_usingsid);
8706 return NULL;
8707 }
8708 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
8709 result = strbuf;
8710 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02008711#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02008712
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008713#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008714 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008715 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8716 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 result = strbuf;
8718 break;
8719#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008720
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008721 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008722 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008723 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 }
8725
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008726 resultlen = (int)STRLEN(result); // length of new string
8727 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 {
8729 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 resultlen = (int)(s - result);
8732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 else if (!skip_mod)
8734 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008735 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 &resultlen);
8737 if (result == NULL)
8738 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008739 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 return NULL;
8741 }
8742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 }
8744
8745 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8746 {
8747 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008748 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008749 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008751 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 result = NULL;
8753 }
8754 else
8755 result = vim_strnsave(result, resultlen);
8756 vim_free(resultbuf);
8757 return result;
8758}
8759
8760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 * Expand the <sfile> string in "arg".
8762 *
8763 * Returns an allocated string, or NULL for any error.
8764 */
8765 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008766expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008768 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 int len;
8770 char_u *result;
8771 char_u *newres;
8772 char_u *repl;
8773 int srclen;
8774 char_u *p;
8775
8776 result = vim_strsave(arg);
8777 if (result == NULL)
8778 return NULL;
8779
8780 for (p = result; *p; )
8781 {
8782 if (STRNCMP(p, "<sfile>", 7) != 0)
8783 ++p;
8784 else
8785 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008786 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008787 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 if (errormsg != NULL)
8789 {
8790 if (*errormsg)
8791 emsg(errormsg);
8792 vim_free(result);
8793 return NULL;
8794 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008795 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 {
8797 p += srclen;
8798 continue;
8799 }
8800 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8801 newres = alloc(len);
8802 if (newres == NULL)
8803 {
8804 vim_free(repl);
8805 vim_free(result);
8806 return NULL;
8807 }
8808 mch_memmove(newres, result, (size_t)(p - result));
8809 STRCPY(newres + (p - result), repl);
8810 len = (int)STRLEN(newres);
8811 STRCAT(newres, p + srclen);
8812 vim_free(repl);
8813 vim_free(result);
8814 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008815 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 }
8817 }
8818
8819 return result;
8820}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008823/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008824 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008825 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008828dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 if (fname == NULL)
8831 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008832 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833}
8834#endif
8835
8836/*
8837 * ":behave {mswin,xterm}"
8838 */
8839 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008840ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841{
8842 if (STRCMP(eap->arg, "mswin") == 0)
8843 {
8844 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8845 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8846 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8847 set_option_value((char_u *)"keymodel", 0L,
8848 (char_u *)"startsel,stopsel", 0);
8849 }
8850 else if (STRCMP(eap->arg, "xterm") == 0)
8851 {
8852 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8853 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8854 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8855 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8856 }
8857 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008858 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859}
8860
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861static int filetype_detect = FALSE;
8862static int filetype_plugin = FALSE;
8863static int filetype_indent = FALSE;
8864
8865/*
8866 * ":filetype [plugin] [indent] {on,off,detect}"
8867 * on: Load the filetype.vim file to install autocommands for file types.
8868 * off: Load the ftoff.vim file to remove all autocommands for file types.
8869 * plugin on: load filetype.vim and ftplugin.vim
8870 * plugin off: load ftplugof.vim
8871 * indent on: load filetype.vim and indent.vim
8872 * indent off: load indoff.vim
8873 */
8874 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008875ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876{
8877 char_u *arg = eap->arg;
8878 int plugin = FALSE;
8879 int indent = FALSE;
8880
8881 if (*eap->arg == NUL)
8882 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008883 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008884 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 filetype_detect ? "ON" : "OFF",
8886 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8887 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8888 return;
8889 }
8890
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008891 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 for (;;)
8893 {
8894 if (STRNCMP(arg, "plugin", 6) == 0)
8895 {
8896 plugin = TRUE;
8897 arg = skipwhite(arg + 6);
8898 continue;
8899 }
8900 if (STRNCMP(arg, "indent", 6) == 0)
8901 {
8902 indent = TRUE;
8903 arg = skipwhite(arg + 6);
8904 continue;
8905 }
8906 break;
8907 }
8908 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8909 {
8910 if (*arg == 'o' || !filetype_detect)
8911 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008912 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 filetype_detect = TRUE;
8914 if (plugin)
8915 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008916 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 filetype_plugin = TRUE;
8918 }
8919 if (indent)
8920 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008921 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 filetype_indent = TRUE;
8923 }
8924 }
8925 if (*arg == 'd')
8926 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008927 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008928 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 }
8930 }
8931 else if (STRCMP(arg, "off") == 0)
8932 {
8933 if (plugin || indent)
8934 {
8935 if (plugin)
8936 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008937 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 filetype_plugin = FALSE;
8939 }
8940 if (indent)
8941 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008942 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 filetype_indent = FALSE;
8944 }
8945 }
8946 else
8947 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008948 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 filetype_detect = FALSE;
8950 }
8951 }
8952 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008953 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954}
8955
8956/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008957 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 */
8959 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008960ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961{
8962 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008963 {
8964 char_u *arg = eap->arg;
8965
8966 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8967 arg += 9;
8968
8969 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8970 if (arg != eap->arg)
8971 did_filetype = FALSE;
8972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008976ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977{
8978#ifdef FEAT_DIGRAPHS
8979 if (*eap->arg != NUL)
8980 putdigraph(eap->arg);
8981 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008982 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008984 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985#endif
8986}
8987
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008988#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8989 void
8990set_no_hlsearch(int flag)
8991{
8992 no_hlsearch = flag;
8993# ifdef FEAT_EVAL
8994 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8995# endif
8996}
8997
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998/*
8999 * ":nohlsearch"
9000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009002ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009004 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009005 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007#endif
9008
9009#ifdef FEAT_CRYPT
9010/*
9011 * ":X": Get crypt key
9012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009014ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009016 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009017 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018}
9019#endif
9020
9021#ifdef FEAT_FOLDING
9022 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009023ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024{
9025 if (foldManualAllowed(TRUE))
9026 foldCreate(eap->line1, eap->line2);
9027}
9028
9029 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009030ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031{
9032 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9033 eap->forceit, FALSE);
9034}
9035
9036 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009037ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038{
9039 linenr_T lnum;
9040
Bram Moolenaar4facea32019-10-12 20:17:40 +02009041# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009042 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009043# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009044
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009045 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9047 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9048 ml_setmarked(lnum);
9049
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009050 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009052 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009053# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009054 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009055# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056}
9057#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009058
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009059#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009060/*
9061 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9062 * instead of a quickfix command.
9063 */
9064 int
9065is_loclist_cmd(int cmdidx)
9066{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009067 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009068 return FALSE;
9069 return cmdnames[cmdidx].cmd_name[0] == 'l';
9070}
9071#endif
9072
Bram Moolenaar4facea32019-10-12 20:17:40 +02009073#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009074 int
9075get_pressedreturn(void)
9076{
9077 return ex_pressedreturn;
9078}
9079
9080 void
9081set_pressedreturn(int val)
9082{
9083 ex_pressedreturn = val;
9084}
9085#endif