blob: a6bdac3abaa5ce7240bbe67667d12ee5fe13646a [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +020023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaar20b23c62020-08-20 15:25:00 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
39static void ex_bmodified(exarg_T *eap);
40static void ex_bnext(exarg_T *eap);
41static void ex_bprevious(exarg_T *eap);
42static void ex_brewind(exarg_T *eap);
43static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifndef FEAT_QUICKFIX
47# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000048# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# define ex_cc ex_ni
50# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020051# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cfile ex_ni
53# define qf_list ex_ni
54# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020055# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000057# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020059#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define ex_cclose ex_ni
61# define ex_copen ex_ni
62# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020063# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000065#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
66# define ex_cexpr ex_ni
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020069static linenr_T default_address(exarg_T *eap);
Bram Moolenaarb7316892019-05-01 18:08:42 +020070static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020071static void address_default_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010072static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020073#if !defined(FEAT_PERL) \
74 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
75 || !defined(FEAT_TCL) \
76 || !defined(FEAT_RUBY) \
77 || !defined(FEAT_LUA) \
78 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000079# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010080static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010082static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000086#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010087static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
88static void ex_highlight(exarg_T *eap);
89static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_cquit(exarg_T *eap);
91static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010092static void ex_close(exarg_T *eap);
93static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
94static void ex_only(exarg_T *eap);
95static void ex_resize(exarg_T *eap);
96static void ex_stag(exarg_T *eap);
97static void ex_tabclose(exarg_T *eap);
98static void ex_tabonly(exarg_T *eap);
99static void ex_tabnext(exarg_T *eap);
100static void ex_tabmove(exarg_T *eap);
101static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200102#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100103static void ex_pclose(exarg_T *eap);
104static void ex_ptag(exarg_T *eap);
105static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106#else
107# define ex_pclose ex_ni
108# define ex_ptag ex_ni
109# define ex_pedit ex_ni
110#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100111static void ex_hide(exarg_T *eap);
112static void ex_stop(exarg_T *eap);
113static void ex_exit(exarg_T *eap);
114static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100116static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#else
118# define ex_goto ex_ni
119#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100120static void ex_shell(exarg_T *eap);
121static void ex_preserve(exarg_T *eap);
122static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100123static void ex_mode(exarg_T *eap);
124static void ex_wrongmodifier(exarg_T *eap);
125static void ex_find(exarg_T *eap);
126static void ex_open(exarg_T *eap);
127static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#ifndef FEAT_GUI
129# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100132#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100133static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#else
135# define ex_tearoff ex_ni
136#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100137#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
138 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#else
141# define ex_popup ex_ni
142#endif
143#ifndef FEAT_GUI_MSWIN
144# define ex_simalt ex_ni
145#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000146#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147# define gui_mch_find_dialog ex_ni
148# define gui_mch_replace_dialog ex_ni
149#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000150#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151# define ex_helpfind ex_ni
152#endif
153#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100154# define ex_cscope ex_ni
155# define ex_scscope ex_ni
156# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#endif
158#ifndef FEAT_SYN_HL
159# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200160# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000161#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200162#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200163# define ex_syntime ex_ni
164#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000165#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000166# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000167# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000168# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000169# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000170# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000171#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200172#ifndef FEAT_PERSISTENT_UNDO
173# define ex_rundo ex_ni
174# define ex_wundo ex_ni
175#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200176#ifndef FEAT_LUA
177# define ex_lua ex_script_ni
178# define ex_luado ex_ni
179# define ex_luafile ex_ni
180#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000181#ifndef FEAT_MZSCHEME
182# define ex_mzscheme ex_script_ni
183# define ex_mzfile ex_ni
184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#ifndef FEAT_PERL
186# define ex_perl ex_script_ni
187# define ex_perldo ex_ni
188#endif
189#ifndef FEAT_PYTHON
190# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200191# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192# define ex_pyfile ex_ni
193#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200194#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200195# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200196# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200197# define ex_py3file ex_ni
198#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100199#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
200# define ex_pyx ex_script_ni
201# define ex_pyxdo ex_ni
202# define ex_pyxfile ex_ni
203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#ifndef FEAT_TCL
205# define ex_tcl ex_script_ni
206# define ex_tcldo ex_ni
207# define ex_tclfile ex_ni
208#endif
209#ifndef FEAT_RUBY
210# define ex_ruby ex_script_ni
211# define ex_rubydo ex_ni
212# define ex_rubyfile ex_ni
213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#ifndef FEAT_KEYMAP
215# define ex_loadkeymap ex_ni
216#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100217static void ex_swapname(exarg_T *eap);
218static void ex_syncbind(exarg_T *eap);
219static void ex_read(exarg_T *eap);
220static void ex_pwd(exarg_T *eap);
221static void ex_equal(exarg_T *eap);
222static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000225#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#else
228# define ex_winpos ex_ni
229#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100230static void ex_operators(exarg_T *eap);
231static void ex_put(exarg_T *eap);
232static void ex_copymove(exarg_T *eap);
233static void ex_submagic(exarg_T *eap);
234static void ex_join(exarg_T *eap);
235static void ex_at(exarg_T *eap);
236static void ex_bang(exarg_T *eap);
237static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200238#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100239static void ex_wundo(exarg_T *eap);
240static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200241#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ex_redo(exarg_T *eap);
243static void ex_later(exarg_T *eap);
244static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100246static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100249static void ex_startinsert(exarg_T *eap);
250static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100252static void ex_checkpath(exarg_T *eap);
253static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254#else
255# define ex_findpat ex_ni
256# define ex_checkpath ex_ni
257#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200258#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100259static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260#else
261# define ex_psearch ex_ni
262#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static void ex_tag(exarg_T *eap);
264static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#ifndef FEAT_EVAL
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200266# define ex_block ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200267# define ex_break ex_ni
268# define ex_breakadd ex_ni
269# define ex_breakdel ex_ni
270# define ex_breaklist ex_ni
271# define ex_call ex_ni
272# define ex_catch ex_ni
273# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200274# define ex_continue ex_ni
275# define ex_debug ex_ni
276# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200277# define ex_def ex_ni
278# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200279# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100280# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281# define ex_echo ex_ni
282# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283# define ex_else ex_ni
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200284# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200285# define ex_endfunction ex_ni
286# define ex_endif ex_ni
287# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200289# define ex_eval ex_ni
290# define ex_execute ex_ni
291# define ex_finally ex_ni
292# define ex_finish ex_ni
293# define ex_function ex_ni
294# define ex_if ex_ni
295# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200296# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200297# define ex_lockvar ex_ni
298# define ex_oldfiles ex_ni
299# define ex_options ex_ni
300# define ex_packadd ex_ni
301# define ex_packloadall ex_ni
302# define ex_return ex_ni
303# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304# define ex_throw ex_ni
305# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000307# define ex_unlockvar ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100308# define ex_vim9script ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200309# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100310# define ex_import ex_ni
311# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200313#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314# define ex_loadview ex_ni
315#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200316#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317# define ex_viminfo ex_ni
318#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100319static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100320static void ex_filetype(exarg_T *eap);
321static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000323# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324# define ex_diffpatch ex_ni
325# define ex_diffgetput ex_ni
326# define ex_diffsplit ex_ni
327# define ex_diffthis ex_ni
328# define ex_diffupdate ex_ni
329#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100330static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100332static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333#else
334# define ex_nohlsearch ex_ni
335# define ex_match ex_ni
336#endif
337#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100338static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339#else
340# define ex_X ex_ni
341#endif
342#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100343static void ex_fold(exarg_T *eap);
344static void ex_foldopen(exarg_T *eap);
345static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346#else
347# define ex_fold ex_ni
348# define ex_foldopen ex_ni
349# define ex_folddo ex_ni
350#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100351#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352# define ex_language ex_ni
353#endif
354#ifndef FEAT_SIGNS
355# define ex_sign ex_ni
356#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000357#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200358# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000359# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200360# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363#ifndef FEAT_JUMPLIST
364# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200365# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366# define ex_changes ex_ni
367#endif
368
Bram Moolenaar05159a02005-02-26 23:04:13 +0000369#ifndef FEAT_PROFILE
370# define ex_profile ex_ni
371#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200372#ifndef FEAT_TERMINAL
373# define ex_terminal ex_ni
374#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200375#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
376# define ex_xrestore ex_ni
377#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100378#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200379# define ex_popupclear ex_ni
380#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000381
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382/*
383 * Declare cmdnames[].
384 */
385#define DO_DECLARE_EXCMD
386#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200387#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100388
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389static char_u dollar_command[2] = {'$', 0};
390
391
392#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100393// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394typedef struct
395{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100396 char_u *line; // command line
397 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398} wcmd_T;
399
400/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000401 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000403 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000405struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100407 garray_T *lines_gap; // growarray with line info
408 int current_line; // last read line from growarray
409 int repeating; // TRUE when looping a second time
410 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200411 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 void *cookie;
413};
414
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200415static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100416static int store_loop_line(garray_T *gap, char_u *line);
417static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000418
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100419// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000420struct dbg_stuff
421{
422 int trylevel;
423 int force_abort;
424 except_T *caught_stack;
425 char_u *vv_exception;
426 char_u *vv_throwpoint;
427 int did_emsg;
428 int got_int;
429 int did_throw;
430 int need_rethrow;
431 int check_cstack;
432 except_T *current_exception;
433};
434
Bram Moolenaared203462004-06-16 11:19:22 +0000435 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100436save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000437{
438 dsp->trylevel = trylevel; trylevel = 0;
439 dsp->force_abort = force_abort; force_abort = FALSE;
440 dsp->caught_stack = caught_stack; caught_stack = NULL;
441 dsp->vv_exception = v_exception(NULL);
442 dsp->vv_throwpoint = v_throwpoint(NULL);
443
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100444 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000445 dsp->did_emsg = did_emsg; did_emsg = FALSE;
446 dsp->got_int = got_int; got_int = FALSE;
447 dsp->did_throw = did_throw; did_throw = FALSE;
448 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
449 dsp->check_cstack = check_cstack; check_cstack = FALSE;
450 dsp->current_exception = current_exception; current_exception = NULL;
451}
452
453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100454restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000455{
456 suppress_errthrow = FALSE;
457 trylevel = dsp->trylevel;
458 force_abort = dsp->force_abort;
459 caught_stack = dsp->caught_stack;
460 (void)v_exception(dsp->vv_exception);
461 (void)v_throwpoint(dsp->vv_throwpoint);
462 did_emsg = dsp->did_emsg;
463 got_int = dsp->got_int;
464 did_throw = dsp->did_throw;
465 need_rethrow = dsp->need_rethrow;
466 check_cstack = dsp->check_cstack;
467 current_exception = dsp->current_exception;
468}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469#endif
470
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471/*
472 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
473 * command is given.
474 */
475 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100476do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100477 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478{
479 int save_msg_scroll;
480 int prev_msg_row;
481 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100482 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000483
484 if (improved)
485 exmode_active = EXMODE_VIM;
486 else
487 exmode_active = EXMODE_NORMAL;
488 State = NORMAL;
489
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100490 // When using ":global /pat/ visual" and then "Q" we return to continue
491 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000492 if (global_busy)
493 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494
495 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100496 ++RedrawingDisabled; // don't redisplay the window
497 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100499 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 ++hold_gui_events;
501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502
Bram Moolenaar32526b32019-01-19 17:43:09 +0100503 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 while (exmode_active)
505 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100506 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000507 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
508 {
509 exmode_active = FALSE;
510 break;
511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 msg_scroll = TRUE;
513 need_wait_return = FALSE;
514 ex_pressedreturn = FALSE;
515 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100516 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 prev_msg_row = msg_row;
518 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 if (improved)
520 {
521 cmdline_row = msg_row;
522 do_cmdline(NULL, getexline, NULL, 0);
523 }
524 else
525 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
526 lines_left = Rows - 1;
527
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100529 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000531 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100532 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000533 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000535 if (ex_pressedreturn)
536 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100537 // go up one line, to overwrite the ":<CR>" line, so the
538 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000539 msg_row = prev_msg_row;
540 if (prev_msg_row == Rows - 1)
541 msg_row--;
542 }
543 msg_col = 0;
544 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
545 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100548 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 {
550 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100551 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000552 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100553 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 }
556
557#ifdef FEAT_GUI
558 --hold_gui_events;
559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 --RedrawingDisabled;
561 --no_wait_return;
562 update_screen(CLEAR);
563 need_wait_return = FALSE;
564 msg_scroll = save_msg_scroll;
565}
566
567/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200568 * Print the executed command for when 'verbose' is set.
569 * When "lnum" is 0 only print the command.
570 */
571 static void
572msg_verbose_cmd(linenr_T lnum, char_u *cmd)
573{
574 ++no_wait_return;
575 verbose_enter_scroll();
576
577 if (lnum == 0)
578 smsg(_("Executing: %s"), cmd);
579 else
580 smsg(_("line %ld: %s"), (long)lnum, cmd);
581 if (msg_silent == 0)
582 msg_puts("\n"); // don't overwrite this
583
584 verbose_leave_scroll();
585 --no_wait_return;
586}
587
588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 * Execute a simple command line. Used for translated commands like "*".
590 */
591 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100592do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593{
594 return do_cmdline(cmd, NULL, NULL,
595 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
596}
597
598/*
599 * do_cmdline(): execute one Ex command line
600 *
601 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100602 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 * 2. Split up in parts separated with '|'.
604 *
605 * This function can be called recursively!
606 *
607 * flags:
608 * DOCMD_VERBOSE - The command will be included in the error message.
609 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100610 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 * DOCMD_KEYTYPED - Don't reset KeyTyped.
612 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
613 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
614 *
615 * return FAIL if cmdline could not be executed, OK otherwise
616 */
617 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100618do_cmdline(
619 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200620 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100621 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100622 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100624 char_u *next_cmdline; // next cmd to execute
625 char_u *cmdline_copy = NULL; // copy of cmd line
626 int used_getline = FALSE; // used "fgetline" to obtain command
627 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100629 int count = 0; // line number count
630 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 int retval = OK;
632#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100633 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100634 garray_T lines_ga; // keep lines for ":while"/":for"
635 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200636 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100637 char_u *fname = NULL; // function or script name
638 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
639 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
640 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200642 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200643 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100645 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200646 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000648 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000650 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100652# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653# define cmd_cookie cookie
654#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100655 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200656#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100657 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100659 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
660 // location for storing error messages to be converted to an exception.
661 // This ensures that the do_errthrow() call in do_one_cmd() does not
662 // combine the messages stored by an earlier invocation of do_one_cmd()
663 // with the command name of the later one. This would happen when
664 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 saved_msg_list = msg_list;
666 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667#endif
668
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100669 // It's possible to create an endless loop with ":execute", catch that
670 // here. The value of 200 allows nested function calls, ":source", etc.
671 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100672 if (call_depth >= 200
673#ifdef FEAT_EVAL
674 && call_depth >= p_mfd
675#endif
676 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100678 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100680 // When converting to an exception, we do not include the command name
681 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100682 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 msg_list = saved_msg_list;
684#endif
685 return FAIL;
686 }
687 ++call_depth;
688
689#ifdef FEAT_EVAL
690 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000691 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 cstack.cs_trylevel = 0;
693 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000694 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
696
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100697 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100699 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100700 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000701 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 ++ex_nesting_level;
703
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100704 // Get the function or script name and the address where the next breakpoint
705 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000706 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {
708 fname = func_name(real_cookie);
709 breakpoint = func_breakpoint(real_cookie);
710 dbg_tick = func_dbg_tick(real_cookie);
711 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100712 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100714 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 breakpoint = source_breakpoint(real_cookie);
716 dbg_tick = source_dbg_tick(real_cookie);
717 }
718
719 /*
720 * Initialize "force_abort" and "suppress_errthrow" at the top level.
721 */
722 if (!recursive)
723 {
724 force_abort = FALSE;
725 suppress_errthrow = FALSE;
726 }
727
728 /*
729 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000730 * exception handling (used when debugging). Otherwise clear it to avoid
731 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000733 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000734 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000735 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200736 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
738 initial_trylevel = trylevel;
739
740 /*
741 * "did_throw" will be set to TRUE when an exception is being thrown.
742 */
743 did_throw = FALSE;
744#endif
745 /*
746 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000747 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 * If force_abort is set, we cancel everything.
749 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100750#ifdef FEAT_EVAL
751 did_emsg_cumul += did_emsg;
752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 did_emsg = FALSE;
754
755 /*
756 * KeyTyped is only set when calling vgetc(). Reset it here when not
757 * calling vgetc() (sourced command lines).
758 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100759 if (!(flags & DOCMD_KEYTYPED)
760 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 KeyTyped = FALSE;
762
763 /*
764 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000765 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 * - for multiple commands on one line, separated with '|'
767 * - when repeating until there are no more lines (for ":source")
768 */
769 next_cmdline = cmdline;
770 do
771 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000772#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100773 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000774#endif
775
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100776 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 if (next_cmdline == NULL
778#ifdef FEAT_EVAL
779 && !force_abort
780 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000781 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782#endif
783 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100784 {
785#ifdef FEAT_EVAL
786 did_emsg_cumul += did_emsg;
787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
791 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000792 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100793 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 * 3. If a line is given: Make a copy, so we can mess with it.
795 */
796
797#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100798 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000799 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100801 // Each '|' separated command is stored separately in lines_ga, to
802 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100803 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100805 // Check if a function has returned or, unless it has an unclosed
806 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000809# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000810 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000811 func_line_end(real_cookie);
812# endif
813 if (func_has_ended(real_cookie))
814 {
815 retval = FAIL;
816 break;
817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000820 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100821 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000822 script_line_end();
823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100825 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100826 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 {
828 retval = FAIL;
829 break;
830 }
831
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100832 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 if (breakpoint != NULL && dbg_tick != NULL
834 && *dbg_tick != debug_tick)
835 {
836 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100837 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100838 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 *dbg_tick = debug_tick;
840 }
841
842 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100843 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100845 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100847 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100849 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100850 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100852 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100853 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 *dbg_tick = debug_tick;
855 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000856# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000857 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000858 {
859 if (getline_is_func)
860 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100861 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000862 script_line_start();
863 }
864# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866#endif
867
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100868 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 if (next_cmdline == NULL)
870 {
871 /*
872 * Need to set msg_didout for the first line after an ":if",
873 * otherwise the ":if" will be overwritten.
874 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100875 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100877 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878#ifdef FEAT_EVAL
879 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
880#else
881 0
882#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200883 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100885 // Don't call wait_return for aborted command line. The NULL
886 // returned for the end of a sourced file or executed function
887 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 if (KeyTyped && !(flags & DOCMD_REPEAT))
889 need_wait_return = FALSE;
890 retval = FAIL;
891 break;
892 }
893 used_getline = TRUE;
894
895 /*
896 * Keep the first typed line. Clear it when more lines are typed.
897 */
898 if (flags & DOCMD_KEEPLINE)
899 {
900 vim_free(repeat_cmdline);
901 if (count == 0)
902 repeat_cmdline = vim_strsave(next_cmdline);
903 else
904 repeat_cmdline = NULL;
905 }
906 }
907
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100908 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 else if (cmdline_copy == NULL)
910 {
911 next_cmdline = vim_strsave(next_cmdline);
912 if (next_cmdline == NULL)
913 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100914 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 retval = FAIL;
916 break;
917 }
918 }
919 cmdline_copy = next_cmdline;
920
921#ifdef FEAT_EVAL
922 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200923 * Inside a while/for loop, and when the command looks like a ":while"
924 * or ":for", the line is stored, because we may need it later when
925 * looping.
926 *
927 * When there is a '|' and another command, it is stored separately,
928 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000929 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200930 *
931 * Pass a different "fgetline" function to do_one_cmd() below,
932 * that it stores lines in or reads them from "lines_ga". Makes it
933 * possible to define a function inside a while/for loop and handles
934 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200936 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200938 cmd_getline = get_loop_line;
939 cmd_cookie = (void *)&cmd_loop_cookie;
940 cmd_loop_cookie.lines_gap = &lines_ga;
941 cmd_loop_cookie.current_line = current_line;
942 cmd_loop_cookie.getline = fgetline;
943 cmd_loop_cookie.cookie = cookie;
944 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
945
946 // Save the current line when encountering it the first time.
947 if (current_line == lines_ga.ga_len
948 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 {
950 retval = FAIL;
951 break;
952 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200953 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200955 else
956 {
957 cmd_getline = fgetline;
958 cmd_cookie = cookie;
959 }
960
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 did_endif = FALSE;
962#endif
963
964 if (count++ == 0)
965 {
966 /*
967 * All output from the commands is put below each other, without
968 * waiting for a return. Don't do this when executing commands
969 * from a script or when being called recursive (e.g. for ":e
970 * +command file").
971 */
972 if (!(flags & DOCMD_NOWAIT) && !recursive)
973 {
974 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100975 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100977 msg_scroll = TRUE; // put messages below each other
978 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 ++RedrawingDisabled;
980 did_inc = TRUE;
981 }
982 }
983
Bram Moolenaar823654b2020-05-29 23:03:09 +0200984 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100985 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
987 /*
988 * 2. Execute one '|' separated command.
989 * do_one_cmd() will return NULL if there is no trailing '|'.
990 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
991 */
992 ++recursive;
993 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
994#ifdef FEAT_EVAL
995 &cstack,
996#endif
997 cmd_getline, cmd_cookie);
998 --recursive;
999
1000#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001001 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001002 // Use "current_line" from "cmd_loop_cookie", it may have been
1003 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001004 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005#endif
1006
1007 if (next_cmdline == NULL)
1008 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001009 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001010
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 /*
1012 * If the command was typed, remember it for the ':' register.
1013 * Do this AFTER executing the command to make :@: work.
1014 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001015 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 && new_last_cmdline != NULL)
1017 {
1018 vim_free(last_cmdline);
1019 last_cmdline = new_last_cmdline;
1020 new_last_cmdline = NULL;
1021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 }
1023 else
1024 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001025 // need to copy the command after the '|' to cmdline_copy, for the
1026 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001027 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 next_cmdline = cmdline_copy;
1029 }
1030
1031
1032#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001033 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001035 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001037 {
1038 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001042 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
1044 ++current_line;
1045
1046 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001047 * An ":endwhile", ":endfor" and ":continue" is handled here.
1048 * If we were executing commands, jump back to the ":while" or
1049 * ":for".
1050 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001052 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001054 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001056 // Jump back to the matching ":while" or ":for". Be careful
1057 // not to use a cs_line[] from an entry that isn't a ":while"
1058 // or ":for": It would make "current_line" invalid and can
1059 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 if (!did_emsg && !got_int && !did_throw
1061 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001062 && (cstack.cs_flags[cstack.cs_idx]
1063 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 && cstack.cs_line[cstack.cs_idx] >= 0
1065 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1066 {
1067 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001068 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001069 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001070 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001072 // Check for the next breakpoint at or after the ":while"
1073 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 if (breakpoint != NULL)
1075 {
1076 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001077 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 fname,
1079 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1080 *dbg_tick = debug_tick;
1081 }
1082 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001083 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001085 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001087 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1088 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
1090 }
1091
1092 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001093 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001095 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001098 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 }
1100 }
1101
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001102 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001103 if (breakpoint != NULL && has_watchexpr())
1104 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001105 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001106 *dbg_tick = debug_tick;
1107 }
1108
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 /*
1110 * When not inside any ":while" loop, clear remembered lines.
1111 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001112 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 {
1114 if (lines_ga.ga_len > 0)
1115 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001116 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1118 free_cmdlines(&lines_ga);
1119 }
1120 current_line = 0;
1121 }
1122
1123 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001124 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1125 * being restored at the ":endtry". Reset them here and set the
1126 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1127 * This includes the case where a missing ":endif", ":endwhile" or
1128 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001130 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001132 cstack.cs_lflags &= ~CSL_HAD_FINA;
1133 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1134 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 did_throw ? (void *)current_exception : NULL);
1136 did_emsg = got_int = did_throw = FALSE;
1137 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1138 }
1139
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001140 // Update global "trylevel" for recursive calls to do_cmdline() from
1141 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 trylevel = initial_trylevel + cstack.cs_trylevel;
1143
1144 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001145 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 * files) is aborted because of an error, an interrupt, or an uncaught
1147 * exception, cancel everything. If it is left normally, reset
1148 * force_abort to get the non-EH compatible abortion behavior for
1149 * the rest of the script.
1150 */
1151 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1152 force_abort = FALSE;
1153
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001154 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001156#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
1158 }
1159 /*
1160 * Continue executing command lines when:
1161 * - no CTRL-C typed, no aborting error, no exception thrown or try
1162 * conditionals need to be checked for executing finally clauses or
1163 * catching an interrupt exception
1164 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001165 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 * looping for ":source" command or function call.
1167 */
1168 while (!((got_int
1169#ifdef FEAT_EVAL
1170 || (did_emsg && force_abort) || did_throw
1171#endif
1172 )
1173#ifdef FEAT_EVAL
1174 && cstack.cs_trylevel == 0
1175#endif
1176 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001177 && !(did_emsg
1178#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001179 // Keep going when inside try/catch, so that the error can be
1180 // deal with, except when it is a syntax error, it may cause
1181 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001182 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1183#endif
1184 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001185 && (getline_equal(fgetline, cookie, getexmodeline)
1186 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 && (next_cmdline != NULL
1188#ifdef FEAT_EVAL
1189 || cstack.cs_idx >= 0
1190#endif
1191 || (flags & DOCMD_REPEAT)));
1192
1193 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001194 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195#ifdef FEAT_EVAL
1196 free_cmdlines(&lines_ga);
1197 ga_clear(&lines_ga);
1198
1199 if (cstack.cs_idx >= 0)
1200 {
1201 /*
1202 * If a sourced file or executed function ran to its end, report the
1203 * unclosed conditional.
1204 */
1205 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001206 && ((getline_equal(fgetline, cookie, getsourceline)
1207 && !source_finished(fgetline, cookie))
1208 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 && !func_has_ended(real_cookie))))
1210 {
1211 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001212 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001214 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001215 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001216 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001218 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 }
1220
1221 /*
1222 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1223 * ":endtry" in a sourced file or executed function. If the try
1224 * conditional is in its finally clause, ignore anything pending.
1225 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001226 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 */
1228 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001229 {
1230 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1231
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001232 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001233 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001234 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1235 &cstack.cs_looplevel);
1236 }
1237 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 trylevel = initial_trylevel;
1239 }
1240
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001241 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1242 // lack was reported above and the error message is to be converted to an
1243 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001244 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 ? (char_u *)"endfunction" : (char_u *)NULL);
1246
1247 if (trylevel == 0)
1248 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001249 // Just in case did_throw got set but current_exception wasn't.
1250 if (current_exception == NULL)
1251 did_throw = FALSE;
1252
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 /*
1254 * When an exception is being thrown out of the outermost try
1255 * conditional, discard the uncaught exception, disable the conversion
1256 * of interrupts or errors to exceptions, and ensure that no more
1257 * commands are executed.
1258 */
1259 if (did_throw)
1260 {
1261 void *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001262 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263
1264 /*
1265 * If the uncaught exception is a user exception, report it as an
1266 * error. If it is an error exception, display the saved error
1267 * message now. For an interrupt exception, do nothing; the
1268 * interrupt message is given elsewhere.
1269 */
1270 switch (current_exception->type)
1271 {
1272 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001273 vim_snprintf((char *)IObuff, IOSIZE,
1274 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 current_exception->value);
1276 p = vim_strsave(IObuff);
1277 break;
1278 case ET_ERROR:
1279 messages = current_exception->messages;
1280 current_exception->messages = NULL;
1281 break;
1282 case ET_INTERRUPT:
1283 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 }
1285
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001286 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1287 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001288 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 current_exception->throw_name = NULL;
1290
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001291 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 suppress_errthrow = TRUE;
1293 force_abort = TRUE;
1294
1295 if (messages != NULL)
1296 {
1297 do
1298 {
1299 next = messages->next;
1300 emsg(messages->msg);
1301 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001302 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 vim_free(messages);
1304 messages = next;
1305 }
1306 while (messages != NULL);
1307 }
1308 else if (p != NULL)
1309 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001310 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 vim_free(p);
1312 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001313 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001314 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001315 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 }
1317
1318 /*
1319 * On an interrupt or an aborting error not converted to an exception,
1320 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001321 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 * when force_abort is set and did_emsg unset in case of an interrupt
1323 * from a finally clause after an error.
1324 */
1325 else if (got_int || (did_emsg && force_abort))
1326 suppress_errthrow = TRUE;
1327 }
1328
1329 /*
1330 * The current cstack will be freed when do_cmdline() returns. An uncaught
1331 * exception will have to be rethrown in the previous cstack. If a function
1332 * has just returned or a script file was just finished and the previous
1333 * cstack belongs to the same function or, respectively, script file, it
1334 * will have to be checked for finally clauses to be executed due to the
1335 * ":return" or ":finish". This is done in do_one_cmd().
1336 */
1337 if (did_throw)
1338 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001339 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001341 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 && ex_nesting_level > func_level(real_cookie) + 1))
1343 {
1344 if (!did_throw)
1345 check_cstack = TRUE;
1346 }
1347 else
1348 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001349 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001350 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 --ex_nesting_level;
1352 /*
1353 * Go to debug mode when returning from a function in which we are
1354 * single-stepping.
1355 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001356 if ((getline_equal(fgetline, cookie, getsourceline)
1357 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001359 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 ? (char_u *)_("End of sourced file")
1361 : (char_u *)_("End of function"));
1362 }
1363
1364 /*
1365 * Restore the exception environment (done after returning from the
1366 * debugger).
1367 */
1368 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001369 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370
1371 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001372
1373 // Cleanup if "cs_emsg_silent_list" remains.
1374 if (cstack.cs_emsg_silent_list != NULL)
1375 {
1376 eslist_T *elem, *temp;
1377
1378 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1379 {
1380 temp = elem->next;
1381 vim_free(elem);
1382 }
1383 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001384#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385
1386 /*
1387 * If there was too much output to fit on the command line, ask the user to
1388 * hit return before redrawing the screen. With the ":global" command we do
1389 * this only once after the command is finished.
1390 */
1391 if (did_inc)
1392 {
1393 --RedrawingDisabled;
1394 --no_wait_return;
1395 msg_scroll = FALSE;
1396
1397 /*
1398 * When just finished an ":if"-":else" which was typed, no need to
1399 * wait for hit-return. Also for an error situation.
1400 */
1401 if (retval == FAIL
1402#ifdef FEAT_EVAL
1403 || (did_endif && KeyTyped && !did_emsg)
1404#endif
1405 )
1406 {
1407 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001408 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 }
1410 else if (need_wait_return)
1411 {
1412 /*
1413 * The msg_start() above clears msg_didout. The wait_return we do
1414 * here should not overwrite the command that may be shown before
1415 * doing that.
1416 */
1417 msg_didout |= msg_didout_before_start;
1418 wait_return(FALSE);
1419 }
1420 }
1421
Bram Moolenaar2a942252012-11-28 23:03:07 +01001422#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001423 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001424#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 /*
1426 * Reset if_level, in case a sourced script file contains more ":if" than
1427 * ":endif" (could be ":if x | foo | endif").
1428 */
1429 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001430#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001431
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 --call_depth;
1433 return retval;
1434}
1435
1436#ifdef FEAT_EVAL
1437/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001438 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 */
1440 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001441get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001443 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 wcmd_T *wp;
1445 char_u *line;
1446
1447 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1448 {
1449 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001450 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001452 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001454 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001456 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001457 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 ++cp->current_line;
1459
1460 return line;
1461 }
1462
1463 KeyTyped = FALSE;
1464 ++cp->current_line;
1465 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001466 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 return vim_strsave(wp->line);
1468}
1469
1470/*
1471 * Store a line in "gap" so that a ":while" loop can execute it again.
1472 */
1473 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001474store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475{
1476 if (ga_grow(gap, 1) == FAIL)
1477 return FAIL;
1478 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001479 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 return OK;
1482}
1483
1484/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001485 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 */
1487 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001488free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489{
1490 while (gap->ga_len > 0)
1491 {
1492 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1493 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 }
1495}
1496#endif
1497
1498/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001499 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1500 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001503getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001504 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001505 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001506 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507{
1508#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001509 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001510 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001512 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1513 // function that's originally used to obtain the lines. This may be
1514 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001515 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001516 cp = (struct loop_cookie *)cookie;
1517 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 {
1519 gp = cp->getline;
1520 cp = cp->cookie;
1521 }
1522 return gp == func;
1523#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001524 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525#endif
1526}
1527
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001529 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 * getline function. Otherwise return "cookie".
1531 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001533getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001534 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001535 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536{
Bram Moolenaar13505972019-01-24 15:04:48 +01001537#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001538 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001539 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001541 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1542 // cookie that's originally used to obtain the lines. This may be nested
1543 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001544 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001545 cp = (struct loop_cookie *)cookie;
1546 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 {
1548 gp = cp->getline;
1549 cp = cp->cookie;
1550 }
1551 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001552#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001555}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556
Bram Moolenaard5053d02020-06-28 15:51:16 +02001557#if defined(FEAT_EVAL) || defined(PROT)
1558/*
1559 * Get the next line source line without advancing.
1560 */
1561 char_u *
1562getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001563 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001564 void *cookie) // argument for fgetline()
1565{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001566 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001567 struct loop_cookie *cp;
1568 wcmd_T *wp;
1569
1570 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1571 // cookie that's originally used to obtain the lines. This may be nested
1572 // several levels.
1573 gp = fgetline;
1574 cp = (struct loop_cookie *)cookie;
1575 while (gp == get_loop_line)
1576 {
1577 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1578 {
1579 // executing lines a second time, use the stored copy
1580 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1581 return wp->line;
1582 }
1583 gp = cp->getline;
1584 cp = cp->cookie;
1585 }
1586 if (gp == getsourceline)
1587 return source_nextline(cp);
1588 return NULL;
1589}
1590#endif
1591
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001592
1593/*
1594 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1595 * ":bwipeout", etc.
1596 * Returns the buffer number.
1597 */
1598 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001599compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001600{
1601 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001602 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001603 int count = offset;
1604
1605 buf = firstbuf;
1606 while (buf->b_next != NULL && buf->b_fnum < lnum)
1607 buf = buf->b_next;
1608 while (count != 0)
1609 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001610 count += (offset < 0) ? 1 : -1;
1611 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1612 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001613 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001614 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001615 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001616 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001617 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 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001625 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001626 if (addr_type == ADDR_LOADED_BUFFERS)
1627 while (buf->b_ml.ml_mfp == NULL)
1628 {
1629 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1630 if (nextbuf == NULL)
1631 break;
1632 buf = nextbuf;
1633 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001634 return buf->b_fnum;
1635}
1636
Bram Moolenaarb7316892019-05-01 18:08:42 +02001637/*
1638 * Return the window number of "win".
1639 * When "win" is NULL return the number of windows.
1640 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001641 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001642current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001643{
1644 win_T *wp;
1645 int nr = 0;
1646
Bram Moolenaar29323592016-07-24 22:04:11 +02001647 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001648 {
1649 ++nr;
1650 if (wp == win)
1651 break;
1652 }
1653 return nr;
1654}
1655
1656 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001657current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001658{
1659 tabpage_T *tp;
1660 int nr = 0;
1661
Bram Moolenaar29323592016-07-24 22:04:11 +02001662 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001663 {
1664 ++nr;
1665 if (tp == tab)
1666 break;
1667 }
1668 return nr;
1669}
1670
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001671 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001672comment_start(char_u *p, int starts_with_colon UNUSED)
1673{
1674#ifdef FEAT_EVAL
1675 if (in_vim9script())
1676 return p[0] == '#' && p[1] != '{' && !starts_with_colon;
1677#endif
1678 return *p == '"';
1679}
1680
Bram Moolenaarf240e182014-11-27 18:33:02 +01001681# define CURRENT_WIN_NR current_win_nr(curwin)
1682# define LAST_WIN_NR current_win_nr(NULL)
1683# define CURRENT_TAB_NR current_tab_nr(curtab)
1684# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001685
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686/*
1687 * Execute one Ex command.
1688 *
1689 * If 'sourcing' is TRUE, the command will be included in the error message.
1690 *
1691 * 1. skip comment lines and leading space
1692 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001693 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001694 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001695 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001696 * 6. parse arguments
1697 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001699 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 *
1701 * This function may be called recursively!
1702 */
1703#if (_MSC_VER == 1200)
1704/*
Bram Moolenaared203462004-06-16 11:19:22 +00001705 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001707 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708#endif
1709 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001710do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001711 char_u **cmdlinep,
1712 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001714 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001716 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001717 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001719 char_u *p;
1720 linenr_T lnum;
1721 long n;
1722 char *errormsg = NULL; // error message
1723 char_u *after_modifier = NULL;
1724 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001725 cmdmod_T save_cmdmod;
1726 int save_reg_executing = reg_executing;
1727 int ni; // set when Not Implemented
1728 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001729 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001730#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001731 int may_have_range;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001732 int vim9script = in_vim9script();
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001733 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735
Bram Moolenaara80faa82020-04-12 19:37:17 +02001736 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 ea.line1 = 1;
1738 ea.line2 = 1;
1739#ifdef FEAT_EVAL
1740 ++ex_nesting_level;
1741#endif
1742
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001743 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 if (quitmore
1745#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001746 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001747 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001748#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001749 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001750 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 --quitmore;
1752
1753 /*
1754 * Reset browse, confirm, etc.. They are restored when returning, for
1755 * recursive calls.
1756 */
1757 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001759 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001760 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1761 goto doend;
1762
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001763/*
1764 * 1. Skip comment lines and leading white space and colons.
1765 * 2. Handle command modifiers.
1766 */
1767 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001769 ea.cmdlinep = cmdlinep;
1770 ea.getline = fgetline;
1771 ea.cookie = cookie;
1772#ifdef FEAT_EVAL
1773 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001774 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001776 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001777 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001778 apply_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001780 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
1782#ifdef FEAT_EVAL
1783 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1784 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1785#else
1786 ea.skip = (if_level > 0);
1787#endif
1788
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001790 * 3. Skip over the range to find the command. Let "p" point to after it.
1791 *
1792 * We need the command to know what kind of range it uses.
1793 */
1794 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001795#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001796 // In Vim9 script a colon is required before the range. This may also be
1797 // after command modifiers.
1798 if (vim9script)
1799 {
1800 may_have_range = FALSE;
1801 for (p = ea.cmd; p >= *cmdlinep; --p)
1802 {
1803 if (*p == ':')
1804 may_have_range = TRUE;
1805 if (p < ea.cmd && !VIM_ISWHITE(*p))
1806 break;
1807 }
1808 }
1809 else
1810 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001811 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001812#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001813 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001814
1815#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001816 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001817 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001818 if (ea.cmd == cmd + 1 && *cmd == '$')
1819 // should be "$VAR = val"
1820 --ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001822 if (ea.cmdidx == CMD_SIZE)
1823 {
1824 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1825
1826 // If a ':' before the range is missing, give a clearer error
1827 // message.
1828 if (ar > ea.cmd)
1829 {
1830 emsg(_(e_colon_required_before_a_range));
1831 goto doend;
1832 }
1833 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001834 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 else
1836#endif
1837 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001838
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001839#ifdef FEAT_EVAL
1840# ifdef FEAT_PROFILE
1841 // Count this line for profiling if skip is TRUE.
1842 if (do_profiling == PROF_YES
1843 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1844 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1845 {
1846 int skip = did_emsg || got_int || did_throw;
1847
1848 if (ea.cmdidx == CMD_catch)
1849 skip = !skip && !(cstack->cs_idx >= 0
1850 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1851 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1852 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1853 skip = skip || !(cstack->cs_idx >= 0
1854 && !(cstack->cs_flags[cstack->cs_idx]
1855 & (CSF_ACTIVE | CSF_TRUE)));
1856 else if (ea.cmdidx == CMD_finally)
1857 skip = FALSE;
1858 else if (ea.cmdidx != CMD_endif
1859 && ea.cmdidx != CMD_endfor
1860 && ea.cmdidx != CMD_endtry
1861 && ea.cmdidx != CMD_endwhile)
1862 skip = ea.skip;
1863
1864 if (!skip)
1865 {
1866 if (getline_equal(fgetline, cookie, get_func_line))
1867 func_line_exec(getline_cookie(fgetline, cookie));
1868 else if (getline_equal(fgetline, cookie, getsourceline))
1869 script_line_exec();
1870 }
1871 }
1872# endif
1873
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001874 // May go to debug mode. If this happens and the ">quit" debug command is
1875 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001876 dbg_check_breakpoint(&ea);
1877 if (!ea.skip && got_int)
1878 {
1879 ea.skip = TRUE;
1880 (void)do_intthrow(cstack);
1881 }
1882#endif
1883
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001884/*
1885 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 *
1887 * where 'addr' is:
1888 *
1889 * % (entire file)
1890 * $ [+-NUM]
1891 * 'x [+-NUM] (where x denotes a currently defined mark)
1892 * . [+-NUM]
1893 * [+-NUM]..
1894 * NUM
1895 *
1896 * The ea.cmd pointer is updated to point to the first character following the
1897 * range spec. If an initial address is found, but no second, the upper bound
1898 * is equal to the lower.
1899 */
1900
Bram Moolenaar25190db2019-05-04 15:05:28 +02001901 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001902 if (!IS_USER_CMDIDX(ea.cmdidx))
1903 {
1904 if (ea.cmdidx != CMD_SIZE)
1905 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1906 else
1907 ea.addr_type = ADDR_LINES;
1908
Bram Moolenaar25190db2019-05-04 15:05:28 +02001909 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001910 if (ea.cmdidx == CMD_wincmd && p != NULL)
1911 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001912#ifdef FEAT_QUICKFIX
1913 // :.cc in quickfix window uses line number
1914 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1915 ea.addr_type = ADDR_OTHER;
1916#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001917 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001918
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001919 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001920#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001921 if (!may_have_range)
1922 ea.line1 = ea.line2 = default_address(&ea);
1923 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001924#endif
1925 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1926 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001929 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 */
1931
1932 /*
1933 * Skip ':' and any white space
1934 */
1935 ea.cmd = skipwhite(ea.cmd);
1936 while (*ea.cmd == ':')
1937 ea.cmd = skipwhite(ea.cmd + 1);
1938
1939 /*
1940 * If we got a line, but no command, then go to the line.
1941 * If we find a '|' or '\n' we set ea.nextcmd.
1942 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001943 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1944 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 {
1946 /*
1947 * strange vi behaviour:
1948 * ":3" jumps to line 3
1949 * ":3|..." prints line 3
1950 * ":|" prints current line
1951 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001952 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001954 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 {
1956 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001957 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 if ((errormsg = invalid_range(&ea)) == NULL)
1959 {
1960 correct_range(&ea);
1961 ex_print(&ea);
1962 }
1963 }
1964 else if (ea.addr_count != 0)
1965 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001966 if (ea.line2 > curbuf->b_ml.ml_line_count)
1967 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001968 // With '-' in 'cpoptions' a line number past the file is an
1969 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001970 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1971 ea.line2 = -1;
1972 else
1973 ea.line2 = curbuf->b_ml.ml_line_count;
1974 }
1975
1976 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001977 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 else
1979 {
1980 if (ea.line2 == 0)
1981 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 else
1983 curwin->w_cursor.lnum = ea.line2;
1984 beginline(BL_SOL | BL_FIX);
1985 }
1986 }
1987 goto doend;
1988 }
1989
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001990 // If this looks like an undefined user command and there are CmdUndefined
1991 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001992 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1993 && ASCII_ISUPPER(*ea.cmd)
1994 && has_cmdundefined())
1995 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001996 int ret;
1997
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001998 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001999 while (ASCII_ISALNUM(*p))
2000 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002001 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002002 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2003 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002004 // If the autocommands did something and didn't cause an error, try
2005 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002006 p = (ret
2007#ifdef FEAT_EVAL
2008 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002009#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002011 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002012
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 if (p == NULL)
2014 {
2015 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002016 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 goto doend;
2018 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002019 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002020 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2021 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 {
2023 errormsg = uc_fun_cmd();
2024 goto doend;
2025 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002026
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 if (ea.cmdidx == CMD_SIZE)
2028 {
2029 if (!ea.skip)
2030 {
2031 STRCPY(IObuff, _("E492: Not an editor command"));
2032 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002033 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002034 // If the modifier was parsed OK the error must be in the
2035 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002036 if (after_modifier != NULL)
2037 append_command(after_modifier);
2038 else
2039 append_command(*cmdlinep);
2040 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002041 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002042 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 }
2044 goto doend;
2045 }
2046
Bram Moolenaar958636c2014-10-21 20:01:58 +02002047 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2048 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002049#ifdef HAVE_EX_SCRIPT_NI
2050 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2051#endif
2052 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053
2054#ifndef FEAT_EVAL
2055 /*
2056 * When the expression evaluation is disabled, recognize the ":if" and
2057 * ":endif" commands and ignore everything in between it.
2058 */
2059 if (ea.cmdidx == CMD_if)
2060 ++if_level;
2061 if (if_level)
2062 {
2063 if (ea.cmdidx == CMD_endif)
2064 --if_level;
2065 goto doend;
2066 }
2067
2068#endif
2069
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002070 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002071 if (*p == '!' && ea.cmdidx != CMD_substitute
2072 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 {
2074 ++p;
2075 ea.forceit = TRUE;
2076 }
2077 else
2078 ea.forceit = FALSE;
2079
2080/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002081 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002083 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002084 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085
2086 if (!ea.skip)
2087 {
2088#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002089 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002091 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002092 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 goto doend;
2094 }
2095#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002096 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002097 {
2098 errormsg = _("E981: Command not allowed in rvim");
2099 goto doend;
2100 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002101 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002103 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002104 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 goto doend;
2106 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002107
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002108 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002110#ifdef FEAT_CMDWIN
2111 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2112 {
2113 // Command not allowed in the command line window
2114 errormsg = _(e_cmdwin);
2115 goto doend;
2116 }
2117#endif
2118 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2119 {
2120 // Command not allowed when text is locked
2121 errormsg = _(get_text_locked_msg());
2122 goto doend;
2123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002125
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002126 // Disallow editing another buffer when "curbuf_lock" is set.
2127 // Do allow ":checktime" (it is postponed).
2128 // Do allow ":edit" (check for an argument later).
2129 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002130 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002131 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002132 && ea.cmdidx != CMD_edit
2133 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002134 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002135 && curbuf_locked())
2136 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002138 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002140 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002141 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 goto doend;
2143 }
2144 }
2145
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002146 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002148 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 goto doend;
2150 }
2151
2152 /*
2153 * Don't complain about the range if it is not used
2154 * (could happen if line_count is accidentally set to 0).
2155 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002156 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 {
2158 /*
2159 * If the range is backwards, ask for confirmation and, if given, swap
2160 * ea.line1 & ea.line2 so it's forwards again.
2161 * When global command is busy, don't ask, will fail below.
2162 */
2163 if (!global_busy && ea.line1 > ea.line2)
2164 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002165 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002167 if (sourcing || exmode_active)
2168 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002169 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002170 goto doend;
2171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 if (ask_yesno((char_u *)
2173 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002174 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 }
2176 lnum = ea.line1;
2177 ea.line1 = ea.line2;
2178 ea.line2 = lnum;
2179 }
2180 if ((errormsg = invalid_range(&ea)) != NULL)
2181 goto doend;
2182 }
2183
Bram Moolenaarb7316892019-05-01 18:08:42 +02002184 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2185 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 ea.line2 = 1;
2187
2188 correct_range(&ea);
2189
2190#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002191 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002192 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002194 // Put the first line at the start of a closed fold, put the last line
2195 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 (void)hasFolding(ea.line1, &ea.line1, NULL);
2197 (void)hasFolding(ea.line2, NULL, &ea.line2);
2198 }
2199#endif
2200
2201#ifdef FEAT_QUICKFIX
2202 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002203 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 * option here, so things like % get expanded.
2205 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002206 p = replace_makeprg(&ea, p, cmdlinep);
2207 if (p == NULL)
2208 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209#endif
2210
2211 /*
2212 * Skip to start of argument.
2213 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2214 */
2215 if (ea.cmdidx == CMD_bang)
2216 ea.arg = p;
2217 else
2218 ea.arg = skipwhite(p);
2219
Bram Moolenaar379fb762018-08-30 15:58:28 +02002220 // ":file" cannot be run with an argument when "curbuf_lock" is set
2221 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2222 goto doend;
2223
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 /*
2225 * Check for "++opt=val" argument.
2226 * Must be first, allow ":w ++enc=utf8 !cmd"
2227 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002228 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2230 if (getargopt(&ea) == FAIL && !ni)
2231 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002232 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 goto doend;
2234 }
2235
2236 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2237 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002238 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002240 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002242 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 goto doend;
2244 }
2245 ea.arg = skipwhite(ea.arg + 1);
2246 ea.append = TRUE;
2247 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002248 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 {
2250 ++ea.arg;
2251 ea.usefilter = TRUE;
2252 }
2253 }
2254
2255 if (ea.cmdidx == CMD_read)
2256 {
2257 if (ea.forceit)
2258 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002259 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 ea.forceit = FALSE;
2261 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002262 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 {
2264 ++ea.arg;
2265 ea.usefilter = TRUE;
2266 }
2267 }
2268
2269 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2270 {
2271 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002272 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
2274 ++ea.arg;
2275 ++ea.amount;
2276 }
2277 ea.arg = skipwhite(ea.arg);
2278 }
2279
2280 /*
2281 * Check for "+command" argument, before checking for next command.
2282 * Don't do this for ":read !cmd" and ":write !cmd".
2283 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002284 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2286
2287 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002288 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 * Don't do this for ":read !cmd" and ":write !cmd".
2290 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002291 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 separate_nextcmd(&ea);
2293
2294 /*
2295 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002296 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2297 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002299 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002300 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002301 || ea.cmdidx == CMD_global
2302 || ea.cmdidx == CMD_vglobal
2303 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 {
2305 for (p = ea.arg; *p; ++p)
2306 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002307 // Remove one backslash before a newline, so that it's possible to
2308 // pass a newline to the shell and also a newline that is preceded
2309 // with a backslash. This makes it impossible to end a shell
2310 // command in a backslash, but that doesn't appear useful.
2311 // Halving the number of backslashes is incompatible with previous
2312 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002314 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 else if (*p == '\n')
2316 {
2317 ea.nextcmd = p + 1;
2318 *p = NUL;
2319 break;
2320 }
2321 }
2322 }
2323
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002324 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002325 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002327 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002328 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002330 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002331 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002332 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002334#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002335 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002336 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002338 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002339 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 }
2341#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002342 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2343 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002344 {
2345 ea.regname = *ea.arg++;
2346#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002347 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002348 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2349 {
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002350 set_expr_line(vim_strsave(ea.arg), &ea);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002351 ea.arg += STRLEN(ea.arg);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002352 did_set_expr_line = TRUE;
Bram Moolenaar85de2062011-05-05 14:26:41 +02002353 }
2354#endif
2355 ea.arg = skipwhite(ea.arg);
2356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 }
2358
2359 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002360 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 * count, it's a buffer name.
2362 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002363 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2364 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002365 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 {
2367 n = getdigits(&ea.arg);
2368 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002369 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002371 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 goto doend;
2373 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002374 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 {
2376 ea.line2 = n;
2377 if (ea.addr_count == 0)
2378 ea.addr_count = 1;
2379 }
2380 else
2381 {
2382 ea.line1 = ea.line2;
2383 ea.line2 += n - 1;
2384 ++ea.addr_count;
2385 /*
2386 * Be vi compatible: no error message for out of range.
2387 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002388 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 ea.line2 = curbuf->b_ml.ml_line_count;
2390 }
2391 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002392
2393 /*
2394 * Check for flags: 'l', 'p' and '#'.
2395 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002396 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002397 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002398 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2399 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002401 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002402 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 goto doend;
2404 }
2405
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002406 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002408 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 goto doend;
2410 }
2411
2412#ifdef FEAT_EVAL
2413 /*
2414 * Skip the command when it's not going to be executed.
2415 * The commands like :if, :endif, etc. always need to be executed.
2416 * Also make an exception for commands that handle a trailing command
2417 * themselves.
2418 */
2419 if (ea.skip)
2420 {
2421 switch (ea.cmdidx)
2422 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002423 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 case CMD_while:
2425 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002426 case CMD_for:
2427 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 case CMD_if:
2429 case CMD_elseif:
2430 case CMD_else:
2431 case CMD_endif:
2432 case CMD_try:
2433 case CMD_catch:
2434 case CMD_finally:
2435 case CMD_endtry:
2436 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002437 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 break;
2439
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002440 // Commands that handle '|' themselves. Check: A command should
2441 // either have the EX_TRLBAR flag, appear in this list or appear in
2442 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 case CMD_aboveleft:
2444 case CMD_and:
2445 case CMD_belowright:
2446 case CMD_botright:
2447 case CMD_browse:
2448 case CMD_call:
2449 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002450 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 case CMD_delfunction:
2452 case CMD_djump:
2453 case CMD_dlist:
2454 case CMD_dsearch:
2455 case CMD_dsplit:
2456 case CMD_echo:
2457 case CMD_echoerr:
2458 case CMD_echomsg:
2459 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002460 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002462 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002463 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 case CMD_help:
2465 case CMD_hide:
2466 case CMD_ijump:
2467 case CMD_ilist:
2468 case CMD_isearch:
2469 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002470 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 case CMD_keepjumps:
2472 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002473 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 case CMD_leftabove:
2475 case CMD_let:
2476 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002477 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002478 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002480 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002481 case CMD_noautocmd:
2482 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 case CMD_perl:
2484 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002485 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002486 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002487 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 case CMD_return:
2489 case CMD_rightbelow:
2490 case CMD_ruby:
2491 case CMD_silent:
2492 case CMD_smagic:
2493 case CMD_snomagic:
2494 case CMD_substitute:
2495 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002496 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 case CMD_tcl:
2498 case CMD_throw:
2499 case CMD_tilde:
2500 case CMD_topleft:
2501 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002502 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002503 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 case CMD_verbose:
2505 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002506 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 break;
2508
2509 default: goto doend;
2510 }
2511 }
2512#endif
2513
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002514 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 {
2516 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2517 goto doend;
2518 }
2519
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 /*
2521 * Accept buffer name. Cannot be used at the same time with a buffer
2522 * number. Don't do this for a user command.
2523 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002524 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002525 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
2527 /*
2528 * :bdelete, :bwipeout and :bunload take several arguments, separated
2529 * by spaces: find next space (skipping over escaped characters).
2530 * The others take one argument: ignore trailing spaces.
2531 */
2532 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2533 || ea.cmdidx == CMD_bunload)
2534 p = skiptowhite_esc(ea.arg);
2535 else
2536 {
2537 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002538 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 --p;
2540 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002541 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002542 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002543 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 goto doend;
2545 ea.addr_count = 1;
2546 ea.arg = skipwhite(p);
2547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002549 // The :try command saves the emsg_silent flag, reset it here when
2550 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002551 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002552 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002553 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002554 if (emsg_silent < 0)
2555 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002556 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002557 }
2558
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002560 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562
Bram Moolenaar958636c2014-10-21 20:01:58 +02002563 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 {
2565 /*
2566 * Execute a user-defined command.
2567 */
2568 do_ucmd(&ea);
2569 }
2570 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 {
2572 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002573 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 */
2575 ea.errmsg = NULL;
2576 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2577 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002578 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 }
2580
2581#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002583 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2584 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002585 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 /*
2588 * If the command just executed called do_cmdline(), any throw or ":return"
2589 * or ":finish" encountered there must also check the cstack of the still
2590 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2591 * exception, or reanimate a returned function or finished script file and
2592 * return or finish it again.
2593 */
2594 if (need_rethrow)
2595 do_throw(cstack);
2596 else if (check_cstack)
2597 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002598 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002600 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 && current_func_returned())
2602 do_return(&ea, TRUE, FALSE, NULL);
2603 }
2604 need_rethrow = check_cstack = FALSE;
2605#endif
2606
2607doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002608 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002609 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002611 curwin->w_cursor.col = 0;
2612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613
2614 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2615 {
2616 if (sourcing)
2617 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002618 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 {
2620 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002621 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002623 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 }
2625 emsg(errormsg);
2626 }
2627#ifdef FEAT_EVAL
2628 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002629 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2630 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002631
2632 if (did_set_expr_line)
2633 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634#endif
2635
Bram Moolenaare1004402020-10-24 20:49:43 +02002636 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002638 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002640 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 ea.nextcmd = NULL;
2642
2643#ifdef FEAT_EVAL
2644 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002645 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646#endif
2647
2648 return ea.nextcmd;
2649}
2650#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002651 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652#endif
2653
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002654static char ex_error_buf[MSG_BUF_LEN];
2655
2656/*
2657 * Return an error message with argument included.
2658 * Uses a static buffer, only the last error will be kept.
2659 * "msg" will be translated, caller should use N_().
2660 */
2661 char *
2662ex_errmsg(char *msg, char_u *arg)
2663{
2664 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2665 return ex_error_buf;
2666}
2667
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002669 * Parse and skip over command modifiers:
2670 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002671 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002672 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002673 * When "skip_only" is TRUE the global variables are not changed, except for
2674 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002675 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2676 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002677 * Call apply_cmdmod() to get the side effects of the modifiers:
2678 * - Increment "sandbox" for ":sandbox"
2679 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002680 * - set msg_silent for ":silent"
2681 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002682 * Return FAIL when the command is not to be executed.
2683 * May set "errormsg" to an error message.
2684 */
2685 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002686parse_command_modifiers(
2687 exarg_T *eap,
2688 char **errormsg,
2689 cmdmod_T *cmod,
2690 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002691{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002692 char_u *p;
2693 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002694
Bram Moolenaare1004402020-10-24 20:49:43 +02002695 CLEAR_POINTER(cmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002696
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002697 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002698 for (;;)
2699 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002700 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002701 {
2702 if (*eap->cmd == ':')
2703 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002704 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002705 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002706
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002707 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002708 if (*eap->cmd == NUL && exmode_active
2709 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2710 || getline_equal(eap->getline, eap->cookie, getexline))
2711 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2712 {
2713 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002714 if (!skip_only)
2715 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002716 }
2717
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002718 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002719 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002720 return FAIL;
2721 if (*eap->cmd == NUL)
2722 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002723 if (!skip_only)
2724 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002725 return FAIL;
2726 }
2727
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002728 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002729 switch (*p)
2730 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002731 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002732 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2733 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002734 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002735 continue;
2736
2737 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2738 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002739 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002740 continue;
2741 }
2742 if (checkforcmd(&eap->cmd, "browse", 3))
2743 {
2744#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002745 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002746#endif
2747 continue;
2748 }
2749 if (!checkforcmd(&eap->cmd, "botright", 2))
2750 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002751 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002752 continue;
2753
2754 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2755 break;
2756#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002757 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002758#endif
2759 continue;
2760
2761 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2762 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002763 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002764 continue;
2765 }
2766 if (checkforcmd(&eap->cmd, "keepalt", 5))
2767 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002768 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002769 continue;
2770 }
2771 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2772 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002773 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002774 continue;
2775 }
2776 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2777 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002778 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002779 continue;
2780
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002781 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002782 {
2783 char_u *reg_pat;
2784
2785 if (!checkforcmd(&p, "filter", 4)
2786 || *p == NUL || ends_excmd(*p))
2787 break;
2788 if (*p == '!')
2789 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002790 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002791 p = skipwhite(p + 1);
2792 if (*p == NUL || ends_excmd(*p))
2793 break;
2794 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002795#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002796 // Avoid that "filter(arg)" is recognized.
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +01002797 if (in_vim9script() && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002798 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002799#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002800 if (skip_only)
2801 p = skip_vimgrep_pat(p, NULL, NULL);
2802 else
2803 // NOTE: This puts a NUL after the pattern.
2804 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002805 if (p == NULL || *p == NUL)
2806 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002807 if (!skip_only)
2808 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002809 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002810 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002811 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002812 break;
2813 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002814 eap->cmd = p;
2815 continue;
2816 }
2817
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002818 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002819 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2820 || *p == NUL || ends_excmd(*p))
2821 break;
2822 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002823 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002824 continue;
2825
2826 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2827 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002828 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002829 continue;
2830 }
2831
2832 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2833 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002834 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002835 continue;
2836
2837 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2838 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002839 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002840 continue;
2841 }
2842 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2843 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002844 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002845 continue;
2846
2847 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2848 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002849 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002850 continue;
2851
2852 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2853 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002854 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02002855 continue;
2856 }
2857 if (!checkforcmd(&eap->cmd, "silent", 3))
2858 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002859 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002860 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2861 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002862 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002863 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02002864 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002865 }
2866 continue;
2867
2868 case 't': if (checkforcmd(&p, "tab", 3))
2869 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002870 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002871 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002872 long tabnr = get_address(eap, &eap->cmd,
2873 ADDR_TABS, eap->skip,
2874 skip_only, FALSE, 1);
2875 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02002876 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01002877 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002878 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002879 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2880 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002881 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002882 return FAIL;
2883 }
Bram Moolenaare1004402020-10-24 20:49:43 +02002884 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002885 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002886 }
2887 eap->cmd = p;
2888 continue;
2889 }
2890 if (!checkforcmd(&eap->cmd, "topleft", 2))
2891 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002892 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02002893 continue;
2894
2895 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2896 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002897 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002898 continue;
2899
2900 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2901 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002902 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002903 continue;
2904 }
2905 if (!checkforcmd(&p, "verbose", 4))
2906 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002907 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02002908 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002909 else
Bram Moolenaare1004402020-10-24 20:49:43 +02002910 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002911 eap->cmd = p;
2912 continue;
2913 }
2914 break;
2915 }
2916
2917 return OK;
2918}
2919
2920/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002921 * Apply the command modifiers. Saves current state in "cmdmod", call
2922 * undo_cmdmod() later.
2923 */
2924 void
2925apply_cmdmod(cmdmod_T *cmod)
2926{
2927#ifdef HAVE_SANDBOX
2928 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
2929 {
2930 ++sandbox;
2931 cmod->cmod_did_sandbox = TRUE;
2932 }
2933#endif
2934 if (cmod->cmod_verbose > 0)
2935 {
2936 if (cmod->cmod_verbose_save == 0)
2937 cmod->cmod_verbose_save = p_verbose + 1;
2938 p_verbose = cmod->cmod_verbose;
2939 }
2940
2941 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
2942 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02002943 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002944 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02002945 cmod->cmod_save_msg_scroll = msg_scroll;
2946 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002947 if (cmod->cmod_flags & CMOD_SILENT)
2948 ++msg_silent;
2949 if (cmod->cmod_flags & CMOD_UNSILENT)
2950 msg_silent = 0;
2951
2952 if (cmod->cmod_flags & CMOD_ERRSILENT)
2953 {
2954 ++emsg_silent;
2955 ++cmod->cmod_did_esilent;
2956 }
2957
Bram Moolenaare1004402020-10-24 20:49:43 +02002958 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002959 {
2960 // Set 'eventignore' to "all".
2961 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02002962 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002963 set_string_option_direct((char_u *)"ei", -1,
2964 (char_u *)"all", OPT_FREE, SID_NONE);
2965 }
2966}
2967
2968/*
Bram Moolenaare1004402020-10-24 20:49:43 +02002969 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002970 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002971 void
Bram Moolenaare1004402020-10-24 20:49:43 +02002972undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002973{
Bram Moolenaare1004402020-10-24 20:49:43 +02002974 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002975 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002976 p_verbose = cmod->cmod_verbose_save - 1;
2977 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002978 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002979
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002980#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02002981 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002982 {
2983 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02002984 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002985 }
2986#endif
2987
Bram Moolenaare1004402020-10-24 20:49:43 +02002988 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002989 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002990 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02002991 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
2992 OPT_FREE, SID_NONE);
2993 free_string_option(cmod->cmod_save_ei);
2994 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002995 }
2996
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01002997 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002998
Bram Moolenaare1004402020-10-24 20:49:43 +02002999 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003000 {
3001 // messages could be enabled for a serious error, need to check if the
3002 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003003 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3004 msg_silent = cmod->cmod_save_msg_silent - 1;
3005 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003006 if (emsg_silent < 0)
3007 emsg_silent = 0;
3008 // Restore msg_scroll, it's set by file I/O commands, even when no
3009 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003010 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003011
3012 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3013 // somewhere in the line. Put it back in the first column.
3014 if (redirecting())
3015 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003016
Bram Moolenaare1004402020-10-24 20:49:43 +02003017 cmod->cmod_save_msg_silent = 0;
3018 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003019 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003020}
3021
3022/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003023 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003024 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003025 * Return FAIL and set "errormsg" or return OK.
3026 */
3027 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003028parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003029{
3030 int address_count = 1;
3031 linenr_T lnum;
3032
3033 // Repeat for all ',' or ';' separated addresses.
3034 for (;;)
3035 {
3036 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003037 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003038 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003039 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003040 eap->addr_count == 0, address_count++);
3041 if (eap->cmd == NULL) // error detected
3042 return FAIL;
3043 if (lnum == MAXLNUM)
3044 {
3045 if (*eap->cmd == '%') // '%' - all lines
3046 {
3047 ++eap->cmd;
3048 switch (eap->addr_type)
3049 {
3050 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003051 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003052 eap->line1 = 1;
3053 eap->line2 = curbuf->b_ml.ml_line_count;
3054 break;
3055 case ADDR_LOADED_BUFFERS:
3056 {
3057 buf_T *buf = firstbuf;
3058
3059 while (buf->b_next != NULL
3060 && buf->b_ml.ml_mfp == NULL)
3061 buf = buf->b_next;
3062 eap->line1 = buf->b_fnum;
3063 buf = lastbuf;
3064 while (buf->b_prev != NULL
3065 && buf->b_ml.ml_mfp == NULL)
3066 buf = buf->b_prev;
3067 eap->line2 = buf->b_fnum;
3068 break;
3069 }
3070 case ADDR_BUFFERS:
3071 eap->line1 = firstbuf->b_fnum;
3072 eap->line2 = lastbuf->b_fnum;
3073 break;
3074 case ADDR_WINDOWS:
3075 case ADDR_TABS:
3076 if (IS_USER_CMDIDX(eap->cmdidx))
3077 {
3078 eap->line1 = 1;
3079 eap->line2 = eap->addr_type == ADDR_WINDOWS
3080 ? LAST_WIN_NR : LAST_TAB_NR;
3081 }
3082 else
3083 {
3084 // there is no Vim command which uses '%' and
3085 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003086 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003087 return FAIL;
3088 }
3089 break;
3090 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003091 case ADDR_UNSIGNED:
3092 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003093 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003094 return FAIL;
3095 case ADDR_ARGUMENTS:
3096 if (ARGCOUNT == 0)
3097 eap->line1 = eap->line2 = 0;
3098 else
3099 {
3100 eap->line1 = 1;
3101 eap->line2 = ARGCOUNT;
3102 }
3103 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003104 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003105#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003106 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003107 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003108 if (eap->line2 == 0)
3109 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003110#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003111 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003112 case ADDR_NONE:
3113 // Will give an error later if a range is found.
3114 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003115 }
3116 ++eap->addr_count;
3117 }
3118 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3119 {
3120 pos_T *fp;
3121
3122 // '*' - visual area
3123 if (eap->addr_type != ADDR_LINES)
3124 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003125 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003126 return FAIL;
3127 }
3128
3129 ++eap->cmd;
3130 if (!eap->skip)
3131 {
3132 fp = getmark('<', FALSE);
3133 if (check_mark(fp) == FAIL)
3134 return FAIL;
3135 eap->line1 = fp->lnum;
3136 fp = getmark('>', FALSE);
3137 if (check_mark(fp) == FAIL)
3138 return FAIL;
3139 eap->line2 = fp->lnum;
3140 ++eap->addr_count;
3141 }
3142 }
3143 }
3144 else
3145 eap->line2 = lnum;
3146 eap->addr_count++;
3147
3148 if (*eap->cmd == ';')
3149 {
3150 if (!eap->skip)
3151 {
3152 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003153 // Don't leave the cursor on an illegal line or column, but do
3154 // accept zero as address, so 0;/PATTERN/ works correctly.
3155 if (eap->line2 > 0)
3156 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003157 }
3158 }
3159 else if (*eap->cmd != ',')
3160 break;
3161 ++eap->cmd;
3162 }
3163
3164 // One address given: set start and end lines.
3165 if (eap->addr_count == 1)
3166 {
3167 eap->line1 = eap->line2;
3168 // ... but only implicit: really no address given
3169 if (lnum == MAXLNUM)
3170 eap->addr_count = 0;
3171 }
3172 return OK;
3173}
3174
3175/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003176 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 * If there is a match advance "pp" to the argument and return TRUE.
3178 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003179 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003180checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003181 char_u **pp, // start of command
3182 char *cmd, // name of command
3183 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184{
3185 int i;
3186
3187 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003188 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 break;
Bram Moolenaar7a66a172020-10-16 19:56:12 +02003190 if (i >= len && !isalpha((*pp)[i]) && (*pp)[i] != '_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 {
3192 *pp = skipwhite(*pp + i);
3193 return TRUE;
3194 }
3195 return FALSE;
3196}
3197
3198/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003199 * Append "cmd" to the error message in IObuff.
3200 * Takes care of limiting the length and handling 0xa0, which would be
3201 * invisible otherwise.
3202 */
3203 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003204append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003205{
3206 char_u *s = cmd;
3207 char_u *d;
3208
3209 STRCAT(IObuff, ": ");
3210 d = IObuff + STRLEN(IObuff);
3211 while (*s != NUL && d - IObuff < IOSIZE - 7)
3212 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003213 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003214 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003215 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003216 STRCPY(d, "<a0>");
3217 d += 4;
3218 }
3219 else
3220 MB_COPY_CHAR(s, d);
3221 }
3222 *d = NUL;
3223}
3224
3225/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003226 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3227 * the name. Otherwise just return "start".
3228 */
3229 char_u *
3230skip_option_env_lead(char_u *start)
3231{
3232 char_u *name = start;
3233
3234 if (*start == '&')
3235 {
3236 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3237 name += 3;
3238 else
3239 name += 1;
3240 }
3241 else if (*start == '$')
3242 name += 1;
3243 return name;
3244}
3245
3246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003248 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003249 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003250 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 *
3252 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3253 * assignment without "let". Sets eap->cmdidx to the command while returning
3254 * "eap->cmd".
3255 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 * Returns NULL for an ambiguous user command.
3257 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 char_u *
3259find_ex_command(
3260 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003261 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003262 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003263 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264{
3265 int len;
3266 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003267 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269#ifdef FEAT_EVAL
3270 /*
3271 * Recognize a Vim9 script function/method call and assignment:
3272 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3273 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003274 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003275 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003277 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278
Bram Moolenaar83144542020-08-02 20:40:43 +02003279 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003280 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003282 int oplen;
3283 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003285 if (
3286 // "(..." is an expression.
3287 // "funcname(" is always a function call.
3288 *p == '('
3289 || (p == eap->cmd
3290 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003291 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003292 *eap->cmd == '{'
3293 // "'string'->func()" is an expression.
3294 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003295 // '"string"->func()' is an expression.
3296 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003297 // "g:varname" is an expression.
3298 || eap->cmd[1] == ':'
3299 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003300 // "varname->func()" is an expression.
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003301 : (*p == '-' && p[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003302 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003303 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3304 {
3305 // "{" by itself is the start of a block.
3306 eap->cmdidx = CMD_block;
3307 return eap->cmd + 1;
3308 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003309 eap->cmdidx = CMD_eval;
3310 return eap->cmd;
3311 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003312
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003313 if (p != eap->cmd && (
3314 // "varname[]" is an expression.
3315 *p == '['
3316 // "varname.key" is an expression.
3317 || (*p == '.' && ASCII_ISALPHA(p[1]))))
3318 {
3319 char_u *after = p;
3320
3321 // When followed by "=" or "+=" then it is an assignment.
3322 ++emsg_silent;
Bram Moolenaar683581e2020-10-22 21:22:58 +02003323 if (skip_expr(&after, NULL) == OK
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003324 && (*after == '='
3325 || (*after != NUL && after[1] == '=')))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003326 eap->cmdidx = CMD_var;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003327 else
3328 eap->cmdidx = CMD_eval;
3329 --emsg_silent;
3330 return eap->cmd;
3331 }
3332
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003333 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3334 // an assignment.
3335 // If there is no line break inside the "[...]" then "p" is
3336 // advanced to after the "]" by to_name_const_end(): check if a "="
3337 // follows.
3338 // If "[...]" has a line break "p" still points at the "[" and it
3339 // can't be an assignment.
3340 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003341 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003342 p = to_name_const_end(eap->cmd);
3343 if (p == eap->cmd || *skipwhite(p) != '=')
3344 {
3345 eap->cmdidx = CMD_eval;
3346 return eap->cmd;
3347 }
3348 if (p > eap->cmd && *skipwhite(p) == '=')
3349 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003350 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003351 return eap->cmd;
3352 }
3353 }
3354
3355 // Recognize an assignment if we recognize the variable name:
3356 // "g:var = expr"
3357 // "var = expr" where "var" is a local var name.
Bram Moolenaar83144542020-08-02 20:40:43 +02003358 if (*eap->cmd == '@')
3359 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003360 oplen = assignment_len(skipwhite(p), &heredoc);
3361 if (oplen > 0)
3362 {
3363 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3364 || *eap->cmd == '&'
3365 || *eap->cmd == '$'
3366 || *eap->cmd == '@'
3367 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
3368 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003369 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003370 return eap->cmd;
3371 }
3372 }
3373
3374 // Recognize using a type for a w:, b:, t: or g: variable:
3375 // "w:varname: number = 123".
3376 if (eap->cmd[1] == ':' && *p == ':')
3377 {
3378 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003379 return eap->cmd;
3380 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003381 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 }
3383#endif
3384
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 /*
3386 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003387 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 * - the 'k' command can directly be followed by any character.
3389 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003390 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003392 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 */
3394 p = eap->cmd;
3395 if (*p == 'k')
3396 {
3397 eap->cmdidx = CMD_k;
3398 ++p;
3399 }
3400 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003401 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3402 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 || p[1] == 'g'
3404 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3405 || p[1] == 'I'
3406 || (p[1] == 'r' && p[2] != 'e')))
3407 {
3408 eap->cmdidx = CMD_substitute;
3409 ++p;
3410 }
3411 else
3412 {
3413 while (ASCII_ISALPHA(*p))
3414 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003415 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003416 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003418 while (ASCII_ISALNUM(*p))
3419 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 }
3421 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3422 {
3423 // include "9" for "vim9script"
3424 ++p;
3425 while (ASCII_ISALPHA(*p))
3426 ++p;
3427 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003428
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003429 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003430 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 ++p;
3432 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003433 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3434 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003435 // Check for ":dl", ":dell", etc. to ":deletel": that's
3436 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003437 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003438 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003439 break;
3440 if (i == len - 1)
3441 {
3442 --len;
3443 if (p[-1] == 'l')
3444 eap->flags |= EXFLAG_LIST;
3445 else
3446 eap->flags |= EXFLAG_PRINT;
3447 }
3448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003450 if (ASCII_ISLOWER(eap->cmd[0]))
3451 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003452 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003453 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003454
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003455 if (command_count != (int)CMD_SIZE)
3456 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003457 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003458 getout(1);
3459 }
3460
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003461 // Use a precomputed index for fast look-up in cmdnames[]
3462 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003463 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3464 if (ASCII_ISLOWER(c2))
3465 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3466 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003467 else if (ASCII_ISUPPER(eap->cmd[0]))
3468 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003470 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471
3472 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3473 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3474 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3475 (size_t)len) == 0)
3476 {
3477#ifdef FEAT_EVAL
3478 if (full != NULL
3479 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3480 *full = TRUE;
3481#endif
3482 break;
3483 }
3484
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003485 // Look for a user defined command as a last resort. Let ":Print" be
3486 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003487 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3488 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003490 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 while (ASCII_ISALNUM(*p))
3492 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003493 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003495 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 eap->cmdidx = CMD_SIZE;
3497 }
3498
Bram Moolenaar373863e2020-09-26 17:20:53 +02003499 // ":fina" means ":finally" for backwards compatibility.
3500 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3501 eap->cmdidx = CMD_finally;
3502
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 return p;
3504}
3505
3506#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003507static struct cmdmod
3508{
3509 char *name;
3510 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003511 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003512} cmdmods[] = {
3513 {"aboveleft", 3, FALSE},
3514 {"belowright", 3, FALSE},
3515 {"botright", 2, FALSE},
3516 {"browse", 3, FALSE},
3517 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003518 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003519 {"hide", 3, FALSE},
3520 {"keepalt", 5, FALSE},
3521 {"keepjumps", 5, FALSE},
3522 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003523 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003524 {"leftabove", 5, FALSE},
3525 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003526 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003527 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003528 {"rightbelow", 6, FALSE},
3529 {"sandbox", 3, FALSE},
3530 {"silent", 3, FALSE},
3531 {"tab", 3, TRUE},
3532 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003533 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003534 {"verbose", 4, TRUE},
3535 {"vertical", 4, FALSE},
3536};
3537
3538/*
3539 * Return length of a command modifier (including optional count).
3540 * Return zero when it's not a modifier.
3541 */
3542 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003543modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003544{
3545 int i, j;
3546 char_u *p = cmd;
3547
3548 if (VIM_ISDIGIT(*cmd))
3549 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003550 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003551 {
3552 for (j = 0; p[j] != NUL; ++j)
3553 if (p[j] != cmdmods[i].name[j])
3554 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003555 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003556 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003557 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003558 }
3559 return 0;
3560}
3561
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562/*
3563 * Return > 0 if an Ex command "name" exists.
3564 * Return 2 if there is an exact match.
3565 * Return 3 if there is an ambiguous match.
3566 */
3567 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003568cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569{
3570 exarg_T ea;
3571 int full = FALSE;
3572 int i;
3573 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003574 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003576 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003577 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 {
3579 for (j = 0; name[j] != NUL; ++j)
3580 if (name[j] != cmdmods[i].name[j])
3581 break;
3582 if (name[j] == NUL && j >= cmdmods[i].minlen)
3583 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3584 }
3585
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003586 // Check built-in commands and user defined commands.
3587 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003588 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003591 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003593 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3594 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003595 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003596 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3598}
3599#endif
3600
Bram Moolenaard0190392019-08-23 21:17:35 +02003601 cmdidx_T
3602excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603{
Bram Moolenaard0190392019-08-23 21:17:35 +02003604 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605
Bram Moolenaard0190392019-08-23 21:17:35 +02003606 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3607 idx = (cmdidx_T)((int)idx + 1))
3608 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 break;
3610
Bram Moolenaard0190392019-08-23 21:17:35 +02003611 return idx;
3612}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
Bram Moolenaard0190392019-08-23 21:17:35 +02003614 long
3615excmd_get_argt(cmdidx_T idx)
3616{
3617 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618}
3619
3620/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003621 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 *
3623 * Backslashed delimiters after / or ? will be skipped, and commands will
3624 * not be expanded between /'s and ?'s or after "'".
3625 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003626 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 * Returns the "cmd" pointer advanced to beyond the range.
3628 */
3629 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003630skip_range(
3631 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003632 int skip_star, // skip "*" used for Visual range
3633 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003635 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003637 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003639 if (*cmd == '\\')
3640 {
3641 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3642 ++cmd;
3643 else
3644 break;
3645 }
3646 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 {
3648 if (*++cmd == NUL && ctx != NULL)
3649 *ctx = EXPAND_NOTHING;
3650 }
3651 else if (*cmd == '/' || *cmd == '?')
3652 {
3653 delim = *cmd++;
3654 while (*cmd != NUL && *cmd != delim)
3655 if (*cmd++ == '\\' && *cmd != NUL)
3656 ++cmd;
3657 if (*cmd == NUL && ctx != NULL)
3658 *ctx = EXPAND_NOTHING;
3659 }
3660 if (*cmd != NUL)
3661 ++cmd;
3662 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003663
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003664 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003665 while (*cmd == ':')
3666 cmd = skipwhite(cmd + 1);
3667
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003668 // Skip "*" used for Visual range.
3669 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3670 cmd = skipwhite(cmd + 1);
3671
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 return cmd;
3673}
3674
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003675 static void
3676addr_error(cmd_addr_T addr_type)
3677{
3678 if (addr_type == ADDR_NONE)
3679 emsg(_(e_norange));
3680 else
3681 emsg(_(e_invrange));
3682}
3683
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003685 * Return the default address for an address type.
3686 */
3687 static linenr_T
3688default_address(exarg_T *eap)
3689{
3690 linenr_T lnum = 0;
3691
3692 switch (eap->addr_type)
3693 {
3694 case ADDR_LINES:
3695 case ADDR_OTHER:
3696 // Default is the cursor line number. Avoid using an invalid
3697 // line number though.
3698 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3699 lnum = curbuf->b_ml.ml_line_count;
3700 else
3701 lnum = curwin->w_cursor.lnum;
3702 break;
3703 case ADDR_WINDOWS:
3704 lnum = CURRENT_WIN_NR;
3705 break;
3706 case ADDR_ARGUMENTS:
3707 lnum = curwin->w_arg_idx + 1;
3708 if (lnum > ARGCOUNT)
3709 lnum = ARGCOUNT;
3710 break;
3711 case ADDR_LOADED_BUFFERS:
3712 case ADDR_BUFFERS:
3713 lnum = curbuf->b_fnum;
3714 break;
3715 case ADDR_TABS:
3716 lnum = CURRENT_TAB_NR;
3717 break;
3718 case ADDR_TABS_RELATIVE:
3719 case ADDR_UNSIGNED:
3720 lnum = 1;
3721 break;
3722 case ADDR_QUICKFIX:
3723#ifdef FEAT_QUICKFIX
3724 lnum = qf_get_cur_idx(eap);
3725#endif
3726 break;
3727 case ADDR_QUICKFIX_VALID:
3728#ifdef FEAT_QUICKFIX
3729 lnum = qf_get_cur_valid_idx(eap);
3730#endif
3731 break;
3732 case ADDR_NONE:
3733 // Will give an error later if a range is found.
3734 break;
3735 }
3736 return lnum;
3737}
3738
3739/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003740 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 *
3742 * Set ptr to the next character after the part that was interpreted.
3743 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003744 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 *
3746 * Return MAXLNUM when no Ex address was found.
3747 */
3748 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003749get_address(
3750 exarg_T *eap UNUSED,
3751 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003752 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003753 int skip, // only skip the address, don't use it
3754 int silent, // no errors or side effects
3755 int to_other_file, // flag: may jump to other file
3756 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757{
3758 int c;
3759 int i;
3760 long n;
3761 char_u *cmd;
3762 pos_T pos;
3763 pos_T *fp;
3764 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003765 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766
3767 cmd = skipwhite(*ptr);
3768 lnum = MAXLNUM;
3769 do
3770 {
3771 switch (*cmd)
3772 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003773 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003774 ++cmd;
3775 switch (addr_type)
3776 {
3777 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003778 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 lnum = curwin->w_cursor.lnum;
3780 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003781 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003782 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003783 break;
3784 case ADDR_ARGUMENTS:
3785 lnum = curwin->w_arg_idx + 1;
3786 break;
3787 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003788 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003789 lnum = curbuf->b_fnum;
3790 break;
3791 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003792 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003793 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003794 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003795 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003796 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003797 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003798 cmd = NULL;
3799 goto error;
3800 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003801 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003802#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003803 lnum = qf_get_cur_idx(eap);
3804#endif
3805 break;
3806 case ADDR_QUICKFIX_VALID:
3807#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003808 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003809#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003810 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003811 }
3812 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003814 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003815 ++cmd;
3816 switch (addr_type)
3817 {
3818 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003819 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 lnum = curbuf->b_ml.ml_line_count;
3821 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003822 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003823 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003824 break;
3825 case ADDR_ARGUMENTS:
3826 lnum = ARGCOUNT;
3827 break;
3828 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003829 buf = lastbuf;
3830 while (buf->b_ml.ml_mfp == NULL)
3831 {
3832 if (buf->b_prev == NULL)
3833 break;
3834 buf = buf->b_prev;
3835 }
3836 lnum = buf->b_fnum;
3837 break;
3838 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003839 lnum = lastbuf->b_fnum;
3840 break;
3841 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003842 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003843 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003844 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003845 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003846 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003847 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003848 cmd = NULL;
3849 goto error;
3850 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003851 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003852#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003853 lnum = qf_get_size(eap);
3854 if (lnum == 0)
3855 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003856#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003857 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003858 case ADDR_QUICKFIX_VALID:
3859#ifdef FEAT_QUICKFIX
3860 lnum = qf_get_valid_size(eap);
3861 if (lnum == 0)
3862 lnum = 1;
3863#endif
3864 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003865 }
3866 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003868 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003869 if (*++cmd == NUL)
3870 {
3871 cmd = NULL;
3872 goto error;
3873 }
3874 if (addr_type != ADDR_LINES)
3875 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003876 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003877 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003878 goto error;
3879 }
3880 if (skip)
3881 ++cmd;
3882 else
3883 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003884 // Only accept a mark in another file when it is
3885 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003886 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3887 ++cmd;
3888 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003889 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003890 lnum = curwin->w_cursor.lnum;
3891 else
3892 {
3893 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 {
3895 cmd = NULL;
3896 goto error;
3897 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003898 lnum = fp->lnum;
3899 }
3900 }
3901 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902
3903 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003904 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003905 c = *cmd++;
3906 if (addr_type != ADDR_LINES)
3907 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003908 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003909 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003910 goto error;
3911 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003912 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003913 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003914 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003915 if (*cmd == c)
3916 ++cmd;
3917 }
3918 else
3919 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003920 int flags;
3921
3922 pos = curwin->w_cursor; // save curwin->w_cursor
3923
3924 // When '/' or '?' follows another address, start from
3925 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003926 if (lnum != MAXLNUM)
3927 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003928
3929 // Start a forward search at the end of the line (unless
3930 // before the first line).
3931 // Start a backward search at the start of the line.
3932 // This makes sure we never match in the current
3933 // line, and can match anywhere in the
3934 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003935 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003936 curwin->w_cursor.col = MAXCOL;
3937 else
3938 curwin->w_cursor.col = 0;
3939 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003940 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003941 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003942 {
3943 curwin->w_cursor = pos;
3944 cmd = NULL;
3945 goto error;
3946 }
3947 lnum = curwin->w_cursor.lnum;
3948 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003949 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003950 cmd += searchcmdlen;
3951 }
3952 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003954 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003955 ++cmd;
3956 if (addr_type != ADDR_LINES)
3957 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003958 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003959 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003960 goto error;
3961 }
3962 if (*cmd == '&')
3963 i = RE_SUBST;
3964 else if (*cmd == '?' || *cmd == '/')
3965 i = RE_SEARCH;
3966 else
3967 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003968 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003969 cmd = NULL;
3970 goto error;
3971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003973 if (!skip)
3974 {
3975 /*
3976 * When search follows another address, start from
3977 * there.
3978 */
3979 if (lnum != MAXLNUM)
3980 pos.lnum = lnum;
3981 else
3982 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003984 /*
3985 * Start the search just like for the above
3986 * do_search().
3987 */
3988 if (*cmd != '?')
3989 pos.col = MAXCOL;
3990 else
3991 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003992 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003993 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003994 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003995 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003996 lnum = pos.lnum;
3997 else
3998 {
3999 cmd = NULL;
4000 goto error;
4001 }
4002 }
4003 ++cmd;
4004 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005
4006 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004007 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004008 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 }
4010
4011 for (;;)
4012 {
4013 cmd = skipwhite(cmd);
4014 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4015 break;
4016
4017 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004018 {
4019 switch (addr_type)
4020 {
4021 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004022 case ADDR_OTHER:
4023 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004024 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004025 break;
4026 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004027 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004028 break;
4029 case ADDR_ARGUMENTS:
4030 lnum = curwin->w_arg_idx + 1;
4031 break;
4032 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004033 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004034 lnum = curbuf->b_fnum;
4035 break;
4036 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004037 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004038 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004039 case ADDR_TABS_RELATIVE:
4040 lnum = 1;
4041 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004042 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004043#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004044 lnum = qf_get_cur_idx(eap);
4045#endif
4046 break;
4047 case ADDR_QUICKFIX_VALID:
4048#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004049 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004050#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004051 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004052 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004053 case ADDR_UNSIGNED:
4054 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004055 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004056 }
4057 }
4058
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004060 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 else
4062 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004063 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 n = 1;
4065 else
4066 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004067
4068 if (addr_type == ADDR_TABS_RELATIVE)
4069 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004070 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004071 cmd = NULL;
4072 goto error;
4073 }
4074 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004075 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004076 lnum = compute_buffer_local_count(
4077 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004079 {
4080#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004081 // Relative line addressing, need to adjust for folded lines
4082 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004083 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4084 && address_count >= 2)
4085 (void)hasFolding(lnum, NULL, &lnum);
4086#endif
4087 if (i == '-')
4088 lnum -= n;
4089 else
4090 lnum += n;
4091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
4093 } while (*cmd == '/' || *cmd == '?');
4094
4095error:
4096 *ptr = cmd;
4097 return lnum;
4098}
4099
4100/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004101 * Set eap->line1 and eap->line2 to the whole range.
4102 * Used for commands with the EX_DFLALL flag and no range given.
4103 */
4104 static void
4105address_default_all(exarg_T *eap)
4106{
4107 eap->line1 = 1;
4108 switch (eap->addr_type)
4109 {
4110 case ADDR_LINES:
4111 case ADDR_OTHER:
4112 eap->line2 = curbuf->b_ml.ml_line_count;
4113 break;
4114 case ADDR_LOADED_BUFFERS:
4115 {
4116 buf_T *buf = firstbuf;
4117
4118 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4119 buf = buf->b_next;
4120 eap->line1 = buf->b_fnum;
4121 buf = lastbuf;
4122 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4123 buf = buf->b_prev;
4124 eap->line2 = buf->b_fnum;
4125 }
4126 break;
4127 case ADDR_BUFFERS:
4128 eap->line1 = firstbuf->b_fnum;
4129 eap->line2 = lastbuf->b_fnum;
4130 break;
4131 case ADDR_WINDOWS:
4132 eap->line2 = LAST_WIN_NR;
4133 break;
4134 case ADDR_TABS:
4135 eap->line2 = LAST_TAB_NR;
4136 break;
4137 case ADDR_TABS_RELATIVE:
4138 eap->line2 = 1;
4139 break;
4140 case ADDR_ARGUMENTS:
4141 if (ARGCOUNT == 0)
4142 eap->line1 = eap->line2 = 0;
4143 else
4144 eap->line2 = ARGCOUNT;
4145 break;
4146 case ADDR_QUICKFIX_VALID:
4147#ifdef FEAT_QUICKFIX
4148 eap->line2 = qf_get_valid_size(eap);
4149 if (eap->line2 == 0)
4150 eap->line2 = 1;
4151#endif
4152 break;
4153 case ADDR_NONE:
4154 case ADDR_UNSIGNED:
4155 case ADDR_QUICKFIX:
4156 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4157 break;
4158 }
4159}
4160
4161
4162/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004163 * Get flags from an Ex command argument.
4164 */
4165 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004166get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004167{
4168 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4169 {
4170 if (*eap->arg == 'l')
4171 eap->flags |= EXFLAG_LIST;
4172 else if (*eap->arg == 'p')
4173 eap->flags |= EXFLAG_PRINT;
4174 else
4175 eap->flags |= EXFLAG_NR;
4176 eap->arg = skipwhite(eap->arg + 1);
4177 }
4178}
4179
4180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 * Function called for command which is Not Implemented. NI!
4182 */
4183 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004184ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185{
4186 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004187 eap->errmsg =
4188 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189}
4190
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004191#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192/*
4193 * Function called for script command which is Not Implemented. NI!
4194 * Skips over ":perl <<EOF" constructs.
4195 */
4196 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004197ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198{
4199 if (!eap->skip)
4200 ex_ni(eap);
4201 else
4202 vim_free(script_get(eap, eap->arg));
4203}
4204#endif
4205
4206/*
4207 * Check range in Ex command for validity.
4208 * Return NULL when valid, error message when invalid.
4209 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004210 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004211invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004213 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004214
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 if ( eap->line1 < 0
4216 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004217 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004218 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004219
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004220 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004221 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004222 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004223 {
4224 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004225 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004226#ifdef FEAT_DIFF
4227 + (eap->cmdidx == CMD_diffget)
4228#endif
4229 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004230 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004231 break;
4232 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004233 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004234 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004235 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004236 break;
4237 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004238 // Only a boundary check, not whether the buffers actually
4239 // exist.
4240 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004241 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004242 break;
4243 case ADDR_LOADED_BUFFERS:
4244 buf = firstbuf;
4245 while (buf->b_ml.ml_mfp == NULL)
4246 {
4247 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004248 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004249 buf = buf->b_next;
4250 }
4251 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004252 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004253 buf = lastbuf;
4254 while (buf->b_ml.ml_mfp == NULL)
4255 {
4256 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004257 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004258 buf = buf->b_prev;
4259 }
4260 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004261 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004262 break;
4263 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004264 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004265 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004266 break;
4267 case ADDR_TABS:
4268 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004269 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004270 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004271 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004272 case ADDR_OTHER:
4273 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004274 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004275 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004276#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004277 // No error for value that is too big, will use the last entry.
4278 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004279 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004280#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004281 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004282 case ADDR_QUICKFIX_VALID:
4283#ifdef FEAT_QUICKFIX
4284 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4285 || eap->line2 < 0)
4286 return _(e_invrange);
4287#endif
4288 break;
4289 case ADDR_UNSIGNED:
4290 if (eap->line2 < 0)
4291 return _(e_invrange);
4292 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004293 case ADDR_NONE:
4294 // Will give an error elsewhere.
4295 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004296 }
4297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 return NULL;
4299}
4300
4301/*
4302 * Correct the range for zero line number, if required.
4303 */
4304 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004305correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004307 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 {
4309 if (eap->line1 == 0)
4310 eap->line1 = 1;
4311 if (eap->line2 == 0)
4312 eap->line2 = 1;
4313 }
4314}
4315
Bram Moolenaar748bf032005-02-02 23:04:36 +00004316#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004317/*
4318 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4319 * pattern. Otherwise return eap->arg.
4320 */
4321 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004322skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004323{
4324 char_u *p = eap->arg;
4325
Bram Moolenaara37420f2006-02-04 22:37:47 +00004326 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4327 || eap->cmdidx == CMD_vimgrepadd
4328 || eap->cmdidx == CMD_lvimgrepadd
4329 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004330 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004331 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004332 if (p == NULL)
4333 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004334 }
4335 return p;
4336}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004337
4338/*
4339 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4340 * in the command line, so that things like % get expanded.
4341 */
4342 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004343replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004344{
4345 char_u *new_cmdline;
4346 char_u *program;
4347 char_u *pos;
4348 char_u *ptr;
4349 int len;
4350 int i;
4351
4352 /*
4353 * Don't do it when ":vimgrep" is used for ":grep".
4354 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004355 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4356 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4357 || eap->cmdidx == CMD_grepadd
4358 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004359 && !grep_internal(eap->cmdidx))
4360 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004361 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4362 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004363 {
4364 if (*curbuf->b_p_gp == NUL)
4365 program = p_gp;
4366 else
4367 program = curbuf->b_p_gp;
4368 }
4369 else
4370 {
4371 if (*curbuf->b_p_mp == NUL)
4372 program = p_mp;
4373 else
4374 program = curbuf->b_p_mp;
4375 }
4376
4377 p = skipwhite(p);
4378
4379 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4380 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004381 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004382 i = 1;
4383 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4384 ++i;
4385 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004386 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004387 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004388 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004389 ptr = new_cmdline;
4390 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4391 {
4392 i = (int)(pos - program);
4393 STRNCPY(ptr, program, i);
4394 STRCPY(ptr += i, p);
4395 ptr += len;
4396 program = pos + 2;
4397 }
4398 STRCPY(ptr, program);
4399 }
4400 else
4401 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004402 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004403 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004404 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004405 STRCPY(new_cmdline, program);
4406 STRCAT(new_cmdline, " ");
4407 STRCAT(new_cmdline, p);
4408 }
4409 msg_make(p);
4410
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004411 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004412 vim_free(*cmdlinep);
4413 *cmdlinep = new_cmdline;
4414 p = new_cmdline;
4415 }
4416 return p;
4417}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004418#endif
4419
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420/*
4421 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004422 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 * Return FAIL for failure, OK otherwise.
4424 */
4425 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004426expand_filename(
4427 exarg_T *eap,
4428 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004429 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004431 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 char_u *repl;
4433 int srclen;
4434 char_u *p;
4435 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004436 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437
Bram Moolenaar748bf032005-02-02 23:04:36 +00004438#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004439 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004440 p = skip_grep_pat(eap);
4441#else
4442 p = eap->arg;
4443#endif
4444
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 /*
4446 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4447 * the file name contains a wildcard it should not cause expanding.
4448 * (it will be expanded anyway if there is a wildcard before replacing).
4449 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004450 has_wildcards = mch_has_wildcard(p);
4451 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004453#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004454 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004455 if (p[0] == '`' && p[1] == '=')
4456 {
4457 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004458 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004459 if (*p == '`')
4460 ++p;
4461 continue;
4462 }
4463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 /*
4465 * Quick check if this cannot be the start of a special string.
4466 * Also removes backslash before '%', '#' and '<'.
4467 */
4468 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4469 {
4470 ++p;
4471 continue;
4472 }
4473
4474 /*
4475 * Try to find a match at this position.
4476 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004477 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4478 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004479 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004481 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 {
4483 p += srclen;
4484 continue;
4485 }
4486
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004487 // Wildcards won't be expanded below, the replacement is taken
4488 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004489 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4490 {
4491 char_u *l = repl;
4492
4493 repl = expand_env_save(repl);
4494 vim_free(l);
4495 }
4496
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004497 // Need to escape white space et al. with a backslash.
4498 // Don't do this for:
4499 // - replacement that already has been escaped: "##"
4500 // - shell commands (may have to use quotes instead).
4501 // - non-unix systems when there is a single argument (spaces don't
4502 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004504 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 && eap->cmdidx != CMD_grep
4507 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004508 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004509 && eap->cmdidx != CMD_lgrep
4510 && eap->cmdidx != CMD_lgrepadd
4511 && eap->cmdidx != CMD_lmake
4512 && eap->cmdidx != CMD_make
4513 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004515 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516#endif
4517 )
4518 {
4519 char_u *l;
4520#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004521 // Don't escape a backslash here, because rem_backslash() doesn't
4522 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 static char_u *nobslash = (char_u *)" \t\"|";
4524# define ESCAPE_CHARS nobslash
4525#else
4526# define ESCAPE_CHARS escape_chars
4527#endif
4528
4529 for (l = repl; *l; ++l)
4530 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4531 {
4532 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4533 if (l != NULL)
4534 {
4535 vim_free(repl);
4536 repl = l;
4537 }
4538 break;
4539 }
4540 }
4541
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004542 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004543 if ((eap->usefilter || eap->cmdidx == CMD_bang
4544 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004545 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 {
4547 char_u *l;
4548
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004549 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 if (l != NULL)
4551 {
4552 vim_free(repl);
4553 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 }
4555 }
4556
4557 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4558 vim_free(repl);
4559 if (p == NULL)
4560 return FAIL;
4561 }
4562
4563 /*
4564 * One file argument: Expand wildcards.
4565 * Don't do this with ":r !command" or ":w !command".
4566 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004567 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 {
4569 /*
4570 * May do this twice:
4571 * 1. Replace environment variables.
4572 * 2. Replace any other wildcards, remove backslashes.
4573 */
4574 for (n = 1; n <= 2; ++n)
4575 {
4576 if (n == 2)
4577 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 /*
4579 * Halve the number of backslashes (this is Vi compatible).
4580 * For Unix and OS/2, when wildcards are expanded, this is
4581 * done by ExpandOne() below.
4582 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004583#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 if (!has_wildcards)
4585#endif
4586 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 }
4588
4589 if (has_wildcards)
4590 {
4591 if (n == 1)
4592 {
4593 /*
4594 * First loop: May expand environment variables. This
4595 * can be done much faster with expand_env() than with
4596 * something else (e.g., calling a shell).
4597 * After expanding environment variables, check again
4598 * if there are still wildcards present.
4599 */
4600 if (vim_strchr(eap->arg, '$') != NULL
4601 || vim_strchr(eap->arg, '~') != NULL)
4602 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004603 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004604 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 has_wildcards = mch_has_wildcard(NameBuff);
4606 p = NameBuff;
4607 }
4608 else
4609 p = NULL;
4610 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004611 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 {
4613 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004614 int options = WILD_LIST_NOTFOUND
4615 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616
4617 ExpandInit(&xpc);
4618 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004619 if (p_wic)
4620 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004622 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 if (p == NULL)
4624 return FAIL;
4625 }
4626 if (p != NULL)
4627 {
4628 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4629 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004630 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 vim_free(p);
4632 }
4633 }
4634 }
4635 }
4636 return OK;
4637}
4638
4639/*
4640 * Replace part of the command line, keeping eap->cmd, eap->arg and
4641 * eap->nextcmd correct.
4642 * "src" points to the part that is to be replaced, of length "srclen".
4643 * "repl" is the replacement string.
4644 * Returns a pointer to the character after the replaced string.
4645 * Returns NULL for failure.
4646 */
4647 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004648repl_cmdline(
4649 exarg_T *eap,
4650 char_u *src,
4651 int srclen,
4652 char_u *repl,
4653 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654{
4655 int len;
4656 int i;
4657 char_u *new_cmdline;
4658
4659 /*
4660 * The new command line is build in new_cmdline[].
4661 * First allocate it.
4662 * Careful: a "+cmd" argument may have been NUL terminated.
4663 */
4664 len = (int)STRLEN(repl);
4665 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004666 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004667 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004668 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004669 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670
4671 /*
4672 * Copy the stuff before the expanded part.
4673 * Copy the expanded stuff.
4674 * Copy what came after the expanded part.
4675 * Copy the next commands, if there are any.
4676 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004677 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004679
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004681 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004683 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004685 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 {
4687 i = (int)STRLEN(new_cmdline) + 1;
4688 STRCPY(new_cmdline + i, eap->nextcmd);
4689 eap->nextcmd = new_cmdline + i;
4690 }
4691 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4692 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4693 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4694 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4695 vim_free(*cmdlinep);
4696 *cmdlinep = new_cmdline;
4697
4698 return src;
4699}
4700
4701/*
4702 * Check for '|' to separate commands and '"' to start comments.
4703 */
4704 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004705separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706{
4707 char_u *p;
4708
Bram Moolenaar86b68352004-12-27 21:59:20 +00004709#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004710 p = skip_grep_pat(eap);
4711#else
4712 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004713#endif
4714
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004715 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 {
4717 if (*p == Ctrl_V)
4718 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004719 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004720 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004722 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004723 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004724 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 break;
4726 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004727
4728#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004729 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004730 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004731 {
4732 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004733 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01004734 if (*p == NUL) // stop at NUL after CTRL-V
4735 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004736 }
4737#endif
4738
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004739 // Check for '"': start of comment or '|': next command
4740 // :@" and :*" do not start a comment!
4741 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004742 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004743#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004744 && !in_vim9script()
4745#endif
4746 && !(eap->argt & EX_NOTRLCOM)
4747 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4748 || p != eap->arg)
4749 && (eap->cmdidx != CMD_redir
4750 || p != eap->arg + 1 || p[-1] != '@'))
4751#ifdef FEAT_EVAL
4752 || (*p == '#'
4753 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004754 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004755 && p[1] != '{'
4756 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 || *p == '|' || *p == '\n')
4759 {
4760 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004761 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 * AND 'b' is present in 'cpoptions'.
4763 */
4764 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004765 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004767 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 --p;
4769 }
4770 else
4771 {
4772 eap->nextcmd = check_nextcmd(p);
4773 *p = NUL;
4774 break;
4775 }
4776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004778
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004779 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 del_trailing_spaces(eap->arg);
4781}
4782
4783/*
4784 * get + command from ex argument
4785 */
4786 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004787getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788{
4789 char_u *arg = *argp;
4790 char_u *command = NULL;
4791
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004792 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 {
4794 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004795 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 command = dollar_command;
4797 else
4798 {
4799 command = arg;
4800 arg = skip_cmd_arg(command, TRUE);
4801 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004802 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 }
4804
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004805 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 *argp = arg;
4807 }
4808 return command;
4809}
4810
4811/*
4812 * Find end of "+command" argument. Skip over "\ " and "\\".
4813 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004814 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004815skip_cmd_arg(
4816 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004817 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818{
4819 while (*p && !vim_isspace(*p))
4820 {
4821 if (*p == '\\' && p[1] != NUL)
4822 {
4823 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004824 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 else
4826 ++p;
4827 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004828 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 }
4830 return p;
4831}
4832
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004833 int
4834get_bad_opt(char_u *p, exarg_T *eap)
4835{
4836 if (STRICMP(p, "keep") == 0)
4837 eap->bad_char = BAD_KEEP;
4838 else if (STRICMP(p, "drop") == 0)
4839 eap->bad_char = BAD_DROP;
4840 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4841 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004842 else
4843 return FAIL;
4844 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004845}
4846
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847/*
4848 * Get "++opt=arg" argument.
4849 * Return FAIL or OK.
4850 */
4851 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004852getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853{
4854 char_u *arg = eap->arg + 2;
4855 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004856 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004859 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4861 {
4862 if (*arg == 'n')
4863 {
4864 arg += 2;
4865 eap->force_bin = FORCE_NOBIN;
4866 }
4867 else
4868 eap->force_bin = FORCE_BIN;
4869 if (!checkforcmd(&arg, "binary", 3))
4870 return FAIL;
4871 eap->arg = skipwhite(arg);
4872 return OK;
4873 }
4874
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004875 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004876 if (STRNCMP(arg, "edit", 4) == 0)
4877 {
4878 eap->read_edit = TRUE;
4879 eap->arg = skipwhite(arg + 4);
4880 return OK;
4881 }
4882
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 if (STRNCMP(arg, "ff", 2) == 0)
4884 {
4885 arg += 2;
4886 pp = &eap->force_ff;
4887 }
4888 else if (STRNCMP(arg, "fileformat", 10) == 0)
4889 {
4890 arg += 10;
4891 pp = &eap->force_ff;
4892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 else if (STRNCMP(arg, "enc", 3) == 0)
4894 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004895 if (STRNCMP(arg, "encoding", 8) == 0)
4896 arg += 8;
4897 else
4898 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 pp = &eap->force_enc;
4900 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004901 else if (STRNCMP(arg, "bad", 3) == 0)
4902 {
4903 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004904 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906
4907 if (pp == NULL || *arg != '=')
4908 return FAIL;
4909
4910 ++arg;
4911 *pp = (int)(arg - eap->cmd);
4912 arg = skip_cmd_arg(arg, FALSE);
4913 eap->arg = skipwhite(arg);
4914 *arg = NUL;
4915
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 if (pp == &eap->force_ff)
4917 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4919 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004920 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004922 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004924 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4926 *p = TOLOWER_ASC(*p);
4927 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004928 else
4929 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004930 // Check ++bad= argument. Must be a single-byte character, "keep" or
4931 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004932 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004933 return FAIL;
4934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935
4936 return OK;
4937}
4938
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004940ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941{
4942 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004943 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 * directory for security reasons.
4945 */
4946 if (secure)
4947 {
4948 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004949 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 }
4951 else if (eap->cmdidx == CMD_autocmd)
4952 do_autocmd(eap->arg, eap->forceit);
4953 else
4954 do_augroup(eap->arg, eap->forceit);
4955}
4956
4957/*
4958 * ":doautocmd": Apply the automatic commands to the current buffer.
4959 */
4960 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004961ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004963 char_u *arg = eap->arg;
4964 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004965 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004966
Bram Moolenaar1610d052016-06-09 22:53:01 +02004967 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004968 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004969 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004970 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973/*
4974 * :[N]bunload[!] [N] [bufname] unload buffer
4975 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4976 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4977 */
4978 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004979ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004981 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004982 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 eap->errmsg = do_bufdel(
4984 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4985 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4986 : DOBUF_UNLOAD, eap->arg,
4987 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4988}
4989
4990/*
4991 * :[N]buffer [N] to buffer N
4992 * :[N]sbuffer [N] to buffer N
4993 */
4994 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004995ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004997 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004998 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005000 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 else
5002 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005003 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5005 else
5006 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005007 if (eap->do_ecmd_cmd != NULL)
5008 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 }
5010}
5011
5012/*
5013 * :[N]bmodified [N] to next mod. buffer
5014 * :[N]sbmodified [N] to next mod. buffer
5015 */
5016 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005017ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018{
5019 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005020 if (eap->do_ecmd_cmd != NULL)
5021 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022}
5023
5024/*
5025 * :[N]bnext [N] to next buffer
5026 * :[N]sbnext [N] split and to next buffer
5027 */
5028 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005029ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005031 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005032 return;
5033
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005035 if (eap->do_ecmd_cmd != NULL)
5036 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037}
5038
5039/*
5040 * :[N]bNext [N] to previous buffer
5041 * :[N]bprevious [N] to previous buffer
5042 * :[N]sbNext [N] split and to previous buffer
5043 * :[N]sbprevious [N] split and to previous buffer
5044 */
5045 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005046ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005048 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005049 return;
5050
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005052 if (eap->do_ecmd_cmd != NULL)
5053 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054}
5055
5056/*
5057 * :brewind to first buffer
5058 * :bfirst to first buffer
5059 * :sbrewind split and to first buffer
5060 * :sbfirst split and to first buffer
5061 */
5062 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005063ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005065 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005066 return;
5067
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005069 if (eap->do_ecmd_cmd != NULL)
5070 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071}
5072
5073/*
5074 * :blast to last buffer
5075 * :sblast split and to last buffer
5076 */
5077 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005078ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005080 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005081 return;
5082
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005084 if (eap->do_ecmd_cmd != NULL)
5085 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087
Bram Moolenaar7a092242020-04-16 22:10:49 +02005088/*
5089 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02005090 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005093ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005095 int comment_char = '"';
5096
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005097#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005098 if (in_vim9script())
5099 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005100#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005101 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005102}
5103
5104/*
5105 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5106 * to "cmd_start" or has a white space character before it.
5107 */
5108 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005109ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005110{
5111 int c = *cmd;
5112
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005113 if (c == NUL || c == '|' || c == '\n')
5114 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005115#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005116 if (in_vim9script())
5117 return c == '#' && cmd[1] != '{'
5118 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005119#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005120 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121}
5122
5123#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5124 || defined(PROTO)
5125/*
5126 * Return the next command, after the first '|' or '\n'.
5127 * Return NULL if not found.
5128 */
5129 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005130find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131{
5132 while (*p != '|' && *p != '\n')
5133 {
5134 if (*p == NUL)
5135 return NULL;
5136 ++p;
5137 }
5138 return (p + 1);
5139}
5140#endif
5141
5142/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005143 * Check if *p is a separator between Ex commands, skipping over white space.
5144 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 */
5146 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005147check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005149 char_u *s = skipwhite(p);
5150
5151 if (*s == '|' || *s == '\n')
5152 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 else
5154 return NULL;
5155}
5156
5157/*
5158 * - if there are more files to edit
5159 * - and this is the last window
5160 * - and forceit not used
5161 * - and not repeated twice on a row
5162 * return FAIL and give error message if 'message' TRUE
5163 * return OK otherwise
5164 */
5165 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005166check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005167 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005168 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169{
5170 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5171
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005172 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005173 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 {
5175 if (message)
5176 {
5177#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005178 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5179 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005181 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005183 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5184 NGETTEXT("%d more file to edit. Quit anyway?",
5185 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5187 return OK;
5188 return FAIL;
5189 }
5190#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005191 semsg(NGETTEXT("E173: %d more file to edit",
5192 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005193 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
5195 return FAIL;
5196 }
5197 return OK;
5198}
5199
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200/*
5201 * Function given to ExpandGeneric() to obtain the list of command names.
5202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005204get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205{
5206 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 return cmdnames[idx].cmd_name;
5209}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005212ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213{
Bram Moolenaare6850792010-05-14 15:28:44 +02005214 if (*eap->arg == NUL)
5215 {
5216#ifdef FEAT_EVAL
5217 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5218 char_u *p = NULL;
5219
5220 if (expr != NULL)
5221 {
5222 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005223 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005224 --emsg_off;
5225 vim_free(expr);
5226 }
5227 if (p != NULL)
5228 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005229 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005230 vim_free(p);
5231 }
5232 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005233 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005234#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005235 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005236#endif
5237 }
5238 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005239 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005240
5241#ifdef FEAT_VTP
5242 else if (has_vtp_working())
5243 {
5244 // background color change requires clear + redraw
5245 update_screen(CLEAR);
5246 redrawcmd();
5247 }
5248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249}
5250
5251 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005252ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253{
5254 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005255 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 do_highlight(eap->arg, eap->forceit, FALSE);
5257}
5258
5259
5260/*
5261 * Call this function if we thought we were going to exit, but we won't
5262 * (because of an error). May need to restore the terminal mode.
5263 */
5264 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005265not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266{
5267 exiting = FALSE;
5268 settmode(TMODE_RAW);
5269}
5270
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005271 static int
5272before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5273{
5274 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5275
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005276 // Bail out when autocommands closed the window.
5277 // Refuse to quit when the buffer in the last window is being closed (can
5278 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005279 if (!win_valid(wp)
5280 || curbuf_locked()
5281 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5282 return TRUE;
5283
5284 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5285 {
5286 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005287 // Refuse to quit when locked or when the window was closed or the
5288 // buffer in the last window is being closed (can only happen in
5289 // autocommands).
5290 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005291 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5292 return TRUE;
5293 }
5294
5295 return FALSE;
5296}
5297
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005299 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005300 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005301 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005303 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005304ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005306 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005307
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308#ifdef FEAT_CMDWIN
5309 if (cmdwin_type != 0)
5310 {
5311 cmdwin_result = Ctrl_C;
5312 return;
5313 }
5314#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005315 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005316 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005317 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005318 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005319 return;
5320 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005321 if (eap->addr_count > 0)
5322 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005323 int wnr = eap->line2;
5324
5325 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5326 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005327 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005328 }
5329 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005330 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005331
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005332 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005333 if (curbuf_locked())
5334 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005335
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005336 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005337 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005338 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339
5340#ifdef FEAT_NETBEANS_INTG
5341 netbeansForcedQuit = eap->forceit;
5342#endif
5343
5344 /*
5345 * If there are more files or windows we won't exit.
5346 */
5347 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5348 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005349 if ((!buf_hide(wp->w_buffer)
5350 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005351 | (eap->forceit ? CCGD_FORCEIT : 0)
5352 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005354 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 {
5356 not_exiting();
5357 }
5358 else
5359 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005360 // quit last window
5361 // Note: only_one_window() returns true, even so a help window is
5362 // still open. In that case only quit, if no address has been
5363 // specified. Example:
5364 // :h|wincmd w|1q - don't quit
5365 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005366 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005368 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005369#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005371#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005372 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005373 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 }
5375}
5376
5377/*
5378 * ":cquit".
5379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005381ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005383 // this does not always pass on the exit code to the Manx compiler. why?
5384 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385}
5386
5387/*
5388 * ":qall": try to quit all windows
5389 */
5390 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005391ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392{
5393# ifdef FEAT_CMDWIN
5394 if (cmdwin_type != 0)
5395 {
5396 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005397 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 else
5399 cmdwin_result = K_XF2;
5400 return;
5401 }
5402# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005403
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005404 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005405 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005406 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005407 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005408 return;
5409 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005410
5411 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005412 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005413
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005415 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 getout(0);
5417 not_exiting();
5418}
5419
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420/*
5421 * ":close": close current window, unless it is the last one
5422 */
5423 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005424ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005426 win_T *win;
5427 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005428#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005430 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005432#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005433 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005434 {
5435 if (eap->addr_count == 0)
5436 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005437 else
5438 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005439 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005440 {
5441 winnr++;
5442 if (winnr == eap->line2)
5443 break;
5444 }
5445 if (win == NULL)
5446 win = lastwin;
5447 ex_win_close(eap->forceit, win, NULL);
5448 }
5449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450}
5451
Bram Moolenaar4033c552017-09-16 20:54:51 +02005452#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005453/*
5454 * ":pclose": Close any preview window.
5455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005457ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005458{
5459 win_T *win;
5460
Bram Moolenaar79648732019-07-18 21:43:07 +02005461 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005462 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005463 if (win->w_p_pvw)
5464 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005465 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005466 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005467 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005468# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005469 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005470 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005471# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005472}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005473#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005474
Bram Moolenaarf740b292006-02-16 22:11:02 +00005475/*
5476 * Close window "win" and take care of handling closing the last window for a
5477 * modified buffer.
5478 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005479 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005480ex_win_close(
5481 int forceit,
5482 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005483 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484{
5485 int need_hide;
5486 buf_T *buf = win->w_buffer;
5487
Bram Moolenaarcf844172020-06-26 19:44:06 +02005488 // Never close the autocommand window.
5489 if (win == aucmd_win)
5490 {
5491 emsg(_(e_autocmd_close));
5492 return;
5493 }
5494
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005496 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005498#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005499 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005501 bufref_T bufref;
5502
5503 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005505 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 return;
5507 need_hide = FALSE;
5508 }
5509 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005512 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 return;
5514 }
5515 }
5516
Bram Moolenaar4033c552017-09-16 20:54:51 +02005517#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005519#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005520
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005521 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005522 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005523 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005524 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005525 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526}
5527
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005529 * Handle the argument for a tabpage related ex command.
5530 * Returns a tabpage number.
5531 * When an error is encountered then eap->errmsg is set.
5532 */
5533 static int
5534get_tabpage_arg(exarg_T *eap)
5535{
5536 int tab_number;
5537 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5538
5539 if (eap->arg && *eap->arg != NUL)
5540 {
5541 char_u *p = eap->arg;
5542 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005543 int relative = 0; // argument +N/-N means: go to N places to the
5544 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005545
5546 if (*p == '-')
5547 {
5548 relative = -1;
5549 p++;
5550 }
5551 else if (*p == '+')
5552 {
5553 relative = 1;
5554 p++;
5555 }
5556
5557 p_save = p;
5558 tab_number = getdigits(&p);
5559
5560 if (relative == 0)
5561 {
5562 if (STRCMP(p, "$") == 0)
5563 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005564 else if (STRCMP(p, "#") == 0)
5565 if (valid_tabpage(lastused_tabpage))
5566 tab_number = tabpage_index(lastused_tabpage);
5567 else
5568 {
5569 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5570 tab_number = 0;
5571 goto theend;
5572 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005573 else if (p == p_save || *p_save == '-' || *p != NUL
5574 || tab_number > LAST_TAB_NR)
5575 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005576 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005577 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005578 goto theend;
5579 }
5580 }
5581 else
5582 {
5583 if (*p_save == NUL)
5584 tab_number = 1;
5585 else if (p == p_save || *p_save == '-' || *p != NUL
5586 || tab_number == 0)
5587 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005588 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005589 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005590 goto theend;
5591 }
5592 tab_number = tab_number * relative + tabpage_index(curtab);
5593 if (!unaccept_arg0 && relative == -1)
5594 --tab_number;
5595 }
5596 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005597 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005598 }
5599 else if (eap->addr_count > 0)
5600 {
5601 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005602 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005603 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005604 tab_number = 0;
5605 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005606 else
5607 {
5608 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005609 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005610 {
5611 --tab_number;
5612 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005613 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005614 }
5615 }
5616 }
5617 else
5618 {
5619 switch (eap->cmdidx)
5620 {
5621 case CMD_tabnext:
5622 tab_number = tabpage_index(curtab) + 1;
5623 if (tab_number > LAST_TAB_NR)
5624 tab_number = 1;
5625 break;
5626 case CMD_tabmove:
5627 tab_number = LAST_TAB_NR;
5628 break;
5629 default:
5630 tab_number = tabpage_index(curtab);
5631 }
5632 }
5633
5634theend:
5635 return tab_number;
5636}
5637
5638/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005639 * ":tabclose": close current tab page, unless it is the last one.
5640 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 */
5642 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005643ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005645 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005646 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005647
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005648# ifdef FEAT_CMDWIN
5649 if (cmdwin_type != 0)
5650 cmdwin_result = K_IGNORE;
5651 else
5652# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005653 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005654 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005655 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005657 tab_number = get_tabpage_arg(eap);
5658 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005659 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005660 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005661 if (tp == NULL)
5662 {
5663 beep_flush();
5664 return;
5665 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005666 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005667 {
5668 tabpage_close_other(tp, eap->forceit);
5669 return;
5670 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005671 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005672 tabpage_close(eap->forceit);
5673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005675}
5676
5677/*
5678 * ":tabonly": close all tab pages except the current one
5679 */
5680 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005681ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005682{
5683 tabpage_T *tp;
5684 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005685 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005686
5687# ifdef FEAT_CMDWIN
5688 if (cmdwin_type != 0)
5689 cmdwin_result = K_IGNORE;
5690 else
5691# endif
5692 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005693 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005694 else
5695 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005696 tab_number = get_tabpage_arg(eap);
5697 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005698 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005699 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005700 // Repeat this up to a 1000 times, because autocommands may
5701 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005702 for (done = 0; done < 1000; ++done)
5703 {
5704 FOR_ALL_TABPAGES(tp)
5705 if (tp->tp_topframe != topframe)
5706 {
5707 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005708 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005709 if (valid_tabpage(tp))
5710 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005711 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005712 break;
5713 }
5714 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005715 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005716 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005717 }
5718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720
5721/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005722 * Close the current tab page.
5723 */
5724 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005725tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005726{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005727 // First close all the windows but the current one. If that worked then
5728 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005729 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005730 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005731 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005732 ex_win_close(forceit, curwin, NULL);
5733# ifdef FEAT_GUI
5734 need_mouse_correct = TRUE;
5735# endif
5736}
5737
5738/*
5739 * Close tab page "tp", which is not the current tab page.
5740 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005741 * Also takes care of the tab pages line disappearing when closing the
5742 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005743 */
5744 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005745tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005746{
5747 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005748 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005749 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005750
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005751 // Limit to 1000 windows, autocommands may add a window while we close
5752 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005753 while (++done < 1000)
5754 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005755 wp = tp->tp_firstwin;
5756 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005757
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005758 // Autocommands may delete the tab page under our fingers and we may
5759 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005760 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005761 break;
5762 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005763
Bram Moolenaar29323592016-07-24 22:04:11 +02005764 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005765
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005766 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005767 if (h != tabline_height())
5768 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005769}
5770
5771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 * ":only".
5773 */
5774 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005775ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005777 win_T *wp;
5778 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779# ifdef FEAT_GUI
5780 need_mouse_correct = TRUE;
5781# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005782 if (eap->addr_count > 0)
5783 {
5784 wnr = eap->line2;
5785 for (wp = firstwin; --wnr > 0; )
5786 {
5787 if (wp->w_next == NULL)
5788 break;
5789 else
5790 wp = wp->w_next;
5791 }
5792 win_goto(wp);
5793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 close_others(TRUE, eap->forceit);
5795}
5796
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005798ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005800 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005801 if (!eap->skip)
5802 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005803#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005804 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005805#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005806 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005807 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005808 else
5809 {
5810 int winnr = 0;
5811 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005812
Bram Moolenaar2256c992016-11-15 21:17:07 +01005813 FOR_ALL_WINDOWS(win)
5814 {
5815 winnr++;
5816 if (winnr == eap->line2)
5817 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005818 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005819 if (win == NULL)
5820 win = lastwin;
5821 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 }
5824}
5825
5826/*
5827 * ":stop" and ":suspend": Suspend Vim.
5828 */
5829 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005830ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831{
5832 /*
5833 * Disallow suspending for "rvim".
5834 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005835 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 {
5837 if (!eap->forceit)
5838 autowrite_all();
5839 windgoto((int)Rows - 1, 0);
5840 out_char('\n');
5841 out_flush();
5842 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005843 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005845 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005847 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848#ifdef FEAT_TITLE
5849 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005850 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851#endif
5852 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005853 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005855 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 }
5857}
5858
5859/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005860 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 */
5862 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005863ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864{
Bram Moolenaar461f2122020-07-28 20:25:47 +02005865#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02005866 if (not_in_vim9(eap) == FAIL)
5867 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02005868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869#ifdef FEAT_CMDWIN
5870 if (cmdwin_type != 0)
5871 {
5872 cmdwin_result = Ctrl_C;
5873 return;
5874 }
5875#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005876 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005877 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005878 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005879 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005880 return;
5881 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005882
5883 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005884 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885
5886 /*
5887 * if more files or windows we won't exit
5888 */
5889 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5890 exiting = TRUE;
5891 if ( ((eap->cmdidx == CMD_wq
5892 || curbufIsChanged())
5893 && do_write(eap) == FAIL)
5894 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005895 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 {
5897 not_exiting();
5898 }
5899 else
5900 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005901 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005903 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904# ifdef FEAT_GUI
5905 need_mouse_correct = TRUE;
5906# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005907 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005908 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 }
5910}
5911
5912/*
5913 * ":print", ":list", ":number".
5914 */
5915 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005916ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005918 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005919 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005920 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005922 for ( ;!got_int; ui_breakcheck())
5923 {
5924 print_line(eap->line1,
5925 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5926 || (eap->flags & EXFLAG_NR)),
5927 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5928 if (++eap->line1 > eap->line2)
5929 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005930 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005931 }
5932 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005933 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005934 curwin->w_cursor.lnum = eap->line2;
5935 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 }
5937
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939}
5940
5941#ifdef FEAT_BYTEOFF
5942 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005943ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944{
5945 goto_byte(eap->line2);
5946}
5947#endif
5948
5949/*
5950 * ":shell".
5951 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005953ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954{
5955 do_shell(NULL, 0);
5956}
5957
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005958#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005960static int drop_busy = FALSE;
5961static int drop_filec;
5962static char_u **drop_filev = NULL;
5963static int drop_split;
5964static void (*drop_callback)(void *);
5965static void *drop_cookie;
5966
5967 static void
5968handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969{
5970 exarg_T ea;
5971 int save_msg_scroll = msg_scroll;
5972
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005973 // Setting the argument list may cause screen updates and being called
5974 // recursively. Avoid that by setting drop_busy.
5975 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005977 // Check whether the current buffer is changed. If so, we will need
5978 // to split the current window or data could be lost.
5979 // We don't need to check if the 'hidden' option is set, as in this
5980 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005981 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 {
5983 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005984 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 --emsg_off;
5986 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005987 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 if (win_split(0, 0) == FAIL)
5990 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005991 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005993 // When splitting the window, create a new alist. Otherwise the
5994 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 alist_unlink(curwin->w_alist);
5996 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 }
5998
5999 /*
6000 * Set up the new argument list.
6001 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006002 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003
6004 /*
6005 * Move to the first file.
6006 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006007 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006008 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 ea.cmd = (char_u *)"next";
6010 do_argfile(&ea, 0);
6011
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006012 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6013 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 need_start_insertmode = FALSE;
6015
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006016 // Restore msg_scroll, otherwise a following command may cause scrolling
6017 // unexpectedly. The screen will be redrawn by the caller, thus
6018 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006020
6021 if (drop_callback != NULL)
6022 drop_callback(drop_cookie);
6023
6024 drop_filev = NULL;
6025 drop_busy = FALSE;
6026}
6027
6028/*
6029 * Handle a file drop. The code is here because a drop is *nearly* like an
6030 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006031 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006032 * code is very similar to :args and hence needs access to a lot of the static
6033 * functions in this file.
6034 *
6035 * The "filev" list must have been allocated using alloc(), as should each item
6036 * in the list. This function takes over responsibility for freeing the "filev"
6037 * list.
6038 */
6039 void
6040handle_drop(
6041 int filec, // the number of files dropped
6042 char_u **filev, // the list of files dropped
6043 int split, // force splitting the window
6044 void (*callback)(void *), // to be called after setting the argument
6045 // list
6046 void *cookie) // argument for "callback" (allocated)
6047{
6048 // Cannot handle recursive drops, finish the pending one.
6049 if (drop_busy)
6050 {
6051 FreeWild(filec, filev);
6052 vim_free(cookie);
6053 return;
6054 }
6055
6056 // When calling handle_drop() more than once in a row we only use the last
6057 // one.
6058 if (drop_filev != NULL)
6059 {
6060 FreeWild(drop_filec, drop_filev);
6061 vim_free(drop_cookie);
6062 }
6063
6064 drop_filec = filec;
6065 drop_filev = filev;
6066 drop_split = split;
6067 drop_callback = callback;
6068 drop_cookie = cookie;
6069
6070 // Postpone this when:
6071 // - editing the command line
6072 // - not possible to change the current buffer
6073 // - updating the screen
6074 // As it may change buffers and window structures that are in use and cause
6075 // freed memory to be used.
6076 if (text_locked() || curbuf_locked() || updating_screen)
6077 return;
6078
6079 handle_drop_internal();
6080}
6081
6082/*
6083 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6084 * reset: Handle a postponed drop.
6085 */
6086 void
6087handle_any_postponed_drop(void)
6088{
6089 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006090 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006091 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092}
6093#endif
6094
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 * ":preserve".
6097 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006099ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006101 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 ml_preserve(curbuf, TRUE);
6103}
6104
6105/*
6106 * ":recover".
6107 */
6108 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006109ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006111 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006113 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6114 | CCGD_MULTWIN
6115 | (eap->forceit ? CCGD_FORCEIT : 0)
6116 | CCGD_EXCMD)
6117
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 && (*eap->arg == NUL
6119 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006120 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 recoverymode = FALSE;
6122}
6123
6124/*
6125 * Command modifier used in a wrong way.
6126 */
6127 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006128ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006130 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131}
6132
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133/*
6134 * :sview [+command] file split window with new file, read-only
6135 * :split [[+command] file] split window with current or new file
6136 * :vsplit [[+command] file] split window vertically with current or new file
6137 * :new [[+command] file] split window with no or new file
6138 * :vnew [[+command] file] split vertically window with no or new file
6139 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006140 *
6141 * :tabedit open new Tab page with empty window
6142 * :tabedit [+command] file open new Tab page and edit "file"
6143 * :tabnew [[+command] file] just like :tabedit
6144 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 */
6146 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006147ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006149 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006150#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006152#endif
6153#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006154 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006155 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006156#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006157 int use_tab = eap->cmdidx == CMD_tabedit
6158 || eap->cmdidx == CMD_tabfind
6159 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006161 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006162 return;
6163
Bram Moolenaar4033c552017-09-16 20:54:51 +02006164#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167
Bram Moolenaar4033c552017-09-16 20:54:51 +02006168#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006169 // A ":split" in the quickfix window works like ":new". Don't want two
6170 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006171 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 {
6173 if (eap->cmdidx == CMD_split)
6174 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 if (eap->cmdidx == CMD_vsplit)
6176 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179
Bram Moolenaar4033c552017-09-16 20:54:51 +02006180#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006181 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 {
6183 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6184 FNAME_MESS, TRUE, curbuf->b_ffname);
6185 if (fname == NULL)
6186 goto theend;
6187 eap->arg = fname;
6188 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006189# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006190 else
6191# endif
6192#endif
6193#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006194 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 && eap->cmdidx != CMD_new)
6197 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006198 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006199# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006200 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006201# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006202 au_has_group((char_u *)"FileExplorer"))
6203 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006204 // No browsing supported but we do have the file explorer:
6205 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006206 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006207 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006208 }
6209 else
6210 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006211 fname = do_browse(0, (char_u *)(use_tab
6212 ? _("Edit File in new tab page")
6213 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006215 if (fname == NULL)
6216 goto theend;
6217 eap->arg = fname;
6218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006220 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006223 /*
6224 * Either open new tab page or split the window.
6225 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006226 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006227 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006228 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006229 : eap->addr_count == 0 ? 0
6230 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006231 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006232 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006233
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006234 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006235 if (curwin != old_curwin
6236 && win_valid(old_curwin)
6237 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006238 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006239 old_curwin->w_alt_fnum = curbuf->b_fnum;
6240 }
6241 }
6242 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6244 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006245 // Reset 'scrollbind' when editing another file, but keep it when
6246 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006247 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006248 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 else
6250 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 do_exedit(eap, old_curwin);
6252 }
6253
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006254# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006255 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006256# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006258# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259theend:
6260 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006261# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006263
6264/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006265 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006266 */
6267 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006268tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006269{
6270 exarg_T ea;
6271
Bram Moolenaara80faa82020-04-12 19:37:17 +02006272 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006273 ea.cmdidx = CMD_tabnew;
6274 ea.cmd = (char_u *)"tabn";
6275 ea.arg = (char_u *)"";
6276 ex_splitview(&ea);
6277}
6278
6279/*
6280 * :tabnext command
6281 */
6282 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006283ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006284{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006285 int tab_number;
6286
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006287 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006288 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006289 switch (eap->cmdidx)
6290 {
6291 case CMD_tabfirst:
6292 case CMD_tabrewind:
6293 goto_tabpage(1);
6294 break;
6295 case CMD_tablast:
6296 goto_tabpage(9999);
6297 break;
6298 case CMD_tabprevious:
6299 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006300 if (eap->arg && *eap->arg != NUL)
6301 {
6302 char_u *p = eap->arg;
6303 char_u *p_save = p;
6304
6305 tab_number = getdigits(&p);
6306 if (p == p_save || *p_save == '-' || *p != NUL
6307 || tab_number == 0)
6308 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006309 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006310 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006311 return;
6312 }
6313 }
6314 else
6315 {
6316 if (eap->addr_count == 0)
6317 tab_number = 1;
6318 else
6319 {
6320 tab_number = eap->line2;
6321 if (tab_number < 1)
6322 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006323 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006324 return;
6325 }
6326 }
6327 }
6328 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006329 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006330 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006331 tab_number = get_tabpage_arg(eap);
6332 if (eap->errmsg == NULL)
6333 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006334 break;
6335 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006336}
6337
6338/*
6339 * :tabmove command
6340 */
6341 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006342ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006343{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006344 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006345
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006346 tab_number = get_tabpage_arg(eap);
6347 if (eap->errmsg == NULL)
6348 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006349}
6350
6351/*
6352 * :tabs command: List tabs and their contents.
6353 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006354 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006355ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006356{
6357 tabpage_T *tp;
6358 win_T *wp;
6359 int tabcount = 1;
6360
6361 msg_start();
6362 msg_scroll = TRUE;
6363 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6364 {
6365 msg_putchar('\n');
6366 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006367 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006368 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006369 ui_breakcheck();
6370
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006371 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006372 wp = firstwin;
6373 else
6374 wp = tp->tp_firstwin;
6375 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6376 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006377 msg_putchar('\n');
6378 msg_putchar(wp == curwin ? '>' : ' ');
6379 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006380 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6381 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006382 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006383 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006384 else
6385 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6386 IObuff, IOSIZE, TRUE);
6387 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006388 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006389 ui_breakcheck();
6390 }
6391 }
6392}
6393
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394/*
6395 * ":mode": Set screen mode.
6396 * If no argument given, just get the screen size and redraw.
6397 */
6398 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006399ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400{
6401 if (*eap->arg == NUL)
6402 shell_resized();
6403 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006404 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405}
6406
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407/*
6408 * ":resize".
6409 * set, increment or decrement current window height
6410 */
6411 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006412ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413{
6414 int n;
6415 win_T *wp = curwin;
6416
6417 if (eap->addr_count > 0)
6418 {
6419 n = eap->line2;
6420 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6421 ;
6422 }
6423
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006424# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006426# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006428 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 {
6430 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006431 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006432 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 n = 9999;
6434 win_setwidth_win((int)n, wp);
6435 }
6436 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 {
6438 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006439 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006440 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 n = 9999;
6442 win_setheight_win((int)n, wp);
6443 }
6444}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445
6446/*
6447 * ":find [+command] <file>" command.
6448 */
6449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006450ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451{
6452#ifdef FEAT_SEARCHPATH
6453 char_u *fname;
6454 int count;
6455
6456 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6457 TRUE, curbuf->b_ffname);
6458 if (eap->addr_count > 0)
6459 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006460 // Repeat finding the file "count" times. This matters when it
6461 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 count = eap->line2;
6463 while (fname != NULL && --count > 0)
6464 {
6465 vim_free(fname);
6466 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6467 FALSE, curbuf->b_ffname);
6468 }
6469 }
6470
6471 if (fname != NULL)
6472 {
6473 eap->arg = fname;
6474#endif
6475 do_exedit(eap, NULL);
6476#ifdef FEAT_SEARCHPATH
6477 vim_free(fname);
6478 }
6479#endif
6480}
6481
6482/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006483 * ":open" simulation: for now just work like ":visual".
6484 */
6485 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006486ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006487{
6488 regmatch_T regmatch;
6489 char_u *p;
6490
6491 curwin->w_cursor.lnum = eap->line2;
6492 beginline(BL_SOL | BL_FIX);
6493 if (*eap->arg == '/')
6494 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006495 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006496 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006497 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006498 *p = NUL;
6499 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6500 if (regmatch.regprog != NULL)
6501 {
6502 regmatch.rm_ic = p_ic;
6503 p = ml_get_curline();
6504 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006505 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006506 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006507 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006508 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006509 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006510 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006511 eap->arg += STRLEN(eap->arg);
6512 }
6513 check_cursor();
6514
6515 eap->cmdidx = CMD_visual;
6516 do_exedit(eap, NULL);
6517}
6518
6519/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006520 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 */
6522 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006523ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524{
6525 do_exedit(eap, NULL);
6526}
6527
6528/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006529 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006532do_exedit(
6533 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006534 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535{
6536 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006538 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006540 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6541 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006542 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 /*
6544 * ":vi" command ends Ex mode.
6545 */
6546 if (exmode_active && (eap->cmdidx == CMD_visual
6547 || eap->cmdidx == CMD_view))
6548 {
6549 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006550 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006552 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006553 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006554 if (global_busy)
6555 {
6556 int rd = RedrawingDisabled;
6557 int nwr = no_wait_return;
6558 int ms = msg_scroll;
6559#ifdef FEAT_GUI
6560 int he = hold_gui_events;
6561#endif
6562
6563 if (eap->nextcmd != NULL)
6564 {
6565 stuffReadbuff(eap->nextcmd);
6566 eap->nextcmd = NULL;
6567 }
6568
6569 if (exmode_was != EXMODE_VIM)
6570 settmode(TMODE_RAW);
6571 RedrawingDisabled = 0;
6572 no_wait_return = 0;
6573 need_wait_return = FALSE;
6574 msg_scroll = 0;
6575#ifdef FEAT_GUI
6576 hold_gui_events = 0;
6577#endif
6578 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006579 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006580
6581 main_loop(FALSE, TRUE);
6582
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006583 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006584 RedrawingDisabled = rd;
6585 no_wait_return = nwr;
6586 msg_scroll = ms;
6587#ifdef FEAT_GUI
6588 hold_gui_events = he;
6589#endif
6590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 }
6594
6595 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006596 || eap->cmdidx == CMD_tabnew
6597 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006598 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006600 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 setpcmark();
6602 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006603 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6604 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006606 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 || *eap->arg != NUL
6608#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006609 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610#endif
6611 )
6612 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006613 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6614 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006615 if (*eap->arg != NUL && curbuf_locked())
6616 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006617
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 n = readonlymode;
6619 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6620 readonlymode = TRUE;
6621 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006622 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6623 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 setpcmark();
6625 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6626 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006627 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6629 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6630 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006631 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006633 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006634 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006635 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
6636 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006637 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006639 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 if (old_curwin != NULL)
6641 {
6642 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006643 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006645#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006646 cleanup_T cs;
6647
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006648 // Reset the error/interrupt/exception state here so that
6649 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006650 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006651#endif
6652#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006654#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006655 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006656
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006657#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006658 // Restore the error/interrupt/exception state if not
6659 // discarded by a new aborting error, interrupt, or
6660 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006661 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 }
6664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 }
6666 else if (readonlymode && curbuf->b_nwindows == 1)
6667 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006668 // When editing an already visited buffer, 'readonly' won't be set
6669 // but the previous value is kept. With ":view" and ":sview" we
6670 // want the file to be readonly, except when another window is
6671 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 curbuf->b_p_ro = TRUE;
6673 }
6674 readonlymode = n;
6675 }
6676 else
6677 {
6678 if (eap->do_ecmd_cmd != NULL)
6679 do_cmdline_cmd(eap->do_ecmd_cmd);
6680#ifdef FEAT_TITLE
6681 n = curwin->w_arg_idx_invalid;
6682#endif
6683 check_arg_idx(curwin);
6684#ifdef FEAT_TITLE
6685 if (n != curwin->w_arg_idx_invalid)
6686 maketitle();
6687#endif
6688 }
6689
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 /*
6691 * if ":split file" worked, set alternate file name in old window to new
6692 * file
6693 */
6694 if (old_curwin != NULL
6695 && *eap->arg != NUL
6696 && curwin != old_curwin
6697 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006698 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006699 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701
6702 ex_no_reprint = TRUE;
6703}
6704
6705#ifndef FEAT_GUI
6706/*
6707 * ":gui" and ":gvim" when there is no GUI.
6708 */
6709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006710ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006712 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713}
6714#endif
6715
Bram Moolenaar4f974752019-02-17 17:44:42 +01006716#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006718ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719{
6720 gui_make_tearoff(eap->arg);
6721}
6722#endif
6723
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006724#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6725 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006727ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006729# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6730 if (gui.in_use)
6731 gui_make_popup(eap->arg, eap->forceit);
6732# ifdef FEAT_TERM_POPUP_MENU
6733 else
6734# endif
6735# endif
6736# ifdef FEAT_TERM_POPUP_MENU
6737 pum_make_popup(eap->arg, eap->forceit);
6738# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739}
6740#endif
6741
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006743ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744{
6745 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006746 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006748 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749}
6750
6751/*
6752 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6753 * offset.
6754 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006757ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006760 win_T *save_curwin = curwin;
6761 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 long topline;
6763 long y;
6764 linenr_T old_linenr = curwin->w_cursor.lnum;
6765
6766 setpcmark();
6767
6768 /*
6769 * determine max topline
6770 */
6771 if (curwin->w_p_scb)
6772 {
6773 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006774 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 {
6776 if (wp->w_p_scb && wp->w_buffer)
6777 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006778 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 if (topline > y)
6780 topline = y;
6781 }
6782 }
6783 if (topline < 1)
6784 topline = 1;
6785 }
6786 else
6787 {
6788 topline = 1;
6789 }
6790
6791
6792 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006793 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006795 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 {
6797 if (curwin->w_p_scb)
6798 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006799 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 y = topline - curwin->w_topline;
6801 if (y > 0)
6802 scrollup(y, TRUE);
6803 else
6804 scrolldown(-y, TRUE);
6805 curwin->w_scbind_pos = topline;
6806 redraw_later(VALID);
6807 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 }
6810 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006811 curwin = save_curwin;
6812 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 if (curwin->w_p_scb)
6814 {
6815 did_syncbind = TRUE;
6816 checkpcmark();
6817 if (old_linenr != curwin->w_cursor.lnum)
6818 {
6819 char_u ctrl_o[2];
6820
6821 ctrl_o[0] = Ctrl_O;
6822 ctrl_o[1] = 0;
6823 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6824 }
6825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826}
6827
6828
6829 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006830ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006832 int i;
6833 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6834 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006836 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 do_bang(1, eap, FALSE, FALSE, TRUE);
6838 else
6839 {
6840 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6841 return;
6842
6843#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006844 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 {
6846 char_u *browseFile;
6847
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006848 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 NULL, NULL, NULL, curbuf);
6850 if (browseFile != NULL)
6851 {
6852 i = readfile(browseFile, NULL,
6853 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6854 vim_free(browseFile);
6855 }
6856 else
6857 i = OK;
6858 }
6859 else
6860#endif
6861 if (*eap->arg == NUL)
6862 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006863 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 return;
6865 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6866 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6867 }
6868 else
6869 {
6870 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6871 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6872 i = readfile(eap->arg, NULL,
6873 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6874
6875 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006876 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006878#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 if (!aborting())
6880#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006881 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 }
6883 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006884 {
6885 if (empty && exmode_active)
6886 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006887 // Delete the empty line that remains. Historically ex does
6888 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006889 if (eap->line2 == 0)
6890 lnum = curbuf->b_ml.ml_line_count;
6891 else
6892 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006893 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006894 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006895 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006896 if (curwin->w_cursor.lnum > 1
6897 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006898 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006899 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006900 }
6901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 }
6905}
6906
Bram Moolenaarea408852005-06-25 22:49:46 +00006907static char_u *prev_dir = NULL;
6908
6909#if defined(EXITFREE) || defined(PROTO)
6910 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006911free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006912{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006913 VIM_CLEAR(prev_dir);
6914 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006915}
6916#endif
6917
Bram Moolenaarf4258302013-06-02 18:20:17 +02006918/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006919 * Get the previous directory for the given chdir scope.
6920 */
6921 static char_u *
6922get_prevdir(cdscope_T scope)
6923{
6924 if (scope == CDSCOPE_WINDOW)
6925 return curwin->w_prevdir;
6926 else if (scope == CDSCOPE_TABPAGE)
6927 return curtab->tp_prevdir;
6928 return prev_dir;
6929}
6930
6931/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006932 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006933 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6934 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006935 */
6936 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006937post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006938{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006939 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006940 // Clear tab local directory for both :cd and :tcd
6941 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006942 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006943 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006944 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006945 char_u *pdir = get_prevdir(scope);
6946
6947 // If still in the global directory, need to remember current
6948 // directory as the global directory.
6949 if (globaldir == NULL && pdir != NULL)
6950 globaldir = vim_strsave(pdir);
6951
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006952 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006953 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006954 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006955 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006956 curtab->tp_localdir = vim_strsave(NameBuff);
6957 else
6958 curwin->w_localdir = vim_strsave(NameBuff);
6959 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006960 }
6961 else
6962 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006963 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006964 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006965 }
6966
6967 shorten_fnames(TRUE);
6968}
6969
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006970/*
6971 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006972 * chdir() function.
6973 * scope == CDSCOPE_WINDOW: changes the window-local directory
6974 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6975 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006976 * Returns TRUE if the directory is successfully changed.
6977 */
6978 int
6979changedir_func(
6980 char_u *new_dir,
6981 int forceit,
6982 cdscope_T scope)
6983{
6984 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006985 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006986 int dir_differs;
6987 int retval = FALSE;
6988
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006989 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006990 return FALSE;
6991
6992 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6993 {
6994 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6995 return FALSE;
6996 }
6997
6998 // ":cd -": Change to previous directory
6999 if (STRCMP(new_dir, "-") == 0)
7000 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007001 pdir = get_prevdir(scope);
7002 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007003 {
7004 emsg(_("E186: No previous directory"));
7005 return FALSE;
7006 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007007 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007008 }
7009
Bram Moolenaar002bc792020-06-05 22:33:42 +02007010 // Free the previous directory
7011 tofree = get_prevdir(scope);
7012
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007013 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007014 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007015 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007016 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007017 pdir = NULL;
7018 if (scope == CDSCOPE_WINDOW)
7019 curwin->w_prevdir = pdir;
7020 else if (scope == CDSCOPE_TABPAGE)
7021 curtab->tp_prevdir = pdir;
7022 else
7023 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007024
7025#if defined(UNIX) || defined(VMS)
7026 // for UNIX ":cd" means: go to home directory
7027 if (*new_dir == NUL)
7028 {
7029 // use NameBuff for home directory name
7030# ifdef VMS
7031 char_u *p;
7032
7033 p = mch_getenv((char_u *)"SYS$LOGIN");
7034 if (p == NULL || *p == NUL) // empty is the same as not set
7035 NameBuff[0] = NUL;
7036 else
7037 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7038# else
7039 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7040# endif
7041 new_dir = NameBuff;
7042 }
7043#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02007044 dir_differs = new_dir == NULL || pdir == NULL
7045 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007046 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7047 emsg(_(e_failed));
7048 else
7049 {
7050 char_u *acmd_fname;
7051
7052 post_chdir(scope);
7053
7054 if (dir_differs)
7055 {
7056 if (scope == CDSCOPE_WINDOW)
7057 acmd_fname = (char_u *)"window";
7058 else if (scope == CDSCOPE_TABPAGE)
7059 acmd_fname = (char_u *)"tabpage";
7060 else
7061 acmd_fname = (char_u *)"global";
7062 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7063 curbuf);
7064 }
7065 retval = TRUE;
7066 }
7067 vim_free(tofree);
7068
7069 return retval;
7070}
Bram Moolenaarea408852005-06-25 22:49:46 +00007071
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007073 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007075 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007076ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007078 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079
7080 new_dir = eap->arg;
7081#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007082 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 if (*new_dir == NUL)
7084 ex_pwd(NULL);
7085 else
7086#endif
7087 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007088 cdscope_T scope = CDSCOPE_GLOBAL;
7089
7090 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7091 scope = CDSCOPE_WINDOW;
7092 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7093 scope = CDSCOPE_TABPAGE;
7094
7095 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007096 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007097 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007098 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 ex_pwd(eap);
7100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 }
7102}
7103
7104/*
7105 * ":pwd".
7106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007108ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109{
7110 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7111 {
7112#ifdef BACKSLASH_IN_FILENAME
7113 slash_adjust(NameBuff);
7114#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007115 if (p_verbose > 0)
7116 {
7117 char *context = "global";
7118
7119 if (curwin->w_localdir != NULL)
7120 context = "window";
7121 else if (curtab->tp_localdir != NULL)
7122 context = "tabpage";
7123 smsg("[%s] %s", context, (char *)NameBuff);
7124 }
7125 else
7126 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 }
7128 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007129 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130}
7131
7132/*
7133 * ":=".
7134 */
7135 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007136ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007138 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007139 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140}
7141
7142 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007143ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007145 int n;
7146 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147
7148 if (cursor_valid())
7149 {
7150 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7151 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007152 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007154
7155 len = eap->line2;
7156 switch (*eap->arg)
7157 {
7158 case 'm': break;
7159 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007160 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007161 }
7162 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163}
7164
7165/*
7166 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7167 */
7168 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007169do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170{
Bram Moolenaar52953192019-08-16 10:27:13 +02007171 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007172 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007173# ifdef ELAPSED_FUNC
7174 elapsed_T start_tv;
7175
7176 // Remember at what time we started, so that we know how much longer we
7177 // should wait after waiting for a bit.
7178 ELAPSED_INIT(start_tv);
7179# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180
7181 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007182 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007183 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007185 wait_now = msec - done > 1000L ? 1000L : msec - done;
7186#ifdef FEAT_TIMERS
7187 {
7188 long due_time = check_due_timer();
7189
7190 if (due_time > 0 && due_time < wait_now)
7191 wait_now = due_time;
7192 }
7193#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007194#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007195 if (has_any_channel() && wait_now > 20L)
7196 wait_now = 20L;
7197#endif
7198#ifdef FEAT_SOUND
7199 if (has_any_sound_callback() && wait_now > 20L)
7200 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007201#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007202 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007203
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007204#ifdef FEAT_JOB_CHANNEL
7205 if (has_any_channel())
7206 ui_breakcheck_force(TRUE);
7207 else
7208#endif
7209 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007210#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007211 // Process the netbeans and clientserver messages that may have been
7212 // received in the call to ui_breakcheck() when the GUI is in use. This
7213 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007214 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007215#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007216
7217# ifdef ELAPSED_FUNC
7218 // actual time passed
7219 done = ELAPSED_FUNC(start_tv);
7220# else
7221 // guestimate time passed (will actually be more)
7222 done += wait_now;
7223# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007225
7226 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7227 // input buffer, otherwise a following call to input() fails.
7228 if (got_int)
7229 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230}
7231
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232/*
7233 * ":winsize" command (obsolete).
7234 */
7235 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007236ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237{
7238 int w, h;
7239 char_u *arg = eap->arg;
7240 char_u *p;
7241
7242 w = getdigits(&arg);
7243 arg = skipwhite(arg);
7244 p = arg;
7245 h = getdigits(&arg);
7246 if (*p != NUL && *arg == NUL)
7247 set_shellsize(w, h, TRUE);
7248 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007249 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250}
7251
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007253ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254{
7255 int xchar = NUL;
7256 char_u *p;
7257
7258 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7259 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007260 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 if (eap->arg[1] == NUL)
7262 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007263 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 return;
7265 }
7266 xchar = eap->arg[1];
7267 p = eap->arg + 2;
7268 }
7269 else
7270 p = eap->arg + 1;
7271
7272 eap->nextcmd = check_nextcmd(p);
7273 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007274 if (*p != NUL && *p != (
7275#ifdef FEAT_EVAL
7276 in_vim9script() ? '#' :
7277#endif
7278 '"')
7279 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007280 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007281 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007283 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007284 postponed_split_flags = cmdmod.cmod_split;
7285 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7287 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007288 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 }
7290}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291
Bram Moolenaar843ee412004-06-30 16:16:41 +00007292#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293/*
7294 * ":winpos".
7295 */
7296 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007297ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298{
7299 int x, y;
7300 char_u *arg = eap->arg;
7301 char_u *p;
7302
7303 if (*arg == NUL)
7304 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007305# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007306# ifdef VIMDLL
7307 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7308 mch_get_winpos(&x, &y) != FAIL)
7309# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007311# else
7312 if (mch_get_winpos(&x, &y) != FAIL)
7313# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 {
7315 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007316 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 }
7318 else
7319# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007320 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 }
7322 else
7323 {
7324 x = getdigits(&arg);
7325 arg = skipwhite(arg);
7326 p = arg;
7327 y = getdigits(&arg);
7328 if (*p == NUL || *arg != NUL)
7329 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007330 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 return;
7332 }
7333# ifdef FEAT_GUI
7334 if (gui.in_use)
7335 gui_mch_set_winpos(x, y);
7336 else if (gui.starting)
7337 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007338 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 gui_win_x = x;
7340 gui_win_y = y;
7341 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007342# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 else
7344# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007345# endif
7346# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007347 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348# endif
7349# ifdef HAVE_TGETENT
7350 if (*T_CWP)
7351 term_set_winpos(x, y);
7352# endif
7353 }
7354}
7355#endif
7356
7357/*
7358 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7359 */
7360 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007361ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362{
7363 oparg_T oa;
7364
7365 clear_oparg(&oa);
7366 oa.regname = eap->regname;
7367 oa.start.lnum = eap->line1;
7368 oa.end.lnum = eap->line2;
7369 oa.line_count = eap->line2 - eap->line1 + 1;
7370 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007372 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 {
7374 setpcmark();
7375 curwin->w_cursor.lnum = eap->line1;
7376 beginline(BL_SOL | BL_FIX);
7377 }
7378
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007379 if (VIsual_active)
7380 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007381
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 switch (eap->cmdidx)
7383 {
7384 case CMD_delete:
7385 oa.op_type = OP_DELETE;
7386 op_delete(&oa);
7387 break;
7388
7389 case CMD_yank:
7390 oa.op_type = OP_YANK;
7391 (void)op_yank(&oa, FALSE, TRUE);
7392 break;
7393
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007394 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007395 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007397 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7398#else
7399 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007401 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 oa.op_type = OP_RSHIFT;
7403 else
7404 oa.op_type = OP_LSHIFT;
7405 op_shift(&oa, FALSE, eap->amount);
7406 break;
7407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007409 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410}
7411
7412/*
7413 * ":put".
7414 */
7415 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007416ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007418 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 if (eap->line2 == 0)
7420 {
7421 eap->line2 = 1;
7422 eap->forceit = TRUE;
7423 }
7424 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007425 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007426 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427}
7428
7429/*
7430 * Handle ":copy" and ":move".
7431 */
7432 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007433ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434{
7435 long n;
7436
Bram Moolenaar491799b2020-08-01 19:23:43 +02007437#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007438 if (not_in_vim9(eap) == FAIL)
7439 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007440#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007441 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007442 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 {
7444 eap->nextcmd = NULL;
7445 return;
7446 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007447 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448
7449 /*
7450 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7451 */
7452 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7453 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007454 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 return;
7456 }
7457
7458 if (eap->cmdidx == CMD_move)
7459 {
7460 if (do_move(eap->line1, eap->line2, n) == FAIL)
7461 return;
7462 }
7463 else
7464 ex_copy(eap->line1, eap->line2, n);
7465 u_clearline();
7466 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007467 ex_may_print(eap);
7468}
7469
7470/*
7471 * Print the current line if flags were given to the Ex command.
7472 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007473 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007474ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007475{
7476 if (eap->flags != 0)
7477 {
7478 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7479 (eap->flags & EXFLAG_LIST));
7480 ex_no_reprint = TRUE;
7481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482}
7483
7484/*
7485 * ":smagic" and ":snomagic".
7486 */
7487 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007488ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489{
7490 int magic_save = p_magic;
7491
7492 p_magic = (eap->cmdidx == CMD_smagic);
Bram Moolenaar66e00142020-08-12 21:58:12 +02007493 ex_substitute(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 p_magic = magic_save;
7495}
7496
7497/*
7498 * ":join".
7499 */
7500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007501ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502{
7503 curwin->w_cursor.lnum = eap->line1;
7504 if (eap->line1 == eap->line2)
7505 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007506 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 return;
7508 if (eap->line2 == curbuf->b_ml.ml_line_count)
7509 {
7510 beep_flush();
7511 return;
7512 }
7513 ++eap->line2;
7514 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007515 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007517 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518}
7519
7520/*
7521 * ":[addr]@r" or ":[addr]*r": execute register
7522 */
7523 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007524ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525{
7526 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007527 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528
7529 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007530 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531
7532#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007533 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534#endif
7535
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007536 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 c = *eap->arg;
7538 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7539 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007540 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007541 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7542 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007543 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 else
7547 {
7548 int save_efr = exec_from_reg;
7549
7550 exec_from_reg = TRUE;
7551
7552 /*
7553 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007554 * Continue until the stuff buffer is empty and all added characters
7555 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007557 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7559
7560 exec_from_reg = save_efr;
7561 }
7562}
7563
7564/*
7565 * ":!".
7566 */
7567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007568ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569{
7570 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7571}
7572
7573/*
7574 * ":undo".
7575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007577ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007579 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007580 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007581 else
7582 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583}
7584
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007585#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007586 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007587ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007588{
7589 char_u hash[UNDO_HASH_SIZE];
7590
7591 u_compute_hash(hash);
7592 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7593}
7594
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007595 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007596ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007597{
7598 char_u hash[UNDO_HASH_SIZE];
7599
7600 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007601 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007602}
7603#endif
7604
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605/*
7606 * ":redo".
7607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007609ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610{
7611 u_redo(1);
7612}
7613
7614/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007615 * ":earlier" and ":later".
7616 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007617 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007618ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007619{
7620 long count = 0;
7621 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007622 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007623 char_u *p = eap->arg;
7624
7625 if (*p == NUL)
7626 count = 1;
7627 else if (isdigit(*p))
7628 {
7629 count = getdigits(&p);
7630 switch (*p)
7631 {
7632 case 's': ++p; sec = TRUE; break;
7633 case 'm': ++p; sec = TRUE; count *= 60; break;
7634 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007635 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7636 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007637 }
7638 }
7639
7640 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007641 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007642 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007643 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7644 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007645}
7646
7647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 * ":redir": start/stop redirection.
7649 */
7650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007651ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652{
7653 char *mode;
7654 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007655 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656
Bram Moolenaarba768492016-07-08 15:32:54 +02007657#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007658 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007659 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007660 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007661 return;
7662 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007663#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007664
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 if (STRICMP(eap->arg, "END") == 0)
7666 close_redir();
7667 else
7668 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007669 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007671 ++arg;
7672 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007674 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 mode = "a";
7676 }
7677 else
7678 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007679 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680
7681 close_redir();
7682
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007683 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007684 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 if (fname == NULL)
7686 return;
7687#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007688 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 {
7690 char_u *browseFile;
7691
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007692 browseFile = do_browse(BROWSE_SAVE,
7693 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007694 fname, NULL, NULL,
7695 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007697 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 vim_free(fname);
7699 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007700 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 }
7702#endif
7703
7704 redir_fd = open_exfile(fname, eap->forceit, mode);
7705 vim_free(fname);
7706 }
7707#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007708 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007710 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007712 ++arg;
7713 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007715 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007716 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007718 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007720 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007721 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007722 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007723 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007725 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007726 if (*arg == '>')
7727 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007728 // Make register empty when not using @A-@Z and the
7729 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007730 if (*arg == NUL && !isupper(redir_reg))
7731 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007733 }
7734 if (*arg != NUL)
7735 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007736 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007737 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007738 }
7739 }
7740 else if (*arg == '=' && arg[1] == '>')
7741 {
7742 int append;
7743
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007744 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007745 close_redir();
7746 arg += 2;
7747
7748 if (*arg == '>')
7749 {
7750 ++arg;
7751 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 }
7753 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007754 append = FALSE;
7755
7756 if (var_redir_start(skipwhite(arg), append) == OK)
7757 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 }
7759#endif
7760
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007761 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007764 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007766
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007767 // Make sure redirection is not off. Can happen for cmdline completion
7768 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007769 if (redir_fd != NULL
7770#ifdef FEAT_EVAL
7771 || redir_reg || redir_vname
7772#endif
7773 )
7774 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775}
7776
7777/*
7778 * ":redraw": force redraw
7779 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007780 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007781ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782{
7783 int r = RedrawingDisabled;
7784 int p = p_lz;
7785
7786 RedrawingDisabled = 0;
7787 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007788 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007790 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791#ifdef FEAT_TITLE
7792 if (need_maketitle)
7793 maketitle();
7794#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007795#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7796# ifdef VIMDLL
7797 if (!gui.in_use)
7798# endif
7799 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 RedrawingDisabled = r;
7802 p_lz = p;
7803
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007804 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 msg_didout = FALSE;
7806 msg_col = 0;
7807
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007808 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007809 need_wait_return = FALSE;
7810
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 out_flush();
7812}
7813
7814/*
7815 * ":redrawstatus": force redraw of status line(s)
7816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007818ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 int r = RedrawingDisabled;
7821 int p = p_lz;
7822
7823 RedrawingDisabled = 0;
7824 p_lz = FALSE;
7825 if (eap->forceit)
7826 status_redraw_all();
7827 else
7828 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007829 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 RedrawingDisabled = r;
7831 p_lz = p;
7832 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833}
7834
Bram Moolenaare12bab32019-01-08 22:02:56 +01007835/*
7836 * ":redrawtabline": force redraw of the tabline
7837 */
7838 static void
7839ex_redrawtabline(exarg_T *eap UNUSED)
7840{
7841 int r = RedrawingDisabled;
7842 int p = p_lz;
7843
7844 RedrawingDisabled = 0;
7845 p_lz = FALSE;
7846
7847 draw_tabline();
7848
7849 RedrawingDisabled = r;
7850 p_lz = p;
7851 out_flush();
7852}
7853
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007855close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856{
7857 if (redir_fd != NULL)
7858 {
7859 fclose(redir_fd);
7860 redir_fd = NULL;
7861 }
7862#ifdef FEAT_EVAL
7863 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007864 if (redir_vname)
7865 {
7866 var_redir_stop();
7867 redir_vname = 0;
7868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869#endif
7870}
7871
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007872#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007873 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007874vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007875{
7876 if (vim_mkdir(name, prot) != 0)
7877 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007878 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007879 return FAIL;
7880 }
7881 return OK;
7882}
7883#endif
7884
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885/*
7886 * Open a file for writing for an Ex command, with some checks.
7887 * Return file descriptor, or NULL on failure.
7888 */
7889 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007890open_exfile(
7891 char_u *fname,
7892 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007893 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894{
7895 FILE *fd;
7896
7897#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007898 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 if (mch_isdir(fname))
7900 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007901 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 return NULL;
7903 }
7904#endif
7905 if (!forceit && *mode != 'a' && vim_fexists(fname))
7906 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007907 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 return NULL;
7909 }
7910
7911 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007912 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913
7914 return fd;
7915}
7916
7917/*
7918 * ":mark" and ":k".
7919 */
7920 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007921ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922{
7923 pos_T pos;
7924
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007925 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007926 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007927 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02007928 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 else
7930 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007931 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 curwin->w_cursor.lnum = eap->line2;
7933 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007934 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007935 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007936 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 }
7938}
7939
7940/*
7941 * Update w_topline, w_leftcol and the cursor position.
7942 */
7943 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007944update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007946 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 update_topline();
7948 if (!curwin->w_p_wrap)
7949 validate_cursor();
7950 update_curswant();
7951}
7952
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007954 * Save the current State and go to Normal mode.
7955 * Return TRUE if the typeahead could be saved.
7956 */
7957 int
7958save_current_state(save_state_T *sst)
7959{
7960 sst->save_msg_scroll = msg_scroll;
7961 sst->save_restart_edit = restart_edit;
7962 sst->save_msg_didout = msg_didout;
7963 sst->save_State = State;
7964 sst->save_insertmode = p_im;
7965 sst->save_finish_op = finish_op;
7966 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007967 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007968
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007969 msg_scroll = FALSE; // no msg scrolling in Normal mode
7970 restart_edit = 0; // don't go to Insert mode
7971 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007972
7973 /*
7974 * Save the current typeahead. This is required to allow using ":normal"
7975 * from an event handler and makes sure we don't hang when the argument
7976 * ends with half a command.
7977 */
7978 save_typeahead(&sst->tabuf);
7979 return sst->tabuf.typebuf_valid;
7980}
7981
7982 void
7983restore_current_state(save_state_T *sst)
7984{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007985 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007986 restore_typeahead(&sst->tabuf);
7987
7988 msg_scroll = sst->save_msg_scroll;
7989 restart_edit = sst->save_restart_edit;
7990 p_im = sst->save_insertmode;
7991 finish_op = sst->save_finish_op;
7992 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007993 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007994 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007995
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007996 // Restore the state (needed when called from a function executed for
7997 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007998 State = sst->save_State;
7999#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008000 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008001#endif
8002}
8003
8004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 * ":normal[!] {commands}": Execute normal mode commands.
8006 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008007 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008008ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008010 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 char_u *arg = NULL;
8012 int l;
8013 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008015 if (ex_normal_lock > 0)
8016 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008017 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008018 return;
8019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 if (ex_normal_busy >= p_mmd)
8021 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008022 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 return;
8024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 /*
8027 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8028 * this for the K_SPECIAL leading byte, otherwise special keys will not
8029 * work.
8030 */
8031 if (has_mbyte)
8032 {
8033 int len = 0;
8034
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008035 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 for (p = eap->arg; *p != NUL; ++p)
8037 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008038#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008039 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008041#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008042 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008043 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008044#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 )
8048 len += 2;
8049 }
8050 if (len > 0)
8051 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008052 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 if (arg != NULL)
8054 {
8055 len = 0;
8056 for (p = eap->arg; *p != NUL; ++p)
8057 {
8058 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008059#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 if (*p == CSI)
8061 {
8062 arg[len++] = KS_EXTRA;
8063 arg[len++] = (int)KE_CSI;
8064 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008065#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008066 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 {
8068 arg[len++] = *++p;
8069 if (*p == K_SPECIAL)
8070 {
8071 arg[len++] = KS_SPECIAL;
8072 arg[len++] = KE_FILLER;
8073 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008074#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 else if (*p == CSI)
8076 {
8077 arg[len++] = KS_EXTRA;
8078 arg[len++] = (int)KE_CSI;
8079 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 }
8082 arg[len] = NUL;
8083 }
8084 }
8085 }
8086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008088 ++ex_normal_busy;
8089 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 {
8091 /*
8092 * Repeat the :normal command for each line in the range. When no
8093 * range given, execute it just once, without positioning the cursor
8094 * first.
8095 */
8096 do
8097 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 if (eap->addr_count != 0)
8099 {
8100 curwin->w_cursor.lnum = eap->line1++;
8101 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008102 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 }
8104
Bram Moolenaar13505972019-01-24 15:04:48 +01008105 exec_normal_cmd(arg != NULL
8106 ? arg
8107 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 }
8109 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8110 }
8111
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008112 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 update_topline_cursor();
8114
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008115 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008117 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008118#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008119 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008120#endif
8121
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123}
8124
8125/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008126 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 */
8128 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008129ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008131 if (eap->forceit)
8132 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008133 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008134 if (!curwin->w_cursor.lnum)
8135 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008136 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008137 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008138#ifdef FEAT_TERMINAL
8139 // Ignore this when running in an active terminal.
8140 if (term_job_running(curbuf->b_term))
8141 return;
8142#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008143
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008144 // Ignore the command when already in Insert mode. Inserting an
8145 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008146 if (State & INSERT)
8147 return;
8148
Bram Moolenaar64969662005-12-14 21:59:55 +00008149 if (eap->cmdidx == CMD_startinsert)
8150 restart_edit = 'a';
8151 else if (eap->cmdidx == CMD_startreplace)
8152 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008154 restart_edit = 'V';
8155
8156 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008158 if (eap->cmdidx == CMD_startinsert)
8159 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008160 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008162
8163 if (VIsual_active)
8164 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165}
8166
8167/*
8168 * ":stopinsert"
8169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008171ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172{
8173 restart_edit = 0;
8174 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008175 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008178/*
8179 * Execute normal mode command "cmd".
8180 * "remap" can be REMAP_NONE or REMAP_YES.
8181 */
8182 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008183exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008184{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008185 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008186 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008187 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008188}
Bram Moolenaar25281632016-01-21 23:32:32 +01008189
Bram Moolenaar25281632016-01-21 23:32:32 +01008190/*
8191 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008192 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008193 */
8194 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008195exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008196{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008197 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008198 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008199
Bram Moolenaar905dd902019-04-07 14:21:47 +02008200 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8201 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008202 clear_oparg(&oa);
8203 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008204 while ((!stuff_empty()
8205 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008206 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008207 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008208 {
8209 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008210#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008211 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008212 && oa.op_type == OP_NOP && oa.regname == NUL
8213 && !VIsual_active)
8214 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008215 // If terminal_loop() returns OK we got a key that is handled
8216 // in Normal model. With FAIL we first need to position the
8217 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008218 if (terminal_loop(TRUE) == OK)
8219 normal_cmd(&oa, TRUE);
8220 }
8221 else
8222#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008223 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008224 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008225 }
8226}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008227
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228#ifdef FEAT_FIND_ID
8229 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008230ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231{
8232 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8233 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8234 (linenr_T)1, (linenr_T)MAXLNUM);
8235}
8236
Bram Moolenaar4033c552017-09-16 20:54:51 +02008237#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238/*
8239 * ":psearch"
8240 */
8241 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008242ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243{
8244 g_do_tagpreview = p_pvh;
8245 ex_findpat(eap);
8246 g_do_tagpreview = 0;
8247}
8248#endif
8249
8250 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008251ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252{
8253 int whole = TRUE;
8254 long n;
8255 char_u *p;
8256 int action;
8257
8258 switch (cmdnames[eap->cmdidx].cmd_name[2])
8259 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008260 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8262 action = ACTION_GOTO;
8263 else
8264 action = ACTION_SHOW;
8265 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008266 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 action = ACTION_SHOW_ALL;
8268 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008269 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 action = ACTION_GOTO;
8271 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008272 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 action = ACTION_SPLIT;
8274 break;
8275 }
8276
8277 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008278 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 {
8280 n = getdigits(&eap->arg);
8281 eap->arg = skipwhite(eap->arg);
8282 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008283 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 {
8285 whole = FALSE;
8286 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008287 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 if (*p)
8289 {
8290 *p++ = NUL;
8291 p = skipwhite(p);
8292
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008293 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008294 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008295 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 else
8297 eap->nextcmd = check_nextcmd(p);
8298 }
8299 }
8300 if (!eap->skip)
8301 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8302 whole, !eap->forceit,
8303 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8304 n, action, eap->line1, eap->line2);
8305}
8306#endif
8307
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308
Bram Moolenaar4033c552017-09-16 20:54:51 +02008309#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310/*
8311 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8312 */
8313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008314ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008316 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8318}
8319
8320/*
8321 * ":pedit"
8322 */
8323 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008324ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325{
8326 win_T *curwin_save = curwin;
8327
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008328 if (ERROR_IF_ANY_POPUP_WINDOW)
8329 return;
8330
Bram Moolenaar026587b2019-08-17 15:08:00 +02008331 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008333 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008334
Bram Moolenaar026587b2019-08-17 15:08:00 +02008335 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008337
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 if (curwin != curwin_save && win_valid(curwin_save))
8339 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008340 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 validate_cursor();
8342 redraw_later(VALID);
8343 win_enter(curwin_save, TRUE);
8344 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008345# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008346 else if (WIN_IS_POPUP(curwin))
8347 {
8348 // can't keep focus in popup window
8349 win_enter(firstwin, TRUE);
8350 }
8351# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 g_do_tagpreview = 0;
8353}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355
8356/*
8357 * ":stag", ":stselect" and ":stjump".
8358 */
8359 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008360ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361{
8362 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008363 postponed_split_flags = cmdmod.cmod_split;
8364 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8366 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008367 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369
8370/*
8371 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8372 */
8373 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008374ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375{
8376 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8377}
8378
8379 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008380ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381{
8382 int cmd;
8383
8384 switch (name[1])
8385 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008386 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008388 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008390 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008392 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008394 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008396 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008398 case 'f': // ":tfirst"
8399 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008401 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008403 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008405 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008407 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 return;
8409 }
8410#endif
8411 cmd = DT_TAG;
8412 break;
8413 }
8414
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008415 if (name[0] == 'l')
8416 {
8417#ifndef FEAT_QUICKFIX
8418 ex_ni(eap);
8419 return;
8420#else
8421 cmd = DT_LTAG;
8422#endif
8423 }
8424
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8426 eap->forceit, TRUE);
8427}
8428
8429/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008430 * Check "str" for starting with a special cmdline variable.
8431 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8432 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8433 */
8434 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008435find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008436{
8437 int len;
8438 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008439 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008440 "%",
8441#define SPEC_PERC 0
8442 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008443#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008444 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008445#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008446 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008447#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008448 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008449#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008450 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008451#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008452 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008453#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008454 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008455#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008456 "<stack>", // call stack
8457#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008458 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008459#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008460 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008461#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008462 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008463#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008464 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008465#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008466 "<SID>", // script ID: <SNR>123_
8467#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008468#ifdef FEAT_CLIENTSERVER
8469 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008470# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008471#endif
8472 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008473
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008474 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008475 {
8476 len = (int)STRLEN(spec_str[i]);
8477 if (STRNCMP(src, spec_str[i], len) == 0)
8478 {
8479 *usedlen = len;
8480 return i;
8481 }
8482 }
8483 return -1;
8484}
8485
8486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 * Evaluate cmdline variables.
8488 *
8489 * change '%' to curbuf->b_ffname
Bram Moolenaar59d8e562020-11-07 18:41:10 +01008490 * '#' to curwin->w_alt_fnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 * '<cword>' to word under the cursor
8492 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008493 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 * '<cfile>' to path name under the cursor
8495 * '<sfile>' to sourced file name
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008496 * '<stack>' to call stack
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008497 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 * '<afile>' to file name for autocommand
8499 * '<abuf>' to buffer number for autocommand
8500 * '<amatch>' to matching name for autocommand
8501 *
8502 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8503 * "" for error without a message) and NULL is returned.
8504 * Returns an allocated string if a valid match was found.
8505 * Returns NULL if no match was found. "usedlen" then still contains the
8506 * number of characters to skip.
8507 */
8508 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008509eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008510 char_u *src, // pointer into commandline
8511 char_u *srcstart, // beginning of valid memory for src
8512 int *usedlen, // characters after src that are used
8513 linenr_T *lnump, // line number for :e command, or NULL
8514 char **errormsg, // pointer to error message
8515 int *escaped) // return value has escaped white space (can
8516 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517{
8518 int i;
8519 char_u *s;
8520 char_u *result;
8521 char_u *resultbuf = NULL;
8522 int resultlen;
8523 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008524 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008526 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529
8530 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008531 if (escaped != NULL)
8532 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533
8534 /*
8535 * Check if there is something to do.
8536 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008537 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008538 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 {
8540 *usedlen = 1;
8541 return NULL;
8542 }
8543
8544 /*
8545 * Skip when preceded with a backslash "\%" and "\#".
8546 * Note: In "\\%" the % is also not recognized!
8547 */
8548 if (src > srcstart && src[-1] == '\\')
8549 {
8550 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008551 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 return NULL;
8553 }
8554
8555 /*
8556 * word or WORD under cursor
8557 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008558 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8559 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008561 resultlen = find_ident_under_cursor(&result,
8562 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8563 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8564 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 if (resultlen == 0)
8566 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008567 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 return NULL;
8569 }
8570 }
8571
8572 /*
8573 * '#': Alternate file name
8574 * '%': Current file name
8575 * File name under the cursor
8576 * File name for autocommand
8577 * and following modifiers
8578 */
8579 else
8580 {
8581 switch (spec_idx)
8582 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008583 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 if (curbuf->b_fname == NULL)
8585 {
8586 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008587 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 }
8589 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008592 tilde_file = STRCMP(result, "~") == 0;
8593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 break;
8595
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008596 case SPEC_HASH: // '#' or "#99": alternate file
8597 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 {
8599 result = arg_all();
8600 resultbuf = result;
8601 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008602 if (escaped != NULL)
8603 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 break;
8606 }
8607 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008608 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008609 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008611 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008612 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008613 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008614 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008616 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008618 if (*usedlen < 2)
8619 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008620 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008621 *usedlen = 1;
8622 return NULL;
8623 }
8624#ifdef FEAT_EVAL
8625 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8626 (long)i);
8627 if (result == NULL)
8628 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008629 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008630 return NULL;
8631 }
8632#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008633 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 }
8637 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008638 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008639 if (i == 0 && src[1] == '<' && *usedlen > 1)
8640 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008641 buf = buflist_findnr(i);
8642 if (buf == NULL)
8643 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008644 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008645 return NULL;
8646 }
8647 if (lnump != NULL)
8648 *lnump = ECMD_LAST;
8649 if (buf->b_fname == NULL)
8650 {
8651 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008652 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008653 }
8654 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008655 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008656 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008657 tilde_file = STRCMP(result, "~") == 0;
8658 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 break;
8661
8662#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008663 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008664 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 if (result == NULL)
8666 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008667 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 return NULL;
8669 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008670 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 break;
8672#endif
8673
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008674 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008676 if (result != NULL && !autocmd_fname_full)
8677 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008678 // Still need to turn the fname into a full path. It is
8679 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008680 autocmd_fname_full = TRUE;
8681 result = FullName_save(autocmd_fname, FALSE);
8682 vim_free(autocmd_fname);
8683 autocmd_fname = result;
8684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 if (result == NULL)
8686 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008687 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 return NULL;
8689 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008690 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 break;
8692
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008693 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 if (autocmd_bufnr <= 0)
8695 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008696 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 return NULL;
8698 }
8699 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8700 result = strbuf;
8701 break;
8702
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008703 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 result = autocmd_match;
8705 if (result == NULL)
8706 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008707 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 return NULL;
8709 }
8710 break;
8711
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008712 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008713 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008714 result = estack_sfile(spec_idx == SPEC_SFILE
8715 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 if (result == NULL)
8717 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008718 *errormsg = spec_idx == SPEC_SFILE
8719 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8720 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 return NULL;
8722 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008723 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008725
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008726 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008727 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008728 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008729 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008730 return NULL;
8731 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008732 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008733 result = strbuf;
8734 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008735
8736#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008737 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008738 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008739 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008740 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008741 return NULL;
8742 }
8743 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008744 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008745 result = strbuf;
8746 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008747
Bram Moolenaar90944302020-08-01 20:45:11 +02008748 case SPEC_SID:
8749 if (current_sctx.sc_sid <= 0)
8750 {
8751 *errormsg = _(e_usingsid);
8752 return NULL;
8753 }
8754 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
8755 result = strbuf;
8756 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02008757#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02008758
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008759#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008760 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008761 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8762 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 result = strbuf;
8764 break;
8765#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008766
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008767 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008768 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008769 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 }
8771
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008772 resultlen = (int)STRLEN(result); // length of new string
8773 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 {
8775 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 resultlen = (int)(s - result);
8778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 else if (!skip_mod)
8780 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008781 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 &resultlen);
8783 if (result == NULL)
8784 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008785 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 return NULL;
8787 }
8788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 }
8790
8791 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8792 {
8793 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008794 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008795 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008797 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 result = NULL;
8799 }
8800 else
8801 result = vim_strnsave(result, resultlen);
8802 vim_free(resultbuf);
8803 return result;
8804}
8805
8806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 * Expand the <sfile> string in "arg".
8808 *
8809 * Returns an allocated string, or NULL for any error.
8810 */
8811 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008812expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008814 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 int len;
8816 char_u *result;
8817 char_u *newres;
8818 char_u *repl;
8819 int srclen;
8820 char_u *p;
8821
8822 result = vim_strsave(arg);
8823 if (result == NULL)
8824 return NULL;
8825
8826 for (p = result; *p; )
8827 {
8828 if (STRNCMP(p, "<sfile>", 7) != 0)
8829 ++p;
8830 else
8831 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008832 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008833 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 if (errormsg != NULL)
8835 {
8836 if (*errormsg)
8837 emsg(errormsg);
8838 vim_free(result);
8839 return NULL;
8840 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008841 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 {
8843 p += srclen;
8844 continue;
8845 }
8846 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8847 newres = alloc(len);
8848 if (newres == NULL)
8849 {
8850 vim_free(repl);
8851 vim_free(result);
8852 return NULL;
8853 }
8854 mch_memmove(newres, result, (size_t)(p - result));
8855 STRCPY(newres + (p - result), repl);
8856 len = (int)STRLEN(newres);
8857 STRCAT(newres, p + srclen);
8858 vim_free(repl);
8859 vim_free(result);
8860 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008861 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 }
8863 }
8864
8865 return result;
8866}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008869/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008870 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008871 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008872 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008874dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 if (fname == NULL)
8877 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008878 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879}
8880#endif
8881
8882/*
8883 * ":behave {mswin,xterm}"
8884 */
8885 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008886ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887{
8888 if (STRCMP(eap->arg, "mswin") == 0)
8889 {
8890 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8891 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8892 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8893 set_option_value((char_u *)"keymodel", 0L,
8894 (char_u *)"startsel,stopsel", 0);
8895 }
8896 else if (STRCMP(eap->arg, "xterm") == 0)
8897 {
8898 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8899 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8900 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8901 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8902 }
8903 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008904 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905}
8906
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907static int filetype_detect = FALSE;
8908static int filetype_plugin = FALSE;
8909static int filetype_indent = FALSE;
8910
8911/*
8912 * ":filetype [plugin] [indent] {on,off,detect}"
8913 * on: Load the filetype.vim file to install autocommands for file types.
8914 * off: Load the ftoff.vim file to remove all autocommands for file types.
8915 * plugin on: load filetype.vim and ftplugin.vim
8916 * plugin off: load ftplugof.vim
8917 * indent on: load filetype.vim and indent.vim
8918 * indent off: load indoff.vim
8919 */
8920 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008921ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922{
8923 char_u *arg = eap->arg;
8924 int plugin = FALSE;
8925 int indent = FALSE;
8926
8927 if (*eap->arg == NUL)
8928 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008929 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008930 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 filetype_detect ? "ON" : "OFF",
8932 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8933 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8934 return;
8935 }
8936
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008937 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 for (;;)
8939 {
8940 if (STRNCMP(arg, "plugin", 6) == 0)
8941 {
8942 plugin = TRUE;
8943 arg = skipwhite(arg + 6);
8944 continue;
8945 }
8946 if (STRNCMP(arg, "indent", 6) == 0)
8947 {
8948 indent = TRUE;
8949 arg = skipwhite(arg + 6);
8950 continue;
8951 }
8952 break;
8953 }
8954 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8955 {
8956 if (*arg == 'o' || !filetype_detect)
8957 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008958 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 filetype_detect = TRUE;
8960 if (plugin)
8961 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008962 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 filetype_plugin = TRUE;
8964 }
8965 if (indent)
8966 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008967 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 filetype_indent = TRUE;
8969 }
8970 }
8971 if (*arg == 'd')
8972 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008973 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008974 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 }
8976 }
8977 else if (STRCMP(arg, "off") == 0)
8978 {
8979 if (plugin || indent)
8980 {
8981 if (plugin)
8982 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008983 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 filetype_plugin = FALSE;
8985 }
8986 if (indent)
8987 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008988 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 filetype_indent = FALSE;
8990 }
8991 }
8992 else
8993 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008994 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 filetype_detect = FALSE;
8996 }
8997 }
8998 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008999 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000}
9001
9002/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009003 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 */
9005 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009006ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007{
9008 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009009 {
9010 char_u *arg = eap->arg;
9011
9012 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9013 arg += 9;
9014
9015 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9016 if (arg != eap->arg)
9017 did_filetype = FALSE;
9018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009022ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023{
9024#ifdef FEAT_DIGRAPHS
9025 if (*eap->arg != NUL)
9026 putdigraph(eap->arg);
9027 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009028 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009030 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031#endif
9032}
9033
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009034#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9035 void
9036set_no_hlsearch(int flag)
9037{
9038 no_hlsearch = flag;
9039# ifdef FEAT_EVAL
9040 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9041# endif
9042}
9043
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044/*
9045 * ":nohlsearch"
9046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009048ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009050 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009051 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053#endif
9054
9055#ifdef FEAT_CRYPT
9056/*
9057 * ":X": Get crypt key
9058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009060ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009062 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009063 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064}
9065#endif
9066
9067#ifdef FEAT_FOLDING
9068 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009069ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070{
9071 if (foldManualAllowed(TRUE))
9072 foldCreate(eap->line1, eap->line2);
9073}
9074
9075 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009076ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077{
9078 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9079 eap->forceit, FALSE);
9080}
9081
9082 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009083ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084{
9085 linenr_T lnum;
9086
Bram Moolenaar4facea32019-10-12 20:17:40 +02009087# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009088 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009089# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009090
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009091 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9093 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9094 ml_setmarked(lnum);
9095
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009096 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009098 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009099# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009100 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009101# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102}
9103#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009104
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009105#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009106/*
9107 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9108 * instead of a quickfix command.
9109 */
9110 int
9111is_loclist_cmd(int cmdidx)
9112{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009113 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009114 return FALSE;
9115 return cmdnames[cmdidx].cmd_name[0] == 'l';
9116}
9117#endif
9118
Bram Moolenaar4facea32019-10-12 20:17:40 +02009119#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009120 int
9121get_pressedreturn(void)
9122{
9123 return ex_pressedreturn;
9124}
9125
9126 void
9127set_pressedreturn(int val)
9128{
9129 ex_pressedreturn = val;
9130}
9131#endif