blob: 1ccd38ec90bb8ac1210591101d99f9443b7aa84c [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;
643 msglist_T *private_msg_list;
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;
667 private_msg_list = NULL;
668#endif
669
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100670 // It's possible to create an endless loop with ":execute", catch that
671 // here. The value of 200 allows nested function calls, ":source", etc.
672 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100673 if (call_depth >= 200
674#ifdef FEAT_EVAL
675 && call_depth >= p_mfd
676#endif
677 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100679 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100681 // When converting to an exception, we do not include the command name
682 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100683 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 msg_list = saved_msg_list;
685#endif
686 return FAIL;
687 }
688 ++call_depth;
689
690#ifdef FEAT_EVAL
691 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000692 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 cstack.cs_trylevel = 0;
694 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000695 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
697
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100698 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100700 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100701 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000702 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 ++ex_nesting_level;
704
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100705 // Get the function or script name and the address where the next breakpoint
706 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000707 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 {
709 fname = func_name(real_cookie);
710 breakpoint = func_breakpoint(real_cookie);
711 dbg_tick = func_dbg_tick(real_cookie);
712 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100713 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100715 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 breakpoint = source_breakpoint(real_cookie);
717 dbg_tick = source_dbg_tick(real_cookie);
718 }
719
720 /*
721 * Initialize "force_abort" and "suppress_errthrow" at the top level.
722 */
723 if (!recursive)
724 {
725 force_abort = FALSE;
726 suppress_errthrow = FALSE;
727 }
728
729 /*
730 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000731 * exception handling (used when debugging). Otherwise clear it to avoid
732 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000734 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000735 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000736 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200737 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738
739 initial_trylevel = trylevel;
740
741 /*
742 * "did_throw" will be set to TRUE when an exception is being thrown.
743 */
744 did_throw = FALSE;
745#endif
746 /*
747 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000748 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 * If force_abort is set, we cancel everything.
750 */
751 did_emsg = FALSE;
752
753 /*
754 * KeyTyped is only set when calling vgetc(). Reset it here when not
755 * calling vgetc() (sourced command lines).
756 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100757 if (!(flags & DOCMD_KEYTYPED)
758 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 KeyTyped = FALSE;
760
761 /*
762 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000763 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 * - for multiple commands on one line, separated with '|'
765 * - when repeating until there are no more lines (for ":source")
766 */
767 next_cmdline = cmdline;
768 do
769 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000770#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100771 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000772#endif
773
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100774 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 if (next_cmdline == NULL
776#ifdef FEAT_EVAL
777 && !force_abort
778 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000779 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780#endif
781 )
782 did_emsg = FALSE;
783
784 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000785 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100786 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 * 3. If a line is given: Make a copy, so we can mess with it.
788 */
789
790#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100791 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000792 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100794 // Each '|' separated command is stored separately in lines_ga, to
795 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100796 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100798 // Check if a function has returned or, unless it has an unclosed
799 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000800 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000802# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000803 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000804 func_line_end(real_cookie);
805# endif
806 if (func_has_ended(real_cookie))
807 {
808 retval = FAIL;
809 break;
810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000813 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100814 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000815 script_line_end();
816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100818 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100819 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 {
821 retval = FAIL;
822 break;
823 }
824
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100825 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 if (breakpoint != NULL && dbg_tick != NULL
827 && *dbg_tick != debug_tick)
828 {
829 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100830 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100831 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 *dbg_tick = debug_tick;
833 }
834
835 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100836 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100838 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100840 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100842 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100843 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100845 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100846 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 *dbg_tick = debug_tick;
848 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000849# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000850 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000851 {
852 if (getline_is_func)
853 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100854 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000855 script_line_start();
856 }
857# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859#endif
860
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100861 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 if (next_cmdline == NULL)
863 {
864 /*
865 * Need to set msg_didout for the first line after an ":if",
866 * otherwise the ":if" will be overwritten.
867 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100868 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100870 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871#ifdef FEAT_EVAL
872 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
873#else
874 0
875#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200876 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100878 // Don't call wait_return for aborted command line. The NULL
879 // returned for the end of a sourced file or executed function
880 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 if (KeyTyped && !(flags & DOCMD_REPEAT))
882 need_wait_return = FALSE;
883 retval = FAIL;
884 break;
885 }
886 used_getline = TRUE;
887
888 /*
889 * Keep the first typed line. Clear it when more lines are typed.
890 */
891 if (flags & DOCMD_KEEPLINE)
892 {
893 vim_free(repeat_cmdline);
894 if (count == 0)
895 repeat_cmdline = vim_strsave(next_cmdline);
896 else
897 repeat_cmdline = NULL;
898 }
899 }
900
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100901 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 else if (cmdline_copy == NULL)
903 {
904 next_cmdline = vim_strsave(next_cmdline);
905 if (next_cmdline == NULL)
906 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100907 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 retval = FAIL;
909 break;
910 }
911 }
912 cmdline_copy = next_cmdline;
913
914#ifdef FEAT_EVAL
915 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200916 * Inside a while/for loop, and when the command looks like a ":while"
917 * or ":for", the line is stored, because we may need it later when
918 * looping.
919 *
920 * When there is a '|' and another command, it is stored separately,
921 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000922 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200923 *
924 * Pass a different "fgetline" function to do_one_cmd() below,
925 * that it stores lines in or reads them from "lines_ga". Makes it
926 * possible to define a function inside a while/for loop and handles
927 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200929 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200931 cmd_getline = get_loop_line;
932 cmd_cookie = (void *)&cmd_loop_cookie;
933 cmd_loop_cookie.lines_gap = &lines_ga;
934 cmd_loop_cookie.current_line = current_line;
935 cmd_loop_cookie.getline = fgetline;
936 cmd_loop_cookie.cookie = cookie;
937 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
938
939 // Save the current line when encountering it the first time.
940 if (current_line == lines_ga.ga_len
941 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 {
943 retval = FAIL;
944 break;
945 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200946 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200948 else
949 {
950 cmd_getline = fgetline;
951 cmd_cookie = cookie;
952 }
953
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 did_endif = FALSE;
955#endif
956
957 if (count++ == 0)
958 {
959 /*
960 * All output from the commands is put below each other, without
961 * waiting for a return. Don't do this when executing commands
962 * from a script or when being called recursive (e.g. for ":e
963 * +command file").
964 */
965 if (!(flags & DOCMD_NOWAIT) && !recursive)
966 {
967 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100968 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100970 msg_scroll = TRUE; // put messages below each other
971 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 ++RedrawingDisabled;
973 did_inc = TRUE;
974 }
975 }
976
Bram Moolenaar823654b2020-05-29 23:03:09 +0200977 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100978 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979
980 /*
981 * 2. Execute one '|' separated command.
982 * do_one_cmd() will return NULL if there is no trailing '|'.
983 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
984 */
985 ++recursive;
986 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
987#ifdef FEAT_EVAL
988 &cstack,
989#endif
990 cmd_getline, cmd_cookie);
991 --recursive;
992
993#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000994 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100995 // Use "current_line" from "cmd_loop_cookie", it may have been
996 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000997 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998#endif
999
1000 if (next_cmdline == NULL)
1001 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001002 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001003
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 /*
1005 * If the command was typed, remember it for the ':' register.
1006 * Do this AFTER executing the command to make :@: work.
1007 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001008 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 && new_last_cmdline != NULL)
1010 {
1011 vim_free(last_cmdline);
1012 last_cmdline = new_last_cmdline;
1013 new_last_cmdline = NULL;
1014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 }
1016 else
1017 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001018 // need to copy the command after the '|' to cmdline_copy, for the
1019 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001020 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 next_cmdline = cmdline_copy;
1022 }
1023
1024
1025#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001026 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001028 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 && !func_has_abort(real_cookie))
1030 did_emsg = FALSE;
1031
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001032 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 {
1034 ++current_line;
1035
1036 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001037 * An ":endwhile", ":endfor" and ":continue" is handled here.
1038 * If we were executing commands, jump back to the ":while" or
1039 * ":for".
1040 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001042 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001044 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001046 // Jump back to the matching ":while" or ":for". Be careful
1047 // not to use a cs_line[] from an entry that isn't a ":while"
1048 // or ":for": It would make "current_line" invalid and can
1049 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 if (!did_emsg && !got_int && !did_throw
1051 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001052 && (cstack.cs_flags[cstack.cs_idx]
1053 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 && cstack.cs_line[cstack.cs_idx] >= 0
1055 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1056 {
1057 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001058 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001059 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001060 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001062 // Check for the next breakpoint at or after the ":while"
1063 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 if (breakpoint != NULL)
1065 {
1066 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001067 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 fname,
1069 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1070 *dbg_tick = debug_tick;
1071 }
1072 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001073 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001075 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001077 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1078 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 }
1080 }
1081
1082 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001083 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001085 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001087 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001088 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
1090 }
1091
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001092 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001093 if (breakpoint != NULL && has_watchexpr())
1094 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001095 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001096 *dbg_tick = debug_tick;
1097 }
1098
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 /*
1100 * When not inside any ":while" loop, clear remembered lines.
1101 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001102 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 {
1104 if (lines_ga.ga_len > 0)
1105 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001106 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1108 free_cmdlines(&lines_ga);
1109 }
1110 current_line = 0;
1111 }
1112
1113 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001114 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1115 * being restored at the ":endtry". Reset them here and set the
1116 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1117 * This includes the case where a missing ":endif", ":endwhile" or
1118 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001120 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001122 cstack.cs_lflags &= ~CSL_HAD_FINA;
1123 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1124 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 did_throw ? (void *)current_exception : NULL);
1126 did_emsg = got_int = did_throw = FALSE;
1127 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1128 }
1129
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001130 // Update global "trylevel" for recursive calls to do_cmdline() from
1131 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 trylevel = initial_trylevel + cstack.cs_trylevel;
1133
1134 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001135 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 * files) is aborted because of an error, an interrupt, or an uncaught
1137 * exception, cancel everything. If it is left normally, reset
1138 * force_abort to get the non-EH compatible abortion behavior for
1139 * the rest of the script.
1140 */
1141 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1142 force_abort = FALSE;
1143
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001144 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001146#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
1148 }
1149 /*
1150 * Continue executing command lines when:
1151 * - no CTRL-C typed, no aborting error, no exception thrown or try
1152 * conditionals need to be checked for executing finally clauses or
1153 * catching an interrupt exception
1154 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001155 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 * looping for ":source" command or function call.
1157 */
1158 while (!((got_int
1159#ifdef FEAT_EVAL
1160 || (did_emsg && force_abort) || did_throw
1161#endif
1162 )
1163#ifdef FEAT_EVAL
1164 && cstack.cs_trylevel == 0
1165#endif
1166 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001167 && !(did_emsg
1168#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001169 // Keep going when inside try/catch, so that the error can be
1170 // deal with, except when it is a syntax error, it may cause
1171 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001172 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1173#endif
1174 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001175 && (getline_equal(fgetline, cookie, getexmodeline)
1176 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 && (next_cmdline != NULL
1178#ifdef FEAT_EVAL
1179 || cstack.cs_idx >= 0
1180#endif
1181 || (flags & DOCMD_REPEAT)));
1182
1183 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001184 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185#ifdef FEAT_EVAL
1186 free_cmdlines(&lines_ga);
1187 ga_clear(&lines_ga);
1188
1189 if (cstack.cs_idx >= 0)
1190 {
1191 /*
1192 * If a sourced file or executed function ran to its end, report the
1193 * unclosed conditional.
1194 */
1195 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001196 && ((getline_equal(fgetline, cookie, getsourceline)
1197 && !source_finished(fgetline, cookie))
1198 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 && !func_has_ended(real_cookie))))
1200 {
1201 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001202 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001204 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001205 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001206 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001208 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 }
1210
1211 /*
1212 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1213 * ":endtry" in a sourced file or executed function. If the try
1214 * conditional is in its finally clause, ignore anything pending.
1215 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001216 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 */
1218 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001219 {
1220 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1221
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001222 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001223 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001224 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1225 &cstack.cs_looplevel);
1226 }
1227 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 trylevel = initial_trylevel;
1229 }
1230
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001231 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1232 // lack was reported above and the error message is to be converted to an
1233 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001234 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 ? (char_u *)"endfunction" : (char_u *)NULL);
1236
1237 if (trylevel == 0)
1238 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001239 // Just in case did_throw got set but current_exception wasn't.
1240 if (current_exception == NULL)
1241 did_throw = FALSE;
1242
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 /*
1244 * When an exception is being thrown out of the outermost try
1245 * conditional, discard the uncaught exception, disable the conversion
1246 * of interrupts or errors to exceptions, and ensure that no more
1247 * commands are executed.
1248 */
1249 if (did_throw)
1250 {
1251 void *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001252 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253
1254 /*
1255 * If the uncaught exception is a user exception, report it as an
1256 * error. If it is an error exception, display the saved error
1257 * message now. For an interrupt exception, do nothing; the
1258 * interrupt message is given elsewhere.
1259 */
1260 switch (current_exception->type)
1261 {
1262 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001263 vim_snprintf((char *)IObuff, IOSIZE,
1264 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 current_exception->value);
1266 p = vim_strsave(IObuff);
1267 break;
1268 case ET_ERROR:
1269 messages = current_exception->messages;
1270 current_exception->messages = NULL;
1271 break;
1272 case ET_INTERRUPT:
1273 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
1275
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001276 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1277 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001278 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 current_exception->throw_name = NULL;
1280
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001281 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 suppress_errthrow = TRUE;
1283 force_abort = TRUE;
1284
1285 if (messages != NULL)
1286 {
1287 do
1288 {
1289 next = messages->next;
1290 emsg(messages->msg);
1291 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001292 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 vim_free(messages);
1294 messages = next;
1295 }
1296 while (messages != NULL);
1297 }
1298 else if (p != NULL)
1299 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001300 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 vim_free(p);
1302 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001303 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001304 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001305 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 }
1307
1308 /*
1309 * On an interrupt or an aborting error not converted to an exception,
1310 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001311 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 * when force_abort is set and did_emsg unset in case of an interrupt
1313 * from a finally clause after an error.
1314 */
1315 else if (got_int || (did_emsg && force_abort))
1316 suppress_errthrow = TRUE;
1317 }
1318
1319 /*
1320 * The current cstack will be freed when do_cmdline() returns. An uncaught
1321 * exception will have to be rethrown in the previous cstack. If a function
1322 * has just returned or a script file was just finished and the previous
1323 * cstack belongs to the same function or, respectively, script file, it
1324 * will have to be checked for finally clauses to be executed due to the
1325 * ":return" or ":finish". This is done in do_one_cmd().
1326 */
1327 if (did_throw)
1328 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001329 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001331 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 && ex_nesting_level > func_level(real_cookie) + 1))
1333 {
1334 if (!did_throw)
1335 check_cstack = TRUE;
1336 }
1337 else
1338 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001339 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001340 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 --ex_nesting_level;
1342 /*
1343 * Go to debug mode when returning from a function in which we are
1344 * single-stepping.
1345 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001346 if ((getline_equal(fgetline, cookie, getsourceline)
1347 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001349 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 ? (char_u *)_("End of sourced file")
1351 : (char_u *)_("End of function"));
1352 }
1353
1354 /*
1355 * Restore the exception environment (done after returning from the
1356 * debugger).
1357 */
1358 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001359 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
1361 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001362
1363 // Cleanup if "cs_emsg_silent_list" remains.
1364 if (cstack.cs_emsg_silent_list != NULL)
1365 {
1366 eslist_T *elem, *temp;
1367
1368 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1369 {
1370 temp = elem->next;
1371 vim_free(elem);
1372 }
1373 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001374#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375
1376 /*
1377 * If there was too much output to fit on the command line, ask the user to
1378 * hit return before redrawing the screen. With the ":global" command we do
1379 * this only once after the command is finished.
1380 */
1381 if (did_inc)
1382 {
1383 --RedrawingDisabled;
1384 --no_wait_return;
1385 msg_scroll = FALSE;
1386
1387 /*
1388 * When just finished an ":if"-":else" which was typed, no need to
1389 * wait for hit-return. Also for an error situation.
1390 */
1391 if (retval == FAIL
1392#ifdef FEAT_EVAL
1393 || (did_endif && KeyTyped && !did_emsg)
1394#endif
1395 )
1396 {
1397 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001398 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 }
1400 else if (need_wait_return)
1401 {
1402 /*
1403 * The msg_start() above clears msg_didout. The wait_return we do
1404 * here should not overwrite the command that may be shown before
1405 * doing that.
1406 */
1407 msg_didout |= msg_didout_before_start;
1408 wait_return(FALSE);
1409 }
1410 }
1411
Bram Moolenaar2a942252012-11-28 23:03:07 +01001412#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001413 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001414#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 /*
1416 * Reset if_level, in case a sourced script file contains more ":if" than
1417 * ":endif" (could be ":if x | foo | endif").
1418 */
1419 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001420#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001421
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 --call_depth;
1423 return retval;
1424}
1425
1426#ifdef FEAT_EVAL
1427/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001428 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 */
1430 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001431get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001433 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 wcmd_T *wp;
1435 char_u *line;
1436
1437 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1438 {
1439 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001440 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001442 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001444 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001446 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001447 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 ++cp->current_line;
1449
1450 return line;
1451 }
1452
1453 KeyTyped = FALSE;
1454 ++cp->current_line;
1455 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001456 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 return vim_strsave(wp->line);
1458}
1459
1460/*
1461 * Store a line in "gap" so that a ":while" loop can execute it again.
1462 */
1463 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001464store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
1466 if (ga_grow(gap, 1) == FAIL)
1467 return FAIL;
1468 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001469 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 return OK;
1472}
1473
1474/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001475 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 */
1477 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001478free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479{
1480 while (gap->ga_len > 0)
1481 {
1482 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1483 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 }
1485}
1486#endif
1487
1488/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001489 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1490 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001493getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001494 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001495 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001496 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497{
1498#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001499 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001500 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001502 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1503 // function that's originally used to obtain the lines. This may be
1504 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001505 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001506 cp = (struct loop_cookie *)cookie;
1507 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 {
1509 gp = cp->getline;
1510 cp = cp->cookie;
1511 }
1512 return gp == func;
1513#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001514 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515#endif
1516}
1517
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001519 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 * getline function. Otherwise return "cookie".
1521 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001523getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001524 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001525 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526{
Bram Moolenaar13505972019-01-24 15:04:48 +01001527#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001528 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001529 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001531 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1532 // cookie that's originally used to obtain the lines. This may be nested
1533 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001534 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001535 cp = (struct loop_cookie *)cookie;
1536 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 {
1538 gp = cp->getline;
1539 cp = cp->cookie;
1540 }
1541 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001542#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001545}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546
Bram Moolenaard5053d02020-06-28 15:51:16 +02001547#if defined(FEAT_EVAL) || defined(PROT)
1548/*
1549 * Get the next line source line without advancing.
1550 */
1551 char_u *
1552getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001553 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001554 void *cookie) // argument for fgetline()
1555{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001556 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001557 struct loop_cookie *cp;
1558 wcmd_T *wp;
1559
1560 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1561 // cookie that's originally used to obtain the lines. This may be nested
1562 // several levels.
1563 gp = fgetline;
1564 cp = (struct loop_cookie *)cookie;
1565 while (gp == get_loop_line)
1566 {
1567 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1568 {
1569 // executing lines a second time, use the stored copy
1570 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1571 return wp->line;
1572 }
1573 gp = cp->getline;
1574 cp = cp->cookie;
1575 }
1576 if (gp == getsourceline)
1577 return source_nextline(cp);
1578 return NULL;
1579}
1580#endif
1581
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001582
1583/*
1584 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1585 * ":bwipeout", etc.
1586 * Returns the buffer number.
1587 */
1588 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001589compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001590{
1591 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001592 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001593 int count = offset;
1594
1595 buf = firstbuf;
1596 while (buf->b_next != NULL && buf->b_fnum < lnum)
1597 buf = buf->b_next;
1598 while (count != 0)
1599 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001600 count += (offset < 0) ? 1 : -1;
1601 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1602 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001603 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001604 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001605 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001606 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001607 while (buf->b_ml.ml_mfp == NULL)
1608 {
1609 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1610 if (nextbuf == NULL)
1611 break;
1612 buf = nextbuf;
1613 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001614 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001615 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001616 if (addr_type == ADDR_LOADED_BUFFERS)
1617 while (buf->b_ml.ml_mfp == NULL)
1618 {
1619 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1620 if (nextbuf == NULL)
1621 break;
1622 buf = nextbuf;
1623 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001624 return buf->b_fnum;
1625}
1626
Bram Moolenaarb7316892019-05-01 18:08:42 +02001627/*
1628 * Return the window number of "win".
1629 * When "win" is NULL return the number of windows.
1630 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001631 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001632current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001633{
1634 win_T *wp;
1635 int nr = 0;
1636
Bram Moolenaar29323592016-07-24 22:04:11 +02001637 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001638 {
1639 ++nr;
1640 if (wp == win)
1641 break;
1642 }
1643 return nr;
1644}
1645
1646 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001647current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001648{
1649 tabpage_T *tp;
1650 int nr = 0;
1651
Bram Moolenaar29323592016-07-24 22:04:11 +02001652 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001653 {
1654 ++nr;
1655 if (tp == tab)
1656 break;
1657 }
1658 return nr;
1659}
1660
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001661 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001662comment_start(char_u *p, int starts_with_colon UNUSED)
1663{
1664#ifdef FEAT_EVAL
1665 if (in_vim9script())
1666 return p[0] == '#' && p[1] != '{' && !starts_with_colon;
1667#endif
1668 return *p == '"';
1669}
1670
Bram Moolenaarf240e182014-11-27 18:33:02 +01001671# define CURRENT_WIN_NR current_win_nr(curwin)
1672# define LAST_WIN_NR current_win_nr(NULL)
1673# define CURRENT_TAB_NR current_tab_nr(curtab)
1674# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001675
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676/*
1677 * Execute one Ex command.
1678 *
1679 * If 'sourcing' is TRUE, the command will be included in the error message.
1680 *
1681 * 1. skip comment lines and leading space
1682 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001683 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001684 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001685 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001686 * 6. parse arguments
1687 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001689 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 *
1691 * This function may be called recursively!
1692 */
1693#if (_MSC_VER == 1200)
1694/*
Bram Moolenaared203462004-06-16 11:19:22 +00001695 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001697 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698#endif
1699 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001700do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001701 char_u **cmdlinep,
1702 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001704 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001706 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001707 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001709 char_u *p;
1710 linenr_T lnum;
1711 long n;
1712 char *errormsg = NULL; // error message
1713 char_u *after_modifier = NULL;
1714 exarg_T ea; // Ex command arguments
1715 int save_msg_scroll = msg_scroll;
1716 cmdmod_T save_cmdmod;
1717 int save_reg_executing = reg_executing;
1718 int ni; // set when Not Implemented
1719 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001720 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001721#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001722 int may_have_range;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001723 int vim9script = in_vim9script();
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725
Bram Moolenaara80faa82020-04-12 19:37:17 +02001726 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 ea.line1 = 1;
1728 ea.line2 = 1;
1729#ifdef FEAT_EVAL
1730 ++ex_nesting_level;
1731#endif
1732
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001733 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 if (quitmore
1735#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001736 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001737 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001738#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001739 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001740 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 --quitmore;
1742
1743 /*
1744 * Reset browse, confirm, etc.. They are restored when returning, for
1745 * recursive calls.
1746 */
1747 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001749 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001750 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1751 goto doend;
1752
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001753/*
1754 * 1. Skip comment lines and leading white space and colons.
1755 * 2. Handle command modifiers.
1756 */
1757 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001759 ea.cmdlinep = cmdlinep;
1760 ea.getline = fgetline;
1761 ea.cookie = cookie;
1762#ifdef FEAT_EVAL
1763 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001764 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001766 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001767 goto doend;
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 Moolenaareffed932018-08-14 13:38:17 +02002519 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002520 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002521 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002522 if (emsg_silent < 0)
2523 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002524 ea.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 Moolenaar47e7d702020-07-05 18:18:42 +02002601 undo_cmdmod(&ea, 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 Moolenaar7171abe2004-10-11 10:06:20 +00002605#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002606 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002607 --sandbox;
2608#endif
2609
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002610 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 ea.nextcmd = NULL;
2612
2613#ifdef FEAT_EVAL
2614 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002615 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616#endif
2617
2618 return ea.nextcmd;
2619}
2620#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002621 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622#endif
2623
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002624static char ex_error_buf[MSG_BUF_LEN];
2625
2626/*
2627 * Return an error message with argument included.
2628 * Uses a static buffer, only the last error will be kept.
2629 * "msg" will be translated, caller should use N_().
2630 */
2631 char *
2632ex_errmsg(char *msg, char_u *arg)
2633{
2634 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2635 return ex_error_buf;
2636}
2637
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002639 * Parse and skip over command modifiers:
2640 * - update eap->cmd
2641 * - store flags in "cmdmod".
2642 * - Set ex_pressedreturn for an empty command line.
2643 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002644 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002645 * - set p_verbose for ":verbose"
2646 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002647 * When "skip_only" is TRUE the global variables are not changed, except for
2648 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002649 * Return FAIL when the command is not to be executed.
2650 * May set "errormsg" to an error message.
2651 */
2652 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002653parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002654{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002655 char_u *p;
2656 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002657
Bram Moolenaara80faa82020-04-12 19:37:17 +02002658 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002659 eap->verbose_save = -1;
2660 eap->save_msg_silent = -1;
2661
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002662 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002663 for (;;)
2664 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002665 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002666 {
2667 if (*eap->cmd == ':')
2668 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002669 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002670 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002671
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002672 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002673 if (*eap->cmd == NUL && exmode_active
2674 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2675 || getline_equal(eap->getline, eap->cookie, getexline))
2676 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2677 {
2678 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002679 if (!skip_only)
2680 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002681 }
2682
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002683 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002684 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002685 return FAIL;
2686 if (*eap->cmd == NUL)
2687 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002688 if (!skip_only)
2689 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002690 return FAIL;
2691 }
2692
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002693 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002694 switch (*p)
2695 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002696 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002697 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2698 break;
2699 cmdmod.split |= WSP_ABOVE;
2700 continue;
2701
2702 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2703 {
2704 cmdmod.split |= WSP_BELOW;
2705 continue;
2706 }
2707 if (checkforcmd(&eap->cmd, "browse", 3))
2708 {
2709#ifdef FEAT_BROWSE_CMD
2710 cmdmod.browse = TRUE;
2711#endif
2712 continue;
2713 }
2714 if (!checkforcmd(&eap->cmd, "botright", 2))
2715 break;
2716 cmdmod.split |= WSP_BOT;
2717 continue;
2718
2719 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2720 break;
2721#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2722 cmdmod.confirm = TRUE;
2723#endif
2724 continue;
2725
2726 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2727 {
2728 cmdmod.keepmarks = TRUE;
2729 continue;
2730 }
2731 if (checkforcmd(&eap->cmd, "keepalt", 5))
2732 {
2733 cmdmod.keepalt = TRUE;
2734 continue;
2735 }
2736 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2737 {
2738 cmdmod.keeppatterns = TRUE;
2739 continue;
2740 }
2741 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2742 break;
2743 cmdmod.keepjumps = TRUE;
2744 continue;
2745
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002746 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002747 {
2748 char_u *reg_pat;
2749
2750 if (!checkforcmd(&p, "filter", 4)
2751 || *p == NUL || ends_excmd(*p))
2752 break;
2753 if (*p == '!')
2754 {
2755 cmdmod.filter_force = TRUE;
2756 p = skipwhite(p + 1);
2757 if (*p == NUL || ends_excmd(*p))
2758 break;
2759 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002760#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002761 // Avoid that "filter(arg)" is recognized.
2762 if (in_vim9script() && !VIM_ISWHITE(*p))
2763 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002764#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002765 if (skip_only)
2766 p = skip_vimgrep_pat(p, NULL, NULL);
2767 else
2768 // NOTE: This puts a NUL after the pattern.
2769 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002770 if (p == NULL || *p == NUL)
2771 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002772 if (!skip_only)
2773 {
2774 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002775 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002776 if (cmdmod.filter_regmatch.regprog == NULL)
2777 break;
2778 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002779 eap->cmd = p;
2780 continue;
2781 }
2782
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002783 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002784 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2785 || *p == NUL || ends_excmd(*p))
2786 break;
2787 eap->cmd = p;
2788 cmdmod.hide = TRUE;
2789 continue;
2790
2791 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2792 {
2793 cmdmod.lockmarks = TRUE;
2794 continue;
2795 }
2796
2797 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2798 break;
2799 cmdmod.split |= WSP_ABOVE;
2800 continue;
2801
2802 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2803 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002804 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002805 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002806 // Set 'eventignore' to "all". Restore the
2807 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002808 cmdmod.save_ei = vim_strsave(p_ei);
2809 set_string_option_direct((char_u *)"ei", -1,
2810 (char_u *)"all", OPT_FREE, SID_NONE);
2811 }
2812 continue;
2813 }
2814 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2815 break;
2816 cmdmod.noswapfile = TRUE;
2817 continue;
2818
2819 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2820 break;
2821 cmdmod.split |= WSP_BELOW;
2822 continue;
2823
2824 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2825 {
2826#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002827 if (!skip_only)
2828 {
2829 if (!eap->did_sandbox)
2830 ++sandbox;
2831 eap->did_sandbox = TRUE;
2832 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002833#endif
2834 continue;
2835 }
2836 if (!checkforcmd(&eap->cmd, "silent", 3))
2837 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002838 if (!skip_only)
2839 {
2840 if (eap->save_msg_silent == -1)
2841 eap->save_msg_silent = msg_silent;
2842 ++msg_silent;
2843 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002844 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2845 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002846 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002847 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002848 if (!skip_only)
2849 {
2850 ++emsg_silent;
2851 ++eap->did_esilent;
2852 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002853 }
2854 continue;
2855
2856 case 't': if (checkforcmd(&p, "tab", 3))
2857 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002858 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002859 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002860 long tabnr = get_address(eap, &eap->cmd,
2861 ADDR_TABS, eap->skip,
2862 skip_only, FALSE, 1);
2863 if (tabnr == MAXLNUM)
2864 cmdmod.tab = tabpage_index(curtab) + 1;
2865 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002866 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002867 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2868 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002869 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002870 return FAIL;
2871 }
2872 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002873 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002874 }
2875 eap->cmd = p;
2876 continue;
2877 }
2878 if (!checkforcmd(&eap->cmd, "topleft", 2))
2879 break;
2880 cmdmod.split |= WSP_TOP;
2881 continue;
2882
2883 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2884 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002885 if (!skip_only)
2886 {
2887 if (eap->save_msg_silent == -1)
2888 eap->save_msg_silent = msg_silent;
2889 msg_silent = 0;
2890 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002891 continue;
2892
2893 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2894 {
2895 cmdmod.split |= WSP_VERT;
2896 continue;
2897 }
2898 if (!checkforcmd(&p, "verbose", 4))
2899 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002900 if (!skip_only)
2901 {
2902 if (eap->verbose_save < 0)
2903 eap->verbose_save = p_verbose;
2904 if (vim_isdigit(*eap->cmd))
2905 p_verbose = atoi((char *)eap->cmd);
2906 else
2907 p_verbose = 1;
2908 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002909 eap->cmd = p;
2910 continue;
2911 }
2912 break;
2913 }
2914
2915 return OK;
2916}
2917
2918/*
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002919 * Undo and free contents of "cmdmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002920 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002921 void
2922undo_cmdmod(exarg_T *eap, int save_msg_scroll)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002923{
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002924 if (eap->verbose_save >= 0)
2925 p_verbose = eap->verbose_save;
2926
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002927 if (cmdmod.save_ei != NULL)
2928 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002929 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002930 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2931 OPT_FREE, SID_NONE);
2932 free_string_option(cmdmod.save_ei);
2933 }
2934
2935 if (cmdmod.filter_regmatch.regprog != NULL)
2936 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002937
2938 if (eap->save_msg_silent != -1)
2939 {
2940 // messages could be enabled for a serious error, need to check if the
2941 // counters don't become negative
2942 if (!did_emsg || msg_silent > eap->save_msg_silent)
2943 msg_silent = eap->save_msg_silent;
2944 emsg_silent -= eap->did_esilent;
2945 if (emsg_silent < 0)
2946 emsg_silent = 0;
2947 // Restore msg_scroll, it's set by file I/O commands, even when no
2948 // message is actually displayed.
2949 msg_scroll = save_msg_scroll;
2950
2951 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2952 // somewhere in the line. Put it back in the first column.
2953 if (redirecting())
2954 msg_col = 0;
2955 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002956}
2957
2958/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002959 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002960 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002961 * Return FAIL and set "errormsg" or return OK.
2962 */
2963 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002964parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002965{
2966 int address_count = 1;
2967 linenr_T lnum;
2968
2969 // Repeat for all ',' or ';' separated addresses.
2970 for (;;)
2971 {
2972 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002973 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002974 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002975 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002976 eap->addr_count == 0, address_count++);
2977 if (eap->cmd == NULL) // error detected
2978 return FAIL;
2979 if (lnum == MAXLNUM)
2980 {
2981 if (*eap->cmd == '%') // '%' - all lines
2982 {
2983 ++eap->cmd;
2984 switch (eap->addr_type)
2985 {
2986 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002987 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002988 eap->line1 = 1;
2989 eap->line2 = curbuf->b_ml.ml_line_count;
2990 break;
2991 case ADDR_LOADED_BUFFERS:
2992 {
2993 buf_T *buf = firstbuf;
2994
2995 while (buf->b_next != NULL
2996 && buf->b_ml.ml_mfp == NULL)
2997 buf = buf->b_next;
2998 eap->line1 = buf->b_fnum;
2999 buf = lastbuf;
3000 while (buf->b_prev != NULL
3001 && buf->b_ml.ml_mfp == NULL)
3002 buf = buf->b_prev;
3003 eap->line2 = buf->b_fnum;
3004 break;
3005 }
3006 case ADDR_BUFFERS:
3007 eap->line1 = firstbuf->b_fnum;
3008 eap->line2 = lastbuf->b_fnum;
3009 break;
3010 case ADDR_WINDOWS:
3011 case ADDR_TABS:
3012 if (IS_USER_CMDIDX(eap->cmdidx))
3013 {
3014 eap->line1 = 1;
3015 eap->line2 = eap->addr_type == ADDR_WINDOWS
3016 ? LAST_WIN_NR : LAST_TAB_NR;
3017 }
3018 else
3019 {
3020 // there is no Vim command which uses '%' and
3021 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003022 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003023 return FAIL;
3024 }
3025 break;
3026 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003027 case ADDR_UNSIGNED:
3028 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003029 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003030 return FAIL;
3031 case ADDR_ARGUMENTS:
3032 if (ARGCOUNT == 0)
3033 eap->line1 = eap->line2 = 0;
3034 else
3035 {
3036 eap->line1 = 1;
3037 eap->line2 = ARGCOUNT;
3038 }
3039 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003040 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003041#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003042 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003043 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003044 if (eap->line2 == 0)
3045 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003046#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003047 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003048 case ADDR_NONE:
3049 // Will give an error later if a range is found.
3050 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003051 }
3052 ++eap->addr_count;
3053 }
3054 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3055 {
3056 pos_T *fp;
3057
3058 // '*' - visual area
3059 if (eap->addr_type != ADDR_LINES)
3060 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003061 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003062 return FAIL;
3063 }
3064
3065 ++eap->cmd;
3066 if (!eap->skip)
3067 {
3068 fp = getmark('<', FALSE);
3069 if (check_mark(fp) == FAIL)
3070 return FAIL;
3071 eap->line1 = fp->lnum;
3072 fp = getmark('>', FALSE);
3073 if (check_mark(fp) == FAIL)
3074 return FAIL;
3075 eap->line2 = fp->lnum;
3076 ++eap->addr_count;
3077 }
3078 }
3079 }
3080 else
3081 eap->line2 = lnum;
3082 eap->addr_count++;
3083
3084 if (*eap->cmd == ';')
3085 {
3086 if (!eap->skip)
3087 {
3088 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003089 // Don't leave the cursor on an illegal line or column, but do
3090 // accept zero as address, so 0;/PATTERN/ works correctly.
3091 if (eap->line2 > 0)
3092 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003093 }
3094 }
3095 else if (*eap->cmd != ',')
3096 break;
3097 ++eap->cmd;
3098 }
3099
3100 // One address given: set start and end lines.
3101 if (eap->addr_count == 1)
3102 {
3103 eap->line1 = eap->line2;
3104 // ... but only implicit: really no address given
3105 if (lnum == MAXLNUM)
3106 eap->addr_count = 0;
3107 }
3108 return OK;
3109}
3110
3111/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003112 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 * If there is a match advance "pp" to the argument and return TRUE.
3114 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003115 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003116checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003117 char_u **pp, // start of command
3118 char *cmd, // name of command
3119 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120{
3121 int i;
3122
3123 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003124 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 break;
Bram Moolenaar7a66a172020-10-16 19:56:12 +02003126 if (i >= len && !isalpha((*pp)[i]) && (*pp)[i] != '_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 {
3128 *pp = skipwhite(*pp + i);
3129 return TRUE;
3130 }
3131 return FALSE;
3132}
3133
3134/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003135 * Append "cmd" to the error message in IObuff.
3136 * Takes care of limiting the length and handling 0xa0, which would be
3137 * invisible otherwise.
3138 */
3139 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003140append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003141{
3142 char_u *s = cmd;
3143 char_u *d;
3144
3145 STRCAT(IObuff, ": ");
3146 d = IObuff + STRLEN(IObuff);
3147 while (*s != NUL && d - IObuff < IOSIZE - 7)
3148 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003149 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003150 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003151 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003152 STRCPY(d, "<a0>");
3153 d += 4;
3154 }
3155 else
3156 MB_COPY_CHAR(s, d);
3157 }
3158 *d = NUL;
3159}
3160
3161/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003162 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3163 * the name. Otherwise just return "start".
3164 */
3165 char_u *
3166skip_option_env_lead(char_u *start)
3167{
3168 char_u *name = start;
3169
3170 if (*start == '&')
3171 {
3172 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3173 name += 3;
3174 else
3175 name += 1;
3176 }
3177 else if (*start == '$')
3178 name += 1;
3179 return name;
3180}
3181
3182/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003184 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003185 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003186 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 *
3188 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3189 * assignment without "let". Sets eap->cmdidx to the command while returning
3190 * "eap->cmd".
3191 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 * Returns NULL for an ambiguous user command.
3193 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194 char_u *
3195find_ex_command(
3196 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003197 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003198 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003199 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200{
3201 int len;
3202 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003203 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205#ifdef FEAT_EVAL
3206 /*
3207 * Recognize a Vim9 script function/method call and assignment:
3208 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3209 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003210 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003211 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003213 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214
Bram Moolenaar83144542020-08-02 20:40:43 +02003215 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003216 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003218 int oplen;
3219 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003221 if (
3222 // "(..." is an expression.
3223 // "funcname(" is always a function call.
3224 *p == '('
3225 || (p == eap->cmd
3226 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003227 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003228 *eap->cmd == '{'
3229 // "'string'->func()" is an expression.
3230 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003231 // '"string"->func()' is an expression.
3232 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003233 // "g:varname" is an expression.
3234 || eap->cmd[1] == ':'
3235 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003236 // "varname->func()" is an expression.
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003237 : (*p == '-' && p[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003238 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003239 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3240 {
3241 // "{" by itself is the start of a block.
3242 eap->cmdidx = CMD_block;
3243 return eap->cmd + 1;
3244 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003245 eap->cmdidx = CMD_eval;
3246 return eap->cmd;
3247 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003248
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003249 if (p != eap->cmd && (
3250 // "varname[]" is an expression.
3251 *p == '['
3252 // "varname.key" is an expression.
3253 || (*p == '.' && ASCII_ISALPHA(p[1]))))
3254 {
3255 char_u *after = p;
3256
3257 // When followed by "=" or "+=" then it is an assignment.
3258 ++emsg_silent;
3259 if (skip_expr(&after) == OK
3260 && (*after == '='
3261 || (*after != NUL && after[1] == '=')))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003262 eap->cmdidx = CMD_var;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003263 else
3264 eap->cmdidx = CMD_eval;
3265 --emsg_silent;
3266 return eap->cmd;
3267 }
3268
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003269 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3270 // an assignment.
3271 // If there is no line break inside the "[...]" then "p" is
3272 // advanced to after the "]" by to_name_const_end(): check if a "="
3273 // follows.
3274 // If "[...]" has a line break "p" still points at the "[" and it
3275 // can't be an assignment.
3276 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003277 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003278 p = to_name_const_end(eap->cmd);
3279 if (p == eap->cmd || *skipwhite(p) != '=')
3280 {
3281 eap->cmdidx = CMD_eval;
3282 return eap->cmd;
3283 }
3284 if (p > eap->cmd && *skipwhite(p) == '=')
3285 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003286 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003287 return eap->cmd;
3288 }
3289 }
3290
3291 // Recognize an assignment if we recognize the variable name:
3292 // "g:var = expr"
3293 // "var = expr" where "var" is a local var name.
Bram Moolenaar83144542020-08-02 20:40:43 +02003294 if (*eap->cmd == '@')
3295 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003296 oplen = assignment_len(skipwhite(p), &heredoc);
3297 if (oplen > 0)
3298 {
3299 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3300 || *eap->cmd == '&'
3301 || *eap->cmd == '$'
3302 || *eap->cmd == '@'
3303 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
3304 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003305 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003306 return eap->cmd;
3307 }
3308 }
3309
3310 // Recognize using a type for a w:, b:, t: or g: variable:
3311 // "w:varname: number = 123".
3312 if (eap->cmd[1] == ':' && *p == ':')
3313 {
3314 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003315 return eap->cmd;
3316 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003317 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 }
3319#endif
3320
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 /*
3322 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003323 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 * - the 'k' command can directly be followed by any character.
3325 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003326 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003328 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 */
3330 p = eap->cmd;
3331 if (*p == 'k')
3332 {
3333 eap->cmdidx = CMD_k;
3334 ++p;
3335 }
3336 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003337 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3338 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 || p[1] == 'g'
3340 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3341 || p[1] == 'I'
3342 || (p[1] == 'r' && p[2] != 'e')))
3343 {
3344 eap->cmdidx = CMD_substitute;
3345 ++p;
3346 }
3347 else
3348 {
3349 while (ASCII_ISALPHA(*p))
3350 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003351 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003352 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003354 while (ASCII_ISALNUM(*p))
3355 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 }
3357 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3358 {
3359 // include "9" for "vim9script"
3360 ++p;
3361 while (ASCII_ISALPHA(*p))
3362 ++p;
3363 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003364
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003365 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003366 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 ++p;
3368 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003369 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3370 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003371 // Check for ":dl", ":dell", etc. to ":deletel": that's
3372 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003373 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003374 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003375 break;
3376 if (i == len - 1)
3377 {
3378 --len;
3379 if (p[-1] == 'l')
3380 eap->flags |= EXFLAG_LIST;
3381 else
3382 eap->flags |= EXFLAG_PRINT;
3383 }
3384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003386 if (ASCII_ISLOWER(eap->cmd[0]))
3387 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003388 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003389 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003390
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003391 if (command_count != (int)CMD_SIZE)
3392 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003393 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003394 getout(1);
3395 }
3396
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003397 // Use a precomputed index for fast look-up in cmdnames[]
3398 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003399 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3400 if (ASCII_ISLOWER(c2))
3401 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3402 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003403 else if (ASCII_ISUPPER(eap->cmd[0]))
3404 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003406 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407
3408 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3409 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3410 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3411 (size_t)len) == 0)
3412 {
3413#ifdef FEAT_EVAL
3414 if (full != NULL
3415 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3416 *full = TRUE;
3417#endif
3418 break;
3419 }
3420
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003421 // Look for a user defined command as a last resort. Let ":Print" be
3422 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003423 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3424 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003426 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 while (ASCII_ISALNUM(*p))
3428 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003429 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003431 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 eap->cmdidx = CMD_SIZE;
3433 }
3434
Bram Moolenaar373863e2020-09-26 17:20:53 +02003435 // ":fina" means ":finally" for backwards compatibility.
3436 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3437 eap->cmdidx = CMD_finally;
3438
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 return p;
3440}
3441
3442#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003443static struct cmdmod
3444{
3445 char *name;
3446 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003447 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003448} cmdmods[] = {
3449 {"aboveleft", 3, FALSE},
3450 {"belowright", 3, FALSE},
3451 {"botright", 2, FALSE},
3452 {"browse", 3, FALSE},
3453 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003454 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003455 {"hide", 3, FALSE},
3456 {"keepalt", 5, FALSE},
3457 {"keepjumps", 5, FALSE},
3458 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003459 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003460 {"leftabove", 5, FALSE},
3461 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003462 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003463 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003464 {"rightbelow", 6, FALSE},
3465 {"sandbox", 3, FALSE},
3466 {"silent", 3, FALSE},
3467 {"tab", 3, TRUE},
3468 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003469 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003470 {"verbose", 4, TRUE},
3471 {"vertical", 4, FALSE},
3472};
3473
3474/*
3475 * Return length of a command modifier (including optional count).
3476 * Return zero when it's not a modifier.
3477 */
3478 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003479modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003480{
3481 int i, j;
3482 char_u *p = cmd;
3483
3484 if (VIM_ISDIGIT(*cmd))
3485 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003486 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003487 {
3488 for (j = 0; p[j] != NUL; ++j)
3489 if (p[j] != cmdmods[i].name[j])
3490 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003491 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003492 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003493 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003494 }
3495 return 0;
3496}
3497
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498/*
3499 * Return > 0 if an Ex command "name" exists.
3500 * Return 2 if there is an exact match.
3501 * Return 3 if there is an ambiguous match.
3502 */
3503 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003504cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505{
3506 exarg_T ea;
3507 int full = FALSE;
3508 int i;
3509 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003510 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003512 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003513 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 {
3515 for (j = 0; name[j] != NUL; ++j)
3516 if (name[j] != cmdmods[i].name[j])
3517 break;
3518 if (name[j] == NUL && j >= cmdmods[i].minlen)
3519 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3520 }
3521
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003522 // Check built-in commands and user defined commands.
3523 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003524 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003527 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003529 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3530 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003531 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003532 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3534}
3535#endif
3536
Bram Moolenaard0190392019-08-23 21:17:35 +02003537 cmdidx_T
3538excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539{
Bram Moolenaard0190392019-08-23 21:17:35 +02003540 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541
Bram Moolenaard0190392019-08-23 21:17:35 +02003542 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3543 idx = (cmdidx_T)((int)idx + 1))
3544 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 break;
3546
Bram Moolenaard0190392019-08-23 21:17:35 +02003547 return idx;
3548}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549
Bram Moolenaard0190392019-08-23 21:17:35 +02003550 long
3551excmd_get_argt(cmdidx_T idx)
3552{
3553 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554}
3555
3556/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003557 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 *
3559 * Backslashed delimiters after / or ? will be skipped, and commands will
3560 * not be expanded between /'s and ?'s or after "'".
3561 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003562 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 * Returns the "cmd" pointer advanced to beyond the range.
3564 */
3565 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003566skip_range(
3567 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003568 int skip_star, // skip "*" used for Visual range
3569 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003571 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003573 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003575 if (*cmd == '\\')
3576 {
3577 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3578 ++cmd;
3579 else
3580 break;
3581 }
3582 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 {
3584 if (*++cmd == NUL && ctx != NULL)
3585 *ctx = EXPAND_NOTHING;
3586 }
3587 else if (*cmd == '/' || *cmd == '?')
3588 {
3589 delim = *cmd++;
3590 while (*cmd != NUL && *cmd != delim)
3591 if (*cmd++ == '\\' && *cmd != NUL)
3592 ++cmd;
3593 if (*cmd == NUL && ctx != NULL)
3594 *ctx = EXPAND_NOTHING;
3595 }
3596 if (*cmd != NUL)
3597 ++cmd;
3598 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003599
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003600 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003601 while (*cmd == ':')
3602 cmd = skipwhite(cmd + 1);
3603
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003604 // Skip "*" used for Visual range.
3605 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3606 cmd = skipwhite(cmd + 1);
3607
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 return cmd;
3609}
3610
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003611 static void
3612addr_error(cmd_addr_T addr_type)
3613{
3614 if (addr_type == ADDR_NONE)
3615 emsg(_(e_norange));
3616 else
3617 emsg(_(e_invrange));
3618}
3619
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003621 * Return the default address for an address type.
3622 */
3623 static linenr_T
3624default_address(exarg_T *eap)
3625{
3626 linenr_T lnum = 0;
3627
3628 switch (eap->addr_type)
3629 {
3630 case ADDR_LINES:
3631 case ADDR_OTHER:
3632 // Default is the cursor line number. Avoid using an invalid
3633 // line number though.
3634 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3635 lnum = curbuf->b_ml.ml_line_count;
3636 else
3637 lnum = curwin->w_cursor.lnum;
3638 break;
3639 case ADDR_WINDOWS:
3640 lnum = CURRENT_WIN_NR;
3641 break;
3642 case ADDR_ARGUMENTS:
3643 lnum = curwin->w_arg_idx + 1;
3644 if (lnum > ARGCOUNT)
3645 lnum = ARGCOUNT;
3646 break;
3647 case ADDR_LOADED_BUFFERS:
3648 case ADDR_BUFFERS:
3649 lnum = curbuf->b_fnum;
3650 break;
3651 case ADDR_TABS:
3652 lnum = CURRENT_TAB_NR;
3653 break;
3654 case ADDR_TABS_RELATIVE:
3655 case ADDR_UNSIGNED:
3656 lnum = 1;
3657 break;
3658 case ADDR_QUICKFIX:
3659#ifdef FEAT_QUICKFIX
3660 lnum = qf_get_cur_idx(eap);
3661#endif
3662 break;
3663 case ADDR_QUICKFIX_VALID:
3664#ifdef FEAT_QUICKFIX
3665 lnum = qf_get_cur_valid_idx(eap);
3666#endif
3667 break;
3668 case ADDR_NONE:
3669 // Will give an error later if a range is found.
3670 break;
3671 }
3672 return lnum;
3673}
3674
3675/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003676 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 *
3678 * Set ptr to the next character after the part that was interpreted.
3679 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003680 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 *
3682 * Return MAXLNUM when no Ex address was found.
3683 */
3684 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003685get_address(
3686 exarg_T *eap UNUSED,
3687 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003688 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003689 int skip, // only skip the address, don't use it
3690 int silent, // no errors or side effects
3691 int to_other_file, // flag: may jump to other file
3692 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693{
3694 int c;
3695 int i;
3696 long n;
3697 char_u *cmd;
3698 pos_T pos;
3699 pos_T *fp;
3700 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003701 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702
3703 cmd = skipwhite(*ptr);
3704 lnum = MAXLNUM;
3705 do
3706 {
3707 switch (*cmd)
3708 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003709 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003710 ++cmd;
3711 switch (addr_type)
3712 {
3713 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003714 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 lnum = curwin->w_cursor.lnum;
3716 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003717 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003718 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003719 break;
3720 case ADDR_ARGUMENTS:
3721 lnum = curwin->w_arg_idx + 1;
3722 break;
3723 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003724 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003725 lnum = curbuf->b_fnum;
3726 break;
3727 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003728 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003729 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003730 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003731 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003732 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003733 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003734 cmd = NULL;
3735 goto error;
3736 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003737 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003738#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003739 lnum = qf_get_cur_idx(eap);
3740#endif
3741 break;
3742 case ADDR_QUICKFIX_VALID:
3743#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003744 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003745#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003746 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003747 }
3748 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003750 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003751 ++cmd;
3752 switch (addr_type)
3753 {
3754 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003755 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 lnum = curbuf->b_ml.ml_line_count;
3757 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003758 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003759 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003760 break;
3761 case ADDR_ARGUMENTS:
3762 lnum = ARGCOUNT;
3763 break;
3764 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003765 buf = lastbuf;
3766 while (buf->b_ml.ml_mfp == NULL)
3767 {
3768 if (buf->b_prev == NULL)
3769 break;
3770 buf = buf->b_prev;
3771 }
3772 lnum = buf->b_fnum;
3773 break;
3774 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003775 lnum = lastbuf->b_fnum;
3776 break;
3777 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003778 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003779 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003780 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003781 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003782 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003783 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003784 cmd = NULL;
3785 goto error;
3786 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003787 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003788#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003789 lnum = qf_get_size(eap);
3790 if (lnum == 0)
3791 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003792#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003793 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003794 case ADDR_QUICKFIX_VALID:
3795#ifdef FEAT_QUICKFIX
3796 lnum = qf_get_valid_size(eap);
3797 if (lnum == 0)
3798 lnum = 1;
3799#endif
3800 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003801 }
3802 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003804 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003805 if (*++cmd == NUL)
3806 {
3807 cmd = NULL;
3808 goto error;
3809 }
3810 if (addr_type != ADDR_LINES)
3811 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003812 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003813 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003814 goto error;
3815 }
3816 if (skip)
3817 ++cmd;
3818 else
3819 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003820 // Only accept a mark in another file when it is
3821 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003822 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3823 ++cmd;
3824 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003825 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003826 lnum = curwin->w_cursor.lnum;
3827 else
3828 {
3829 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 {
3831 cmd = NULL;
3832 goto error;
3833 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003834 lnum = fp->lnum;
3835 }
3836 }
3837 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838
3839 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003840 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003841 c = *cmd++;
3842 if (addr_type != ADDR_LINES)
3843 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003844 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003845 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003846 goto error;
3847 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003848 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003849 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003850 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003851 if (*cmd == c)
3852 ++cmd;
3853 }
3854 else
3855 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003856 int flags;
3857
3858 pos = curwin->w_cursor; // save curwin->w_cursor
3859
3860 // When '/' or '?' follows another address, start from
3861 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003862 if (lnum != MAXLNUM)
3863 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003864
3865 // Start a forward search at the end of the line (unless
3866 // before the first line).
3867 // Start a backward search at the start of the line.
3868 // This makes sure we never match in the current
3869 // line, and can match anywhere in the
3870 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003871 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003872 curwin->w_cursor.col = MAXCOL;
3873 else
3874 curwin->w_cursor.col = 0;
3875 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003876 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003877 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003878 {
3879 curwin->w_cursor = pos;
3880 cmd = NULL;
3881 goto error;
3882 }
3883 lnum = curwin->w_cursor.lnum;
3884 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003885 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003886 cmd += searchcmdlen;
3887 }
3888 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003890 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003891 ++cmd;
3892 if (addr_type != ADDR_LINES)
3893 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003894 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003895 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003896 goto error;
3897 }
3898 if (*cmd == '&')
3899 i = RE_SUBST;
3900 else if (*cmd == '?' || *cmd == '/')
3901 i = RE_SEARCH;
3902 else
3903 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003904 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003905 cmd = NULL;
3906 goto error;
3907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003909 if (!skip)
3910 {
3911 /*
3912 * When search follows another address, start from
3913 * there.
3914 */
3915 if (lnum != MAXLNUM)
3916 pos.lnum = lnum;
3917 else
3918 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003920 /*
3921 * Start the search just like for the above
3922 * do_search().
3923 */
3924 if (*cmd != '?')
3925 pos.col = MAXCOL;
3926 else
3927 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003928 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003929 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003930 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003931 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003932 lnum = pos.lnum;
3933 else
3934 {
3935 cmd = NULL;
3936 goto error;
3937 }
3938 }
3939 ++cmd;
3940 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941
3942 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003943 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003944 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 }
3946
3947 for (;;)
3948 {
3949 cmd = skipwhite(cmd);
3950 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3951 break;
3952
3953 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003954 {
3955 switch (addr_type)
3956 {
3957 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003958 case ADDR_OTHER:
3959 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003960 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003961 break;
3962 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003963 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003964 break;
3965 case ADDR_ARGUMENTS:
3966 lnum = curwin->w_arg_idx + 1;
3967 break;
3968 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003969 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003970 lnum = curbuf->b_fnum;
3971 break;
3972 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003973 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003974 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003975 case ADDR_TABS_RELATIVE:
3976 lnum = 1;
3977 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003978 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003979#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003980 lnum = qf_get_cur_idx(eap);
3981#endif
3982 break;
3983 case ADDR_QUICKFIX_VALID:
3984#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003985 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003986#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003987 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003988 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003989 case ADDR_UNSIGNED:
3990 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003991 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003992 }
3993 }
3994
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003996 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 else
3998 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003999 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 n = 1;
4001 else
4002 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004003
4004 if (addr_type == ADDR_TABS_RELATIVE)
4005 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004006 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004007 cmd = NULL;
4008 goto error;
4009 }
4010 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004011 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004012 lnum = compute_buffer_local_count(
4013 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004015 {
4016#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004017 // Relative line addressing, need to adjust for folded lines
4018 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004019 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4020 && address_count >= 2)
4021 (void)hasFolding(lnum, NULL, &lnum);
4022#endif
4023 if (i == '-')
4024 lnum -= n;
4025 else
4026 lnum += n;
4027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 }
4029 } while (*cmd == '/' || *cmd == '?');
4030
4031error:
4032 *ptr = cmd;
4033 return lnum;
4034}
4035
4036/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004037 * Set eap->line1 and eap->line2 to the whole range.
4038 * Used for commands with the EX_DFLALL flag and no range given.
4039 */
4040 static void
4041address_default_all(exarg_T *eap)
4042{
4043 eap->line1 = 1;
4044 switch (eap->addr_type)
4045 {
4046 case ADDR_LINES:
4047 case ADDR_OTHER:
4048 eap->line2 = curbuf->b_ml.ml_line_count;
4049 break;
4050 case ADDR_LOADED_BUFFERS:
4051 {
4052 buf_T *buf = firstbuf;
4053
4054 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4055 buf = buf->b_next;
4056 eap->line1 = buf->b_fnum;
4057 buf = lastbuf;
4058 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4059 buf = buf->b_prev;
4060 eap->line2 = buf->b_fnum;
4061 }
4062 break;
4063 case ADDR_BUFFERS:
4064 eap->line1 = firstbuf->b_fnum;
4065 eap->line2 = lastbuf->b_fnum;
4066 break;
4067 case ADDR_WINDOWS:
4068 eap->line2 = LAST_WIN_NR;
4069 break;
4070 case ADDR_TABS:
4071 eap->line2 = LAST_TAB_NR;
4072 break;
4073 case ADDR_TABS_RELATIVE:
4074 eap->line2 = 1;
4075 break;
4076 case ADDR_ARGUMENTS:
4077 if (ARGCOUNT == 0)
4078 eap->line1 = eap->line2 = 0;
4079 else
4080 eap->line2 = ARGCOUNT;
4081 break;
4082 case ADDR_QUICKFIX_VALID:
4083#ifdef FEAT_QUICKFIX
4084 eap->line2 = qf_get_valid_size(eap);
4085 if (eap->line2 == 0)
4086 eap->line2 = 1;
4087#endif
4088 break;
4089 case ADDR_NONE:
4090 case ADDR_UNSIGNED:
4091 case ADDR_QUICKFIX:
4092 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4093 break;
4094 }
4095}
4096
4097
4098/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004099 * Get flags from an Ex command argument.
4100 */
4101 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004102get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004103{
4104 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4105 {
4106 if (*eap->arg == 'l')
4107 eap->flags |= EXFLAG_LIST;
4108 else if (*eap->arg == 'p')
4109 eap->flags |= EXFLAG_PRINT;
4110 else
4111 eap->flags |= EXFLAG_NR;
4112 eap->arg = skipwhite(eap->arg + 1);
4113 }
4114}
4115
4116/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 * Function called for command which is Not Implemented. NI!
4118 */
4119 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004120ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121{
4122 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004123 eap->errmsg =
4124 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125}
4126
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004127#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128/*
4129 * Function called for script command which is Not Implemented. NI!
4130 * Skips over ":perl <<EOF" constructs.
4131 */
4132 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004133ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134{
4135 if (!eap->skip)
4136 ex_ni(eap);
4137 else
4138 vim_free(script_get(eap, eap->arg));
4139}
4140#endif
4141
4142/*
4143 * Check range in Ex command for validity.
4144 * Return NULL when valid, error message when invalid.
4145 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004146 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004147invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004149 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004150
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 if ( eap->line1 < 0
4152 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004153 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004154 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004155
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004156 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004157 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004158 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004159 {
4160 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004161 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004162#ifdef FEAT_DIFF
4163 + (eap->cmdidx == CMD_diffget)
4164#endif
4165 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004166 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004167 break;
4168 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004169 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004170 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004171 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004172 break;
4173 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004174 // Only a boundary check, not whether the buffers actually
4175 // exist.
4176 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004177 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004178 break;
4179 case ADDR_LOADED_BUFFERS:
4180 buf = firstbuf;
4181 while (buf->b_ml.ml_mfp == NULL)
4182 {
4183 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004184 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004185 buf = buf->b_next;
4186 }
4187 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004188 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004189 buf = lastbuf;
4190 while (buf->b_ml.ml_mfp == NULL)
4191 {
4192 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004193 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004194 buf = buf->b_prev;
4195 }
4196 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004197 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004198 break;
4199 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004200 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004201 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004202 break;
4203 case ADDR_TABS:
4204 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004205 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004206 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004207 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004208 case ADDR_OTHER:
4209 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004210 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004211 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004212#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004213 // No error for value that is too big, will use the last entry.
4214 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004215 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004216#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004217 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004218 case ADDR_QUICKFIX_VALID:
4219#ifdef FEAT_QUICKFIX
4220 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4221 || eap->line2 < 0)
4222 return _(e_invrange);
4223#endif
4224 break;
4225 case ADDR_UNSIGNED:
4226 if (eap->line2 < 0)
4227 return _(e_invrange);
4228 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004229 case ADDR_NONE:
4230 // Will give an error elsewhere.
4231 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004232 }
4233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 return NULL;
4235}
4236
4237/*
4238 * Correct the range for zero line number, if required.
4239 */
4240 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004241correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004243 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 {
4245 if (eap->line1 == 0)
4246 eap->line1 = 1;
4247 if (eap->line2 == 0)
4248 eap->line2 = 1;
4249 }
4250}
4251
Bram Moolenaar748bf032005-02-02 23:04:36 +00004252#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004253/*
4254 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4255 * pattern. Otherwise return eap->arg.
4256 */
4257 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004258skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004259{
4260 char_u *p = eap->arg;
4261
Bram Moolenaara37420f2006-02-04 22:37:47 +00004262 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4263 || eap->cmdidx == CMD_vimgrepadd
4264 || eap->cmdidx == CMD_lvimgrepadd
4265 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004266 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004267 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004268 if (p == NULL)
4269 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004270 }
4271 return p;
4272}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004273
4274/*
4275 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4276 * in the command line, so that things like % get expanded.
4277 */
4278 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004279replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004280{
4281 char_u *new_cmdline;
4282 char_u *program;
4283 char_u *pos;
4284 char_u *ptr;
4285 int len;
4286 int i;
4287
4288 /*
4289 * Don't do it when ":vimgrep" is used for ":grep".
4290 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004291 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4292 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4293 || eap->cmdidx == CMD_grepadd
4294 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004295 && !grep_internal(eap->cmdidx))
4296 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004297 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4298 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004299 {
4300 if (*curbuf->b_p_gp == NUL)
4301 program = p_gp;
4302 else
4303 program = curbuf->b_p_gp;
4304 }
4305 else
4306 {
4307 if (*curbuf->b_p_mp == NUL)
4308 program = p_mp;
4309 else
4310 program = curbuf->b_p_mp;
4311 }
4312
4313 p = skipwhite(p);
4314
4315 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4316 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004317 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004318 i = 1;
4319 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4320 ++i;
4321 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004322 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004323 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004324 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004325 ptr = new_cmdline;
4326 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4327 {
4328 i = (int)(pos - program);
4329 STRNCPY(ptr, program, i);
4330 STRCPY(ptr += i, p);
4331 ptr += len;
4332 program = pos + 2;
4333 }
4334 STRCPY(ptr, program);
4335 }
4336 else
4337 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004338 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004339 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004340 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004341 STRCPY(new_cmdline, program);
4342 STRCAT(new_cmdline, " ");
4343 STRCAT(new_cmdline, p);
4344 }
4345 msg_make(p);
4346
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004347 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004348 vim_free(*cmdlinep);
4349 *cmdlinep = new_cmdline;
4350 p = new_cmdline;
4351 }
4352 return p;
4353}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004354#endif
4355
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356/*
4357 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004358 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 * Return FAIL for failure, OK otherwise.
4360 */
4361 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004362expand_filename(
4363 exarg_T *eap,
4364 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004365 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004367 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 char_u *repl;
4369 int srclen;
4370 char_u *p;
4371 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004372 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373
Bram Moolenaar748bf032005-02-02 23:04:36 +00004374#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004375 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004376 p = skip_grep_pat(eap);
4377#else
4378 p = eap->arg;
4379#endif
4380
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 /*
4382 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4383 * the file name contains a wildcard it should not cause expanding.
4384 * (it will be expanded anyway if there is a wildcard before replacing).
4385 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004386 has_wildcards = mch_has_wildcard(p);
4387 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004389#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004390 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004391 if (p[0] == '`' && p[1] == '=')
4392 {
4393 p += 2;
4394 (void)skip_expr(&p);
4395 if (*p == '`')
4396 ++p;
4397 continue;
4398 }
4399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 /*
4401 * Quick check if this cannot be the start of a special string.
4402 * Also removes backslash before '%', '#' and '<'.
4403 */
4404 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4405 {
4406 ++p;
4407 continue;
4408 }
4409
4410 /*
4411 * Try to find a match at this position.
4412 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004413 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4414 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004415 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004417 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 {
4419 p += srclen;
4420 continue;
4421 }
4422
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004423 // Wildcards won't be expanded below, the replacement is taken
4424 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004425 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4426 {
4427 char_u *l = repl;
4428
4429 repl = expand_env_save(repl);
4430 vim_free(l);
4431 }
4432
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004433 // Need to escape white space et al. with a backslash.
4434 // Don't do this for:
4435 // - replacement that already has been escaped: "##"
4436 // - shell commands (may have to use quotes instead).
4437 // - non-unix systems when there is a single argument (spaces don't
4438 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004440 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 && eap->cmdidx != CMD_grep
4443 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004444 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004445 && eap->cmdidx != CMD_lgrep
4446 && eap->cmdidx != CMD_lgrepadd
4447 && eap->cmdidx != CMD_lmake
4448 && eap->cmdidx != CMD_make
4449 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004451 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452#endif
4453 )
4454 {
4455 char_u *l;
4456#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004457 // Don't escape a backslash here, because rem_backslash() doesn't
4458 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 static char_u *nobslash = (char_u *)" \t\"|";
4460# define ESCAPE_CHARS nobslash
4461#else
4462# define ESCAPE_CHARS escape_chars
4463#endif
4464
4465 for (l = repl; *l; ++l)
4466 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4467 {
4468 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4469 if (l != NULL)
4470 {
4471 vim_free(repl);
4472 repl = l;
4473 }
4474 break;
4475 }
4476 }
4477
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004478 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004479 if ((eap->usefilter || eap->cmdidx == CMD_bang
4480 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004481 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 {
4483 char_u *l;
4484
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004485 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 if (l != NULL)
4487 {
4488 vim_free(repl);
4489 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 }
4491 }
4492
4493 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4494 vim_free(repl);
4495 if (p == NULL)
4496 return FAIL;
4497 }
4498
4499 /*
4500 * One file argument: Expand wildcards.
4501 * Don't do this with ":r !command" or ":w !command".
4502 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004503 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 {
4505 /*
4506 * May do this twice:
4507 * 1. Replace environment variables.
4508 * 2. Replace any other wildcards, remove backslashes.
4509 */
4510 for (n = 1; n <= 2; ++n)
4511 {
4512 if (n == 2)
4513 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 /*
4515 * Halve the number of backslashes (this is Vi compatible).
4516 * For Unix and OS/2, when wildcards are expanded, this is
4517 * done by ExpandOne() below.
4518 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004519#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 if (!has_wildcards)
4521#endif
4522 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 }
4524
4525 if (has_wildcards)
4526 {
4527 if (n == 1)
4528 {
4529 /*
4530 * First loop: May expand environment variables. This
4531 * can be done much faster with expand_env() than with
4532 * something else (e.g., calling a shell).
4533 * After expanding environment variables, check again
4534 * if there are still wildcards present.
4535 */
4536 if (vim_strchr(eap->arg, '$') != NULL
4537 || vim_strchr(eap->arg, '~') != NULL)
4538 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004539 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004540 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 has_wildcards = mch_has_wildcard(NameBuff);
4542 p = NameBuff;
4543 }
4544 else
4545 p = NULL;
4546 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004547 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 {
4549 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004550 int options = WILD_LIST_NOTFOUND
4551 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552
4553 ExpandInit(&xpc);
4554 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004555 if (p_wic)
4556 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004558 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 if (p == NULL)
4560 return FAIL;
4561 }
4562 if (p != NULL)
4563 {
4564 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4565 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004566 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 vim_free(p);
4568 }
4569 }
4570 }
4571 }
4572 return OK;
4573}
4574
4575/*
4576 * Replace part of the command line, keeping eap->cmd, eap->arg and
4577 * eap->nextcmd correct.
4578 * "src" points to the part that is to be replaced, of length "srclen".
4579 * "repl" is the replacement string.
4580 * Returns a pointer to the character after the replaced string.
4581 * Returns NULL for failure.
4582 */
4583 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004584repl_cmdline(
4585 exarg_T *eap,
4586 char_u *src,
4587 int srclen,
4588 char_u *repl,
4589 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590{
4591 int len;
4592 int i;
4593 char_u *new_cmdline;
4594
4595 /*
4596 * The new command line is build in new_cmdline[].
4597 * First allocate it.
4598 * Careful: a "+cmd" argument may have been NUL terminated.
4599 */
4600 len = (int)STRLEN(repl);
4601 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004602 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004603 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004604 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004605 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606
4607 /*
4608 * Copy the stuff before the expanded part.
4609 * Copy the expanded stuff.
4610 * Copy what came after the expanded part.
4611 * Copy the next commands, if there are any.
4612 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004613 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004615
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004617 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004619 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004621 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 {
4623 i = (int)STRLEN(new_cmdline) + 1;
4624 STRCPY(new_cmdline + i, eap->nextcmd);
4625 eap->nextcmd = new_cmdline + i;
4626 }
4627 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4628 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4629 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4630 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4631 vim_free(*cmdlinep);
4632 *cmdlinep = new_cmdline;
4633
4634 return src;
4635}
4636
4637/*
4638 * Check for '|' to separate commands and '"' to start comments.
4639 */
4640 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004641separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642{
4643 char_u *p;
4644
Bram Moolenaar86b68352004-12-27 21:59:20 +00004645#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004646 p = skip_grep_pat(eap);
4647#else
4648 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004649#endif
4650
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004651 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 {
4653 if (*p == Ctrl_V)
4654 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004655 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004656 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004658 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004659 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004660 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 break;
4662 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004663
4664#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004665 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004666 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004667 {
4668 p += 2;
4669 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004670 }
4671#endif
4672
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004673 // Check for '"': start of comment or '|': next command
4674 // :@" and :*" do not start a comment!
4675 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004676 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004677#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004678 && !in_vim9script()
4679#endif
4680 && !(eap->argt & EX_NOTRLCOM)
4681 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4682 || p != eap->arg)
4683 && (eap->cmdidx != CMD_redir
4684 || p != eap->arg + 1 || p[-1] != '@'))
4685#ifdef FEAT_EVAL
4686 || (*p == '#'
4687 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004688 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004689 && p[1] != '{'
4690 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 || *p == '|' || *p == '\n')
4693 {
4694 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004695 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 * AND 'b' is present in 'cpoptions'.
4697 */
4698 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004699 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004701 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 --p;
4703 }
4704 else
4705 {
4706 eap->nextcmd = check_nextcmd(p);
4707 *p = NUL;
4708 break;
4709 }
4710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004712
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004713 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 del_trailing_spaces(eap->arg);
4715}
4716
4717/*
4718 * get + command from ex argument
4719 */
4720 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004721getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722{
4723 char_u *arg = *argp;
4724 char_u *command = NULL;
4725
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004726 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 {
4728 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004729 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 command = dollar_command;
4731 else
4732 {
4733 command = arg;
4734 arg = skip_cmd_arg(command, TRUE);
4735 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004736 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 }
4738
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004739 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 *argp = arg;
4741 }
4742 return command;
4743}
4744
4745/*
4746 * Find end of "+command" argument. Skip over "\ " and "\\".
4747 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004748 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004749skip_cmd_arg(
4750 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004751 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752{
4753 while (*p && !vim_isspace(*p))
4754 {
4755 if (*p == '\\' && p[1] != NUL)
4756 {
4757 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004758 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 else
4760 ++p;
4761 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004762 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 }
4764 return p;
4765}
4766
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004767 int
4768get_bad_opt(char_u *p, exarg_T *eap)
4769{
4770 if (STRICMP(p, "keep") == 0)
4771 eap->bad_char = BAD_KEEP;
4772 else if (STRICMP(p, "drop") == 0)
4773 eap->bad_char = BAD_DROP;
4774 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4775 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004776 else
4777 return FAIL;
4778 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004779}
4780
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781/*
4782 * Get "++opt=arg" argument.
4783 * Return FAIL or OK.
4784 */
4785 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004786getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787{
4788 char_u *arg = eap->arg + 2;
4789 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004790 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004793 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4795 {
4796 if (*arg == 'n')
4797 {
4798 arg += 2;
4799 eap->force_bin = FORCE_NOBIN;
4800 }
4801 else
4802 eap->force_bin = FORCE_BIN;
4803 if (!checkforcmd(&arg, "binary", 3))
4804 return FAIL;
4805 eap->arg = skipwhite(arg);
4806 return OK;
4807 }
4808
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004809 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004810 if (STRNCMP(arg, "edit", 4) == 0)
4811 {
4812 eap->read_edit = TRUE;
4813 eap->arg = skipwhite(arg + 4);
4814 return OK;
4815 }
4816
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 if (STRNCMP(arg, "ff", 2) == 0)
4818 {
4819 arg += 2;
4820 pp = &eap->force_ff;
4821 }
4822 else if (STRNCMP(arg, "fileformat", 10) == 0)
4823 {
4824 arg += 10;
4825 pp = &eap->force_ff;
4826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 else if (STRNCMP(arg, "enc", 3) == 0)
4828 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004829 if (STRNCMP(arg, "encoding", 8) == 0)
4830 arg += 8;
4831 else
4832 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 pp = &eap->force_enc;
4834 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004835 else if (STRNCMP(arg, "bad", 3) == 0)
4836 {
4837 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004838 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840
4841 if (pp == NULL || *arg != '=')
4842 return FAIL;
4843
4844 ++arg;
4845 *pp = (int)(arg - eap->cmd);
4846 arg = skip_cmd_arg(arg, FALSE);
4847 eap->arg = skipwhite(arg);
4848 *arg = NUL;
4849
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 if (pp == &eap->force_ff)
4851 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4853 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004854 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004856 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004858 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4860 *p = TOLOWER_ASC(*p);
4861 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004862 else
4863 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004864 // Check ++bad= argument. Must be a single-byte character, "keep" or
4865 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004866 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004867 return FAIL;
4868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869
4870 return OK;
4871}
4872
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004874ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875{
4876 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004877 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 * directory for security reasons.
4879 */
4880 if (secure)
4881 {
4882 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004883 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 }
4885 else if (eap->cmdidx == CMD_autocmd)
4886 do_autocmd(eap->arg, eap->forceit);
4887 else
4888 do_augroup(eap->arg, eap->forceit);
4889}
4890
4891/*
4892 * ":doautocmd": Apply the automatic commands to the current buffer.
4893 */
4894 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004895ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004897 char_u *arg = eap->arg;
4898 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004899 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004900
Bram Moolenaar1610d052016-06-09 22:53:01 +02004901 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004902 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004903 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004904 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907/*
4908 * :[N]bunload[!] [N] [bufname] unload buffer
4909 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4910 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4911 */
4912 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004913ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004915 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004916 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 eap->errmsg = do_bufdel(
4918 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4919 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4920 : DOBUF_UNLOAD, eap->arg,
4921 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4922}
4923
4924/*
4925 * :[N]buffer [N] to buffer N
4926 * :[N]sbuffer [N] to buffer N
4927 */
4928 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004929ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004931 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004932 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004934 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 else
4936 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004937 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4939 else
4940 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004941 if (eap->do_ecmd_cmd != NULL)
4942 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 }
4944}
4945
4946/*
4947 * :[N]bmodified [N] to next mod. buffer
4948 * :[N]sbmodified [N] to next mod. buffer
4949 */
4950 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004951ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952{
4953 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004954 if (eap->do_ecmd_cmd != NULL)
4955 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956}
4957
4958/*
4959 * :[N]bnext [N] to next buffer
4960 * :[N]sbnext [N] split and to next buffer
4961 */
4962 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004963ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004965 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004966 return;
4967
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004969 if (eap->do_ecmd_cmd != NULL)
4970 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971}
4972
4973/*
4974 * :[N]bNext [N] to previous buffer
4975 * :[N]bprevious [N] to previous buffer
4976 * :[N]sbNext [N] split and to previous buffer
4977 * :[N]sbprevious [N] split and to previous buffer
4978 */
4979 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004980ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004982 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004983 return;
4984
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004986 if (eap->do_ecmd_cmd != NULL)
4987 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988}
4989
4990/*
4991 * :brewind to first buffer
4992 * :bfirst to first buffer
4993 * :sbrewind split and to first buffer
4994 * :sbfirst split and to first buffer
4995 */
4996 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004997ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004999 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005000 return;
5001
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005003 if (eap->do_ecmd_cmd != NULL)
5004 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005}
5006
5007/*
5008 * :blast to last buffer
5009 * :sblast split and to last buffer
5010 */
5011 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005012ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005014 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005015 return;
5016
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005018 if (eap->do_ecmd_cmd != NULL)
5019 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021
Bram Moolenaar7a092242020-04-16 22:10:49 +02005022/*
5023 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02005024 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005027ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005029 int comment_char = '"';
5030
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005031#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005032 if (in_vim9script())
5033 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005034#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005035 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005036}
5037
5038/*
5039 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5040 * to "cmd_start" or has a white space character before it.
5041 */
5042 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005043ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005044{
5045 int c = *cmd;
5046
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005047 if (c == NUL || c == '|' || c == '\n')
5048 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005049#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005050 if (in_vim9script())
5051 return c == '#' && cmd[1] != '{'
5052 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005053#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005054 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055}
5056
5057#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5058 || defined(PROTO)
5059/*
5060 * Return the next command, after the first '|' or '\n'.
5061 * Return NULL if not found.
5062 */
5063 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005064find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065{
5066 while (*p != '|' && *p != '\n')
5067 {
5068 if (*p == NUL)
5069 return NULL;
5070 ++p;
5071 }
5072 return (p + 1);
5073}
5074#endif
5075
5076/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005077 * Check if *p is a separator between Ex commands, skipping over white space.
5078 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 */
5080 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005081check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005083 char_u *s = skipwhite(p);
5084
5085 if (*s == '|' || *s == '\n')
5086 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 else
5088 return NULL;
5089}
5090
5091/*
5092 * - if there are more files to edit
5093 * - and this is the last window
5094 * - and forceit not used
5095 * - and not repeated twice on a row
5096 * return FAIL and give error message if 'message' TRUE
5097 * return OK otherwise
5098 */
5099 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005100check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005101 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005102 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103{
5104 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5105
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005106 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005107 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 {
5109 if (message)
5110 {
5111#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5112 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5113 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005114 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005116 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5117 NGETTEXT("%d more file to edit. Quit anyway?",
5118 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5120 return OK;
5121 return FAIL;
5122 }
5123#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005124 semsg(NGETTEXT("E173: %d more file to edit",
5125 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005126 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
5128 return FAIL;
5129 }
5130 return OK;
5131}
5132
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133/*
5134 * Function given to ExpandGeneric() to obtain the list of command names.
5135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005137get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138{
5139 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 return cmdnames[idx].cmd_name;
5142}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005145ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146{
Bram Moolenaare6850792010-05-14 15:28:44 +02005147 if (*eap->arg == NUL)
5148 {
5149#ifdef FEAT_EVAL
5150 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5151 char_u *p = NULL;
5152
5153 if (expr != NULL)
5154 {
5155 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005156 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005157 --emsg_off;
5158 vim_free(expr);
5159 }
5160 if (p != NULL)
5161 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005162 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005163 vim_free(p);
5164 }
5165 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005166 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005167#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005168 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005169#endif
5170 }
5171 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005172 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005173
5174#ifdef FEAT_VTP
5175 else if (has_vtp_working())
5176 {
5177 // background color change requires clear + redraw
5178 update_screen(CLEAR);
5179 redrawcmd();
5180 }
5181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182}
5183
5184 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005185ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186{
5187 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005188 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 do_highlight(eap->arg, eap->forceit, FALSE);
5190}
5191
5192
5193/*
5194 * Call this function if we thought we were going to exit, but we won't
5195 * (because of an error). May need to restore the terminal mode.
5196 */
5197 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005198not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199{
5200 exiting = FALSE;
5201 settmode(TMODE_RAW);
5202}
5203
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005204 static int
5205before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5206{
5207 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5208
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005209 // Bail out when autocommands closed the window.
5210 // Refuse to quit when the buffer in the last window is being closed (can
5211 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005212 if (!win_valid(wp)
5213 || curbuf_locked()
5214 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5215 return TRUE;
5216
5217 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5218 {
5219 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005220 // Refuse to quit when locked or when the window was closed or the
5221 // buffer in the last window is being closed (can only happen in
5222 // autocommands).
5223 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005224 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5225 return TRUE;
5226 }
5227
5228 return FALSE;
5229}
5230
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005232 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005233 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005234 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005236 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005237ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005239 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005240
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241#ifdef FEAT_CMDWIN
5242 if (cmdwin_type != 0)
5243 {
5244 cmdwin_result = Ctrl_C;
5245 return;
5246 }
5247#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005248 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005249 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005250 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005251 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005252 return;
5253 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005254 if (eap->addr_count > 0)
5255 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005256 int wnr = eap->line2;
5257
5258 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5259 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005260 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005261 }
5262 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005263 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005264
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005265 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005266 if (curbuf_locked())
5267 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005268
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005269 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005270 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005271 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
5273#ifdef FEAT_NETBEANS_INTG
5274 netbeansForcedQuit = eap->forceit;
5275#endif
5276
5277 /*
5278 * If there are more files or windows we won't exit.
5279 */
5280 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5281 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005282 if ((!buf_hide(wp->w_buffer)
5283 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005284 | (eap->forceit ? CCGD_FORCEIT : 0)
5285 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005287 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 {
5289 not_exiting();
5290 }
5291 else
5292 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005293 // quit last window
5294 // Note: only_one_window() returns true, even so a help window is
5295 // still open. In that case only quit, if no address has been
5296 // specified. Example:
5297 // :h|wincmd w|1q - don't quit
5298 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005299 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005301 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005302#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005304#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005305 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005306 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 }
5308}
5309
5310/*
5311 * ":cquit".
5312 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005314ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005316 // this does not always pass on the exit code to the Manx compiler. why?
5317 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318}
5319
5320/*
5321 * ":qall": try to quit all windows
5322 */
5323 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005324ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325{
5326# ifdef FEAT_CMDWIN
5327 if (cmdwin_type != 0)
5328 {
5329 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005330 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 else
5332 cmdwin_result = K_XF2;
5333 return;
5334 }
5335# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005336
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005337 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005338 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005339 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005340 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005341 return;
5342 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005343
5344 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005345 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005346
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005348 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 getout(0);
5350 not_exiting();
5351}
5352
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353/*
5354 * ":close": close current window, unless it is the last one
5355 */
5356 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005357ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005359 win_T *win;
5360 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005361#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005363 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005365#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005366 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005367 {
5368 if (eap->addr_count == 0)
5369 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005370 else
5371 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005372 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005373 {
5374 winnr++;
5375 if (winnr == eap->line2)
5376 break;
5377 }
5378 if (win == NULL)
5379 win = lastwin;
5380 ex_win_close(eap->forceit, win, NULL);
5381 }
5382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383}
5384
Bram Moolenaar4033c552017-09-16 20:54:51 +02005385#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005386/*
5387 * ":pclose": Close any preview window.
5388 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005390ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005391{
5392 win_T *win;
5393
Bram Moolenaar79648732019-07-18 21:43:07 +02005394 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005395 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005396 if (win->w_p_pvw)
5397 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005398 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005399 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005400 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005401# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005402 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005403 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005404# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005405}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005406#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005407
Bram Moolenaarf740b292006-02-16 22:11:02 +00005408/*
5409 * Close window "win" and take care of handling closing the last window for a
5410 * modified buffer.
5411 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005412 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005413ex_win_close(
5414 int forceit,
5415 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005416 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417{
5418 int need_hide;
5419 buf_T *buf = win->w_buffer;
5420
Bram Moolenaarcf844172020-06-26 19:44:06 +02005421 // Never close the autocommand window.
5422 if (win == aucmd_win)
5423 {
5424 emsg(_(e_autocmd_close));
5425 return;
5426 }
5427
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005429 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005431#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 if ((p_confirm || cmdmod.confirm) && p_write)
5433 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005434 bufref_T bufref;
5435
5436 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005438 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 return;
5440 need_hide = FALSE;
5441 }
5442 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005445 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 return;
5447 }
5448 }
5449
Bram Moolenaar4033c552017-09-16 20:54:51 +02005450#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005452#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005453
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005454 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005455 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005456 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005457 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005458 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459}
5460
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005462 * Handle the argument for a tabpage related ex command.
5463 * Returns a tabpage number.
5464 * When an error is encountered then eap->errmsg is set.
5465 */
5466 static int
5467get_tabpage_arg(exarg_T *eap)
5468{
5469 int tab_number;
5470 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5471
5472 if (eap->arg && *eap->arg != NUL)
5473 {
5474 char_u *p = eap->arg;
5475 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005476 int relative = 0; // argument +N/-N means: go to N places to the
5477 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005478
5479 if (*p == '-')
5480 {
5481 relative = -1;
5482 p++;
5483 }
5484 else if (*p == '+')
5485 {
5486 relative = 1;
5487 p++;
5488 }
5489
5490 p_save = p;
5491 tab_number = getdigits(&p);
5492
5493 if (relative == 0)
5494 {
5495 if (STRCMP(p, "$") == 0)
5496 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005497 else if (STRCMP(p, "#") == 0)
5498 if (valid_tabpage(lastused_tabpage))
5499 tab_number = tabpage_index(lastused_tabpage);
5500 else
5501 {
5502 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5503 tab_number = 0;
5504 goto theend;
5505 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005506 else if (p == p_save || *p_save == '-' || *p != NUL
5507 || tab_number > LAST_TAB_NR)
5508 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005509 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005510 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005511 goto theend;
5512 }
5513 }
5514 else
5515 {
5516 if (*p_save == NUL)
5517 tab_number = 1;
5518 else if (p == p_save || *p_save == '-' || *p != NUL
5519 || tab_number == 0)
5520 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005521 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005522 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005523 goto theend;
5524 }
5525 tab_number = tab_number * relative + tabpage_index(curtab);
5526 if (!unaccept_arg0 && relative == -1)
5527 --tab_number;
5528 }
5529 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005530 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005531 }
5532 else if (eap->addr_count > 0)
5533 {
5534 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005535 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005536 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005537 tab_number = 0;
5538 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005539 else
5540 {
5541 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005542 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005543 {
5544 --tab_number;
5545 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005546 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005547 }
5548 }
5549 }
5550 else
5551 {
5552 switch (eap->cmdidx)
5553 {
5554 case CMD_tabnext:
5555 tab_number = tabpage_index(curtab) + 1;
5556 if (tab_number > LAST_TAB_NR)
5557 tab_number = 1;
5558 break;
5559 case CMD_tabmove:
5560 tab_number = LAST_TAB_NR;
5561 break;
5562 default:
5563 tab_number = tabpage_index(curtab);
5564 }
5565 }
5566
5567theend:
5568 return tab_number;
5569}
5570
5571/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005572 * ":tabclose": close current tab page, unless it is the last one.
5573 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 */
5575 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005576ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005578 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005579 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005580
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005581# ifdef FEAT_CMDWIN
5582 if (cmdwin_type != 0)
5583 cmdwin_result = K_IGNORE;
5584 else
5585# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005586 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005587 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005588 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005590 tab_number = get_tabpage_arg(eap);
5591 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005592 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005593 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005594 if (tp == NULL)
5595 {
5596 beep_flush();
5597 return;
5598 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005599 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005600 {
5601 tabpage_close_other(tp, eap->forceit);
5602 return;
5603 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005604 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005605 tabpage_close(eap->forceit);
5606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005608}
5609
5610/*
5611 * ":tabonly": close all tab pages except the current one
5612 */
5613 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005614ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005615{
5616 tabpage_T *tp;
5617 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005618 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005619
5620# ifdef FEAT_CMDWIN
5621 if (cmdwin_type != 0)
5622 cmdwin_result = K_IGNORE;
5623 else
5624# endif
5625 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005626 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005627 else
5628 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005629 tab_number = get_tabpage_arg(eap);
5630 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005631 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005632 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005633 // Repeat this up to a 1000 times, because autocommands may
5634 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005635 for (done = 0; done < 1000; ++done)
5636 {
5637 FOR_ALL_TABPAGES(tp)
5638 if (tp->tp_topframe != topframe)
5639 {
5640 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005641 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005642 if (valid_tabpage(tp))
5643 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005644 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005645 break;
5646 }
5647 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005648 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005649 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005650 }
5651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653
5654/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005655 * Close the current tab page.
5656 */
5657 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005658tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005659{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005660 // First close all the windows but the current one. If that worked then
5661 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005662 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005663 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005664 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005665 ex_win_close(forceit, curwin, NULL);
5666# ifdef FEAT_GUI
5667 need_mouse_correct = TRUE;
5668# endif
5669}
5670
5671/*
5672 * Close tab page "tp", which is not the current tab page.
5673 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005674 * Also takes care of the tab pages line disappearing when closing the
5675 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005676 */
5677 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005678tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005679{
5680 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005681 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005682 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005683
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005684 // Limit to 1000 windows, autocommands may add a window while we close
5685 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005686 while (++done < 1000)
5687 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005688 wp = tp->tp_firstwin;
5689 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005690
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005691 // Autocommands may delete the tab page under our fingers and we may
5692 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005693 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005694 break;
5695 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005696
Bram Moolenaar29323592016-07-24 22:04:11 +02005697 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005698
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005699 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005700 if (h != tabline_height())
5701 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005702}
5703
5704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 * ":only".
5706 */
5707 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005708ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005710 win_T *wp;
5711 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712# ifdef FEAT_GUI
5713 need_mouse_correct = TRUE;
5714# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005715 if (eap->addr_count > 0)
5716 {
5717 wnr = eap->line2;
5718 for (wp = firstwin; --wnr > 0; )
5719 {
5720 if (wp->w_next == NULL)
5721 break;
5722 else
5723 wp = wp->w_next;
5724 }
5725 win_goto(wp);
5726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 close_others(TRUE, eap->forceit);
5728}
5729
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005731ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005733 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005734 if (!eap->skip)
5735 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005736#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005737 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005738#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005739 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005740 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005741 else
5742 {
5743 int winnr = 0;
5744 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005745
Bram Moolenaar2256c992016-11-15 21:17:07 +01005746 FOR_ALL_WINDOWS(win)
5747 {
5748 winnr++;
5749 if (winnr == eap->line2)
5750 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005751 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005752 if (win == NULL)
5753 win = lastwin;
5754 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 }
5757}
5758
5759/*
5760 * ":stop" and ":suspend": Suspend Vim.
5761 */
5762 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005763ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764{
5765 /*
5766 * Disallow suspending for "rvim".
5767 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005768 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 {
5770 if (!eap->forceit)
5771 autowrite_all();
5772 windgoto((int)Rows - 1, 0);
5773 out_char('\n');
5774 out_flush();
5775 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005776 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005778 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005780 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781#ifdef FEAT_TITLE
5782 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005783 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784#endif
5785 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005786 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005788 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 }
5790}
5791
5792/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005793 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 */
5795 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005796ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797{
Bram Moolenaar461f2122020-07-28 20:25:47 +02005798#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02005799 if (not_in_vim9(eap) == FAIL)
5800 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02005801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802#ifdef FEAT_CMDWIN
5803 if (cmdwin_type != 0)
5804 {
5805 cmdwin_result = Ctrl_C;
5806 return;
5807 }
5808#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005809 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005810 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005811 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005812 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005813 return;
5814 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005815
5816 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005817 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818
5819 /*
5820 * if more files or windows we won't exit
5821 */
5822 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5823 exiting = TRUE;
5824 if ( ((eap->cmdidx == CMD_wq
5825 || curbufIsChanged())
5826 && do_write(eap) == FAIL)
5827 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005828 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 {
5830 not_exiting();
5831 }
5832 else
5833 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005834 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005836 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837# ifdef FEAT_GUI
5838 need_mouse_correct = TRUE;
5839# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005840 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005841 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 }
5843}
5844
5845/*
5846 * ":print", ":list", ":number".
5847 */
5848 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005849ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005851 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005852 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005853 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005855 for ( ;!got_int; ui_breakcheck())
5856 {
5857 print_line(eap->line1,
5858 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5859 || (eap->flags & EXFLAG_NR)),
5860 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5861 if (++eap->line1 > eap->line2)
5862 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005863 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005864 }
5865 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005866 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005867 curwin->w_cursor.lnum = eap->line2;
5868 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 }
5870
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872}
5873
5874#ifdef FEAT_BYTEOFF
5875 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005876ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877{
5878 goto_byte(eap->line2);
5879}
5880#endif
5881
5882/*
5883 * ":shell".
5884 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005886ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887{
5888 do_shell(NULL, 0);
5889}
5890
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005891#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005893static int drop_busy = FALSE;
5894static int drop_filec;
5895static char_u **drop_filev = NULL;
5896static int drop_split;
5897static void (*drop_callback)(void *);
5898static void *drop_cookie;
5899
5900 static void
5901handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902{
5903 exarg_T ea;
5904 int save_msg_scroll = msg_scroll;
5905
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005906 // Setting the argument list may cause screen updates and being called
5907 // recursively. Avoid that by setting drop_busy.
5908 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005910 // Check whether the current buffer is changed. If so, we will need
5911 // to split the current window or data could be lost.
5912 // We don't need to check if the 'hidden' option is set, as in this
5913 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005914 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 {
5916 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005917 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 --emsg_off;
5919 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005920 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 if (win_split(0, 0) == FAIL)
5923 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005924 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005926 // When splitting the window, create a new alist. Otherwise the
5927 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 alist_unlink(curwin->w_alist);
5929 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 }
5931
5932 /*
5933 * Set up the new argument list.
5934 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005935 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936
5937 /*
5938 * Move to the first file.
5939 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005940 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005941 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 ea.cmd = (char_u *)"next";
5943 do_argfile(&ea, 0);
5944
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005945 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5946 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 need_start_insertmode = FALSE;
5948
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005949 // Restore msg_scroll, otherwise a following command may cause scrolling
5950 // unexpectedly. The screen will be redrawn by the caller, thus
5951 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005953
5954 if (drop_callback != NULL)
5955 drop_callback(drop_cookie);
5956
5957 drop_filev = NULL;
5958 drop_busy = FALSE;
5959}
5960
5961/*
5962 * Handle a file drop. The code is here because a drop is *nearly* like an
5963 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005964 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005965 * code is very similar to :args and hence needs access to a lot of the static
5966 * functions in this file.
5967 *
5968 * The "filev" list must have been allocated using alloc(), as should each item
5969 * in the list. This function takes over responsibility for freeing the "filev"
5970 * list.
5971 */
5972 void
5973handle_drop(
5974 int filec, // the number of files dropped
5975 char_u **filev, // the list of files dropped
5976 int split, // force splitting the window
5977 void (*callback)(void *), // to be called after setting the argument
5978 // list
5979 void *cookie) // argument for "callback" (allocated)
5980{
5981 // Cannot handle recursive drops, finish the pending one.
5982 if (drop_busy)
5983 {
5984 FreeWild(filec, filev);
5985 vim_free(cookie);
5986 return;
5987 }
5988
5989 // When calling handle_drop() more than once in a row we only use the last
5990 // one.
5991 if (drop_filev != NULL)
5992 {
5993 FreeWild(drop_filec, drop_filev);
5994 vim_free(drop_cookie);
5995 }
5996
5997 drop_filec = filec;
5998 drop_filev = filev;
5999 drop_split = split;
6000 drop_callback = callback;
6001 drop_cookie = cookie;
6002
6003 // Postpone this when:
6004 // - editing the command line
6005 // - not possible to change the current buffer
6006 // - updating the screen
6007 // As it may change buffers and window structures that are in use and cause
6008 // freed memory to be used.
6009 if (text_locked() || curbuf_locked() || updating_screen)
6010 return;
6011
6012 handle_drop_internal();
6013}
6014
6015/*
6016 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6017 * reset: Handle a postponed drop.
6018 */
6019 void
6020handle_any_postponed_drop(void)
6021{
6022 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006023 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006024 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025}
6026#endif
6027
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 * ":preserve".
6030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006032ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006034 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 ml_preserve(curbuf, TRUE);
6036}
6037
6038/*
6039 * ":recover".
6040 */
6041 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006042ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006044 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006046 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6047 | CCGD_MULTWIN
6048 | (eap->forceit ? CCGD_FORCEIT : 0)
6049 | CCGD_EXCMD)
6050
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 && (*eap->arg == NUL
6052 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006053 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 recoverymode = FALSE;
6055}
6056
6057/*
6058 * Command modifier used in a wrong way.
6059 */
6060 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006061ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006063 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064}
6065
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066/*
6067 * :sview [+command] file split window with new file, read-only
6068 * :split [[+command] file] split window with current or new file
6069 * :vsplit [[+command] file] split window vertically with current or new file
6070 * :new [[+command] file] split window with no or new file
6071 * :vnew [[+command] file] split vertically window with no or new file
6072 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006073 *
6074 * :tabedit open new Tab page with empty window
6075 * :tabedit [+command] file open new Tab page and edit "file"
6076 * :tabnew [[+command] file] just like :tabedit
6077 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 */
6079 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006080ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006082 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006083#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006085#endif
6086#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006087 char_u dot_path[] = ".";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006089#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006090 int use_tab = eap->cmdidx == CMD_tabedit
6091 || eap->cmdidx == CMD_tabfind
6092 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006094 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006095 return;
6096
Bram Moolenaar4033c552017-09-16 20:54:51 +02006097#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100
Bram Moolenaar4033c552017-09-16 20:54:51 +02006101#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006102 // A ":split" in the quickfix window works like ":new". Don't want two
6103 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006104 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 {
6106 if (eap->cmdidx == CMD_split)
6107 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 if (eap->cmdidx == CMD_vsplit)
6109 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112
Bram Moolenaar4033c552017-09-16 20:54:51 +02006113#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006114 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 {
6116 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6117 FNAME_MESS, TRUE, curbuf->b_ffname);
6118 if (fname == NULL)
6119 goto theend;
6120 eap->arg = fname;
6121 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006122# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006123 else
6124# endif
6125#endif
6126#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 && eap->cmdidx != CMD_new)
6130 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006131 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006132# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006133 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006134# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006135 au_has_group((char_u *)"FileExplorer"))
6136 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006137 // No browsing supported but we do have the file explorer:
6138 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006139 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006140 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006141 }
6142 else
6143 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006144 fname = do_browse(0, (char_u *)(use_tab
6145 ? _("Edit File in new tab page")
6146 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006148 if (fname == NULL)
6149 goto theend;
6150 eap->arg = fname;
6151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006153 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006156 /*
6157 * Either open new tab page or split the window.
6158 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006159 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006160 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006161 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6162 : eap->addr_count == 0 ? 0
6163 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006164 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006165 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006166
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006167 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006168 if (curwin != old_curwin
6169 && win_valid(old_curwin)
6170 && old_curwin->w_buffer != curbuf
6171 && !cmdmod.keepalt)
6172 old_curwin->w_alt_fnum = curbuf->b_fnum;
6173 }
6174 }
6175 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6177 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006178 // Reset 'scrollbind' when editing another file, but keep it when
6179 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006181# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006183# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006185 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 else
6187 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 do_exedit(eap, old_curwin);
6189 }
6190
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006191# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006193# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006195# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196theend:
6197 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006198# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006200
6201/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006202 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006203 */
6204 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006205tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006206{
6207 exarg_T ea;
6208
Bram Moolenaara80faa82020-04-12 19:37:17 +02006209 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006210 ea.cmdidx = CMD_tabnew;
6211 ea.cmd = (char_u *)"tabn";
6212 ea.arg = (char_u *)"";
6213 ex_splitview(&ea);
6214}
6215
6216/*
6217 * :tabnext command
6218 */
6219 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006220ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006221{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006222 int tab_number;
6223
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006224 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006225 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006226 switch (eap->cmdidx)
6227 {
6228 case CMD_tabfirst:
6229 case CMD_tabrewind:
6230 goto_tabpage(1);
6231 break;
6232 case CMD_tablast:
6233 goto_tabpage(9999);
6234 break;
6235 case CMD_tabprevious:
6236 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006237 if (eap->arg && *eap->arg != NUL)
6238 {
6239 char_u *p = eap->arg;
6240 char_u *p_save = p;
6241
6242 tab_number = getdigits(&p);
6243 if (p == p_save || *p_save == '-' || *p != NUL
6244 || tab_number == 0)
6245 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006246 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006247 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006248 return;
6249 }
6250 }
6251 else
6252 {
6253 if (eap->addr_count == 0)
6254 tab_number = 1;
6255 else
6256 {
6257 tab_number = eap->line2;
6258 if (tab_number < 1)
6259 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006260 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006261 return;
6262 }
6263 }
6264 }
6265 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006266 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006267 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006268 tab_number = get_tabpage_arg(eap);
6269 if (eap->errmsg == NULL)
6270 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006271 break;
6272 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006273}
6274
6275/*
6276 * :tabmove command
6277 */
6278 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006279ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006280{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006281 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006282
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006283 tab_number = get_tabpage_arg(eap);
6284 if (eap->errmsg == NULL)
6285 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006286}
6287
6288/*
6289 * :tabs command: List tabs and their contents.
6290 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006291 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006292ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006293{
6294 tabpage_T *tp;
6295 win_T *wp;
6296 int tabcount = 1;
6297
6298 msg_start();
6299 msg_scroll = TRUE;
6300 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6301 {
6302 msg_putchar('\n');
6303 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006304 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006305 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006306 ui_breakcheck();
6307
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006308 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006309 wp = firstwin;
6310 else
6311 wp = tp->tp_firstwin;
6312 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6313 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006314 msg_putchar('\n');
6315 msg_putchar(wp == curwin ? '>' : ' ');
6316 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006317 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6318 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006319 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006320 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006321 else
6322 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6323 IObuff, IOSIZE, TRUE);
6324 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006325 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006326 ui_breakcheck();
6327 }
6328 }
6329}
6330
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331/*
6332 * ":mode": Set screen mode.
6333 * If no argument given, just get the screen size and redraw.
6334 */
6335 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006336ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337{
6338 if (*eap->arg == NUL)
6339 shell_resized();
6340 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006341 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342}
6343
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344/*
6345 * ":resize".
6346 * set, increment or decrement current window height
6347 */
6348 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006349ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350{
6351 int n;
6352 win_T *wp = curwin;
6353
6354 if (eap->addr_count > 0)
6355 {
6356 n = eap->line2;
6357 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6358 ;
6359 }
6360
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006361# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006363# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 if (cmdmod.split & WSP_VERT)
6366 {
6367 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006368 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006369 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 n = 9999;
6371 win_setwidth_win((int)n, wp);
6372 }
6373 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 {
6375 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006376 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006377 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 n = 9999;
6379 win_setheight_win((int)n, wp);
6380 }
6381}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382
6383/*
6384 * ":find [+command] <file>" command.
6385 */
6386 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006387ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388{
6389#ifdef FEAT_SEARCHPATH
6390 char_u *fname;
6391 int count;
6392
6393 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6394 TRUE, curbuf->b_ffname);
6395 if (eap->addr_count > 0)
6396 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006397 // Repeat finding the file "count" times. This matters when it
6398 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 count = eap->line2;
6400 while (fname != NULL && --count > 0)
6401 {
6402 vim_free(fname);
6403 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6404 FALSE, curbuf->b_ffname);
6405 }
6406 }
6407
6408 if (fname != NULL)
6409 {
6410 eap->arg = fname;
6411#endif
6412 do_exedit(eap, NULL);
6413#ifdef FEAT_SEARCHPATH
6414 vim_free(fname);
6415 }
6416#endif
6417}
6418
6419/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006420 * ":open" simulation: for now just work like ":visual".
6421 */
6422 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006423ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006424{
6425 regmatch_T regmatch;
6426 char_u *p;
6427
6428 curwin->w_cursor.lnum = eap->line2;
6429 beginline(BL_SOL | BL_FIX);
6430 if (*eap->arg == '/')
6431 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006432 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006433 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006434 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006435 *p = NUL;
6436 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6437 if (regmatch.regprog != NULL)
6438 {
6439 regmatch.rm_ic = p_ic;
6440 p = ml_get_curline();
6441 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006442 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006443 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006444 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006445 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006446 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006447 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006448 eap->arg += STRLEN(eap->arg);
6449 }
6450 check_cursor();
6451
6452 eap->cmdidx = CMD_visual;
6453 do_exedit(eap, NULL);
6454}
6455
6456/*
6457 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 */
6459 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006460ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461{
6462 do_exedit(eap, NULL);
6463}
6464
6465/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006466 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006469do_exedit(
6470 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006471 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472{
6473 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006475 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006477 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6478 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006479 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 /*
6481 * ":vi" command ends Ex mode.
6482 */
6483 if (exmode_active && (eap->cmdidx == CMD_visual
6484 || eap->cmdidx == CMD_view))
6485 {
6486 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006487 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006489 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006490 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006491 if (global_busy)
6492 {
6493 int rd = RedrawingDisabled;
6494 int nwr = no_wait_return;
6495 int ms = msg_scroll;
6496#ifdef FEAT_GUI
6497 int he = hold_gui_events;
6498#endif
6499
6500 if (eap->nextcmd != NULL)
6501 {
6502 stuffReadbuff(eap->nextcmd);
6503 eap->nextcmd = NULL;
6504 }
6505
6506 if (exmode_was != EXMODE_VIM)
6507 settmode(TMODE_RAW);
6508 RedrawingDisabled = 0;
6509 no_wait_return = 0;
6510 need_wait_return = FALSE;
6511 msg_scroll = 0;
6512#ifdef FEAT_GUI
6513 hold_gui_events = 0;
6514#endif
6515 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006516 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006517
6518 main_loop(FALSE, TRUE);
6519
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006520 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006521 RedrawingDisabled = rd;
6522 no_wait_return = nwr;
6523 msg_scroll = ms;
6524#ifdef FEAT_GUI
6525 hold_gui_events = he;
6526#endif
6527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 }
6531
6532 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006533 || eap->cmdidx == CMD_tabnew
6534 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006535 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006537 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 setpcmark();
6539 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006540 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6541 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006543 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 || *eap->arg != NUL
6545#ifdef FEAT_BROWSE
6546 || cmdmod.browse
6547#endif
6548 )
6549 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006550 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6551 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006552 if (*eap->arg != NUL && curbuf_locked())
6553 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006554
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 n = readonlymode;
6556 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6557 readonlymode = TRUE;
6558 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006559 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6560 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 setpcmark();
6562 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6563 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006564 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6566 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6567 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006568 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006570 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006571 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006573 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006575 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 if (old_curwin != NULL)
6577 {
6578 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006579 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006581#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006582 cleanup_T cs;
6583
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006584 // Reset the error/interrupt/exception state here so that
6585 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006586 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006587#endif
6588#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006590#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006591 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006592
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006593#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006594 // Restore the error/interrupt/exception state if not
6595 // discarded by a new aborting error, interrupt, or
6596 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006597 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 }
6600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 }
6602 else if (readonlymode && curbuf->b_nwindows == 1)
6603 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006604 // When editing an already visited buffer, 'readonly' won't be set
6605 // but the previous value is kept. With ":view" and ":sview" we
6606 // want the file to be readonly, except when another window is
6607 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 curbuf->b_p_ro = TRUE;
6609 }
6610 readonlymode = n;
6611 }
6612 else
6613 {
6614 if (eap->do_ecmd_cmd != NULL)
6615 do_cmdline_cmd(eap->do_ecmd_cmd);
6616#ifdef FEAT_TITLE
6617 n = curwin->w_arg_idx_invalid;
6618#endif
6619 check_arg_idx(curwin);
6620#ifdef FEAT_TITLE
6621 if (n != curwin->w_arg_idx_invalid)
6622 maketitle();
6623#endif
6624 }
6625
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 /*
6627 * if ":split file" worked, set alternate file name in old window to new
6628 * file
6629 */
6630 if (old_curwin != NULL
6631 && *eap->arg != NUL
6632 && curwin != old_curwin
6633 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006634 && old_curwin->w_buffer != curbuf
6635 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637
6638 ex_no_reprint = TRUE;
6639}
6640
6641#ifndef FEAT_GUI
6642/*
6643 * ":gui" and ":gvim" when there is no GUI.
6644 */
6645 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006646ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006648 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649}
6650#endif
6651
Bram Moolenaar4f974752019-02-17 17:44:42 +01006652#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006654ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655{
6656 gui_make_tearoff(eap->arg);
6657}
6658#endif
6659
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006660#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6661 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006663ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006665# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6666 if (gui.in_use)
6667 gui_make_popup(eap->arg, eap->forceit);
6668# ifdef FEAT_TERM_POPUP_MENU
6669 else
6670# endif
6671# endif
6672# ifdef FEAT_TERM_POPUP_MENU
6673 pum_make_popup(eap->arg, eap->forceit);
6674# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675}
6676#endif
6677
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006679ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680{
6681 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006682 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006684 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685}
6686
6687/*
6688 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6689 * offset.
6690 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6691 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006693ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006696 win_T *save_curwin = curwin;
6697 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 long topline;
6699 long y;
6700 linenr_T old_linenr = curwin->w_cursor.lnum;
6701
6702 setpcmark();
6703
6704 /*
6705 * determine max topline
6706 */
6707 if (curwin->w_p_scb)
6708 {
6709 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006710 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 {
6712 if (wp->w_p_scb && wp->w_buffer)
6713 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006714 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 if (topline > y)
6716 topline = y;
6717 }
6718 }
6719 if (topline < 1)
6720 topline = 1;
6721 }
6722 else
6723 {
6724 topline = 1;
6725 }
6726
6727
6728 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006729 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006731 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 {
6733 if (curwin->w_p_scb)
6734 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006735 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 y = topline - curwin->w_topline;
6737 if (y > 0)
6738 scrollup(y, TRUE);
6739 else
6740 scrolldown(-y, TRUE);
6741 curwin->w_scbind_pos = topline;
6742 redraw_later(VALID);
6743 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 }
6746 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006747 curwin = save_curwin;
6748 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 if (curwin->w_p_scb)
6750 {
6751 did_syncbind = TRUE;
6752 checkpcmark();
6753 if (old_linenr != curwin->w_cursor.lnum)
6754 {
6755 char_u ctrl_o[2];
6756
6757 ctrl_o[0] = Ctrl_O;
6758 ctrl_o[1] = 0;
6759 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6760 }
6761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762}
6763
6764
6765 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006766ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006768 int i;
6769 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6770 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006772 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 do_bang(1, eap, FALSE, FALSE, TRUE);
6774 else
6775 {
6776 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6777 return;
6778
6779#ifdef FEAT_BROWSE
6780 if (cmdmod.browse)
6781 {
6782 char_u *browseFile;
6783
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006784 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 NULL, NULL, NULL, curbuf);
6786 if (browseFile != NULL)
6787 {
6788 i = readfile(browseFile, NULL,
6789 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6790 vim_free(browseFile);
6791 }
6792 else
6793 i = OK;
6794 }
6795 else
6796#endif
6797 if (*eap->arg == NUL)
6798 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006799 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 return;
6801 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6802 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6803 }
6804 else
6805 {
6806 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6807 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6808 i = readfile(eap->arg, NULL,
6809 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6810
6811 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006812 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006814#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 if (!aborting())
6816#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006817 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 }
6819 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006820 {
6821 if (empty && exmode_active)
6822 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006823 // Delete the empty line that remains. Historically ex does
6824 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006825 if (eap->line2 == 0)
6826 lnum = curbuf->b_ml.ml_line_count;
6827 else
6828 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006829 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006830 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006831 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006832 if (curwin->w_cursor.lnum > 1
6833 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006834 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006835 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006836 }
6837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 }
6841}
6842
Bram Moolenaarea408852005-06-25 22:49:46 +00006843static char_u *prev_dir = NULL;
6844
6845#if defined(EXITFREE) || defined(PROTO)
6846 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006847free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006848{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006849 VIM_CLEAR(prev_dir);
6850 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006851}
6852#endif
6853
Bram Moolenaarf4258302013-06-02 18:20:17 +02006854/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006855 * Get the previous directory for the given chdir scope.
6856 */
6857 static char_u *
6858get_prevdir(cdscope_T scope)
6859{
6860 if (scope == CDSCOPE_WINDOW)
6861 return curwin->w_prevdir;
6862 else if (scope == CDSCOPE_TABPAGE)
6863 return curtab->tp_prevdir;
6864 return prev_dir;
6865}
6866
6867/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006868 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006869 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6870 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006871 */
6872 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006873post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006874{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006875 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006876 // Clear tab local directory for both :cd and :tcd
6877 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006878 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006879 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006880 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006881 char_u *pdir = get_prevdir(scope);
6882
6883 // If still in the global directory, need to remember current
6884 // directory as the global directory.
6885 if (globaldir == NULL && pdir != NULL)
6886 globaldir = vim_strsave(pdir);
6887
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006888 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006889 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006890 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006891 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006892 curtab->tp_localdir = vim_strsave(NameBuff);
6893 else
6894 curwin->w_localdir = vim_strsave(NameBuff);
6895 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006896 }
6897 else
6898 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006899 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006900 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006901 }
6902
6903 shorten_fnames(TRUE);
6904}
6905
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006906/*
6907 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006908 * chdir() function.
6909 * scope == CDSCOPE_WINDOW: changes the window-local directory
6910 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6911 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006912 * Returns TRUE if the directory is successfully changed.
6913 */
6914 int
6915changedir_func(
6916 char_u *new_dir,
6917 int forceit,
6918 cdscope_T scope)
6919{
6920 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006921 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006922 int dir_differs;
6923 int retval = FALSE;
6924
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006925 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006926 return FALSE;
6927
6928 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6929 {
6930 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6931 return FALSE;
6932 }
6933
6934 // ":cd -": Change to previous directory
6935 if (STRCMP(new_dir, "-") == 0)
6936 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006937 pdir = get_prevdir(scope);
6938 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006939 {
6940 emsg(_("E186: No previous directory"));
6941 return FALSE;
6942 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006943 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006944 }
6945
Bram Moolenaar002bc792020-06-05 22:33:42 +02006946 // Free the previous directory
6947 tofree = get_prevdir(scope);
6948
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006949 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006950 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006951 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006952 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006953 pdir = NULL;
6954 if (scope == CDSCOPE_WINDOW)
6955 curwin->w_prevdir = pdir;
6956 else if (scope == CDSCOPE_TABPAGE)
6957 curtab->tp_prevdir = pdir;
6958 else
6959 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006960
6961#if defined(UNIX) || defined(VMS)
6962 // for UNIX ":cd" means: go to home directory
6963 if (*new_dir == NUL)
6964 {
6965 // use NameBuff for home directory name
6966# ifdef VMS
6967 char_u *p;
6968
6969 p = mch_getenv((char_u *)"SYS$LOGIN");
6970 if (p == NULL || *p == NUL) // empty is the same as not set
6971 NameBuff[0] = NUL;
6972 else
6973 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6974# else
6975 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6976# endif
6977 new_dir = NameBuff;
6978 }
6979#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006980 dir_differs = new_dir == NULL || pdir == NULL
6981 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006982 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6983 emsg(_(e_failed));
6984 else
6985 {
6986 char_u *acmd_fname;
6987
6988 post_chdir(scope);
6989
6990 if (dir_differs)
6991 {
6992 if (scope == CDSCOPE_WINDOW)
6993 acmd_fname = (char_u *)"window";
6994 else if (scope == CDSCOPE_TABPAGE)
6995 acmd_fname = (char_u *)"tabpage";
6996 else
6997 acmd_fname = (char_u *)"global";
6998 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6999 curbuf);
7000 }
7001 retval = TRUE;
7002 }
7003 vim_free(tofree);
7004
7005 return retval;
7006}
Bram Moolenaarea408852005-06-25 22:49:46 +00007007
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007009 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007011 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007012ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007014 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015
7016 new_dir = eap->arg;
7017#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007018 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 if (*new_dir == NUL)
7020 ex_pwd(NULL);
7021 else
7022#endif
7023 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007024 cdscope_T scope = CDSCOPE_GLOBAL;
7025
7026 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7027 scope = CDSCOPE_WINDOW;
7028 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7029 scope = CDSCOPE_TABPAGE;
7030
7031 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007032 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007033 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007034 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 ex_pwd(eap);
7036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 }
7038}
7039
7040/*
7041 * ":pwd".
7042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007044ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045{
7046 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7047 {
7048#ifdef BACKSLASH_IN_FILENAME
7049 slash_adjust(NameBuff);
7050#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007051 if (p_verbose > 0)
7052 {
7053 char *context = "global";
7054
7055 if (curwin->w_localdir != NULL)
7056 context = "window";
7057 else if (curtab->tp_localdir != NULL)
7058 context = "tabpage";
7059 smsg("[%s] %s", context, (char *)NameBuff);
7060 }
7061 else
7062 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 }
7064 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007065 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066}
7067
7068/*
7069 * ":=".
7070 */
7071 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007072ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007074 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007075 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076}
7077
7078 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007079ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007081 int n;
7082 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083
7084 if (cursor_valid())
7085 {
7086 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7087 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007088 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007090
7091 len = eap->line2;
7092 switch (*eap->arg)
7093 {
7094 case 'm': break;
7095 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007096 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007097 }
7098 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099}
7100
7101/*
7102 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7103 */
7104 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007105do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106{
Bram Moolenaar52953192019-08-16 10:27:13 +02007107 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007108 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007109# ifdef ELAPSED_FUNC
7110 elapsed_T start_tv;
7111
7112 // Remember at what time we started, so that we know how much longer we
7113 // should wait after waiting for a bit.
7114 ELAPSED_INIT(start_tv);
7115# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116
7117 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007118 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007119 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007121 wait_now = msec - done > 1000L ? 1000L : msec - done;
7122#ifdef FEAT_TIMERS
7123 {
7124 long due_time = check_due_timer();
7125
7126 if (due_time > 0 && due_time < wait_now)
7127 wait_now = due_time;
7128 }
7129#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007130#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007131 if (has_any_channel() && wait_now > 20L)
7132 wait_now = 20L;
7133#endif
7134#ifdef FEAT_SOUND
7135 if (has_any_sound_callback() && wait_now > 20L)
7136 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007137#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007138 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007139
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007140#ifdef FEAT_JOB_CHANNEL
7141 if (has_any_channel())
7142 ui_breakcheck_force(TRUE);
7143 else
7144#endif
7145 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007146#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007147 // Process the netbeans and clientserver messages that may have been
7148 // received in the call to ui_breakcheck() when the GUI is in use. This
7149 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007150 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007151#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007152
7153# ifdef ELAPSED_FUNC
7154 // actual time passed
7155 done = ELAPSED_FUNC(start_tv);
7156# else
7157 // guestimate time passed (will actually be more)
7158 done += wait_now;
7159# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007161
7162 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7163 // input buffer, otherwise a following call to input() fails.
7164 if (got_int)
7165 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166}
7167
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168/*
7169 * ":winsize" command (obsolete).
7170 */
7171 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007172ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173{
7174 int w, h;
7175 char_u *arg = eap->arg;
7176 char_u *p;
7177
7178 w = getdigits(&arg);
7179 arg = skipwhite(arg);
7180 p = arg;
7181 h = getdigits(&arg);
7182 if (*p != NUL && *arg == NUL)
7183 set_shellsize(w, h, TRUE);
7184 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007185 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186}
7187
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007189ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190{
7191 int xchar = NUL;
7192 char_u *p;
7193
7194 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7195 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007196 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 if (eap->arg[1] == NUL)
7198 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007199 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 return;
7201 }
7202 xchar = eap->arg[1];
7203 p = eap->arg + 2;
7204 }
7205 else
7206 p = eap->arg + 1;
7207
7208 eap->nextcmd = check_nextcmd(p);
7209 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007210 if (*p != NUL && *p != (
7211#ifdef FEAT_EVAL
7212 in_vim9script() ? '#' :
7213#endif
7214 '"')
7215 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007216 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007217 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007219 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007221 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7223 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007224 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 }
7226}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227
Bram Moolenaar843ee412004-06-30 16:16:41 +00007228#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229/*
7230 * ":winpos".
7231 */
7232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007233ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234{
7235 int x, y;
7236 char_u *arg = eap->arg;
7237 char_u *p;
7238
7239 if (*arg == NUL)
7240 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007241# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007242# ifdef VIMDLL
7243 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7244 mch_get_winpos(&x, &y) != FAIL)
7245# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007247# else
7248 if (mch_get_winpos(&x, &y) != FAIL)
7249# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 {
7251 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007252 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 }
7254 else
7255# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007256 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 }
7258 else
7259 {
7260 x = getdigits(&arg);
7261 arg = skipwhite(arg);
7262 p = arg;
7263 y = getdigits(&arg);
7264 if (*p == NUL || *arg != NUL)
7265 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007266 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 return;
7268 }
7269# ifdef FEAT_GUI
7270 if (gui.in_use)
7271 gui_mch_set_winpos(x, y);
7272 else if (gui.starting)
7273 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007274 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 gui_win_x = x;
7276 gui_win_y = y;
7277 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007278# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 else
7280# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007281# endif
7282# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007283 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284# endif
7285# ifdef HAVE_TGETENT
7286 if (*T_CWP)
7287 term_set_winpos(x, y);
7288# endif
7289 }
7290}
7291#endif
7292
7293/*
7294 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7295 */
7296 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007297ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298{
7299 oparg_T oa;
7300
7301 clear_oparg(&oa);
7302 oa.regname = eap->regname;
7303 oa.start.lnum = eap->line1;
7304 oa.end.lnum = eap->line2;
7305 oa.line_count = eap->line2 - eap->line1 + 1;
7306 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007308 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 {
7310 setpcmark();
7311 curwin->w_cursor.lnum = eap->line1;
7312 beginline(BL_SOL | BL_FIX);
7313 }
7314
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007315 if (VIsual_active)
7316 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007317
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 switch (eap->cmdidx)
7319 {
7320 case CMD_delete:
7321 oa.op_type = OP_DELETE;
7322 op_delete(&oa);
7323 break;
7324
7325 case CMD_yank:
7326 oa.op_type = OP_YANK;
7327 (void)op_yank(&oa, FALSE, TRUE);
7328 break;
7329
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007330 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007331 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007333 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7334#else
7335 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007337 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 oa.op_type = OP_RSHIFT;
7339 else
7340 oa.op_type = OP_LSHIFT;
7341 op_shift(&oa, FALSE, eap->amount);
7342 break;
7343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007345 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346}
7347
7348/*
7349 * ":put".
7350 */
7351 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007352ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007354 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 if (eap->line2 == 0)
7356 {
7357 eap->line2 = 1;
7358 eap->forceit = TRUE;
7359 }
7360 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007361 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007362 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363}
7364
7365/*
7366 * Handle ":copy" and ":move".
7367 */
7368 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007369ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370{
7371 long n;
7372
Bram Moolenaar491799b2020-08-01 19:23:43 +02007373#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007374 if (not_in_vim9(eap) == FAIL)
7375 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007376#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007377 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007378 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 {
7380 eap->nextcmd = NULL;
7381 return;
7382 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007383 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384
7385 /*
7386 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7387 */
7388 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7389 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007390 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 return;
7392 }
7393
7394 if (eap->cmdidx == CMD_move)
7395 {
7396 if (do_move(eap->line1, eap->line2, n) == FAIL)
7397 return;
7398 }
7399 else
7400 ex_copy(eap->line1, eap->line2, n);
7401 u_clearline();
7402 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007403 ex_may_print(eap);
7404}
7405
7406/*
7407 * Print the current line if flags were given to the Ex command.
7408 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007409 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007410ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007411{
7412 if (eap->flags != 0)
7413 {
7414 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7415 (eap->flags & EXFLAG_LIST));
7416 ex_no_reprint = TRUE;
7417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418}
7419
7420/*
7421 * ":smagic" and ":snomagic".
7422 */
7423 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007424ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425{
7426 int magic_save = p_magic;
7427
7428 p_magic = (eap->cmdidx == CMD_smagic);
Bram Moolenaar66e00142020-08-12 21:58:12 +02007429 ex_substitute(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 p_magic = magic_save;
7431}
7432
7433/*
7434 * ":join".
7435 */
7436 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007437ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438{
7439 curwin->w_cursor.lnum = eap->line1;
7440 if (eap->line1 == eap->line2)
7441 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007442 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 return;
7444 if (eap->line2 == curbuf->b_ml.ml_line_count)
7445 {
7446 beep_flush();
7447 return;
7448 }
7449 ++eap->line2;
7450 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007451 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007453 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454}
7455
7456/*
7457 * ":[addr]@r" or ":[addr]*r": execute register
7458 */
7459 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007460ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461{
7462 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007463 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464
7465 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007466 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467
7468#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007469 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470#endif
7471
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007472 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 c = *eap->arg;
7474 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7475 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007476 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007477 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7478 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007479 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 else
7483 {
7484 int save_efr = exec_from_reg;
7485
7486 exec_from_reg = TRUE;
7487
7488 /*
7489 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007490 * Continue until the stuff buffer is empty and all added characters
7491 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007493 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7495
7496 exec_from_reg = save_efr;
7497 }
7498}
7499
7500/*
7501 * ":!".
7502 */
7503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007504ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505{
7506 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7507}
7508
7509/*
7510 * ":undo".
7511 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007513ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007515 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007516 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007517 else
7518 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519}
7520
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007521#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007522 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007523ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007524{
7525 char_u hash[UNDO_HASH_SIZE];
7526
7527 u_compute_hash(hash);
7528 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7529}
7530
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007531 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007532ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007533{
7534 char_u hash[UNDO_HASH_SIZE];
7535
7536 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007537 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007538}
7539#endif
7540
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541/*
7542 * ":redo".
7543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007545ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546{
7547 u_redo(1);
7548}
7549
7550/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007551 * ":earlier" and ":later".
7552 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007553 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007554ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007555{
7556 long count = 0;
7557 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007558 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007559 char_u *p = eap->arg;
7560
7561 if (*p == NUL)
7562 count = 1;
7563 else if (isdigit(*p))
7564 {
7565 count = getdigits(&p);
7566 switch (*p)
7567 {
7568 case 's': ++p; sec = TRUE; break;
7569 case 'm': ++p; sec = TRUE; count *= 60; break;
7570 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007571 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7572 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007573 }
7574 }
7575
7576 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007577 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007578 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007579 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7580 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007581}
7582
7583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 * ":redir": start/stop redirection.
7585 */
7586 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007587ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588{
7589 char *mode;
7590 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007591 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592
Bram Moolenaarba768492016-07-08 15:32:54 +02007593#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007594 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007595 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007596 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007597 return;
7598 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007599#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007600
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 if (STRICMP(eap->arg, "END") == 0)
7602 close_redir();
7603 else
7604 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007605 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007607 ++arg;
7608 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007610 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 mode = "a";
7612 }
7613 else
7614 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007615 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616
7617 close_redir();
7618
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007619 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007620 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 if (fname == NULL)
7622 return;
7623#ifdef FEAT_BROWSE
7624 if (cmdmod.browse)
7625 {
7626 char_u *browseFile;
7627
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007628 browseFile = do_browse(BROWSE_SAVE,
7629 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007630 fname, NULL, NULL,
7631 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007633 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 vim_free(fname);
7635 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007636 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 }
7638#endif
7639
7640 redir_fd = open_exfile(fname, eap->forceit, mode);
7641 vim_free(fname);
7642 }
7643#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007644 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007646 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007648 ++arg;
7649 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007651 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007652 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007654 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007656 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007657 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007658 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007659 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007661 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007662 if (*arg == '>')
7663 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007664 // Make register empty when not using @A-@Z and the
7665 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007666 if (*arg == NUL && !isupper(redir_reg))
7667 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007669 }
7670 if (*arg != NUL)
7671 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007672 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007673 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007674 }
7675 }
7676 else if (*arg == '=' && arg[1] == '>')
7677 {
7678 int append;
7679
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007680 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007681 close_redir();
7682 arg += 2;
7683
7684 if (*arg == '>')
7685 {
7686 ++arg;
7687 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 }
7689 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007690 append = FALSE;
7691
7692 if (var_redir_start(skipwhite(arg), append) == OK)
7693 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 }
7695#endif
7696
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007697 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007700 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007702
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007703 // Make sure redirection is not off. Can happen for cmdline completion
7704 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007705 if (redir_fd != NULL
7706#ifdef FEAT_EVAL
7707 || redir_reg || redir_vname
7708#endif
7709 )
7710 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711}
7712
7713/*
7714 * ":redraw": force redraw
7715 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007716 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007717ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718{
7719 int r = RedrawingDisabled;
7720 int p = p_lz;
7721
7722 RedrawingDisabled = 0;
7723 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007724 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007726 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727#ifdef FEAT_TITLE
7728 if (need_maketitle)
7729 maketitle();
7730#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007731#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7732# ifdef VIMDLL
7733 if (!gui.in_use)
7734# endif
7735 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 RedrawingDisabled = r;
7738 p_lz = p;
7739
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007740 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 msg_didout = FALSE;
7742 msg_col = 0;
7743
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007744 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007745 need_wait_return = FALSE;
7746
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 out_flush();
7748}
7749
7750/*
7751 * ":redrawstatus": force redraw of status line(s)
7752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007754ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 int r = RedrawingDisabled;
7757 int p = p_lz;
7758
7759 RedrawingDisabled = 0;
7760 p_lz = FALSE;
7761 if (eap->forceit)
7762 status_redraw_all();
7763 else
7764 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007765 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 RedrawingDisabled = r;
7767 p_lz = p;
7768 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769}
7770
Bram Moolenaare12bab32019-01-08 22:02:56 +01007771/*
7772 * ":redrawtabline": force redraw of the tabline
7773 */
7774 static void
7775ex_redrawtabline(exarg_T *eap UNUSED)
7776{
7777 int r = RedrawingDisabled;
7778 int p = p_lz;
7779
7780 RedrawingDisabled = 0;
7781 p_lz = FALSE;
7782
7783 draw_tabline();
7784
7785 RedrawingDisabled = r;
7786 p_lz = p;
7787 out_flush();
7788}
7789
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007791close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792{
7793 if (redir_fd != NULL)
7794 {
7795 fclose(redir_fd);
7796 redir_fd = NULL;
7797 }
7798#ifdef FEAT_EVAL
7799 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007800 if (redir_vname)
7801 {
7802 var_redir_stop();
7803 redir_vname = 0;
7804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805#endif
7806}
7807
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007808#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007809 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007810vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007811{
7812 if (vim_mkdir(name, prot) != 0)
7813 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007814 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007815 return FAIL;
7816 }
7817 return OK;
7818}
7819#endif
7820
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821/*
7822 * Open a file for writing for an Ex command, with some checks.
7823 * Return file descriptor, or NULL on failure.
7824 */
7825 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007826open_exfile(
7827 char_u *fname,
7828 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007829 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830{
7831 FILE *fd;
7832
7833#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007834 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 if (mch_isdir(fname))
7836 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007837 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 return NULL;
7839 }
7840#endif
7841 if (!forceit && *mode != 'a' && vim_fexists(fname))
7842 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007843 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 return NULL;
7845 }
7846
7847 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007848 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849
7850 return fd;
7851}
7852
7853/*
7854 * ":mark" and ":k".
7855 */
7856 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007857ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858{
7859 pos_T pos;
7860
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007861 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007862 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007863 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02007864 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 else
7866 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007867 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 curwin->w_cursor.lnum = eap->line2;
7869 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007870 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007871 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007872 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 }
7874}
7875
7876/*
7877 * Update w_topline, w_leftcol and the cursor position.
7878 */
7879 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007880update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007882 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 update_topline();
7884 if (!curwin->w_p_wrap)
7885 validate_cursor();
7886 update_curswant();
7887}
7888
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007890 * Save the current State and go to Normal mode.
7891 * Return TRUE if the typeahead could be saved.
7892 */
7893 int
7894save_current_state(save_state_T *sst)
7895{
7896 sst->save_msg_scroll = msg_scroll;
7897 sst->save_restart_edit = restart_edit;
7898 sst->save_msg_didout = msg_didout;
7899 sst->save_State = State;
7900 sst->save_insertmode = p_im;
7901 sst->save_finish_op = finish_op;
7902 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007903 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007904
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007905 msg_scroll = FALSE; // no msg scrolling in Normal mode
7906 restart_edit = 0; // don't go to Insert mode
7907 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007908
7909 /*
7910 * Save the current typeahead. This is required to allow using ":normal"
7911 * from an event handler and makes sure we don't hang when the argument
7912 * ends with half a command.
7913 */
7914 save_typeahead(&sst->tabuf);
7915 return sst->tabuf.typebuf_valid;
7916}
7917
7918 void
7919restore_current_state(save_state_T *sst)
7920{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007921 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007922 restore_typeahead(&sst->tabuf);
7923
7924 msg_scroll = sst->save_msg_scroll;
7925 restart_edit = sst->save_restart_edit;
7926 p_im = sst->save_insertmode;
7927 finish_op = sst->save_finish_op;
7928 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007929 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007930 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007931
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007932 // Restore the state (needed when called from a function executed for
7933 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007934 State = sst->save_State;
7935#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007936 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007937#endif
7938}
7939
7940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 * ":normal[!] {commands}": Execute normal mode commands.
7942 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007943 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007944ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007946 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 char_u *arg = NULL;
7948 int l;
7949 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007951 if (ex_normal_lock > 0)
7952 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007953 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007954 return;
7955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 if (ex_normal_busy >= p_mmd)
7957 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007958 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 return;
7960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 /*
7963 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7964 * this for the K_SPECIAL leading byte, otherwise special keys will not
7965 * work.
7966 */
7967 if (has_mbyte)
7968 {
7969 int len = 0;
7970
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007971 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 for (p = eap->arg; *p != NUL; ++p)
7973 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007974#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007975 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007977#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007978 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007979 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007980#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 )
7984 len += 2;
7985 }
7986 if (len > 0)
7987 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007988 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 if (arg != NULL)
7990 {
7991 len = 0;
7992 for (p = eap->arg; *p != NUL; ++p)
7993 {
7994 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007995#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 if (*p == CSI)
7997 {
7998 arg[len++] = KS_EXTRA;
7999 arg[len++] = (int)KE_CSI;
8000 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008001#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008002 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 {
8004 arg[len++] = *++p;
8005 if (*p == K_SPECIAL)
8006 {
8007 arg[len++] = KS_SPECIAL;
8008 arg[len++] = KE_FILLER;
8009 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008010#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 else if (*p == CSI)
8012 {
8013 arg[len++] = KS_EXTRA;
8014 arg[len++] = (int)KE_CSI;
8015 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 }
8018 arg[len] = NUL;
8019 }
8020 }
8021 }
8022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008024 ++ex_normal_busy;
8025 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 {
8027 /*
8028 * Repeat the :normal command for each line in the range. When no
8029 * range given, execute it just once, without positioning the cursor
8030 * first.
8031 */
8032 do
8033 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 if (eap->addr_count != 0)
8035 {
8036 curwin->w_cursor.lnum = eap->line1++;
8037 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008038 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 }
8040
Bram Moolenaar13505972019-01-24 15:04:48 +01008041 exec_normal_cmd(arg != NULL
8042 ? arg
8043 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 }
8045 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8046 }
8047
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008048 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 update_topline_cursor();
8050
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008051 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008053 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008054#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008055 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008056#endif
8057
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059}
8060
8061/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008062 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 */
8064 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008065ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008067 if (eap->forceit)
8068 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008069 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008070 if (!curwin->w_cursor.lnum)
8071 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008072 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008073 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008074#ifdef FEAT_TERMINAL
8075 // Ignore this when running in an active terminal.
8076 if (term_job_running(curbuf->b_term))
8077 return;
8078#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008079
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008080 // Ignore the command when already in Insert mode. Inserting an
8081 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008082 if (State & INSERT)
8083 return;
8084
Bram Moolenaar64969662005-12-14 21:59:55 +00008085 if (eap->cmdidx == CMD_startinsert)
8086 restart_edit = 'a';
8087 else if (eap->cmdidx == CMD_startreplace)
8088 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008090 restart_edit = 'V';
8091
8092 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008094 if (eap->cmdidx == CMD_startinsert)
8095 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008096 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 }
8098}
8099
8100/*
8101 * ":stopinsert"
8102 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008104ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105{
8106 restart_edit = 0;
8107 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008108 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008111/*
8112 * Execute normal mode command "cmd".
8113 * "remap" can be REMAP_NONE or REMAP_YES.
8114 */
8115 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008116exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008117{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008118 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008119 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008120 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008121}
Bram Moolenaar25281632016-01-21 23:32:32 +01008122
Bram Moolenaar25281632016-01-21 23:32:32 +01008123/*
8124 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008125 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008126 */
8127 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008128exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008129{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008130 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008131 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008132
Bram Moolenaar905dd902019-04-07 14:21:47 +02008133 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8134 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008135 clear_oparg(&oa);
8136 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008137 while ((!stuff_empty()
8138 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008139 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008140 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008141 {
8142 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008143#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008144 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008145 && oa.op_type == OP_NOP && oa.regname == NUL
8146 && !VIsual_active)
8147 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008148 // If terminal_loop() returns OK we got a key that is handled
8149 // in Normal model. With FAIL we first need to position the
8150 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008151 if (terminal_loop(TRUE) == OK)
8152 normal_cmd(&oa, TRUE);
8153 }
8154 else
8155#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008156 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008157 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008158 }
8159}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008160
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161#ifdef FEAT_FIND_ID
8162 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008163ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164{
8165 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8166 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8167 (linenr_T)1, (linenr_T)MAXLNUM);
8168}
8169
Bram Moolenaar4033c552017-09-16 20:54:51 +02008170#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171/*
8172 * ":psearch"
8173 */
8174 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008175ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176{
8177 g_do_tagpreview = p_pvh;
8178 ex_findpat(eap);
8179 g_do_tagpreview = 0;
8180}
8181#endif
8182
8183 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008184ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185{
8186 int whole = TRUE;
8187 long n;
8188 char_u *p;
8189 int action;
8190
8191 switch (cmdnames[eap->cmdidx].cmd_name[2])
8192 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008193 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8195 action = ACTION_GOTO;
8196 else
8197 action = ACTION_SHOW;
8198 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008199 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 action = ACTION_SHOW_ALL;
8201 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008202 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 action = ACTION_GOTO;
8204 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008205 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 action = ACTION_SPLIT;
8207 break;
8208 }
8209
8210 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008211 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 {
8213 n = getdigits(&eap->arg);
8214 eap->arg = skipwhite(eap->arg);
8215 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008216 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {
8218 whole = FALSE;
8219 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008220 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 if (*p)
8222 {
8223 *p++ = NUL;
8224 p = skipwhite(p);
8225
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008226 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008227 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008228 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 else
8230 eap->nextcmd = check_nextcmd(p);
8231 }
8232 }
8233 if (!eap->skip)
8234 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8235 whole, !eap->forceit,
8236 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8237 n, action, eap->line1, eap->line2);
8238}
8239#endif
8240
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241
Bram Moolenaar4033c552017-09-16 20:54:51 +02008242#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243/*
8244 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8245 */
8246 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008247ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008249 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8251}
8252
8253/*
8254 * ":pedit"
8255 */
8256 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008257ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258{
8259 win_T *curwin_save = curwin;
8260
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008261 if (ERROR_IF_ANY_POPUP_WINDOW)
8262 return;
8263
Bram Moolenaar026587b2019-08-17 15:08:00 +02008264 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008266 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008267
Bram Moolenaar026587b2019-08-17 15:08:00 +02008268 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008270
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 if (curwin != curwin_save && win_valid(curwin_save))
8272 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008273 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 validate_cursor();
8275 redraw_later(VALID);
8276 win_enter(curwin_save, TRUE);
8277 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008278# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008279 else if (WIN_IS_POPUP(curwin))
8280 {
8281 // can't keep focus in popup window
8282 win_enter(firstwin, TRUE);
8283 }
8284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 g_do_tagpreview = 0;
8286}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288
8289/*
8290 * ":stag", ":stselect" and ":stjump".
8291 */
8292 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008293ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294{
8295 postponed_split = -1;
8296 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008297 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8299 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008300 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302
8303/*
8304 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8305 */
8306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008307ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308{
8309 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8310}
8311
8312 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008313ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314{
8315 int cmd;
8316
8317 switch (name[1])
8318 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008319 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008321 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008323 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008325 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008327 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008329 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008331 case 'f': // ":tfirst"
8332 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008334 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008336 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008338 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008340 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 return;
8342 }
8343#endif
8344 cmd = DT_TAG;
8345 break;
8346 }
8347
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008348 if (name[0] == 'l')
8349 {
8350#ifndef FEAT_QUICKFIX
8351 ex_ni(eap);
8352 return;
8353#else
8354 cmd = DT_LTAG;
8355#endif
8356 }
8357
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8359 eap->forceit, TRUE);
8360}
8361
8362/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008363 * Check "str" for starting with a special cmdline variable.
8364 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8365 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8366 */
8367 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008368find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008369{
8370 int len;
8371 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008372 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008373 "%",
8374#define SPEC_PERC 0
8375 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008376#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008377 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008378#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008379 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008380#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008381 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008382#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008383 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008384#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008385 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008386#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008387 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008388#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008389 "<stack>", // call stack
8390#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008391 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008392#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008393 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008394#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008395 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008396#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008397 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008398#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008399 "<SID>", // script ID: <SNR>123_
8400#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008401#ifdef FEAT_CLIENTSERVER
8402 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008403# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008404#endif
8405 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008406
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008407 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008408 {
8409 len = (int)STRLEN(spec_str[i]);
8410 if (STRNCMP(src, spec_str[i], len) == 0)
8411 {
8412 *usedlen = len;
8413 return i;
8414 }
8415 }
8416 return -1;
8417}
8418
8419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 * Evaluate cmdline variables.
8421 *
8422 * change '%' to curbuf->b_ffname
8423 * '#' to curwin->w_altfile
8424 * '<cword>' to word under the cursor
8425 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008426 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 * '<cfile>' to path name under the cursor
8428 * '<sfile>' to sourced file name
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008429 * '<stack>' to call stack
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008430 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 * '<afile>' to file name for autocommand
8432 * '<abuf>' to buffer number for autocommand
8433 * '<amatch>' to matching name for autocommand
8434 *
8435 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8436 * "" for error without a message) and NULL is returned.
8437 * Returns an allocated string if a valid match was found.
8438 * Returns NULL if no match was found. "usedlen" then still contains the
8439 * number of characters to skip.
8440 */
8441 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008442eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008443 char_u *src, // pointer into commandline
8444 char_u *srcstart, // beginning of valid memory for src
8445 int *usedlen, // characters after src that are used
8446 linenr_T *lnump, // line number for :e command, or NULL
8447 char **errormsg, // pointer to error message
8448 int *escaped) // return value has escaped white space (can
8449 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450{
8451 int i;
8452 char_u *s;
8453 char_u *result;
8454 char_u *resultbuf = NULL;
8455 int resultlen;
8456 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008457 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008459 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462
8463 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008464 if (escaped != NULL)
8465 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466
8467 /*
8468 * Check if there is something to do.
8469 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008470 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008471 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 {
8473 *usedlen = 1;
8474 return NULL;
8475 }
8476
8477 /*
8478 * Skip when preceded with a backslash "\%" and "\#".
8479 * Note: In "\\%" the % is also not recognized!
8480 */
8481 if (src > srcstart && src[-1] == '\\')
8482 {
8483 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008484 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 return NULL;
8486 }
8487
8488 /*
8489 * word or WORD under cursor
8490 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008491 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8492 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008494 resultlen = find_ident_under_cursor(&result,
8495 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8496 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8497 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 if (resultlen == 0)
8499 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008500 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 return NULL;
8502 }
8503 }
8504
8505 /*
8506 * '#': Alternate file name
8507 * '%': Current file name
8508 * File name under the cursor
8509 * File name for autocommand
8510 * and following modifiers
8511 */
8512 else
8513 {
8514 switch (spec_idx)
8515 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008516 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 if (curbuf->b_fname == NULL)
8518 {
8519 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008520 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 }
8522 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008523 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008525 tilde_file = STRCMP(result, "~") == 0;
8526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 break;
8528
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008529 case SPEC_HASH: // '#' or "#99": alternate file
8530 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 {
8532 result = arg_all();
8533 resultbuf = result;
8534 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008535 if (escaped != NULL)
8536 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 break;
8539 }
8540 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008541 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008542 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008544 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008545 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008546 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008547 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008549 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008551 if (*usedlen < 2)
8552 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008553 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008554 *usedlen = 1;
8555 return NULL;
8556 }
8557#ifdef FEAT_EVAL
8558 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8559 (long)i);
8560 if (result == NULL)
8561 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008562 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008563 return NULL;
8564 }
8565#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008566 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 }
8570 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008571 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008572 if (i == 0 && src[1] == '<' && *usedlen > 1)
8573 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008574 buf = buflist_findnr(i);
8575 if (buf == NULL)
8576 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008577 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008578 return NULL;
8579 }
8580 if (lnump != NULL)
8581 *lnump = ECMD_LAST;
8582 if (buf->b_fname == NULL)
8583 {
8584 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008585 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008586 }
8587 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008588 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008589 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008590 tilde_file = STRCMP(result, "~") == 0;
8591 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 break;
8594
8595#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008596 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008597 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 if (result == NULL)
8599 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008600 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 return NULL;
8602 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008603 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 break;
8605#endif
8606
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008607 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008609 if (result != NULL && !autocmd_fname_full)
8610 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008611 // Still need to turn the fname into a full path. It is
8612 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008613 autocmd_fname_full = TRUE;
8614 result = FullName_save(autocmd_fname, FALSE);
8615 vim_free(autocmd_fname);
8616 autocmd_fname = result;
8617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 if (result == NULL)
8619 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008620 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 return NULL;
8622 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008623 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 break;
8625
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008626 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 if (autocmd_bufnr <= 0)
8628 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008629 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 return NULL;
8631 }
8632 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8633 result = strbuf;
8634 break;
8635
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008636 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 result = autocmd_match;
8638 if (result == NULL)
8639 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008640 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 return NULL;
8642 }
8643 break;
8644
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008645 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008646 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008647 result = estack_sfile(spec_idx == SPEC_SFILE
8648 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 if (result == NULL)
8650 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008651 *errormsg = spec_idx == SPEC_SFILE
8652 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8653 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 return NULL;
8655 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008656 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008658
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008659 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008660 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008661 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008662 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008663 return NULL;
8664 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008665 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008666 result = strbuf;
8667 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008668
8669#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008670 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008671 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008672 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008673 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008674 return NULL;
8675 }
8676 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008677 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008678 result = strbuf;
8679 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008680
Bram Moolenaar90944302020-08-01 20:45:11 +02008681 case SPEC_SID:
8682 if (current_sctx.sc_sid <= 0)
8683 {
8684 *errormsg = _(e_usingsid);
8685 return NULL;
8686 }
8687 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
8688 result = strbuf;
8689 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02008690#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02008691
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008692#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008693 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008694 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8695 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 result = strbuf;
8697 break;
8698#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008699
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008700 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008701 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008702 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 }
8704
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008705 resultlen = (int)STRLEN(result); // length of new string
8706 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 {
8708 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 resultlen = (int)(s - result);
8711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 else if (!skip_mod)
8713 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008714 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 &resultlen);
8716 if (result == NULL)
8717 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008718 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 return NULL;
8720 }
8721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 }
8723
8724 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8725 {
8726 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008727 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008728 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008730 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 result = NULL;
8732 }
8733 else
8734 result = vim_strnsave(result, resultlen);
8735 vim_free(resultbuf);
8736 return result;
8737}
8738
8739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 * Expand the <sfile> string in "arg".
8741 *
8742 * Returns an allocated string, or NULL for any error.
8743 */
8744 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008745expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008747 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 int len;
8749 char_u *result;
8750 char_u *newres;
8751 char_u *repl;
8752 int srclen;
8753 char_u *p;
8754
8755 result = vim_strsave(arg);
8756 if (result == NULL)
8757 return NULL;
8758
8759 for (p = result; *p; )
8760 {
8761 if (STRNCMP(p, "<sfile>", 7) != 0)
8762 ++p;
8763 else
8764 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008765 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008766 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 if (errormsg != NULL)
8768 {
8769 if (*errormsg)
8770 emsg(errormsg);
8771 vim_free(result);
8772 return NULL;
8773 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008774 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 {
8776 p += srclen;
8777 continue;
8778 }
8779 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8780 newres = alloc(len);
8781 if (newres == NULL)
8782 {
8783 vim_free(repl);
8784 vim_free(result);
8785 return NULL;
8786 }
8787 mch_memmove(newres, result, (size_t)(p - result));
8788 STRCPY(newres + (p - result), repl);
8789 len = (int)STRLEN(newres);
8790 STRCAT(newres, p + srclen);
8791 vim_free(repl);
8792 vim_free(result);
8793 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008794 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 }
8796 }
8797
8798 return result;
8799}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008802/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008803 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008804 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008807dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 if (fname == NULL)
8810 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008811 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812}
8813#endif
8814
8815/*
8816 * ":behave {mswin,xterm}"
8817 */
8818 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008819ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820{
8821 if (STRCMP(eap->arg, "mswin") == 0)
8822 {
8823 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8824 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8825 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8826 set_option_value((char_u *)"keymodel", 0L,
8827 (char_u *)"startsel,stopsel", 0);
8828 }
8829 else if (STRCMP(eap->arg, "xterm") == 0)
8830 {
8831 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8832 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8833 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8834 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8835 }
8836 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008837 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838}
8839
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840static int filetype_detect = FALSE;
8841static int filetype_plugin = FALSE;
8842static int filetype_indent = FALSE;
8843
8844/*
8845 * ":filetype [plugin] [indent] {on,off,detect}"
8846 * on: Load the filetype.vim file to install autocommands for file types.
8847 * off: Load the ftoff.vim file to remove all autocommands for file types.
8848 * plugin on: load filetype.vim and ftplugin.vim
8849 * plugin off: load ftplugof.vim
8850 * indent on: load filetype.vim and indent.vim
8851 * indent off: load indoff.vim
8852 */
8853 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008854ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855{
8856 char_u *arg = eap->arg;
8857 int plugin = FALSE;
8858 int indent = FALSE;
8859
8860 if (*eap->arg == NUL)
8861 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008862 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008863 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 filetype_detect ? "ON" : "OFF",
8865 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8866 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8867 return;
8868 }
8869
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008870 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 for (;;)
8872 {
8873 if (STRNCMP(arg, "plugin", 6) == 0)
8874 {
8875 plugin = TRUE;
8876 arg = skipwhite(arg + 6);
8877 continue;
8878 }
8879 if (STRNCMP(arg, "indent", 6) == 0)
8880 {
8881 indent = TRUE;
8882 arg = skipwhite(arg + 6);
8883 continue;
8884 }
8885 break;
8886 }
8887 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8888 {
8889 if (*arg == 'o' || !filetype_detect)
8890 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008891 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 filetype_detect = TRUE;
8893 if (plugin)
8894 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008895 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 filetype_plugin = TRUE;
8897 }
8898 if (indent)
8899 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008900 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 filetype_indent = TRUE;
8902 }
8903 }
8904 if (*arg == 'd')
8905 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008906 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008907 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 }
8909 }
8910 else if (STRCMP(arg, "off") == 0)
8911 {
8912 if (plugin || indent)
8913 {
8914 if (plugin)
8915 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008916 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 filetype_plugin = FALSE;
8918 }
8919 if (indent)
8920 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008921 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 filetype_indent = FALSE;
8923 }
8924 }
8925 else
8926 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008927 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 filetype_detect = FALSE;
8929 }
8930 }
8931 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008932 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933}
8934
8935/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008936 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 */
8938 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008939ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940{
8941 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008942 {
8943 char_u *arg = eap->arg;
8944
8945 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8946 arg += 9;
8947
8948 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8949 if (arg != eap->arg)
8950 did_filetype = FALSE;
8951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008955ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956{
8957#ifdef FEAT_DIGRAPHS
8958 if (*eap->arg != NUL)
8959 putdigraph(eap->arg);
8960 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008961 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008963 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964#endif
8965}
8966
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008967#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8968 void
8969set_no_hlsearch(int flag)
8970{
8971 no_hlsearch = flag;
8972# ifdef FEAT_EVAL
8973 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8974# endif
8975}
8976
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977/*
8978 * ":nohlsearch"
8979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008981ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008983 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008984 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986#endif
8987
8988#ifdef FEAT_CRYPT
8989/*
8990 * ":X": Get crypt key
8991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008993ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008995 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008996 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997}
8998#endif
8999
9000#ifdef FEAT_FOLDING
9001 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009002ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003{
9004 if (foldManualAllowed(TRUE))
9005 foldCreate(eap->line1, eap->line2);
9006}
9007
9008 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009009ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010{
9011 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9012 eap->forceit, FALSE);
9013}
9014
9015 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009016ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017{
9018 linenr_T lnum;
9019
Bram Moolenaar4facea32019-10-12 20:17:40 +02009020# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009021 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009022# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009023
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009024 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9026 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9027 ml_setmarked(lnum);
9028
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009029 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009031 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009032# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009033 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035}
9036#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009037
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009038#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009039/*
9040 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9041 * instead of a quickfix command.
9042 */
9043 int
9044is_loclist_cmd(int cmdidx)
9045{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009046 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009047 return FALSE;
9048 return cmdnames[cmdidx].cmd_name[0] == 'l';
9049}
9050#endif
9051
Bram Moolenaar4facea32019-10-12 20:17:40 +02009052#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009053 int
9054get_pressedreturn(void)
9055{
9056 return ex_pressedreturn;
9057}
9058
9059 void
9060set_pressedreturn(int val)
9061{
9062 ex_pressedreturn = val;
9063}
9064#endif