blob: 2c35c8ef21944439cc010d44873e03b6352a0bd7 [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 Moolenaar95388e32020-11-20 21:07:00 +01003485 // Not not recognize ":*" as the star command unless '*' is in
3486 // 'cpoptions'.
3487 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3488 p = eap->cmd;
3489
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003490 // Look for a user defined command as a last resort. Let ":Print" be
3491 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003492 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3493 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003495 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 while (ASCII_ISALNUM(*p))
3497 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003498 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003500 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 eap->cmdidx = CMD_SIZE;
3502 }
3503
Bram Moolenaar373863e2020-09-26 17:20:53 +02003504 // ":fina" means ":finally" for backwards compatibility.
3505 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3506 eap->cmdidx = CMD_finally;
3507
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 return p;
3509}
3510
3511#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003512static struct cmdmod
3513{
3514 char *name;
3515 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003516 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003517} cmdmods[] = {
3518 {"aboveleft", 3, FALSE},
3519 {"belowright", 3, FALSE},
3520 {"botright", 2, FALSE},
3521 {"browse", 3, FALSE},
3522 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003523 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003524 {"hide", 3, FALSE},
3525 {"keepalt", 5, FALSE},
3526 {"keepjumps", 5, FALSE},
3527 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003528 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003529 {"leftabove", 5, FALSE},
3530 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003531 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003532 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003533 {"rightbelow", 6, FALSE},
3534 {"sandbox", 3, FALSE},
3535 {"silent", 3, FALSE},
3536 {"tab", 3, TRUE},
3537 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003538 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003539 {"verbose", 4, TRUE},
3540 {"vertical", 4, FALSE},
3541};
3542
3543/*
3544 * Return length of a command modifier (including optional count).
3545 * Return zero when it's not a modifier.
3546 */
3547 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003548modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003549{
3550 int i, j;
3551 char_u *p = cmd;
3552
3553 if (VIM_ISDIGIT(*cmd))
3554 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003555 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003556 {
3557 for (j = 0; p[j] != NUL; ++j)
3558 if (p[j] != cmdmods[i].name[j])
3559 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003560 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003561 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003562 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003563 }
3564 return 0;
3565}
3566
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567/*
3568 * Return > 0 if an Ex command "name" exists.
3569 * Return 2 if there is an exact match.
3570 * Return 3 if there is an ambiguous match.
3571 */
3572 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003573cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574{
3575 exarg_T ea;
3576 int full = FALSE;
3577 int i;
3578 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003579 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003581 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003582 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 {
3584 for (j = 0; name[j] != NUL; ++j)
3585 if (name[j] != cmdmods[i].name[j])
3586 break;
3587 if (name[j] == NUL && j >= cmdmods[i].minlen)
3588 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3589 }
3590
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003591 // Check built-in commands and user defined commands.
3592 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003593 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003596 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003598 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3599 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003600 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003601 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3603}
3604#endif
3605
Bram Moolenaard0190392019-08-23 21:17:35 +02003606 cmdidx_T
3607excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608{
Bram Moolenaard0190392019-08-23 21:17:35 +02003609 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610
Bram Moolenaard0190392019-08-23 21:17:35 +02003611 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3612 idx = (cmdidx_T)((int)idx + 1))
3613 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 break;
3615
Bram Moolenaard0190392019-08-23 21:17:35 +02003616 return idx;
3617}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618
Bram Moolenaard0190392019-08-23 21:17:35 +02003619 long
3620excmd_get_argt(cmdidx_T idx)
3621{
3622 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623}
3624
3625/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003626 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 *
3628 * Backslashed delimiters after / or ? will be skipped, and commands will
3629 * not be expanded between /'s and ?'s or after "'".
3630 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003631 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 * Returns the "cmd" pointer advanced to beyond the range.
3633 */
3634 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003635skip_range(
3636 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003637 int skip_star, // skip "*" used for Visual range
3638 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003640 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003642 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003644 if (*cmd == '\\')
3645 {
3646 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3647 ++cmd;
3648 else
3649 break;
3650 }
3651 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 {
3653 if (*++cmd == NUL && ctx != NULL)
3654 *ctx = EXPAND_NOTHING;
3655 }
3656 else if (*cmd == '/' || *cmd == '?')
3657 {
3658 delim = *cmd++;
3659 while (*cmd != NUL && *cmd != delim)
3660 if (*cmd++ == '\\' && *cmd != NUL)
3661 ++cmd;
3662 if (*cmd == NUL && ctx != NULL)
3663 *ctx = EXPAND_NOTHING;
3664 }
3665 if (*cmd != NUL)
3666 ++cmd;
3667 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003668
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003669 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003670 while (*cmd == ':')
3671 cmd = skipwhite(cmd + 1);
3672
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003673 // Skip "*" used for Visual range.
3674 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3675 cmd = skipwhite(cmd + 1);
3676
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 return cmd;
3678}
3679
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003680 static void
3681addr_error(cmd_addr_T addr_type)
3682{
3683 if (addr_type == ADDR_NONE)
3684 emsg(_(e_norange));
3685 else
3686 emsg(_(e_invrange));
3687}
3688
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003690 * Return the default address for an address type.
3691 */
3692 static linenr_T
3693default_address(exarg_T *eap)
3694{
3695 linenr_T lnum = 0;
3696
3697 switch (eap->addr_type)
3698 {
3699 case ADDR_LINES:
3700 case ADDR_OTHER:
3701 // Default is the cursor line number. Avoid using an invalid
3702 // line number though.
3703 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3704 lnum = curbuf->b_ml.ml_line_count;
3705 else
3706 lnum = curwin->w_cursor.lnum;
3707 break;
3708 case ADDR_WINDOWS:
3709 lnum = CURRENT_WIN_NR;
3710 break;
3711 case ADDR_ARGUMENTS:
3712 lnum = curwin->w_arg_idx + 1;
3713 if (lnum > ARGCOUNT)
3714 lnum = ARGCOUNT;
3715 break;
3716 case ADDR_LOADED_BUFFERS:
3717 case ADDR_BUFFERS:
3718 lnum = curbuf->b_fnum;
3719 break;
3720 case ADDR_TABS:
3721 lnum = CURRENT_TAB_NR;
3722 break;
3723 case ADDR_TABS_RELATIVE:
3724 case ADDR_UNSIGNED:
3725 lnum = 1;
3726 break;
3727 case ADDR_QUICKFIX:
3728#ifdef FEAT_QUICKFIX
3729 lnum = qf_get_cur_idx(eap);
3730#endif
3731 break;
3732 case ADDR_QUICKFIX_VALID:
3733#ifdef FEAT_QUICKFIX
3734 lnum = qf_get_cur_valid_idx(eap);
3735#endif
3736 break;
3737 case ADDR_NONE:
3738 // Will give an error later if a range is found.
3739 break;
3740 }
3741 return lnum;
3742}
3743
3744/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003745 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 *
3747 * Set ptr to the next character after the part that was interpreted.
3748 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003749 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 *
3751 * Return MAXLNUM when no Ex address was found.
3752 */
3753 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003754get_address(
3755 exarg_T *eap UNUSED,
3756 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003757 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003758 int skip, // only skip the address, don't use it
3759 int silent, // no errors or side effects
3760 int to_other_file, // flag: may jump to other file
3761 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762{
3763 int c;
3764 int i;
3765 long n;
3766 char_u *cmd;
3767 pos_T pos;
3768 pos_T *fp;
3769 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003770 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771
3772 cmd = skipwhite(*ptr);
3773 lnum = MAXLNUM;
3774 do
3775 {
3776 switch (*cmd)
3777 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003778 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003779 ++cmd;
3780 switch (addr_type)
3781 {
3782 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003783 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 lnum = curwin->w_cursor.lnum;
3785 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003786 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003787 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003788 break;
3789 case ADDR_ARGUMENTS:
3790 lnum = curwin->w_arg_idx + 1;
3791 break;
3792 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003793 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003794 lnum = curbuf->b_fnum;
3795 break;
3796 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003797 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003798 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003799 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003800 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003801 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003802 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003803 cmd = NULL;
3804 goto error;
3805 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003806 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003807#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003808 lnum = qf_get_cur_idx(eap);
3809#endif
3810 break;
3811 case ADDR_QUICKFIX_VALID:
3812#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003813 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003814#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003815 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003816 }
3817 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003819 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003820 ++cmd;
3821 switch (addr_type)
3822 {
3823 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003824 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 lnum = curbuf->b_ml.ml_line_count;
3826 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003827 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003828 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003829 break;
3830 case ADDR_ARGUMENTS:
3831 lnum = ARGCOUNT;
3832 break;
3833 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003834 buf = lastbuf;
3835 while (buf->b_ml.ml_mfp == NULL)
3836 {
3837 if (buf->b_prev == NULL)
3838 break;
3839 buf = buf->b_prev;
3840 }
3841 lnum = buf->b_fnum;
3842 break;
3843 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003844 lnum = lastbuf->b_fnum;
3845 break;
3846 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003847 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003848 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003849 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003850 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003851 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003852 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003853 cmd = NULL;
3854 goto error;
3855 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003856 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003857#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003858 lnum = qf_get_size(eap);
3859 if (lnum == 0)
3860 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003861#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003862 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003863 case ADDR_QUICKFIX_VALID:
3864#ifdef FEAT_QUICKFIX
3865 lnum = qf_get_valid_size(eap);
3866 if (lnum == 0)
3867 lnum = 1;
3868#endif
3869 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003870 }
3871 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003873 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003874 if (*++cmd == NUL)
3875 {
3876 cmd = NULL;
3877 goto error;
3878 }
3879 if (addr_type != ADDR_LINES)
3880 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003881 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003882 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003883 goto error;
3884 }
3885 if (skip)
3886 ++cmd;
3887 else
3888 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003889 // Only accept a mark in another file when it is
3890 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003891 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3892 ++cmd;
3893 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003894 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003895 lnum = curwin->w_cursor.lnum;
3896 else
3897 {
3898 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 {
3900 cmd = NULL;
3901 goto error;
3902 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003903 lnum = fp->lnum;
3904 }
3905 }
3906 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907
3908 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003909 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003910 c = *cmd++;
3911 if (addr_type != ADDR_LINES)
3912 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003913 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003914 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003915 goto error;
3916 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003917 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003918 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003919 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003920 if (*cmd == c)
3921 ++cmd;
3922 }
3923 else
3924 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003925 int flags;
3926
3927 pos = curwin->w_cursor; // save curwin->w_cursor
3928
3929 // When '/' or '?' follows another address, start from
3930 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003931 if (lnum != MAXLNUM)
3932 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003933
3934 // Start a forward search at the end of the line (unless
3935 // before the first line).
3936 // Start a backward search at the start of the line.
3937 // This makes sure we never match in the current
3938 // line, and can match anywhere in the
3939 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003940 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003941 curwin->w_cursor.col = MAXCOL;
3942 else
3943 curwin->w_cursor.col = 0;
3944 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003945 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003946 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003947 {
3948 curwin->w_cursor = pos;
3949 cmd = NULL;
3950 goto error;
3951 }
3952 lnum = curwin->w_cursor.lnum;
3953 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003954 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003955 cmd += searchcmdlen;
3956 }
3957 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003959 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003960 ++cmd;
3961 if (addr_type != ADDR_LINES)
3962 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003963 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003964 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003965 goto error;
3966 }
3967 if (*cmd == '&')
3968 i = RE_SUBST;
3969 else if (*cmd == '?' || *cmd == '/')
3970 i = RE_SEARCH;
3971 else
3972 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003973 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003974 cmd = NULL;
3975 goto error;
3976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003978 if (!skip)
3979 {
3980 /*
3981 * When search follows another address, start from
3982 * there.
3983 */
3984 if (lnum != MAXLNUM)
3985 pos.lnum = lnum;
3986 else
3987 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003989 /*
3990 * Start the search just like for the above
3991 * do_search().
3992 */
3993 if (*cmd != '?')
3994 pos.col = MAXCOL;
3995 else
3996 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003997 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003998 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003999 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004000 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004001 lnum = pos.lnum;
4002 else
4003 {
4004 cmd = NULL;
4005 goto error;
4006 }
4007 }
4008 ++cmd;
4009 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010
4011 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004012 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004013 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 }
4015
4016 for (;;)
4017 {
4018 cmd = skipwhite(cmd);
4019 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4020 break;
4021
4022 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004023 {
4024 switch (addr_type)
4025 {
4026 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004027 case ADDR_OTHER:
4028 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004029 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004030 break;
4031 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004032 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004033 break;
4034 case ADDR_ARGUMENTS:
4035 lnum = curwin->w_arg_idx + 1;
4036 break;
4037 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004038 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004039 lnum = curbuf->b_fnum;
4040 break;
4041 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004042 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004043 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004044 case ADDR_TABS_RELATIVE:
4045 lnum = 1;
4046 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004047 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004048#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004049 lnum = qf_get_cur_idx(eap);
4050#endif
4051 break;
4052 case ADDR_QUICKFIX_VALID:
4053#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004054 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004055#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004056 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004057 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004058 case ADDR_UNSIGNED:
4059 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004060 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004061 }
4062 }
4063
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004065 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 else
4067 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004068 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 n = 1;
4070 else
4071 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004072
4073 if (addr_type == ADDR_TABS_RELATIVE)
4074 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004075 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004076 cmd = NULL;
4077 goto error;
4078 }
4079 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004080 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004081 lnum = compute_buffer_local_count(
4082 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004084 {
4085#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004086 // Relative line addressing, need to adjust for folded lines
4087 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004088 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4089 && address_count >= 2)
4090 (void)hasFolding(lnum, NULL, &lnum);
4091#endif
4092 if (i == '-')
4093 lnum -= n;
4094 else
4095 lnum += n;
4096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 }
4098 } while (*cmd == '/' || *cmd == '?');
4099
4100error:
4101 *ptr = cmd;
4102 return lnum;
4103}
4104
4105/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004106 * Set eap->line1 and eap->line2 to the whole range.
4107 * Used for commands with the EX_DFLALL flag and no range given.
4108 */
4109 static void
4110address_default_all(exarg_T *eap)
4111{
4112 eap->line1 = 1;
4113 switch (eap->addr_type)
4114 {
4115 case ADDR_LINES:
4116 case ADDR_OTHER:
4117 eap->line2 = curbuf->b_ml.ml_line_count;
4118 break;
4119 case ADDR_LOADED_BUFFERS:
4120 {
4121 buf_T *buf = firstbuf;
4122
4123 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4124 buf = buf->b_next;
4125 eap->line1 = buf->b_fnum;
4126 buf = lastbuf;
4127 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4128 buf = buf->b_prev;
4129 eap->line2 = buf->b_fnum;
4130 }
4131 break;
4132 case ADDR_BUFFERS:
4133 eap->line1 = firstbuf->b_fnum;
4134 eap->line2 = lastbuf->b_fnum;
4135 break;
4136 case ADDR_WINDOWS:
4137 eap->line2 = LAST_WIN_NR;
4138 break;
4139 case ADDR_TABS:
4140 eap->line2 = LAST_TAB_NR;
4141 break;
4142 case ADDR_TABS_RELATIVE:
4143 eap->line2 = 1;
4144 break;
4145 case ADDR_ARGUMENTS:
4146 if (ARGCOUNT == 0)
4147 eap->line1 = eap->line2 = 0;
4148 else
4149 eap->line2 = ARGCOUNT;
4150 break;
4151 case ADDR_QUICKFIX_VALID:
4152#ifdef FEAT_QUICKFIX
4153 eap->line2 = qf_get_valid_size(eap);
4154 if (eap->line2 == 0)
4155 eap->line2 = 1;
4156#endif
4157 break;
4158 case ADDR_NONE:
4159 case ADDR_UNSIGNED:
4160 case ADDR_QUICKFIX:
4161 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4162 break;
4163 }
4164}
4165
4166
4167/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004168 * Get flags from an Ex command argument.
4169 */
4170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004171get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004172{
4173 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4174 {
4175 if (*eap->arg == 'l')
4176 eap->flags |= EXFLAG_LIST;
4177 else if (*eap->arg == 'p')
4178 eap->flags |= EXFLAG_PRINT;
4179 else
4180 eap->flags |= EXFLAG_NR;
4181 eap->arg = skipwhite(eap->arg + 1);
4182 }
4183}
4184
4185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 * Function called for command which is Not Implemented. NI!
4187 */
4188 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004189ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190{
4191 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004192 eap->errmsg =
4193 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194}
4195
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004196#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197/*
4198 * Function called for script command which is Not Implemented. NI!
4199 * Skips over ":perl <<EOF" constructs.
4200 */
4201 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004202ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203{
4204 if (!eap->skip)
4205 ex_ni(eap);
4206 else
4207 vim_free(script_get(eap, eap->arg));
4208}
4209#endif
4210
4211/*
4212 * Check range in Ex command for validity.
4213 * Return NULL when valid, error message when invalid.
4214 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004215 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004216invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004218 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004219
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 if ( eap->line1 < 0
4221 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004222 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004223 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004224
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004225 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004226 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004227 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004228 {
4229 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004230 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004231#ifdef FEAT_DIFF
4232 + (eap->cmdidx == CMD_diffget)
4233#endif
4234 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004235 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004236 break;
4237 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004238 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004239 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004240 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004241 break;
4242 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004243 // Only a boundary check, not whether the buffers actually
4244 // exist.
4245 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004246 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004247 break;
4248 case ADDR_LOADED_BUFFERS:
4249 buf = firstbuf;
4250 while (buf->b_ml.ml_mfp == NULL)
4251 {
4252 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004253 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004254 buf = buf->b_next;
4255 }
4256 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004257 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004258 buf = lastbuf;
4259 while (buf->b_ml.ml_mfp == NULL)
4260 {
4261 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004262 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004263 buf = buf->b_prev;
4264 }
4265 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004266 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004267 break;
4268 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004269 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004270 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004271 break;
4272 case ADDR_TABS:
4273 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004274 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004275 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004276 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004277 case ADDR_OTHER:
4278 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004279 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004280 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004281#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004282 // No error for value that is too big, will use the last entry.
4283 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004284 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004285#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004286 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004287 case ADDR_QUICKFIX_VALID:
4288#ifdef FEAT_QUICKFIX
4289 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4290 || eap->line2 < 0)
4291 return _(e_invrange);
4292#endif
4293 break;
4294 case ADDR_UNSIGNED:
4295 if (eap->line2 < 0)
4296 return _(e_invrange);
4297 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004298 case ADDR_NONE:
4299 // Will give an error elsewhere.
4300 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004301 }
4302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 return NULL;
4304}
4305
4306/*
4307 * Correct the range for zero line number, if required.
4308 */
4309 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004310correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004312 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 {
4314 if (eap->line1 == 0)
4315 eap->line1 = 1;
4316 if (eap->line2 == 0)
4317 eap->line2 = 1;
4318 }
4319}
4320
Bram Moolenaar748bf032005-02-02 23:04:36 +00004321#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004322/*
4323 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4324 * pattern. Otherwise return eap->arg.
4325 */
4326 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004327skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004328{
4329 char_u *p = eap->arg;
4330
Bram Moolenaara37420f2006-02-04 22:37:47 +00004331 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4332 || eap->cmdidx == CMD_vimgrepadd
4333 || eap->cmdidx == CMD_lvimgrepadd
4334 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004335 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004336 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004337 if (p == NULL)
4338 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004339 }
4340 return p;
4341}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004342
4343/*
4344 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4345 * in the command line, so that things like % get expanded.
4346 */
4347 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004348replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004349{
4350 char_u *new_cmdline;
4351 char_u *program;
4352 char_u *pos;
4353 char_u *ptr;
4354 int len;
4355 int i;
4356
4357 /*
4358 * Don't do it when ":vimgrep" is used for ":grep".
4359 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004360 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4361 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4362 || eap->cmdidx == CMD_grepadd
4363 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004364 && !grep_internal(eap->cmdidx))
4365 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004366 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4367 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004368 {
4369 if (*curbuf->b_p_gp == NUL)
4370 program = p_gp;
4371 else
4372 program = curbuf->b_p_gp;
4373 }
4374 else
4375 {
4376 if (*curbuf->b_p_mp == NUL)
4377 program = p_mp;
4378 else
4379 program = curbuf->b_p_mp;
4380 }
4381
4382 p = skipwhite(p);
4383
4384 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4385 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004386 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004387 i = 1;
4388 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4389 ++i;
4390 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004391 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004392 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004393 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004394 ptr = new_cmdline;
4395 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4396 {
4397 i = (int)(pos - program);
4398 STRNCPY(ptr, program, i);
4399 STRCPY(ptr += i, p);
4400 ptr += len;
4401 program = pos + 2;
4402 }
4403 STRCPY(ptr, program);
4404 }
4405 else
4406 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004407 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004408 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004409 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004410 STRCPY(new_cmdline, program);
4411 STRCAT(new_cmdline, " ");
4412 STRCAT(new_cmdline, p);
4413 }
4414 msg_make(p);
4415
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004416 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004417 vim_free(*cmdlinep);
4418 *cmdlinep = new_cmdline;
4419 p = new_cmdline;
4420 }
4421 return p;
4422}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004423#endif
4424
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425/*
4426 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004427 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 * Return FAIL for failure, OK otherwise.
4429 */
4430 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004431expand_filename(
4432 exarg_T *eap,
4433 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004434 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004436 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 char_u *repl;
4438 int srclen;
4439 char_u *p;
4440 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004441 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442
Bram Moolenaar748bf032005-02-02 23:04:36 +00004443#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004444 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004445 p = skip_grep_pat(eap);
4446#else
4447 p = eap->arg;
4448#endif
4449
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 /*
4451 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4452 * the file name contains a wildcard it should not cause expanding.
4453 * (it will be expanded anyway if there is a wildcard before replacing).
4454 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004455 has_wildcards = mch_has_wildcard(p);
4456 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004458#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004459 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004460 if (p[0] == '`' && p[1] == '=')
4461 {
4462 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004463 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004464 if (*p == '`')
4465 ++p;
4466 continue;
4467 }
4468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 /*
4470 * Quick check if this cannot be the start of a special string.
4471 * Also removes backslash before '%', '#' and '<'.
4472 */
4473 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4474 {
4475 ++p;
4476 continue;
4477 }
4478
4479 /*
4480 * Try to find a match at this position.
4481 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004482 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4483 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004484 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004486 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 {
4488 p += srclen;
4489 continue;
4490 }
4491
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004492 // Wildcards won't be expanded below, the replacement is taken
4493 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004494 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4495 {
4496 char_u *l = repl;
4497
4498 repl = expand_env_save(repl);
4499 vim_free(l);
4500 }
4501
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004502 // Need to escape white space et al. with a backslash.
4503 // Don't do this for:
4504 // - replacement that already has been escaped: "##"
4505 // - shell commands (may have to use quotes instead).
4506 // - non-unix systems when there is a single argument (spaces don't
4507 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004509 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 && eap->cmdidx != CMD_grep
4512 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004513 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004514 && eap->cmdidx != CMD_lgrep
4515 && eap->cmdidx != CMD_lgrepadd
4516 && eap->cmdidx != CMD_lmake
4517 && eap->cmdidx != CMD_make
4518 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004520 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521#endif
4522 )
4523 {
4524 char_u *l;
4525#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004526 // Don't escape a backslash here, because rem_backslash() doesn't
4527 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 static char_u *nobslash = (char_u *)" \t\"|";
4529# define ESCAPE_CHARS nobslash
4530#else
4531# define ESCAPE_CHARS escape_chars
4532#endif
4533
4534 for (l = repl; *l; ++l)
4535 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4536 {
4537 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4538 if (l != NULL)
4539 {
4540 vim_free(repl);
4541 repl = l;
4542 }
4543 break;
4544 }
4545 }
4546
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004547 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004548 if ((eap->usefilter || eap->cmdidx == CMD_bang
4549 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004550 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
4552 char_u *l;
4553
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004554 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 if (l != NULL)
4556 {
4557 vim_free(repl);
4558 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 }
4560 }
4561
4562 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4563 vim_free(repl);
4564 if (p == NULL)
4565 return FAIL;
4566 }
4567
4568 /*
4569 * One file argument: Expand wildcards.
4570 * Don't do this with ":r !command" or ":w !command".
4571 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004572 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 {
4574 /*
4575 * May do this twice:
4576 * 1. Replace environment variables.
4577 * 2. Replace any other wildcards, remove backslashes.
4578 */
4579 for (n = 1; n <= 2; ++n)
4580 {
4581 if (n == 2)
4582 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 /*
4584 * Halve the number of backslashes (this is Vi compatible).
4585 * For Unix and OS/2, when wildcards are expanded, this is
4586 * done by ExpandOne() below.
4587 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004588#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 if (!has_wildcards)
4590#endif
4591 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
4593
4594 if (has_wildcards)
4595 {
4596 if (n == 1)
4597 {
4598 /*
4599 * First loop: May expand environment variables. This
4600 * can be done much faster with expand_env() than with
4601 * something else (e.g., calling a shell).
4602 * After expanding environment variables, check again
4603 * if there are still wildcards present.
4604 */
4605 if (vim_strchr(eap->arg, '$') != NULL
4606 || vim_strchr(eap->arg, '~') != NULL)
4607 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004608 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004609 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 has_wildcards = mch_has_wildcard(NameBuff);
4611 p = NameBuff;
4612 }
4613 else
4614 p = NULL;
4615 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004616 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 {
4618 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004619 int options = WILD_LIST_NOTFOUND
4620 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621
4622 ExpandInit(&xpc);
4623 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004624 if (p_wic)
4625 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004627 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 if (p == NULL)
4629 return FAIL;
4630 }
4631 if (p != NULL)
4632 {
4633 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4634 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004635 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 vim_free(p);
4637 }
4638 }
4639 }
4640 }
4641 return OK;
4642}
4643
4644/*
4645 * Replace part of the command line, keeping eap->cmd, eap->arg and
4646 * eap->nextcmd correct.
4647 * "src" points to the part that is to be replaced, of length "srclen".
4648 * "repl" is the replacement string.
4649 * Returns a pointer to the character after the replaced string.
4650 * Returns NULL for failure.
4651 */
4652 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004653repl_cmdline(
4654 exarg_T *eap,
4655 char_u *src,
4656 int srclen,
4657 char_u *repl,
4658 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659{
4660 int len;
4661 int i;
4662 char_u *new_cmdline;
4663
4664 /*
4665 * The new command line is build in new_cmdline[].
4666 * First allocate it.
4667 * Careful: a "+cmd" argument may have been NUL terminated.
4668 */
4669 len = (int)STRLEN(repl);
4670 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004671 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004672 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004673 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004674 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675
4676 /*
4677 * Copy the stuff before the expanded part.
4678 * Copy the expanded stuff.
4679 * Copy what came after the expanded part.
4680 * Copy the next commands, if there are any.
4681 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004682 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004684
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004686 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004688 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004690 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 {
4692 i = (int)STRLEN(new_cmdline) + 1;
4693 STRCPY(new_cmdline + i, eap->nextcmd);
4694 eap->nextcmd = new_cmdline + i;
4695 }
4696 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4697 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4698 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4699 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4700 vim_free(*cmdlinep);
4701 *cmdlinep = new_cmdline;
4702
4703 return src;
4704}
4705
4706/*
4707 * Check for '|' to separate commands and '"' to start comments.
4708 */
4709 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004710separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711{
4712 char_u *p;
4713
Bram Moolenaar86b68352004-12-27 21:59:20 +00004714#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004715 p = skip_grep_pat(eap);
4716#else
4717 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004718#endif
4719
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004720 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 {
4722 if (*p == Ctrl_V)
4723 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004724 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004725 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004727 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004728 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004729 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 break;
4731 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004732
4733#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004734 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004735 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004736 {
4737 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004738 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01004739 if (*p == NUL) // stop at NUL after CTRL-V
4740 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004741 }
4742#endif
4743
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004744 // Check for '"': start of comment or '|': next command
4745 // :@" and :*" do not start a comment!
4746 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004747 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004748#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004749 && !in_vim9script()
4750#endif
4751 && !(eap->argt & EX_NOTRLCOM)
4752 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4753 || p != eap->arg)
4754 && (eap->cmdidx != CMD_redir
4755 || p != eap->arg + 1 || p[-1] != '@'))
4756#ifdef FEAT_EVAL
4757 || (*p == '#'
4758 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004759 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004760 && p[1] != '{'
4761 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 || *p == '|' || *p == '\n')
4764 {
4765 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004766 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 * AND 'b' is present in 'cpoptions'.
4768 */
4769 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004770 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004772 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 --p;
4774 }
4775 else
4776 {
4777 eap->nextcmd = check_nextcmd(p);
4778 *p = NUL;
4779 break;
4780 }
4781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004783
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004784 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 del_trailing_spaces(eap->arg);
4786}
4787
4788/*
4789 * get + command from ex argument
4790 */
4791 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004792getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793{
4794 char_u *arg = *argp;
4795 char_u *command = NULL;
4796
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004797 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 {
4799 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004800 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 command = dollar_command;
4802 else
4803 {
4804 command = arg;
4805 arg = skip_cmd_arg(command, TRUE);
4806 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004807 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 }
4809
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004810 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 *argp = arg;
4812 }
4813 return command;
4814}
4815
4816/*
4817 * Find end of "+command" argument. Skip over "\ " and "\\".
4818 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004819 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004820skip_cmd_arg(
4821 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004822 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823{
4824 while (*p && !vim_isspace(*p))
4825 {
4826 if (*p == '\\' && p[1] != NUL)
4827 {
4828 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004829 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 else
4831 ++p;
4832 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004833 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 }
4835 return p;
4836}
4837
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004838 int
4839get_bad_opt(char_u *p, exarg_T *eap)
4840{
4841 if (STRICMP(p, "keep") == 0)
4842 eap->bad_char = BAD_KEEP;
4843 else if (STRICMP(p, "drop") == 0)
4844 eap->bad_char = BAD_DROP;
4845 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4846 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004847 else
4848 return FAIL;
4849 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004850}
4851
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852/*
4853 * Get "++opt=arg" argument.
4854 * Return FAIL or OK.
4855 */
4856 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004857getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858{
4859 char_u *arg = eap->arg + 2;
4860 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004861 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004864 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4866 {
4867 if (*arg == 'n')
4868 {
4869 arg += 2;
4870 eap->force_bin = FORCE_NOBIN;
4871 }
4872 else
4873 eap->force_bin = FORCE_BIN;
4874 if (!checkforcmd(&arg, "binary", 3))
4875 return FAIL;
4876 eap->arg = skipwhite(arg);
4877 return OK;
4878 }
4879
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004880 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004881 if (STRNCMP(arg, "edit", 4) == 0)
4882 {
4883 eap->read_edit = TRUE;
4884 eap->arg = skipwhite(arg + 4);
4885 return OK;
4886 }
4887
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 if (STRNCMP(arg, "ff", 2) == 0)
4889 {
4890 arg += 2;
4891 pp = &eap->force_ff;
4892 }
4893 else if (STRNCMP(arg, "fileformat", 10) == 0)
4894 {
4895 arg += 10;
4896 pp = &eap->force_ff;
4897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 else if (STRNCMP(arg, "enc", 3) == 0)
4899 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004900 if (STRNCMP(arg, "encoding", 8) == 0)
4901 arg += 8;
4902 else
4903 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 pp = &eap->force_enc;
4905 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004906 else if (STRNCMP(arg, "bad", 3) == 0)
4907 {
4908 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004909 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911
4912 if (pp == NULL || *arg != '=')
4913 return FAIL;
4914
4915 ++arg;
4916 *pp = (int)(arg - eap->cmd);
4917 arg = skip_cmd_arg(arg, FALSE);
4918 eap->arg = skipwhite(arg);
4919 *arg = NUL;
4920
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 if (pp == &eap->force_ff)
4922 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4924 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004925 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004927 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004929 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4931 *p = TOLOWER_ASC(*p);
4932 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004933 else
4934 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004935 // Check ++bad= argument. Must be a single-byte character, "keep" or
4936 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004937 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004938 return FAIL;
4939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940
4941 return OK;
4942}
4943
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004945ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946{
4947 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004948 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 * directory for security reasons.
4950 */
4951 if (secure)
4952 {
4953 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004954 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 }
4956 else if (eap->cmdidx == CMD_autocmd)
4957 do_autocmd(eap->arg, eap->forceit);
4958 else
4959 do_augroup(eap->arg, eap->forceit);
4960}
4961
4962/*
4963 * ":doautocmd": Apply the automatic commands to the current buffer.
4964 */
4965 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004966ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004968 char_u *arg = eap->arg;
4969 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004970 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004971
Bram Moolenaar1610d052016-06-09 22:53:01 +02004972 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004973 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004974 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004975 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978/*
4979 * :[N]bunload[!] [N] [bufname] unload buffer
4980 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4981 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4982 */
4983 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004984ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004986 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004987 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 eap->errmsg = do_bufdel(
4989 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4990 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4991 : DOBUF_UNLOAD, eap->arg,
4992 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4993}
4994
4995/*
4996 * :[N]buffer [N] to buffer N
4997 * :[N]sbuffer [N] to buffer N
4998 */
4999 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005000ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005002 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005003 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005005 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 else
5007 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005008 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5010 else
5011 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005012 if (eap->do_ecmd_cmd != NULL)
5013 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 }
5015}
5016
5017/*
5018 * :[N]bmodified [N] to next mod. buffer
5019 * :[N]sbmodified [N] to next mod. buffer
5020 */
5021 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005022ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023{
5024 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005025 if (eap->do_ecmd_cmd != NULL)
5026 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027}
5028
5029/*
5030 * :[N]bnext [N] to next buffer
5031 * :[N]sbnext [N] split and to next buffer
5032 */
5033 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005034ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005036 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005037 return;
5038
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005040 if (eap->do_ecmd_cmd != NULL)
5041 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042}
5043
5044/*
5045 * :[N]bNext [N] to previous buffer
5046 * :[N]bprevious [N] to previous buffer
5047 * :[N]sbNext [N] split and to previous buffer
5048 * :[N]sbprevious [N] split and to previous buffer
5049 */
5050 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005051ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005053 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005054 return;
5055
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005057 if (eap->do_ecmd_cmd != NULL)
5058 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059}
5060
5061/*
5062 * :brewind to first buffer
5063 * :bfirst to first buffer
5064 * :sbrewind split and to first buffer
5065 * :sbfirst split and to first buffer
5066 */
5067 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005068ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005070 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005071 return;
5072
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005074 if (eap->do_ecmd_cmd != NULL)
5075 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076}
5077
5078/*
5079 * :blast to last buffer
5080 * :sblast split and to last buffer
5081 */
5082 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005083ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005085 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005086 return;
5087
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005089 if (eap->do_ecmd_cmd != NULL)
5090 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092
Bram Moolenaar7a092242020-04-16 22:10:49 +02005093/*
5094 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02005095 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005096 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005098ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005100 int comment_char = '"';
5101
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005102#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005103 if (in_vim9script())
5104 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005105#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005106 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005107}
5108
5109/*
5110 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5111 * to "cmd_start" or has a white space character before it.
5112 */
5113 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005114ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005115{
5116 int c = *cmd;
5117
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005118 if (c == NUL || c == '|' || c == '\n')
5119 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005120#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005121 if (in_vim9script())
5122 return c == '#' && cmd[1] != '{'
5123 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005124#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005125 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126}
5127
5128#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5129 || defined(PROTO)
5130/*
5131 * Return the next command, after the first '|' or '\n'.
5132 * Return NULL if not found.
5133 */
5134 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005135find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136{
5137 while (*p != '|' && *p != '\n')
5138 {
5139 if (*p == NUL)
5140 return NULL;
5141 ++p;
5142 }
5143 return (p + 1);
5144}
5145#endif
5146
5147/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005148 * Check if *p is a separator between Ex commands, skipping over white space.
5149 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 */
5151 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005152check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005154 char_u *s = skipwhite(p);
5155
5156 if (*s == '|' || *s == '\n')
5157 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 else
5159 return NULL;
5160}
5161
5162/*
5163 * - if there are more files to edit
5164 * - and this is the last window
5165 * - and forceit not used
5166 * - and not repeated twice on a row
5167 * return FAIL and give error message if 'message' TRUE
5168 * return OK otherwise
5169 */
5170 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005171check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005172 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005173 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174{
5175 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5176
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005177 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005178 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 {
5180 if (message)
5181 {
5182#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005183 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5184 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005186 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005188 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5189 NGETTEXT("%d more file to edit. Quit anyway?",
5190 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5192 return OK;
5193 return FAIL;
5194 }
5195#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005196 semsg(NGETTEXT("E173: %d more file to edit",
5197 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005198 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 }
5200 return FAIL;
5201 }
5202 return OK;
5203}
5204
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205/*
5206 * Function given to ExpandGeneric() to obtain the list of command names.
5207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005209get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210{
5211 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 return cmdnames[idx].cmd_name;
5214}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005217ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218{
Bram Moolenaare6850792010-05-14 15:28:44 +02005219 if (*eap->arg == NUL)
5220 {
5221#ifdef FEAT_EVAL
5222 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5223 char_u *p = NULL;
5224
5225 if (expr != NULL)
5226 {
5227 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005228 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005229 --emsg_off;
5230 vim_free(expr);
5231 }
5232 if (p != NULL)
5233 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005234 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005235 vim_free(p);
5236 }
5237 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005238 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005239#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005240 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005241#endif
5242 }
5243 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005244 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005245
5246#ifdef FEAT_VTP
5247 else if (has_vtp_working())
5248 {
5249 // background color change requires clear + redraw
5250 update_screen(CLEAR);
5251 redrawcmd();
5252 }
5253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254}
5255
5256 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005257ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258{
5259 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005260 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 do_highlight(eap->arg, eap->forceit, FALSE);
5262}
5263
5264
5265/*
5266 * Call this function if we thought we were going to exit, but we won't
5267 * (because of an error). May need to restore the terminal mode.
5268 */
5269 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005270not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271{
5272 exiting = FALSE;
5273 settmode(TMODE_RAW);
5274}
5275
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005276 static int
5277before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5278{
5279 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5280
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005281 // Bail out when autocommands closed the window.
5282 // Refuse to quit when the buffer in the last window is being closed (can
5283 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005284 if (!win_valid(wp)
5285 || curbuf_locked()
5286 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5287 return TRUE;
5288
5289 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5290 {
5291 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005292 // Refuse to quit when locked or when the window was closed or the
5293 // buffer in the last window is being closed (can only happen in
5294 // autocommands).
5295 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005296 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5297 return TRUE;
5298 }
5299
5300 return FALSE;
5301}
5302
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005304 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005305 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005306 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005308 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005309ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005311 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005312
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313#ifdef FEAT_CMDWIN
5314 if (cmdwin_type != 0)
5315 {
5316 cmdwin_result = Ctrl_C;
5317 return;
5318 }
5319#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005320 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005321 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005322 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005323 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005324 return;
5325 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005326 if (eap->addr_count > 0)
5327 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005328 int wnr = eap->line2;
5329
5330 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5331 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005332 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005333 }
5334 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005335 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005336
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005337 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005338 if (curbuf_locked())
5339 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005340
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005341 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005342 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005343 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344
5345#ifdef FEAT_NETBEANS_INTG
5346 netbeansForcedQuit = eap->forceit;
5347#endif
5348
5349 /*
5350 * If there are more files or windows we won't exit.
5351 */
5352 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5353 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005354 if ((!buf_hide(wp->w_buffer)
5355 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005356 | (eap->forceit ? CCGD_FORCEIT : 0)
5357 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005359 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 {
5361 not_exiting();
5362 }
5363 else
5364 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005365 // quit last window
5366 // Note: only_one_window() returns true, even so a help window is
5367 // still open. In that case only quit, if no address has been
5368 // specified. Example:
5369 // :h|wincmd w|1q - don't quit
5370 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005371 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005373 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005374#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005376#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005377 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005378 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 }
5380}
5381
5382/*
5383 * ":cquit".
5384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005386ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005388 // this does not always pass on the exit code to the Manx compiler. why?
5389 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390}
5391
5392/*
5393 * ":qall": try to quit all windows
5394 */
5395 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005396ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397{
5398# ifdef FEAT_CMDWIN
5399 if (cmdwin_type != 0)
5400 {
5401 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005402 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 else
5404 cmdwin_result = K_XF2;
5405 return;
5406 }
5407# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005408
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005409 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005410 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005411 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005412 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005413 return;
5414 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005415
5416 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005417 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005418
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005420 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 getout(0);
5422 not_exiting();
5423}
5424
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425/*
5426 * ":close": close current window, unless it is the last one
5427 */
5428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005429ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005431 win_T *win;
5432 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005433#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005435 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005437#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005438 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005439 {
5440 if (eap->addr_count == 0)
5441 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005442 else
5443 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005444 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005445 {
5446 winnr++;
5447 if (winnr == eap->line2)
5448 break;
5449 }
5450 if (win == NULL)
5451 win = lastwin;
5452 ex_win_close(eap->forceit, win, NULL);
5453 }
5454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455}
5456
Bram Moolenaar4033c552017-09-16 20:54:51 +02005457#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005458/*
5459 * ":pclose": Close any preview window.
5460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005462ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005463{
5464 win_T *win;
5465
Bram Moolenaar79648732019-07-18 21:43:07 +02005466 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005467 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005468 if (win->w_p_pvw)
5469 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005470 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005471 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005472 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005473# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005474 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005475 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005476# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005477}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005478#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005479
Bram Moolenaarf740b292006-02-16 22:11:02 +00005480/*
5481 * Close window "win" and take care of handling closing the last window for a
5482 * modified buffer.
5483 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005484 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005485ex_win_close(
5486 int forceit,
5487 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005488 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489{
5490 int need_hide;
5491 buf_T *buf = win->w_buffer;
5492
Bram Moolenaarcf844172020-06-26 19:44:06 +02005493 // Never close the autocommand window.
5494 if (win == aucmd_win)
5495 {
5496 emsg(_(e_autocmd_close));
5497 return;
5498 }
5499
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005501 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005503#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005504 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005506 bufref_T bufref;
5507
5508 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005510 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 return;
5512 need_hide = FALSE;
5513 }
5514 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005517 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 return;
5519 }
5520 }
5521
Bram Moolenaar4033c552017-09-16 20:54:51 +02005522#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005524#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005525
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005526 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005527 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005528 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005529 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005530 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531}
5532
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005534 * Handle the argument for a tabpage related ex command.
5535 * Returns a tabpage number.
5536 * When an error is encountered then eap->errmsg is set.
5537 */
5538 static int
5539get_tabpage_arg(exarg_T *eap)
5540{
5541 int tab_number;
5542 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5543
5544 if (eap->arg && *eap->arg != NUL)
5545 {
5546 char_u *p = eap->arg;
5547 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005548 int relative = 0; // argument +N/-N means: go to N places to the
5549 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005550
5551 if (*p == '-')
5552 {
5553 relative = -1;
5554 p++;
5555 }
5556 else if (*p == '+')
5557 {
5558 relative = 1;
5559 p++;
5560 }
5561
5562 p_save = p;
5563 tab_number = getdigits(&p);
5564
5565 if (relative == 0)
5566 {
5567 if (STRCMP(p, "$") == 0)
5568 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005569 else if (STRCMP(p, "#") == 0)
5570 if (valid_tabpage(lastused_tabpage))
5571 tab_number = tabpage_index(lastused_tabpage);
5572 else
5573 {
5574 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5575 tab_number = 0;
5576 goto theend;
5577 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005578 else if (p == p_save || *p_save == '-' || *p != NUL
5579 || tab_number > LAST_TAB_NR)
5580 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005581 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005582 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005583 goto theend;
5584 }
5585 }
5586 else
5587 {
5588 if (*p_save == NUL)
5589 tab_number = 1;
5590 else if (p == p_save || *p_save == '-' || *p != NUL
5591 || tab_number == 0)
5592 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005593 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005594 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005595 goto theend;
5596 }
5597 tab_number = tab_number * relative + tabpage_index(curtab);
5598 if (!unaccept_arg0 && relative == -1)
5599 --tab_number;
5600 }
5601 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005602 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005603 }
5604 else if (eap->addr_count > 0)
5605 {
5606 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005607 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005608 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005609 tab_number = 0;
5610 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005611 else
5612 {
5613 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005614 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005615 {
5616 --tab_number;
5617 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005618 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005619 }
5620 }
5621 }
5622 else
5623 {
5624 switch (eap->cmdidx)
5625 {
5626 case CMD_tabnext:
5627 tab_number = tabpage_index(curtab) + 1;
5628 if (tab_number > LAST_TAB_NR)
5629 tab_number = 1;
5630 break;
5631 case CMD_tabmove:
5632 tab_number = LAST_TAB_NR;
5633 break;
5634 default:
5635 tab_number = tabpage_index(curtab);
5636 }
5637 }
5638
5639theend:
5640 return tab_number;
5641}
5642
5643/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005644 * ":tabclose": close current tab page, unless it is the last one.
5645 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 */
5647 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005648ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005650 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005651 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005652
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005653# ifdef FEAT_CMDWIN
5654 if (cmdwin_type != 0)
5655 cmdwin_result = K_IGNORE;
5656 else
5657# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005658 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005659 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005660 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005662 tab_number = get_tabpage_arg(eap);
5663 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005664 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005665 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005666 if (tp == NULL)
5667 {
5668 beep_flush();
5669 return;
5670 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005671 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005672 {
5673 tabpage_close_other(tp, eap->forceit);
5674 return;
5675 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005676 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005677 tabpage_close(eap->forceit);
5678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005680}
5681
5682/*
5683 * ":tabonly": close all tab pages except the current one
5684 */
5685 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005686ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005687{
5688 tabpage_T *tp;
5689 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005690 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005691
5692# ifdef FEAT_CMDWIN
5693 if (cmdwin_type != 0)
5694 cmdwin_result = K_IGNORE;
5695 else
5696# endif
5697 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005698 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005699 else
5700 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005701 tab_number = get_tabpage_arg(eap);
5702 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005703 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005704 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005705 // Repeat this up to a 1000 times, because autocommands may
5706 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005707 for (done = 0; done < 1000; ++done)
5708 {
5709 FOR_ALL_TABPAGES(tp)
5710 if (tp->tp_topframe != topframe)
5711 {
5712 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005713 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005714 if (valid_tabpage(tp))
5715 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005716 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005717 break;
5718 }
5719 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005720 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005721 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005722 }
5723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725
5726/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005727 * Close the current tab page.
5728 */
5729 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005730tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005731{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005732 // First close all the windows but the current one. If that worked then
5733 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005734 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005735 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005736 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005737 ex_win_close(forceit, curwin, NULL);
5738# ifdef FEAT_GUI
5739 need_mouse_correct = TRUE;
5740# endif
5741}
5742
5743/*
5744 * Close tab page "tp", which is not the current tab page.
5745 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005746 * Also takes care of the tab pages line disappearing when closing the
5747 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005748 */
5749 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005750tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005751{
5752 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005753 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005754 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005755
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005756 // Limit to 1000 windows, autocommands may add a window while we close
5757 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005758 while (++done < 1000)
5759 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005760 wp = tp->tp_firstwin;
5761 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005762
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005763 // Autocommands may delete the tab page under our fingers and we may
5764 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005765 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005766 break;
5767 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005768
Bram Moolenaar29323592016-07-24 22:04:11 +02005769 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005770
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005771 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005772 if (h != tabline_height())
5773 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005774}
5775
5776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 * ":only".
5778 */
5779 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005780ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005782 win_T *wp;
5783 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784# ifdef FEAT_GUI
5785 need_mouse_correct = TRUE;
5786# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005787 if (eap->addr_count > 0)
5788 {
5789 wnr = eap->line2;
5790 for (wp = firstwin; --wnr > 0; )
5791 {
5792 if (wp->w_next == NULL)
5793 break;
5794 else
5795 wp = wp->w_next;
5796 }
5797 win_goto(wp);
5798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 close_others(TRUE, eap->forceit);
5800}
5801
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005803ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005805 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005806 if (!eap->skip)
5807 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005808#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005809 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005810#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005811 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005812 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005813 else
5814 {
5815 int winnr = 0;
5816 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005817
Bram Moolenaar2256c992016-11-15 21:17:07 +01005818 FOR_ALL_WINDOWS(win)
5819 {
5820 winnr++;
5821 if (winnr == eap->line2)
5822 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005823 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005824 if (win == NULL)
5825 win = lastwin;
5826 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 }
5829}
5830
5831/*
5832 * ":stop" and ":suspend": Suspend Vim.
5833 */
5834 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005835ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836{
5837 /*
5838 * Disallow suspending for "rvim".
5839 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005840 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 {
5842 if (!eap->forceit)
5843 autowrite_all();
5844 windgoto((int)Rows - 1, 0);
5845 out_char('\n');
5846 out_flush();
5847 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005848 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005850 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005852 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853#ifdef FEAT_TITLE
5854 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005855 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856#endif
5857 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005858 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005860 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 }
5862}
5863
5864/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005865 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 */
5867 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005868ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869{
Bram Moolenaar461f2122020-07-28 20:25:47 +02005870#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02005871 if (not_in_vim9(eap) == FAIL)
5872 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02005873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874#ifdef FEAT_CMDWIN
5875 if (cmdwin_type != 0)
5876 {
5877 cmdwin_result = Ctrl_C;
5878 return;
5879 }
5880#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005881 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005882 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005883 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005884 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005885 return;
5886 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005887
5888 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005889 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890
5891 /*
5892 * if more files or windows we won't exit
5893 */
5894 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5895 exiting = TRUE;
5896 if ( ((eap->cmdidx == CMD_wq
5897 || curbufIsChanged())
5898 && do_write(eap) == FAIL)
5899 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005900 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 {
5902 not_exiting();
5903 }
5904 else
5905 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005906 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005908 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909# ifdef FEAT_GUI
5910 need_mouse_correct = TRUE;
5911# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005912 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005913 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 }
5915}
5916
5917/*
5918 * ":print", ":list", ":number".
5919 */
5920 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005921ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005923 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005924 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005925 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005927 for ( ;!got_int; ui_breakcheck())
5928 {
5929 print_line(eap->line1,
5930 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5931 || (eap->flags & EXFLAG_NR)),
5932 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5933 if (++eap->line1 > eap->line2)
5934 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005935 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005936 }
5937 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005938 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005939 curwin->w_cursor.lnum = eap->line2;
5940 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 }
5942
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944}
5945
5946#ifdef FEAT_BYTEOFF
5947 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005948ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949{
5950 goto_byte(eap->line2);
5951}
5952#endif
5953
5954/*
5955 * ":shell".
5956 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005958ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959{
5960 do_shell(NULL, 0);
5961}
5962
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005963#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005965static int drop_busy = FALSE;
5966static int drop_filec;
5967static char_u **drop_filev = NULL;
5968static int drop_split;
5969static void (*drop_callback)(void *);
5970static void *drop_cookie;
5971
5972 static void
5973handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974{
5975 exarg_T ea;
5976 int save_msg_scroll = msg_scroll;
5977
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005978 // Setting the argument list may cause screen updates and being called
5979 // recursively. Avoid that by setting drop_busy.
5980 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005982 // Check whether the current buffer is changed. If so, we will need
5983 // to split the current window or data could be lost.
5984 // We don't need to check if the 'hidden' option is set, as in this
5985 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005986 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 {
5988 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005989 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 --emsg_off;
5991 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005992 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 if (win_split(0, 0) == FAIL)
5995 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005996 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005998 // When splitting the window, create a new alist. Otherwise the
5999 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 alist_unlink(curwin->w_alist);
6001 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 }
6003
6004 /*
6005 * Set up the new argument list.
6006 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006007 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008
6009 /*
6010 * Move to the first file.
6011 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006012 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006013 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 ea.cmd = (char_u *)"next";
6015 do_argfile(&ea, 0);
6016
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006017 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6018 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 need_start_insertmode = FALSE;
6020
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006021 // Restore msg_scroll, otherwise a following command may cause scrolling
6022 // unexpectedly. The screen will be redrawn by the caller, thus
6023 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006025
6026 if (drop_callback != NULL)
6027 drop_callback(drop_cookie);
6028
6029 drop_filev = NULL;
6030 drop_busy = FALSE;
6031}
6032
6033/*
6034 * Handle a file drop. The code is here because a drop is *nearly* like an
6035 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006036 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006037 * code is very similar to :args and hence needs access to a lot of the static
6038 * functions in this file.
6039 *
6040 * The "filev" list must have been allocated using alloc(), as should each item
6041 * in the list. This function takes over responsibility for freeing the "filev"
6042 * list.
6043 */
6044 void
6045handle_drop(
6046 int filec, // the number of files dropped
6047 char_u **filev, // the list of files dropped
6048 int split, // force splitting the window
6049 void (*callback)(void *), // to be called after setting the argument
6050 // list
6051 void *cookie) // argument for "callback" (allocated)
6052{
6053 // Cannot handle recursive drops, finish the pending one.
6054 if (drop_busy)
6055 {
6056 FreeWild(filec, filev);
6057 vim_free(cookie);
6058 return;
6059 }
6060
6061 // When calling handle_drop() more than once in a row we only use the last
6062 // one.
6063 if (drop_filev != NULL)
6064 {
6065 FreeWild(drop_filec, drop_filev);
6066 vim_free(drop_cookie);
6067 }
6068
6069 drop_filec = filec;
6070 drop_filev = filev;
6071 drop_split = split;
6072 drop_callback = callback;
6073 drop_cookie = cookie;
6074
6075 // Postpone this when:
6076 // - editing the command line
6077 // - not possible to change the current buffer
6078 // - updating the screen
6079 // As it may change buffers and window structures that are in use and cause
6080 // freed memory to be used.
6081 if (text_locked() || curbuf_locked() || updating_screen)
6082 return;
6083
6084 handle_drop_internal();
6085}
6086
6087/*
6088 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6089 * reset: Handle a postponed drop.
6090 */
6091 void
6092handle_any_postponed_drop(void)
6093{
6094 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006095 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006096 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097}
6098#endif
6099
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 * ":preserve".
6102 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006104ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006106 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 ml_preserve(curbuf, TRUE);
6108}
6109
6110/*
6111 * ":recover".
6112 */
6113 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006114ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006116 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006118 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6119 | CCGD_MULTWIN
6120 | (eap->forceit ? CCGD_FORCEIT : 0)
6121 | CCGD_EXCMD)
6122
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 && (*eap->arg == NUL
6124 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006125 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 recoverymode = FALSE;
6127}
6128
6129/*
6130 * Command modifier used in a wrong way.
6131 */
6132 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006133ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006135 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136}
6137
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138/*
6139 * :sview [+command] file split window with new file, read-only
6140 * :split [[+command] file] split window with current or new file
6141 * :vsplit [[+command] file] split window vertically with current or new file
6142 * :new [[+command] file] split window with no or new file
6143 * :vnew [[+command] file] split vertically window with no or new file
6144 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006145 *
6146 * :tabedit open new Tab page with empty window
6147 * :tabedit [+command] file open new Tab page and edit "file"
6148 * :tabnew [[+command] file] just like :tabedit
6149 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 */
6151 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006152ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006154 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006155#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006157#endif
6158#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006159 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006160 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006161#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006162 int use_tab = eap->cmdidx == CMD_tabedit
6163 || eap->cmdidx == CMD_tabfind
6164 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006166 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006167 return;
6168
Bram Moolenaar4033c552017-09-16 20:54:51 +02006169#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172
Bram Moolenaar4033c552017-09-16 20:54:51 +02006173#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006174 // A ":split" in the quickfix window works like ":new". Don't want two
6175 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006176 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 {
6178 if (eap->cmdidx == CMD_split)
6179 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 if (eap->cmdidx == CMD_vsplit)
6181 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184
Bram Moolenaar4033c552017-09-16 20:54:51 +02006185#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006186 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 {
6188 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6189 FNAME_MESS, TRUE, curbuf->b_ffname);
6190 if (fname == NULL)
6191 goto theend;
6192 eap->arg = fname;
6193 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006194# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006195 else
6196# endif
6197#endif
6198#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006199 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 && eap->cmdidx != CMD_new)
6202 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006203 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006204# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006205 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006206# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006207 au_has_group((char_u *)"FileExplorer"))
6208 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006209 // No browsing supported but we do have the file explorer:
6210 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006211 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006212 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006213 }
6214 else
6215 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006216 fname = do_browse(0, (char_u *)(use_tab
6217 ? _("Edit File in new tab page")
6218 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006220 if (fname == NULL)
6221 goto theend;
6222 eap->arg = fname;
6223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006225 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006228 /*
6229 * Either open new tab page or split the window.
6230 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006231 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006232 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006233 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006234 : eap->addr_count == 0 ? 0
6235 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006236 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006237 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006238
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006239 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006240 if (curwin != old_curwin
6241 && win_valid(old_curwin)
6242 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006243 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006244 old_curwin->w_alt_fnum = curbuf->b_fnum;
6245 }
6246 }
6247 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6249 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006250 // Reset 'scrollbind' when editing another file, but keep it when
6251 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006252 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006253 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 else
6255 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 do_exedit(eap, old_curwin);
6257 }
6258
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006259# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006260 cmdmod.cmod_flags = save_cmod_flags;
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# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264theend:
6265 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006266# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006268
6269/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006270 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006271 */
6272 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006273tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006274{
6275 exarg_T ea;
6276
Bram Moolenaara80faa82020-04-12 19:37:17 +02006277 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006278 ea.cmdidx = CMD_tabnew;
6279 ea.cmd = (char_u *)"tabn";
6280 ea.arg = (char_u *)"";
6281 ex_splitview(&ea);
6282}
6283
6284/*
6285 * :tabnext command
6286 */
6287 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006288ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006289{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006290 int tab_number;
6291
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006292 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006293 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006294 switch (eap->cmdidx)
6295 {
6296 case CMD_tabfirst:
6297 case CMD_tabrewind:
6298 goto_tabpage(1);
6299 break;
6300 case CMD_tablast:
6301 goto_tabpage(9999);
6302 break;
6303 case CMD_tabprevious:
6304 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006305 if (eap->arg && *eap->arg != NUL)
6306 {
6307 char_u *p = eap->arg;
6308 char_u *p_save = p;
6309
6310 tab_number = getdigits(&p);
6311 if (p == p_save || *p_save == '-' || *p != NUL
6312 || tab_number == 0)
6313 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006314 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006315 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006316 return;
6317 }
6318 }
6319 else
6320 {
6321 if (eap->addr_count == 0)
6322 tab_number = 1;
6323 else
6324 {
6325 tab_number = eap->line2;
6326 if (tab_number < 1)
6327 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006328 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006329 return;
6330 }
6331 }
6332 }
6333 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006334 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006335 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006336 tab_number = get_tabpage_arg(eap);
6337 if (eap->errmsg == NULL)
6338 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006339 break;
6340 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006341}
6342
6343/*
6344 * :tabmove command
6345 */
6346 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006347ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006348{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006349 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006350
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006351 tab_number = get_tabpage_arg(eap);
6352 if (eap->errmsg == NULL)
6353 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006354}
6355
6356/*
6357 * :tabs command: List tabs and their contents.
6358 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006359 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006360ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006361{
6362 tabpage_T *tp;
6363 win_T *wp;
6364 int tabcount = 1;
6365
6366 msg_start();
6367 msg_scroll = TRUE;
6368 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6369 {
6370 msg_putchar('\n');
6371 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006372 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006373 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006374 ui_breakcheck();
6375
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006376 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006377 wp = firstwin;
6378 else
6379 wp = tp->tp_firstwin;
6380 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6381 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006382 msg_putchar('\n');
6383 msg_putchar(wp == curwin ? '>' : ' ');
6384 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006385 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6386 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006387 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006388 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006389 else
6390 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6391 IObuff, IOSIZE, TRUE);
6392 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006393 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006394 ui_breakcheck();
6395 }
6396 }
6397}
6398
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399/*
6400 * ":mode": Set screen mode.
6401 * If no argument given, just get the screen size and redraw.
6402 */
6403 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006404ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405{
6406 if (*eap->arg == NUL)
6407 shell_resized();
6408 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006409 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410}
6411
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412/*
6413 * ":resize".
6414 * set, increment or decrement current window height
6415 */
6416 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006417ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418{
6419 int n;
6420 win_T *wp = curwin;
6421
6422 if (eap->addr_count > 0)
6423 {
6424 n = eap->line2;
6425 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6426 ;
6427 }
6428
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006429# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006431# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006433 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 {
6435 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006436 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006437 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 n = 9999;
6439 win_setwidth_win((int)n, wp);
6440 }
6441 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 {
6443 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006444 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006445 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 n = 9999;
6447 win_setheight_win((int)n, wp);
6448 }
6449}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450
6451/*
6452 * ":find [+command] <file>" command.
6453 */
6454 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006455ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456{
6457#ifdef FEAT_SEARCHPATH
6458 char_u *fname;
6459 int count;
6460
6461 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6462 TRUE, curbuf->b_ffname);
6463 if (eap->addr_count > 0)
6464 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006465 // Repeat finding the file "count" times. This matters when it
6466 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 count = eap->line2;
6468 while (fname != NULL && --count > 0)
6469 {
6470 vim_free(fname);
6471 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6472 FALSE, curbuf->b_ffname);
6473 }
6474 }
6475
6476 if (fname != NULL)
6477 {
6478 eap->arg = fname;
6479#endif
6480 do_exedit(eap, NULL);
6481#ifdef FEAT_SEARCHPATH
6482 vim_free(fname);
6483 }
6484#endif
6485}
6486
6487/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006488 * ":open" simulation: for now just work like ":visual".
6489 */
6490 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006491ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006492{
6493 regmatch_T regmatch;
6494 char_u *p;
6495
6496 curwin->w_cursor.lnum = eap->line2;
6497 beginline(BL_SOL | BL_FIX);
6498 if (*eap->arg == '/')
6499 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006500 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006501 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006502 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006503 *p = NUL;
6504 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6505 if (regmatch.regprog != NULL)
6506 {
6507 regmatch.rm_ic = p_ic;
6508 p = ml_get_curline();
6509 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006510 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006511 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006512 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006513 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006514 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006515 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006516 eap->arg += STRLEN(eap->arg);
6517 }
6518 check_cursor();
6519
6520 eap->cmdidx = CMD_visual;
6521 do_exedit(eap, NULL);
6522}
6523
6524/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006525 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 */
6527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006528ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529{
6530 do_exedit(eap, NULL);
6531}
6532
6533/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006534 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006537do_exedit(
6538 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006539 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540{
6541 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006543 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006545 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6546 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006547 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 /*
6549 * ":vi" command ends Ex mode.
6550 */
6551 if (exmode_active && (eap->cmdidx == CMD_visual
6552 || eap->cmdidx == CMD_view))
6553 {
6554 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006555 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006557 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006558 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006559 if (global_busy)
6560 {
6561 int rd = RedrawingDisabled;
6562 int nwr = no_wait_return;
6563 int ms = msg_scroll;
6564#ifdef FEAT_GUI
6565 int he = hold_gui_events;
6566#endif
6567
6568 if (eap->nextcmd != NULL)
6569 {
6570 stuffReadbuff(eap->nextcmd);
6571 eap->nextcmd = NULL;
6572 }
6573
6574 if (exmode_was != EXMODE_VIM)
6575 settmode(TMODE_RAW);
6576 RedrawingDisabled = 0;
6577 no_wait_return = 0;
6578 need_wait_return = FALSE;
6579 msg_scroll = 0;
6580#ifdef FEAT_GUI
6581 hold_gui_events = 0;
6582#endif
6583 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006584 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006585
6586 main_loop(FALSE, TRUE);
6587
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006588 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006589 RedrawingDisabled = rd;
6590 no_wait_return = nwr;
6591 msg_scroll = ms;
6592#ifdef FEAT_GUI
6593 hold_gui_events = he;
6594#endif
6595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 }
6599
6600 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006601 || eap->cmdidx == CMD_tabnew
6602 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006603 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006605 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 setpcmark();
6607 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006608 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6609 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006611 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 || *eap->arg != NUL
6613#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006614 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615#endif
6616 )
6617 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006618 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6619 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006620 if (*eap->arg != NUL && curbuf_locked())
6621 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006622
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 n = readonlymode;
6624 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6625 readonlymode = TRUE;
6626 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006627 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6628 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 setpcmark();
6630 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6631 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006632 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6634 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6635 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006636 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006638 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006639 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006640 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
6641 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006642 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006644 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 if (old_curwin != NULL)
6646 {
6647 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006648 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006650#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006651 cleanup_T cs;
6652
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006653 // Reset the error/interrupt/exception state here so that
6654 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006655 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006656#endif
6657#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006659#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006660 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006661
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006662#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006663 // Restore the error/interrupt/exception state if not
6664 // discarded by a new aborting error, interrupt, or
6665 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006666 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 }
6669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 }
6671 else if (readonlymode && curbuf->b_nwindows == 1)
6672 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006673 // When editing an already visited buffer, 'readonly' won't be set
6674 // but the previous value is kept. With ":view" and ":sview" we
6675 // want the file to be readonly, except when another window is
6676 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 curbuf->b_p_ro = TRUE;
6678 }
6679 readonlymode = n;
6680 }
6681 else
6682 {
6683 if (eap->do_ecmd_cmd != NULL)
6684 do_cmdline_cmd(eap->do_ecmd_cmd);
6685#ifdef FEAT_TITLE
6686 n = curwin->w_arg_idx_invalid;
6687#endif
6688 check_arg_idx(curwin);
6689#ifdef FEAT_TITLE
6690 if (n != curwin->w_arg_idx_invalid)
6691 maketitle();
6692#endif
6693 }
6694
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 /*
6696 * if ":split file" worked, set alternate file name in old window to new
6697 * file
6698 */
6699 if (old_curwin != NULL
6700 && *eap->arg != NUL
6701 && curwin != old_curwin
6702 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006703 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006704 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706
6707 ex_no_reprint = TRUE;
6708}
6709
6710#ifndef FEAT_GUI
6711/*
6712 * ":gui" and ":gvim" when there is no GUI.
6713 */
6714 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006715ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006717 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718}
6719#endif
6720
Bram Moolenaar4f974752019-02-17 17:44:42 +01006721#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006723ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724{
6725 gui_make_tearoff(eap->arg);
6726}
6727#endif
6728
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006729#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6730 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006732ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006734# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6735 if (gui.in_use)
6736 gui_make_popup(eap->arg, eap->forceit);
6737# ifdef FEAT_TERM_POPUP_MENU
6738 else
6739# endif
6740# endif
6741# ifdef FEAT_TERM_POPUP_MENU
6742 pum_make_popup(eap->arg, eap->forceit);
6743# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744}
6745#endif
6746
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006748ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749{
6750 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006751 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006753 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754}
6755
6756/*
6757 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6758 * offset.
6759 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006762ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006765 win_T *save_curwin = curwin;
6766 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 long topline;
6768 long y;
6769 linenr_T old_linenr = curwin->w_cursor.lnum;
6770
6771 setpcmark();
6772
6773 /*
6774 * determine max topline
6775 */
6776 if (curwin->w_p_scb)
6777 {
6778 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006779 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 {
6781 if (wp->w_p_scb && wp->w_buffer)
6782 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006783 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 if (topline > y)
6785 topline = y;
6786 }
6787 }
6788 if (topline < 1)
6789 topline = 1;
6790 }
6791 else
6792 {
6793 topline = 1;
6794 }
6795
6796
6797 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006798 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006800 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 {
6802 if (curwin->w_p_scb)
6803 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006804 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 y = topline - curwin->w_topline;
6806 if (y > 0)
6807 scrollup(y, TRUE);
6808 else
6809 scrolldown(-y, TRUE);
6810 curwin->w_scbind_pos = topline;
6811 redraw_later(VALID);
6812 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 }
6815 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006816 curwin = save_curwin;
6817 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 if (curwin->w_p_scb)
6819 {
6820 did_syncbind = TRUE;
6821 checkpcmark();
6822 if (old_linenr != curwin->w_cursor.lnum)
6823 {
6824 char_u ctrl_o[2];
6825
6826 ctrl_o[0] = Ctrl_O;
6827 ctrl_o[1] = 0;
6828 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6829 }
6830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831}
6832
6833
6834 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006835ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006837 int i;
6838 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6839 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006841 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 do_bang(1, eap, FALSE, FALSE, TRUE);
6843 else
6844 {
6845 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6846 return;
6847
6848#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006849 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 {
6851 char_u *browseFile;
6852
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006853 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 NULL, NULL, NULL, curbuf);
6855 if (browseFile != NULL)
6856 {
6857 i = readfile(browseFile, NULL,
6858 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6859 vim_free(browseFile);
6860 }
6861 else
6862 i = OK;
6863 }
6864 else
6865#endif
6866 if (*eap->arg == NUL)
6867 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006868 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 return;
6870 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6871 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6872 }
6873 else
6874 {
6875 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6876 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6877 i = readfile(eap->arg, NULL,
6878 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6879
6880 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006881 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006883#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 if (!aborting())
6885#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006886 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 }
6888 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006889 {
6890 if (empty && exmode_active)
6891 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006892 // Delete the empty line that remains. Historically ex does
6893 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006894 if (eap->line2 == 0)
6895 lnum = curbuf->b_ml.ml_line_count;
6896 else
6897 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006898 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006899 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006900 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006901 if (curwin->w_cursor.lnum > 1
6902 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006903 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006904 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006905 }
6906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 }
6910}
6911
Bram Moolenaarea408852005-06-25 22:49:46 +00006912static char_u *prev_dir = NULL;
6913
6914#if defined(EXITFREE) || defined(PROTO)
6915 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006916free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006917{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006918 VIM_CLEAR(prev_dir);
6919 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006920}
6921#endif
6922
Bram Moolenaarf4258302013-06-02 18:20:17 +02006923/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006924 * Get the previous directory for the given chdir scope.
6925 */
6926 static char_u *
6927get_prevdir(cdscope_T scope)
6928{
6929 if (scope == CDSCOPE_WINDOW)
6930 return curwin->w_prevdir;
6931 else if (scope == CDSCOPE_TABPAGE)
6932 return curtab->tp_prevdir;
6933 return prev_dir;
6934}
6935
6936/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006937 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006938 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6939 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006940 */
6941 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006942post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006943{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006944 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006945 // Clear tab local directory for both :cd and :tcd
6946 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006947 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006948 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006949 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006950 char_u *pdir = get_prevdir(scope);
6951
6952 // If still in the global directory, need to remember current
6953 // directory as the global directory.
6954 if (globaldir == NULL && pdir != NULL)
6955 globaldir = vim_strsave(pdir);
6956
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006957 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006958 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006959 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006960 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006961 curtab->tp_localdir = vim_strsave(NameBuff);
6962 else
6963 curwin->w_localdir = vim_strsave(NameBuff);
6964 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006965 }
6966 else
6967 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006968 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006969 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006970 }
6971
6972 shorten_fnames(TRUE);
6973}
6974
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006975/*
6976 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006977 * chdir() function.
6978 * scope == CDSCOPE_WINDOW: changes the window-local directory
6979 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6980 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006981 * Returns TRUE if the directory is successfully changed.
6982 */
6983 int
6984changedir_func(
6985 char_u *new_dir,
6986 int forceit,
6987 cdscope_T scope)
6988{
6989 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006990 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006991 int dir_differs;
6992 int retval = FALSE;
6993
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006994 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006995 return FALSE;
6996
6997 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6998 {
6999 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7000 return FALSE;
7001 }
7002
7003 // ":cd -": Change to previous directory
7004 if (STRCMP(new_dir, "-") == 0)
7005 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007006 pdir = get_prevdir(scope);
7007 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007008 {
7009 emsg(_("E186: No previous directory"));
7010 return FALSE;
7011 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007012 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007013 }
7014
Bram Moolenaar002bc792020-06-05 22:33:42 +02007015 // Free the previous directory
7016 tofree = get_prevdir(scope);
7017
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007018 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007019 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007020 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007021 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007022 pdir = NULL;
7023 if (scope == CDSCOPE_WINDOW)
7024 curwin->w_prevdir = pdir;
7025 else if (scope == CDSCOPE_TABPAGE)
7026 curtab->tp_prevdir = pdir;
7027 else
7028 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007029
7030#if defined(UNIX) || defined(VMS)
7031 // for UNIX ":cd" means: go to home directory
7032 if (*new_dir == NUL)
7033 {
7034 // use NameBuff for home directory name
7035# ifdef VMS
7036 char_u *p;
7037
7038 p = mch_getenv((char_u *)"SYS$LOGIN");
7039 if (p == NULL || *p == NUL) // empty is the same as not set
7040 NameBuff[0] = NUL;
7041 else
7042 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7043# else
7044 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7045# endif
7046 new_dir = NameBuff;
7047 }
7048#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02007049 dir_differs = new_dir == NULL || pdir == NULL
7050 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007051 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7052 emsg(_(e_failed));
7053 else
7054 {
7055 char_u *acmd_fname;
7056
7057 post_chdir(scope);
7058
7059 if (dir_differs)
7060 {
7061 if (scope == CDSCOPE_WINDOW)
7062 acmd_fname = (char_u *)"window";
7063 else if (scope == CDSCOPE_TABPAGE)
7064 acmd_fname = (char_u *)"tabpage";
7065 else
7066 acmd_fname = (char_u *)"global";
7067 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7068 curbuf);
7069 }
7070 retval = TRUE;
7071 }
7072 vim_free(tofree);
7073
7074 return retval;
7075}
Bram Moolenaarea408852005-06-25 22:49:46 +00007076
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007078 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007080 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007081ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007083 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084
7085 new_dir = eap->arg;
7086#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007087 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 if (*new_dir == NUL)
7089 ex_pwd(NULL);
7090 else
7091#endif
7092 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007093 cdscope_T scope = CDSCOPE_GLOBAL;
7094
7095 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7096 scope = CDSCOPE_WINDOW;
7097 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7098 scope = CDSCOPE_TABPAGE;
7099
7100 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007101 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007102 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007103 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 ex_pwd(eap);
7105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 }
7107}
7108
7109/*
7110 * ":pwd".
7111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007113ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114{
7115 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7116 {
7117#ifdef BACKSLASH_IN_FILENAME
7118 slash_adjust(NameBuff);
7119#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007120 if (p_verbose > 0)
7121 {
7122 char *context = "global";
7123
7124 if (curwin->w_localdir != NULL)
7125 context = "window";
7126 else if (curtab->tp_localdir != NULL)
7127 context = "tabpage";
7128 smsg("[%s] %s", context, (char *)NameBuff);
7129 }
7130 else
7131 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 }
7133 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007134 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135}
7136
7137/*
7138 * ":=".
7139 */
7140 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007141ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007143 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007144 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145}
7146
7147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007148ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007150 int n;
7151 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152
7153 if (cursor_valid())
7154 {
7155 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7156 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007157 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007159
7160 len = eap->line2;
7161 switch (*eap->arg)
7162 {
7163 case 'm': break;
7164 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007165 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007166 }
7167 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168}
7169
7170/*
7171 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7172 */
7173 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007174do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175{
Bram Moolenaar52953192019-08-16 10:27:13 +02007176 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007177 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007178# ifdef ELAPSED_FUNC
7179 elapsed_T start_tv;
7180
7181 // Remember at what time we started, so that we know how much longer we
7182 // should wait after waiting for a bit.
7183 ELAPSED_INIT(start_tv);
7184# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185
7186 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007187 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007188 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007190 wait_now = msec - done > 1000L ? 1000L : msec - done;
7191#ifdef FEAT_TIMERS
7192 {
7193 long due_time = check_due_timer();
7194
7195 if (due_time > 0 && due_time < wait_now)
7196 wait_now = due_time;
7197 }
7198#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007199#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007200 if (has_any_channel() && wait_now > 20L)
7201 wait_now = 20L;
7202#endif
7203#ifdef FEAT_SOUND
7204 if (has_any_sound_callback() && wait_now > 20L)
7205 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007206#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007207 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007208
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007209#ifdef FEAT_JOB_CHANNEL
7210 if (has_any_channel())
7211 ui_breakcheck_force(TRUE);
7212 else
7213#endif
7214 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007215#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007216 // Process the netbeans and clientserver messages that may have been
7217 // received in the call to ui_breakcheck() when the GUI is in use. This
7218 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007219 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007220#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007221
7222# ifdef ELAPSED_FUNC
7223 // actual time passed
7224 done = ELAPSED_FUNC(start_tv);
7225# else
7226 // guestimate time passed (will actually be more)
7227 done += wait_now;
7228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007230
7231 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7232 // input buffer, otherwise a following call to input() fails.
7233 if (got_int)
7234 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235}
7236
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237/*
7238 * ":winsize" command (obsolete).
7239 */
7240 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007241ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242{
7243 int w, h;
7244 char_u *arg = eap->arg;
7245 char_u *p;
7246
7247 w = getdigits(&arg);
7248 arg = skipwhite(arg);
7249 p = arg;
7250 h = getdigits(&arg);
7251 if (*p != NUL && *arg == NUL)
7252 set_shellsize(w, h, TRUE);
7253 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007254 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255}
7256
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007258ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259{
7260 int xchar = NUL;
7261 char_u *p;
7262
7263 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7264 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007265 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 if (eap->arg[1] == NUL)
7267 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007268 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 return;
7270 }
7271 xchar = eap->arg[1];
7272 p = eap->arg + 2;
7273 }
7274 else
7275 p = eap->arg + 1;
7276
7277 eap->nextcmd = check_nextcmd(p);
7278 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007279 if (*p != NUL && *p != (
7280#ifdef FEAT_EVAL
7281 in_vim9script() ? '#' :
7282#endif
7283 '"')
7284 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007285 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007286 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007288 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007289 postponed_split_flags = cmdmod.cmod_split;
7290 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7292 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007293 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 }
7295}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296
Bram Moolenaar843ee412004-06-30 16:16:41 +00007297#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298/*
7299 * ":winpos".
7300 */
7301 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007302ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303{
7304 int x, y;
7305 char_u *arg = eap->arg;
7306 char_u *p;
7307
7308 if (*arg == NUL)
7309 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007310# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007311# ifdef VIMDLL
7312 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7313 mch_get_winpos(&x, &y) != FAIL)
7314# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007316# else
7317 if (mch_get_winpos(&x, &y) != FAIL)
7318# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 {
7320 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007321 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 }
7323 else
7324# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007325 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 }
7327 else
7328 {
7329 x = getdigits(&arg);
7330 arg = skipwhite(arg);
7331 p = arg;
7332 y = getdigits(&arg);
7333 if (*p == NUL || *arg != NUL)
7334 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007335 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 return;
7337 }
7338# ifdef FEAT_GUI
7339 if (gui.in_use)
7340 gui_mch_set_winpos(x, y);
7341 else if (gui.starting)
7342 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007343 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 gui_win_x = x;
7345 gui_win_y = y;
7346 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007347# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 else
7349# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007350# endif
7351# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007352 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353# endif
7354# ifdef HAVE_TGETENT
7355 if (*T_CWP)
7356 term_set_winpos(x, y);
7357# endif
7358 }
7359}
7360#endif
7361
7362/*
7363 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7364 */
7365 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007366ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367{
7368 oparg_T oa;
7369
7370 clear_oparg(&oa);
7371 oa.regname = eap->regname;
7372 oa.start.lnum = eap->line1;
7373 oa.end.lnum = eap->line2;
7374 oa.line_count = eap->line2 - eap->line1 + 1;
7375 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007377 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 {
7379 setpcmark();
7380 curwin->w_cursor.lnum = eap->line1;
7381 beginline(BL_SOL | BL_FIX);
7382 }
7383
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007384 if (VIsual_active)
7385 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007386
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 switch (eap->cmdidx)
7388 {
7389 case CMD_delete:
7390 oa.op_type = OP_DELETE;
7391 op_delete(&oa);
7392 break;
7393
7394 case CMD_yank:
7395 oa.op_type = OP_YANK;
7396 (void)op_yank(&oa, FALSE, TRUE);
7397 break;
7398
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007399 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007400 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007402 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7403#else
7404 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007406 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 oa.op_type = OP_RSHIFT;
7408 else
7409 oa.op_type = OP_LSHIFT;
7410 op_shift(&oa, FALSE, eap->amount);
7411 break;
7412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007414 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415}
7416
7417/*
7418 * ":put".
7419 */
7420 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007421ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007423 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 if (eap->line2 == 0)
7425 {
7426 eap->line2 = 1;
7427 eap->forceit = TRUE;
7428 }
7429 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007430 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007431 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432}
7433
7434/*
7435 * Handle ":copy" and ":move".
7436 */
7437 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007438ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439{
7440 long n;
7441
Bram Moolenaar491799b2020-08-01 19:23:43 +02007442#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007443 if (not_in_vim9(eap) == FAIL)
7444 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007445#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007446 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007447 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 {
7449 eap->nextcmd = NULL;
7450 return;
7451 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007452 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453
7454 /*
7455 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7456 */
7457 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7458 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007459 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 return;
7461 }
7462
7463 if (eap->cmdidx == CMD_move)
7464 {
7465 if (do_move(eap->line1, eap->line2, n) == FAIL)
7466 return;
7467 }
7468 else
7469 ex_copy(eap->line1, eap->line2, n);
7470 u_clearline();
7471 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007472 ex_may_print(eap);
7473}
7474
7475/*
7476 * Print the current line if flags were given to the Ex command.
7477 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007478 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007479ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007480{
7481 if (eap->flags != 0)
7482 {
7483 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7484 (eap->flags & EXFLAG_LIST));
7485 ex_no_reprint = TRUE;
7486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487}
7488
7489/*
7490 * ":smagic" and ":snomagic".
7491 */
7492 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007493ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494{
7495 int magic_save = p_magic;
7496
7497 p_magic = (eap->cmdidx == CMD_smagic);
Bram Moolenaar66e00142020-08-12 21:58:12 +02007498 ex_substitute(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 p_magic = magic_save;
7500}
7501
7502/*
7503 * ":join".
7504 */
7505 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007506ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507{
7508 curwin->w_cursor.lnum = eap->line1;
7509 if (eap->line1 == eap->line2)
7510 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007511 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 return;
7513 if (eap->line2 == curbuf->b_ml.ml_line_count)
7514 {
7515 beep_flush();
7516 return;
7517 }
7518 ++eap->line2;
7519 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007520 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007522 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523}
7524
7525/*
7526 * ":[addr]@r" or ":[addr]*r": execute register
7527 */
7528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007529ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530{
7531 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007532 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533
7534 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007535 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536
7537#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007538 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539#endif
7540
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007541 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 c = *eap->arg;
7543 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7544 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007545 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007546 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7547 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007548 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 else
7552 {
7553 int save_efr = exec_from_reg;
7554
7555 exec_from_reg = TRUE;
7556
7557 /*
7558 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007559 * Continue until the stuff buffer is empty and all added characters
7560 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007562 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7564
7565 exec_from_reg = save_efr;
7566 }
7567}
7568
7569/*
7570 * ":!".
7571 */
7572 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007573ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574{
7575 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7576}
7577
7578/*
7579 * ":undo".
7580 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007582ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007584 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007585 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007586 else
7587 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588}
7589
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007590#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007592ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007593{
7594 char_u hash[UNDO_HASH_SIZE];
7595
7596 u_compute_hash(hash);
7597 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7598}
7599
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007600 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007601ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007602{
7603 char_u hash[UNDO_HASH_SIZE];
7604
7605 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007606 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007607}
7608#endif
7609
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610/*
7611 * ":redo".
7612 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007614ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615{
7616 u_redo(1);
7617}
7618
7619/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007620 * ":earlier" and ":later".
7621 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007622 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007623ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007624{
7625 long count = 0;
7626 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007627 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007628 char_u *p = eap->arg;
7629
7630 if (*p == NUL)
7631 count = 1;
7632 else if (isdigit(*p))
7633 {
7634 count = getdigits(&p);
7635 switch (*p)
7636 {
7637 case 's': ++p; sec = TRUE; break;
7638 case 'm': ++p; sec = TRUE; count *= 60; break;
7639 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007640 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7641 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007642 }
7643 }
7644
7645 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007646 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007647 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007648 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7649 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007650}
7651
7652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 * ":redir": start/stop redirection.
7654 */
7655 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007656ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657{
7658 char *mode;
7659 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007660 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661
Bram Moolenaarba768492016-07-08 15:32:54 +02007662#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007663 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007664 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007665 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007666 return;
7667 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007668#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007669
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 if (STRICMP(eap->arg, "END") == 0)
7671 close_redir();
7672 else
7673 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007674 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007676 ++arg;
7677 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007679 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 mode = "a";
7681 }
7682 else
7683 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007684 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685
7686 close_redir();
7687
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007688 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007689 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 if (fname == NULL)
7691 return;
7692#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007693 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 {
7695 char_u *browseFile;
7696
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007697 browseFile = do_browse(BROWSE_SAVE,
7698 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007699 fname, NULL, NULL,
7700 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007702 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 vim_free(fname);
7704 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007705 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 }
7707#endif
7708
7709 redir_fd = open_exfile(fname, eap->forceit, mode);
7710 vim_free(fname);
7711 }
7712#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007713 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007715 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007717 ++arg;
7718 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007720 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007721 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007723 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007725 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007726 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007727 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007728 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007730 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007731 if (*arg == '>')
7732 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007733 // Make register empty when not using @A-@Z and the
7734 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007735 if (*arg == NUL && !isupper(redir_reg))
7736 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007738 }
7739 if (*arg != NUL)
7740 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007741 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007742 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007743 }
7744 }
7745 else if (*arg == '=' && arg[1] == '>')
7746 {
7747 int append;
7748
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007749 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007750 close_redir();
7751 arg += 2;
7752
7753 if (*arg == '>')
7754 {
7755 ++arg;
7756 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 }
7758 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007759 append = FALSE;
7760
7761 if (var_redir_start(skipwhite(arg), append) == OK)
7762 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 }
7764#endif
7765
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007766 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007769 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007771
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007772 // Make sure redirection is not off. Can happen for cmdline completion
7773 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007774 if (redir_fd != NULL
7775#ifdef FEAT_EVAL
7776 || redir_reg || redir_vname
7777#endif
7778 )
7779 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780}
7781
7782/*
7783 * ":redraw": force redraw
7784 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007785 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007786ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787{
7788 int r = RedrawingDisabled;
7789 int p = p_lz;
7790
7791 RedrawingDisabled = 0;
7792 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007793 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007795 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796#ifdef FEAT_TITLE
7797 if (need_maketitle)
7798 maketitle();
7799#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007800#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7801# ifdef VIMDLL
7802 if (!gui.in_use)
7803# endif
7804 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 RedrawingDisabled = r;
7807 p_lz = p;
7808
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007809 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 msg_didout = FALSE;
7811 msg_col = 0;
7812
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007813 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007814 need_wait_return = FALSE;
7815
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 out_flush();
7817}
7818
7819/*
7820 * ":redrawstatus": force redraw of status line(s)
7821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007823ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 int r = RedrawingDisabled;
7826 int p = p_lz;
7827
7828 RedrawingDisabled = 0;
7829 p_lz = FALSE;
7830 if (eap->forceit)
7831 status_redraw_all();
7832 else
7833 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007834 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 RedrawingDisabled = r;
7836 p_lz = p;
7837 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838}
7839
Bram Moolenaare12bab32019-01-08 22:02:56 +01007840/*
7841 * ":redrawtabline": force redraw of the tabline
7842 */
7843 static void
7844ex_redrawtabline(exarg_T *eap UNUSED)
7845{
7846 int r = RedrawingDisabled;
7847 int p = p_lz;
7848
7849 RedrawingDisabled = 0;
7850 p_lz = FALSE;
7851
7852 draw_tabline();
7853
7854 RedrawingDisabled = r;
7855 p_lz = p;
7856 out_flush();
7857}
7858
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007860close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861{
7862 if (redir_fd != NULL)
7863 {
7864 fclose(redir_fd);
7865 redir_fd = NULL;
7866 }
7867#ifdef FEAT_EVAL
7868 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007869 if (redir_vname)
7870 {
7871 var_redir_stop();
7872 redir_vname = 0;
7873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874#endif
7875}
7876
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007877#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007878 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007879vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007880{
7881 if (vim_mkdir(name, prot) != 0)
7882 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007883 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007884 return FAIL;
7885 }
7886 return OK;
7887}
7888#endif
7889
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890/*
7891 * Open a file for writing for an Ex command, with some checks.
7892 * Return file descriptor, or NULL on failure.
7893 */
7894 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007895open_exfile(
7896 char_u *fname,
7897 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007898 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899{
7900 FILE *fd;
7901
7902#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007903 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 if (mch_isdir(fname))
7905 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007906 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 return NULL;
7908 }
7909#endif
7910 if (!forceit && *mode != 'a' && vim_fexists(fname))
7911 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007912 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 return NULL;
7914 }
7915
7916 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007917 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918
7919 return fd;
7920}
7921
7922/*
7923 * ":mark" and ":k".
7924 */
7925 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007926ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927{
7928 pos_T pos;
7929
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007930 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007931 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007932 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02007933 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 else
7935 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007936 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 curwin->w_cursor.lnum = eap->line2;
7938 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007939 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007940 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007941 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 }
7943}
7944
7945/*
7946 * Update w_topline, w_leftcol and the cursor position.
7947 */
7948 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007949update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007951 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 update_topline();
7953 if (!curwin->w_p_wrap)
7954 validate_cursor();
7955 update_curswant();
7956}
7957
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007959 * Save the current State and go to Normal mode.
7960 * Return TRUE if the typeahead could be saved.
7961 */
7962 int
7963save_current_state(save_state_T *sst)
7964{
7965 sst->save_msg_scroll = msg_scroll;
7966 sst->save_restart_edit = restart_edit;
7967 sst->save_msg_didout = msg_didout;
7968 sst->save_State = State;
7969 sst->save_insertmode = p_im;
7970 sst->save_finish_op = finish_op;
7971 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007972 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007973
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007974 msg_scroll = FALSE; // no msg scrolling in Normal mode
7975 restart_edit = 0; // don't go to Insert mode
7976 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007977
7978 /*
7979 * Save the current typeahead. This is required to allow using ":normal"
7980 * from an event handler and makes sure we don't hang when the argument
7981 * ends with half a command.
7982 */
7983 save_typeahead(&sst->tabuf);
7984 return sst->tabuf.typebuf_valid;
7985}
7986
7987 void
7988restore_current_state(save_state_T *sst)
7989{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007990 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007991 restore_typeahead(&sst->tabuf);
7992
7993 msg_scroll = sst->save_msg_scroll;
7994 restart_edit = sst->save_restart_edit;
7995 p_im = sst->save_insertmode;
7996 finish_op = sst->save_finish_op;
7997 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007998 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007999 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008000
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008001 // Restore the state (needed when called from a function executed for
8002 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008003 State = sst->save_State;
8004#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008005 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008006#endif
8007}
8008
8009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 * ":normal[!] {commands}": Execute normal mode commands.
8011 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008012 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008013ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008015 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 char_u *arg = NULL;
8017 int l;
8018 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008020 if (ex_normal_lock > 0)
8021 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008022 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008023 return;
8024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 if (ex_normal_busy >= p_mmd)
8026 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008027 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 return;
8029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 /*
8032 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8033 * this for the K_SPECIAL leading byte, otherwise special keys will not
8034 * work.
8035 */
8036 if (has_mbyte)
8037 {
8038 int len = 0;
8039
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008040 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 for (p = eap->arg; *p != NUL; ++p)
8042 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008043#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008044 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008046#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008047 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008048 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008049#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 )
8053 len += 2;
8054 }
8055 if (len > 0)
8056 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008057 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 if (arg != NULL)
8059 {
8060 len = 0;
8061 for (p = eap->arg; *p != NUL; ++p)
8062 {
8063 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008064#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 if (*p == CSI)
8066 {
8067 arg[len++] = KS_EXTRA;
8068 arg[len++] = (int)KE_CSI;
8069 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008070#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008071 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 {
8073 arg[len++] = *++p;
8074 if (*p == K_SPECIAL)
8075 {
8076 arg[len++] = KS_SPECIAL;
8077 arg[len++] = KE_FILLER;
8078 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008079#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 else if (*p == CSI)
8081 {
8082 arg[len++] = KS_EXTRA;
8083 arg[len++] = (int)KE_CSI;
8084 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 }
8087 arg[len] = NUL;
8088 }
8089 }
8090 }
8091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008093 ++ex_normal_busy;
8094 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 {
8096 /*
8097 * Repeat the :normal command for each line in the range. When no
8098 * range given, execute it just once, without positioning the cursor
8099 * first.
8100 */
8101 do
8102 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 if (eap->addr_count != 0)
8104 {
8105 curwin->w_cursor.lnum = eap->line1++;
8106 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008107 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 }
8109
Bram Moolenaar13505972019-01-24 15:04:48 +01008110 exec_normal_cmd(arg != NULL
8111 ? arg
8112 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 }
8114 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8115 }
8116
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008117 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 update_topline_cursor();
8119
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008120 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008122 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008123#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008124 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008125#endif
8126
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128}
8129
8130/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008131 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 */
8133 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008134ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008136 if (eap->forceit)
8137 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008138 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008139 if (!curwin->w_cursor.lnum)
8140 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008141 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008142 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008143#ifdef FEAT_TERMINAL
8144 // Ignore this when running in an active terminal.
8145 if (term_job_running(curbuf->b_term))
8146 return;
8147#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008148
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008149 // Ignore the command when already in Insert mode. Inserting an
8150 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008151 if (State & INSERT)
8152 return;
8153
Bram Moolenaar64969662005-12-14 21:59:55 +00008154 if (eap->cmdidx == CMD_startinsert)
8155 restart_edit = 'a';
8156 else if (eap->cmdidx == CMD_startreplace)
8157 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008159 restart_edit = 'V';
8160
8161 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008163 if (eap->cmdidx == CMD_startinsert)
8164 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008165 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008167
8168 if (VIsual_active)
8169 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170}
8171
8172/*
8173 * ":stopinsert"
8174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008176ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177{
8178 restart_edit = 0;
8179 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008180 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008183/*
8184 * Execute normal mode command "cmd".
8185 * "remap" can be REMAP_NONE or REMAP_YES.
8186 */
8187 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008188exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008189{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008190 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008191 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008192 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008193}
Bram Moolenaar25281632016-01-21 23:32:32 +01008194
Bram Moolenaar25281632016-01-21 23:32:32 +01008195/*
8196 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008197 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008198 */
8199 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008200exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008201{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008202 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008203 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008204
Bram Moolenaar905dd902019-04-07 14:21:47 +02008205 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8206 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008207 clear_oparg(&oa);
8208 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008209 while ((!stuff_empty()
8210 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008211 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008212 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008213 {
8214 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008215#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008216 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008217 && oa.op_type == OP_NOP && oa.regname == NUL
8218 && !VIsual_active)
8219 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008220 // If terminal_loop() returns OK we got a key that is handled
8221 // in Normal model. With FAIL we first need to position the
8222 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008223 if (terminal_loop(TRUE) == OK)
8224 normal_cmd(&oa, TRUE);
8225 }
8226 else
8227#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008228 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008229 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008230 }
8231}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008232
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233#ifdef FEAT_FIND_ID
8234 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008235ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236{
8237 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8238 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8239 (linenr_T)1, (linenr_T)MAXLNUM);
8240}
8241
Bram Moolenaar4033c552017-09-16 20:54:51 +02008242#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243/*
8244 * ":psearch"
8245 */
8246 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008247ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248{
8249 g_do_tagpreview = p_pvh;
8250 ex_findpat(eap);
8251 g_do_tagpreview = 0;
8252}
8253#endif
8254
8255 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008256ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257{
8258 int whole = TRUE;
8259 long n;
8260 char_u *p;
8261 int action;
8262
8263 switch (cmdnames[eap->cmdidx].cmd_name[2])
8264 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008265 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8267 action = ACTION_GOTO;
8268 else
8269 action = ACTION_SHOW;
8270 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008271 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 action = ACTION_SHOW_ALL;
8273 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008274 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 action = ACTION_GOTO;
8276 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008277 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 action = ACTION_SPLIT;
8279 break;
8280 }
8281
8282 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008283 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 {
8285 n = getdigits(&eap->arg);
8286 eap->arg = skipwhite(eap->arg);
8287 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008288 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 {
8290 whole = FALSE;
8291 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008292 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 if (*p)
8294 {
8295 *p++ = NUL;
8296 p = skipwhite(p);
8297
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008298 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008299 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008300 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 else
8302 eap->nextcmd = check_nextcmd(p);
8303 }
8304 }
8305 if (!eap->skip)
8306 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8307 whole, !eap->forceit,
8308 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8309 n, action, eap->line1, eap->line2);
8310}
8311#endif
8312
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313
Bram Moolenaar4033c552017-09-16 20:54:51 +02008314#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315/*
8316 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8317 */
8318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008319ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008321 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8323}
8324
8325/*
8326 * ":pedit"
8327 */
8328 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008329ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330{
8331 win_T *curwin_save = curwin;
8332
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008333 if (ERROR_IF_ANY_POPUP_WINDOW)
8334 return;
8335
Bram Moolenaar026587b2019-08-17 15:08:00 +02008336 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008338 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008339
Bram Moolenaar026587b2019-08-17 15:08:00 +02008340 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008342
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 if (curwin != curwin_save && win_valid(curwin_save))
8344 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008345 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 validate_cursor();
8347 redraw_later(VALID);
8348 win_enter(curwin_save, TRUE);
8349 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008350# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008351 else if (WIN_IS_POPUP(curwin))
8352 {
8353 // can't keep focus in popup window
8354 win_enter(firstwin, TRUE);
8355 }
8356# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 g_do_tagpreview = 0;
8358}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360
8361/*
8362 * ":stag", ":stselect" and ":stjump".
8363 */
8364 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008365ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366{
8367 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008368 postponed_split_flags = cmdmod.cmod_split;
8369 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8371 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008372 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374
8375/*
8376 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8377 */
8378 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008379ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380{
8381 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8382}
8383
8384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008385ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386{
8387 int cmd;
8388
8389 switch (name[1])
8390 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008391 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008393 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008395 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008397 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008399 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008401 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008403 case 'f': // ":tfirst"
8404 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008406 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008408 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008410 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008412 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 return;
8414 }
8415#endif
8416 cmd = DT_TAG;
8417 break;
8418 }
8419
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008420 if (name[0] == 'l')
8421 {
8422#ifndef FEAT_QUICKFIX
8423 ex_ni(eap);
8424 return;
8425#else
8426 cmd = DT_LTAG;
8427#endif
8428 }
8429
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8431 eap->forceit, TRUE);
8432}
8433
8434/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008435 * Check "str" for starting with a special cmdline variable.
8436 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8437 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8438 */
8439 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008440find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008441{
8442 int len;
8443 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008444 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008445 "%",
8446#define SPEC_PERC 0
8447 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008448#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008449 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008450#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008451 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008452#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008453 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008454#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008455 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008456#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008457 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008458#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008459 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008460#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008461 "<stack>", // call stack
8462#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008463 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008464#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008465 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008466#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008467 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008468#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008469 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008470#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008471 "<SID>", // script ID: <SNR>123_
8472#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008473#ifdef FEAT_CLIENTSERVER
8474 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008475# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008476#endif
8477 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008478
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008479 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008480 {
8481 len = (int)STRLEN(spec_str[i]);
8482 if (STRNCMP(src, spec_str[i], len) == 0)
8483 {
8484 *usedlen = len;
8485 return i;
8486 }
8487 }
8488 return -1;
8489}
8490
8491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 * Evaluate cmdline variables.
8493 *
8494 * change '%' to curbuf->b_ffname
Bram Moolenaar59d8e562020-11-07 18:41:10 +01008495 * '#' to curwin->w_alt_fnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 * '<cword>' to word under the cursor
8497 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008498 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 * '<cfile>' to path name under the cursor
8500 * '<sfile>' to sourced file name
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008501 * '<stack>' to call stack
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008502 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 * '<afile>' to file name for autocommand
8504 * '<abuf>' to buffer number for autocommand
8505 * '<amatch>' to matching name for autocommand
8506 *
8507 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8508 * "" for error without a message) and NULL is returned.
8509 * Returns an allocated string if a valid match was found.
8510 * Returns NULL if no match was found. "usedlen" then still contains the
8511 * number of characters to skip.
8512 */
8513 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008514eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008515 char_u *src, // pointer into commandline
8516 char_u *srcstart, // beginning of valid memory for src
8517 int *usedlen, // characters after src that are used
8518 linenr_T *lnump, // line number for :e command, or NULL
8519 char **errormsg, // pointer to error message
8520 int *escaped) // return value has escaped white space (can
8521 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522{
8523 int i;
8524 char_u *s;
8525 char_u *result;
8526 char_u *resultbuf = NULL;
8527 int resultlen;
8528 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008529 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008531 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534
8535 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008536 if (escaped != NULL)
8537 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538
8539 /*
8540 * Check if there is something to do.
8541 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008542 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008543 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 {
8545 *usedlen = 1;
8546 return NULL;
8547 }
8548
8549 /*
8550 * Skip when preceded with a backslash "\%" and "\#".
8551 * Note: In "\\%" the % is also not recognized!
8552 */
8553 if (src > srcstart && src[-1] == '\\')
8554 {
8555 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008556 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 return NULL;
8558 }
8559
8560 /*
8561 * word or WORD under cursor
8562 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008563 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8564 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008566 resultlen = find_ident_under_cursor(&result,
8567 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8568 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8569 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 if (resultlen == 0)
8571 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008572 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 return NULL;
8574 }
8575 }
8576
8577 /*
8578 * '#': Alternate file name
8579 * '%': Current file name
8580 * File name under the cursor
8581 * File name for autocommand
8582 * and following modifiers
8583 */
8584 else
8585 {
8586 switch (spec_idx)
8587 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008588 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 if (curbuf->b_fname == NULL)
8590 {
8591 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008592 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 }
8594 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008595 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008597 tilde_file = STRCMP(result, "~") == 0;
8598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 break;
8600
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008601 case SPEC_HASH: // '#' or "#99": alternate file
8602 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 {
8604 result = arg_all();
8605 resultbuf = result;
8606 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008607 if (escaped != NULL)
8608 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 break;
8611 }
8612 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008613 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008614 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008616 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008617 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008618 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008619 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008621 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008623 if (*usedlen < 2)
8624 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008625 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008626 *usedlen = 1;
8627 return NULL;
8628 }
8629#ifdef FEAT_EVAL
8630 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8631 (long)i);
8632 if (result == NULL)
8633 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008634 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008635 return NULL;
8636 }
8637#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008638 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 }
8642 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008643 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008644 if (i == 0 && src[1] == '<' && *usedlen > 1)
8645 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008646 buf = buflist_findnr(i);
8647 if (buf == NULL)
8648 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008649 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008650 return NULL;
8651 }
8652 if (lnump != NULL)
8653 *lnump = ECMD_LAST;
8654 if (buf->b_fname == NULL)
8655 {
8656 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008657 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008658 }
8659 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008660 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008661 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008662 tilde_file = STRCMP(result, "~") == 0;
8663 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 break;
8666
8667#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008668 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008669 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 if (result == NULL)
8671 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008672 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 return NULL;
8674 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008675 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 break;
8677#endif
8678
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008679 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008681 if (result != NULL && !autocmd_fname_full)
8682 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008683 // Still need to turn the fname into a full path. It is
8684 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008685 autocmd_fname_full = TRUE;
8686 result = FullName_save(autocmd_fname, FALSE);
8687 vim_free(autocmd_fname);
8688 autocmd_fname = result;
8689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 if (result == NULL)
8691 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008692 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 return NULL;
8694 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008695 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 break;
8697
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008698 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 if (autocmd_bufnr <= 0)
8700 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008701 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 return NULL;
8703 }
8704 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8705 result = strbuf;
8706 break;
8707
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008708 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 result = autocmd_match;
8710 if (result == NULL)
8711 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008712 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 return NULL;
8714 }
8715 break;
8716
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008717 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008718 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008719 result = estack_sfile(spec_idx == SPEC_SFILE
8720 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 if (result == NULL)
8722 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008723 *errormsg = spec_idx == SPEC_SFILE
8724 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8725 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 return NULL;
8727 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008728 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008730
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008731 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008732 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008733 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008734 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008735 return NULL;
8736 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008737 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008738 result = strbuf;
8739 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008740
8741#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008742 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008743 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008744 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008745 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008746 return NULL;
8747 }
8748 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008749 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008750 result = strbuf;
8751 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008752
Bram Moolenaar90944302020-08-01 20:45:11 +02008753 case SPEC_SID:
8754 if (current_sctx.sc_sid <= 0)
8755 {
8756 *errormsg = _(e_usingsid);
8757 return NULL;
8758 }
8759 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
8760 result = strbuf;
8761 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02008762#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02008763
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008764#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008765 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008766 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8767 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 result = strbuf;
8769 break;
8770#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008771
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008772 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008773 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008774 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 }
8776
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008777 resultlen = (int)STRLEN(result); // length of new string
8778 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 {
8780 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 resultlen = (int)(s - result);
8783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 else if (!skip_mod)
8785 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008786 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 &resultlen);
8788 if (result == NULL)
8789 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008790 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 return NULL;
8792 }
8793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 }
8795
8796 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8797 {
8798 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008799 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008800 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008802 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 result = NULL;
8804 }
8805 else
8806 result = vim_strnsave(result, resultlen);
8807 vim_free(resultbuf);
8808 return result;
8809}
8810
8811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 * Expand the <sfile> string in "arg".
8813 *
8814 * Returns an allocated string, or NULL for any error.
8815 */
8816 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008817expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008819 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 int len;
8821 char_u *result;
8822 char_u *newres;
8823 char_u *repl;
8824 int srclen;
8825 char_u *p;
8826
8827 result = vim_strsave(arg);
8828 if (result == NULL)
8829 return NULL;
8830
8831 for (p = result; *p; )
8832 {
8833 if (STRNCMP(p, "<sfile>", 7) != 0)
8834 ++p;
8835 else
8836 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008837 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008838 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 if (errormsg != NULL)
8840 {
8841 if (*errormsg)
8842 emsg(errormsg);
8843 vim_free(result);
8844 return NULL;
8845 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008846 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 {
8848 p += srclen;
8849 continue;
8850 }
8851 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8852 newres = alloc(len);
8853 if (newres == NULL)
8854 {
8855 vim_free(repl);
8856 vim_free(result);
8857 return NULL;
8858 }
8859 mch_memmove(newres, result, (size_t)(p - result));
8860 STRCPY(newres + (p - result), repl);
8861 len = (int)STRLEN(newres);
8862 STRCAT(newres, p + srclen);
8863 vim_free(repl);
8864 vim_free(result);
8865 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008866 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 }
8868 }
8869
8870 return result;
8871}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008874/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008875 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008876 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008877 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008879dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 if (fname == NULL)
8882 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008883 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884}
8885#endif
8886
8887/*
8888 * ":behave {mswin,xterm}"
8889 */
8890 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008891ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892{
8893 if (STRCMP(eap->arg, "mswin") == 0)
8894 {
8895 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8896 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8897 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8898 set_option_value((char_u *)"keymodel", 0L,
8899 (char_u *)"startsel,stopsel", 0);
8900 }
8901 else if (STRCMP(eap->arg, "xterm") == 0)
8902 {
8903 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8904 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8905 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8906 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8907 }
8908 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008909 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910}
8911
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912static int filetype_detect = FALSE;
8913static int filetype_plugin = FALSE;
8914static int filetype_indent = FALSE;
8915
8916/*
8917 * ":filetype [plugin] [indent] {on,off,detect}"
8918 * on: Load the filetype.vim file to install autocommands for file types.
8919 * off: Load the ftoff.vim file to remove all autocommands for file types.
8920 * plugin on: load filetype.vim and ftplugin.vim
8921 * plugin off: load ftplugof.vim
8922 * indent on: load filetype.vim and indent.vim
8923 * indent off: load indoff.vim
8924 */
8925 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008926ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927{
8928 char_u *arg = eap->arg;
8929 int plugin = FALSE;
8930 int indent = FALSE;
8931
8932 if (*eap->arg == NUL)
8933 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008934 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008935 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 filetype_detect ? "ON" : "OFF",
8937 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8938 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8939 return;
8940 }
8941
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008942 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 for (;;)
8944 {
8945 if (STRNCMP(arg, "plugin", 6) == 0)
8946 {
8947 plugin = TRUE;
8948 arg = skipwhite(arg + 6);
8949 continue;
8950 }
8951 if (STRNCMP(arg, "indent", 6) == 0)
8952 {
8953 indent = TRUE;
8954 arg = skipwhite(arg + 6);
8955 continue;
8956 }
8957 break;
8958 }
8959 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8960 {
8961 if (*arg == 'o' || !filetype_detect)
8962 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008963 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 filetype_detect = TRUE;
8965 if (plugin)
8966 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008967 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 filetype_plugin = TRUE;
8969 }
8970 if (indent)
8971 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008972 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 filetype_indent = TRUE;
8974 }
8975 }
8976 if (*arg == 'd')
8977 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008978 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008979 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 }
8981 }
8982 else if (STRCMP(arg, "off") == 0)
8983 {
8984 if (plugin || indent)
8985 {
8986 if (plugin)
8987 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008988 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 filetype_plugin = FALSE;
8990 }
8991 if (indent)
8992 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008993 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 filetype_indent = FALSE;
8995 }
8996 }
8997 else
8998 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008999 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 filetype_detect = FALSE;
9001 }
9002 }
9003 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009004 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005}
9006
9007/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009008 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 */
9010 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009011ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012{
9013 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009014 {
9015 char_u *arg = eap->arg;
9016
9017 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9018 arg += 9;
9019
9020 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9021 if (arg != eap->arg)
9022 did_filetype = FALSE;
9023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009027ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028{
9029#ifdef FEAT_DIGRAPHS
9030 if (*eap->arg != NUL)
9031 putdigraph(eap->arg);
9032 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009033 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009035 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036#endif
9037}
9038
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009039#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9040 void
9041set_no_hlsearch(int flag)
9042{
9043 no_hlsearch = flag;
9044# ifdef FEAT_EVAL
9045 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9046# endif
9047}
9048
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049/*
9050 * ":nohlsearch"
9051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009053ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009055 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009056 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058#endif
9059
9060#ifdef FEAT_CRYPT
9061/*
9062 * ":X": Get crypt key
9063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009065ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009067 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009068 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069}
9070#endif
9071
9072#ifdef FEAT_FOLDING
9073 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009074ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075{
9076 if (foldManualAllowed(TRUE))
9077 foldCreate(eap->line1, eap->line2);
9078}
9079
9080 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009081ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082{
9083 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9084 eap->forceit, FALSE);
9085}
9086
9087 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009088ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089{
9090 linenr_T lnum;
9091
Bram Moolenaar4facea32019-10-12 20:17:40 +02009092# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009093 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009094# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009095
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009096 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9098 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9099 ml_setmarked(lnum);
9100
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009101 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009103 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009104# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009105 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009106# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107}
9108#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009109
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009110#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009111/*
9112 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9113 * instead of a quickfix command.
9114 */
9115 int
9116is_loclist_cmd(int cmdidx)
9117{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009118 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009119 return FALSE;
9120 return cmdnames[cmdidx].cmd_name[0] == 'l';
9121}
9122#endif
9123
Bram Moolenaar4facea32019-10-12 20:17:40 +02009124#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009125 int
9126get_pressedreturn(void)
9127{
9128 return ex_pressedreturn;
9129}
9130
9131 void
9132set_pressedreturn(int val)
9133{
9134 ex_pressedreturn = val;
9135}
9136#endif