blob: d2cc05f59a56aa99d2d14e3a2fc6c419d276573f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +020023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaar20b23c62020-08-20 15:25:00 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
39static void ex_bmodified(exarg_T *eap);
40static void ex_bnext(exarg_T *eap);
41static void ex_bprevious(exarg_T *eap);
42static void ex_brewind(exarg_T *eap);
43static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifndef FEAT_QUICKFIX
47# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000048# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# define ex_cc ex_ni
50# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020051# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cfile ex_ni
53# define qf_list ex_ni
54# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020055# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000057# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020059#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define ex_cclose ex_ni
61# define ex_copen ex_ni
62# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020063# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000065#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
66# define ex_cexpr ex_ni
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020069static linenr_T default_address(exarg_T *eap);
Bram Moolenaarb7316892019-05-01 18:08:42 +020070static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020071static void address_default_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010072static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020073#if !defined(FEAT_PERL) \
74 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
75 || !defined(FEAT_TCL) \
76 || !defined(FEAT_RUBY) \
77 || !defined(FEAT_LUA) \
78 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000079# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010080static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010082static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000086#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010087static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
88static void ex_highlight(exarg_T *eap);
89static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_cquit(exarg_T *eap);
91static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010092static void ex_close(exarg_T *eap);
93static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
94static void ex_only(exarg_T *eap);
95static void ex_resize(exarg_T *eap);
96static void ex_stag(exarg_T *eap);
97static void ex_tabclose(exarg_T *eap);
98static void ex_tabonly(exarg_T *eap);
99static void ex_tabnext(exarg_T *eap);
100static void ex_tabmove(exarg_T *eap);
101static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200102#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100103static void ex_pclose(exarg_T *eap);
104static void ex_ptag(exarg_T *eap);
105static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106#else
107# define ex_pclose ex_ni
108# define ex_ptag ex_ni
109# define ex_pedit ex_ni
110#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100111static void ex_hide(exarg_T *eap);
112static void ex_stop(exarg_T *eap);
113static void ex_exit(exarg_T *eap);
114static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100116static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#else
118# define ex_goto ex_ni
119#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100120static void ex_shell(exarg_T *eap);
121static void ex_preserve(exarg_T *eap);
122static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100123static void ex_mode(exarg_T *eap);
124static void ex_wrongmodifier(exarg_T *eap);
125static void ex_find(exarg_T *eap);
126static void ex_open(exarg_T *eap);
127static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#ifndef FEAT_GUI
129# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100132#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100133static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#else
135# define ex_tearoff ex_ni
136#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100137#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
138 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#else
141# define ex_popup ex_ni
142#endif
143#ifndef FEAT_GUI_MSWIN
144# define ex_simalt ex_ni
145#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000146#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147# define gui_mch_find_dialog ex_ni
148# define gui_mch_replace_dialog ex_ni
149#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000150#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151# define ex_helpfind ex_ni
152#endif
153#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100154# define ex_cscope ex_ni
155# define ex_scscope ex_ni
156# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#endif
158#ifndef FEAT_SYN_HL
159# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200160# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000161#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200162#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200163# define ex_syntime ex_ni
164#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000165#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000166# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000167# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000168# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000169# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000170# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000171#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200172#ifndef FEAT_PERSISTENT_UNDO
173# define ex_rundo ex_ni
174# define ex_wundo ex_ni
175#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200176#ifndef FEAT_LUA
177# define ex_lua ex_script_ni
178# define ex_luado ex_ni
179# define ex_luafile ex_ni
180#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000181#ifndef FEAT_MZSCHEME
182# define ex_mzscheme ex_script_ni
183# define ex_mzfile ex_ni
184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#ifndef FEAT_PERL
186# define ex_perl ex_script_ni
187# define ex_perldo ex_ni
188#endif
189#ifndef FEAT_PYTHON
190# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200191# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192# define ex_pyfile ex_ni
193#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200194#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200195# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200196# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200197# define ex_py3file ex_ni
198#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100199#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
200# define ex_pyx ex_script_ni
201# define ex_pyxdo ex_ni
202# define ex_pyxfile ex_ni
203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#ifndef FEAT_TCL
205# define ex_tcl ex_script_ni
206# define ex_tcldo ex_ni
207# define ex_tclfile ex_ni
208#endif
209#ifndef FEAT_RUBY
210# define ex_ruby ex_script_ni
211# define ex_rubydo ex_ni
212# define ex_rubyfile ex_ni
213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#ifndef FEAT_KEYMAP
215# define ex_loadkeymap ex_ni
216#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100217static void ex_swapname(exarg_T *eap);
218static void ex_syncbind(exarg_T *eap);
219static void ex_read(exarg_T *eap);
220static void ex_pwd(exarg_T *eap);
221static void ex_equal(exarg_T *eap);
222static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000225#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#else
228# define ex_winpos ex_ni
229#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100230static void ex_operators(exarg_T *eap);
231static void ex_put(exarg_T *eap);
232static void ex_copymove(exarg_T *eap);
233static void ex_submagic(exarg_T *eap);
234static void ex_join(exarg_T *eap);
235static void ex_at(exarg_T *eap);
236static void ex_bang(exarg_T *eap);
237static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200238#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100239static void ex_wundo(exarg_T *eap);
240static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200241#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ex_redo(exarg_T *eap);
243static void ex_later(exarg_T *eap);
244static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100246static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100249static void ex_startinsert(exarg_T *eap);
250static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100252static void ex_checkpath(exarg_T *eap);
253static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254#else
255# define ex_findpat ex_ni
256# define ex_checkpath ex_ni
257#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200258#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100259static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260#else
261# define ex_psearch ex_ni
262#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static void ex_tag(exarg_T *eap);
264static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#ifndef FEAT_EVAL
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200266# define ex_block ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200267# define ex_break ex_ni
268# define ex_breakadd ex_ni
269# define ex_breakdel ex_ni
270# define ex_breaklist ex_ni
271# define ex_call ex_ni
272# define ex_catch ex_ni
273# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200274# define ex_continue ex_ni
275# define ex_debug ex_ni
276# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200277# define ex_def ex_ni
278# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200279# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100280# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281# define ex_echo ex_ni
282# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283# define ex_else ex_ni
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200284# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200285# define ex_endfunction ex_ni
286# define ex_endif ex_ni
287# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200289# define ex_eval ex_ni
290# define ex_execute ex_ni
291# define ex_finally ex_ni
292# define ex_finish ex_ni
293# define ex_function ex_ni
294# define ex_if ex_ni
295# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200296# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200297# define ex_lockvar ex_ni
298# define ex_oldfiles ex_ni
299# define ex_options ex_ni
300# define ex_packadd ex_ni
301# define ex_packloadall ex_ni
302# define ex_return ex_ni
303# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304# define ex_throw ex_ni
305# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000307# define ex_unlockvar ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100308# define ex_vim9script ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200309# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100310# define ex_import ex_ni
311# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200313#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314# define ex_loadview ex_ni
315#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200316#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317# define ex_viminfo ex_ni
318#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100319static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100320static void ex_filetype(exarg_T *eap);
321static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000323# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324# define ex_diffpatch ex_ni
325# define ex_diffgetput ex_ni
326# define ex_diffsplit ex_ni
327# define ex_diffthis ex_ni
328# define ex_diffupdate ex_ni
329#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100330static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100332static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333#else
334# define ex_nohlsearch ex_ni
335# define ex_match ex_ni
336#endif
337#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100338static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339#else
340# define ex_X ex_ni
341#endif
342#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100343static void ex_fold(exarg_T *eap);
344static void ex_foldopen(exarg_T *eap);
345static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346#else
347# define ex_fold ex_ni
348# define ex_foldopen ex_ni
349# define ex_folddo ex_ni
350#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100351#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352# define ex_language ex_ni
353#endif
354#ifndef FEAT_SIGNS
355# define ex_sign ex_ni
356#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000357#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200358# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000359# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200360# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363#ifndef FEAT_JUMPLIST
364# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200365# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366# define ex_changes ex_ni
367#endif
368
Bram Moolenaar05159a02005-02-26 23:04:13 +0000369#ifndef FEAT_PROFILE
370# define ex_profile ex_ni
371#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200372#ifndef FEAT_TERMINAL
373# define ex_terminal ex_ni
374#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200375#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
376# define ex_xrestore ex_ni
377#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100378#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200379# define ex_popupclear ex_ni
380#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000381
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382/*
383 * Declare cmdnames[].
384 */
385#define DO_DECLARE_EXCMD
386#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200387#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100388
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389static char_u dollar_command[2] = {'$', 0};
390
391
392#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100393// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394typedef struct
395{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100396 char_u *line; // command line
397 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398} wcmd_T;
399
400/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000401 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000403 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000405struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100407 garray_T *lines_gap; // growarray with line info
408 int current_line; // last read line from growarray
409 int repeating; // TRUE when looping a second time
410 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200411 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 void *cookie;
413};
414
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200415static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100416static int store_loop_line(garray_T *gap, char_u *line);
417static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000418
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100419// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000420struct dbg_stuff
421{
422 int trylevel;
423 int force_abort;
424 except_T *caught_stack;
425 char_u *vv_exception;
426 char_u *vv_throwpoint;
427 int did_emsg;
428 int got_int;
429 int did_throw;
430 int need_rethrow;
431 int check_cstack;
432 except_T *current_exception;
433};
434
Bram Moolenaared203462004-06-16 11:19:22 +0000435 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100436save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000437{
438 dsp->trylevel = trylevel; trylevel = 0;
439 dsp->force_abort = force_abort; force_abort = FALSE;
440 dsp->caught_stack = caught_stack; caught_stack = NULL;
441 dsp->vv_exception = v_exception(NULL);
442 dsp->vv_throwpoint = v_throwpoint(NULL);
443
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100444 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000445 dsp->did_emsg = did_emsg; did_emsg = FALSE;
446 dsp->got_int = got_int; got_int = FALSE;
447 dsp->did_throw = did_throw; did_throw = FALSE;
448 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
449 dsp->check_cstack = check_cstack; check_cstack = FALSE;
450 dsp->current_exception = current_exception; current_exception = NULL;
451}
452
453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100454restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000455{
456 suppress_errthrow = FALSE;
457 trylevel = dsp->trylevel;
458 force_abort = dsp->force_abort;
459 caught_stack = dsp->caught_stack;
460 (void)v_exception(dsp->vv_exception);
461 (void)v_throwpoint(dsp->vv_throwpoint);
462 did_emsg = dsp->did_emsg;
463 got_int = dsp->got_int;
464 did_throw = dsp->did_throw;
465 need_rethrow = dsp->need_rethrow;
466 check_cstack = dsp->check_cstack;
467 current_exception = dsp->current_exception;
468}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469#endif
470
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471/*
472 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
473 * command is given.
474 */
475 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100476do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100477 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478{
479 int save_msg_scroll;
480 int prev_msg_row;
481 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100482 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000483
484 if (improved)
485 exmode_active = EXMODE_VIM;
486 else
487 exmode_active = EXMODE_NORMAL;
488 State = NORMAL;
489
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100490 // When using ":global /pat/ visual" and then "Q" we return to continue
491 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000492 if (global_busy)
493 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494
495 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100496 ++RedrawingDisabled; // don't redisplay the window
497 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100499 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 ++hold_gui_events;
501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502
Bram Moolenaar32526b32019-01-19 17:43:09 +0100503 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 while (exmode_active)
505 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100506 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000507 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
508 {
509 exmode_active = FALSE;
510 break;
511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 msg_scroll = TRUE;
513 need_wait_return = FALSE;
514 ex_pressedreturn = FALSE;
515 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100516 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 prev_msg_row = msg_row;
518 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 if (improved)
520 {
521 cmdline_row = msg_row;
522 do_cmdline(NULL, getexline, NULL, 0);
523 }
524 else
525 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
526 lines_left = Rows - 1;
527
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100529 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000531 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100532 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000533 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000535 if (ex_pressedreturn)
536 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100537 // go up one line, to overwrite the ":<CR>" line, so the
538 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000539 msg_row = prev_msg_row;
540 if (prev_msg_row == Rows - 1)
541 msg_row--;
542 }
543 msg_col = 0;
544 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
545 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100548 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 {
550 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100551 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000552 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100553 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 }
556
557#ifdef FEAT_GUI
558 --hold_gui_events;
559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 --RedrawingDisabled;
561 --no_wait_return;
562 update_screen(CLEAR);
563 need_wait_return = FALSE;
564 msg_scroll = save_msg_scroll;
565}
566
567/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200568 * Print the executed command for when 'verbose' is set.
569 * When "lnum" is 0 only print the command.
570 */
571 static void
572msg_verbose_cmd(linenr_T lnum, char_u *cmd)
573{
574 ++no_wait_return;
575 verbose_enter_scroll();
576
577 if (lnum == 0)
578 smsg(_("Executing: %s"), cmd);
579 else
580 smsg(_("line %ld: %s"), (long)lnum, cmd);
581 if (msg_silent == 0)
582 msg_puts("\n"); // don't overwrite this
583
584 verbose_leave_scroll();
585 --no_wait_return;
586}
587
588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 * Execute a simple command line. Used for translated commands like "*".
590 */
591 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100592do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593{
594 return do_cmdline(cmd, NULL, NULL,
595 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
596}
597
598/*
599 * do_cmdline(): execute one Ex command line
600 *
601 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100602 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 * 2. Split up in parts separated with '|'.
604 *
605 * This function can be called recursively!
606 *
607 * flags:
608 * DOCMD_VERBOSE - The command will be included in the error message.
609 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100610 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 * DOCMD_KEYTYPED - Don't reset KeyTyped.
612 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
613 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
614 *
615 * return FAIL if cmdline could not be executed, OK otherwise
616 */
617 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100618do_cmdline(
619 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200620 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100621 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100622 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100624 char_u *next_cmdline; // next cmd to execute
625 char_u *cmdline_copy = NULL; // copy of cmd line
626 int used_getline = FALSE; // used "fgetline" to obtain command
627 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100629 int count = 0; // line number count
630 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 int retval = OK;
632#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100633 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100634 garray_T lines_ga; // keep lines for ":while"/":for"
635 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200636 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100637 char_u *fname = NULL; // function or script name
638 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
639 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
640 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200642 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200643 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100645 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200646 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000648 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000650 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100652# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653# define cmd_cookie cookie
654#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100655 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200656#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100657 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100659 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
660 // location for storing error messages to be converted to an exception.
661 // This ensures that the do_errthrow() call in do_one_cmd() does not
662 // combine the messages stored by an earlier invocation of do_one_cmd()
663 // with the command name of the later one. This would happen when
664 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 saved_msg_list = msg_list;
666 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667#endif
668
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100669 // It's possible to create an endless loop with ":execute", catch that
670 // here. The value of 200 allows nested function calls, ":source", etc.
671 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100672 if (call_depth >= 200
673#ifdef FEAT_EVAL
674 && call_depth >= p_mfd
675#endif
676 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100678 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100680 // When converting to an exception, we do not include the command name
681 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100682 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 msg_list = saved_msg_list;
684#endif
685 return FAIL;
686 }
687 ++call_depth;
688
689#ifdef FEAT_EVAL
690 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000691 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 cstack.cs_trylevel = 0;
693 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000694 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
696
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100697 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100699 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100700 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000701 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 ++ex_nesting_level;
703
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100704 // Get the function or script name and the address where the next breakpoint
705 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000706 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {
708 fname = func_name(real_cookie);
709 breakpoint = func_breakpoint(real_cookie);
710 dbg_tick = func_dbg_tick(real_cookie);
711 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100712 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100714 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 breakpoint = source_breakpoint(real_cookie);
716 dbg_tick = source_dbg_tick(real_cookie);
717 }
718
719 /*
720 * Initialize "force_abort" and "suppress_errthrow" at the top level.
721 */
722 if (!recursive)
723 {
724 force_abort = FALSE;
725 suppress_errthrow = FALSE;
726 }
727
728 /*
729 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000730 * exception handling (used when debugging). Otherwise clear it to avoid
731 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000733 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000734 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000735 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200736 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
738 initial_trylevel = trylevel;
739
740 /*
741 * "did_throw" will be set to TRUE when an exception is being thrown.
742 */
743 did_throw = FALSE;
744#endif
745 /*
746 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000747 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 * If force_abort is set, we cancel everything.
749 */
750 did_emsg = FALSE;
751
752 /*
753 * KeyTyped is only set when calling vgetc(). Reset it here when not
754 * calling vgetc() (sourced command lines).
755 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100756 if (!(flags & DOCMD_KEYTYPED)
757 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 KeyTyped = FALSE;
759
760 /*
761 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000762 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 * - for multiple commands on one line, separated with '|'
764 * - when repeating until there are no more lines (for ":source")
765 */
766 next_cmdline = cmdline;
767 do
768 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000769#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100770 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000771#endif
772
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100773 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 if (next_cmdline == NULL
775#ifdef FEAT_EVAL
776 && !force_abort
777 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000778 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779#endif
780 )
781 did_emsg = FALSE;
782
783 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000784 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100785 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 * 3. If a line is given: Make a copy, so we can mess with it.
787 */
788
789#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100790 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000791 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100793 // Each '|' separated command is stored separately in lines_ga, to
794 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100795 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100797 // Check if a function has returned or, unless it has an unclosed
798 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000799 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000801# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000802 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000803 func_line_end(real_cookie);
804# endif
805 if (func_has_ended(real_cookie))
806 {
807 retval = FAIL;
808 break;
809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000811#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000812 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100813 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000814 script_line_end();
815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100817 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100818 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 {
820 retval = FAIL;
821 break;
822 }
823
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100824 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 if (breakpoint != NULL && dbg_tick != NULL
826 && *dbg_tick != debug_tick)
827 {
828 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100829 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100830 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 *dbg_tick = debug_tick;
832 }
833
834 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100835 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100837 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100839 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100841 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100842 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100844 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100845 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 *dbg_tick = debug_tick;
847 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000848# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000849 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000850 {
851 if (getline_is_func)
852 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100853 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000854 script_line_start();
855 }
856# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858#endif
859
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100860 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 if (next_cmdline == NULL)
862 {
863 /*
864 * Need to set msg_didout for the first line after an ":if",
865 * otherwise the ":if" will be overwritten.
866 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100867 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100869 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870#ifdef FEAT_EVAL
871 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
872#else
873 0
874#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200875 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100877 // Don't call wait_return for aborted command line. The NULL
878 // returned for the end of a sourced file or executed function
879 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 if (KeyTyped && !(flags & DOCMD_REPEAT))
881 need_wait_return = FALSE;
882 retval = FAIL;
883 break;
884 }
885 used_getline = TRUE;
886
887 /*
888 * Keep the first typed line. Clear it when more lines are typed.
889 */
890 if (flags & DOCMD_KEEPLINE)
891 {
892 vim_free(repeat_cmdline);
893 if (count == 0)
894 repeat_cmdline = vim_strsave(next_cmdline);
895 else
896 repeat_cmdline = NULL;
897 }
898 }
899
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100900 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 else if (cmdline_copy == NULL)
902 {
903 next_cmdline = vim_strsave(next_cmdline);
904 if (next_cmdline == NULL)
905 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100906 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 retval = FAIL;
908 break;
909 }
910 }
911 cmdline_copy = next_cmdline;
912
913#ifdef FEAT_EVAL
914 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200915 * Inside a while/for loop, and when the command looks like a ":while"
916 * or ":for", the line is stored, because we may need it later when
917 * looping.
918 *
919 * When there is a '|' and another command, it is stored separately,
920 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000921 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200922 *
923 * Pass a different "fgetline" function to do_one_cmd() below,
924 * that it stores lines in or reads them from "lines_ga". Makes it
925 * possible to define a function inside a while/for loop and handles
926 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200928 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200930 cmd_getline = get_loop_line;
931 cmd_cookie = (void *)&cmd_loop_cookie;
932 cmd_loop_cookie.lines_gap = &lines_ga;
933 cmd_loop_cookie.current_line = current_line;
934 cmd_loop_cookie.getline = fgetline;
935 cmd_loop_cookie.cookie = cookie;
936 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
937
938 // Save the current line when encountering it the first time.
939 if (current_line == lines_ga.ga_len
940 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 {
942 retval = FAIL;
943 break;
944 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200945 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200947 else
948 {
949 cmd_getline = fgetline;
950 cmd_cookie = cookie;
951 }
952
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 did_endif = FALSE;
954#endif
955
956 if (count++ == 0)
957 {
958 /*
959 * All output from the commands is put below each other, without
960 * waiting for a return. Don't do this when executing commands
961 * from a script or when being called recursive (e.g. for ":e
962 * +command file").
963 */
964 if (!(flags & DOCMD_NOWAIT) && !recursive)
965 {
966 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100967 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100969 msg_scroll = TRUE; // put messages below each other
970 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 ++RedrawingDisabled;
972 did_inc = TRUE;
973 }
974 }
975
Bram Moolenaar823654b2020-05-29 23:03:09 +0200976 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100977 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978
979 /*
980 * 2. Execute one '|' separated command.
981 * do_one_cmd() will return NULL if there is no trailing '|'.
982 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
983 */
984 ++recursive;
985 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
986#ifdef FEAT_EVAL
987 &cstack,
988#endif
989 cmd_getline, cmd_cookie);
990 --recursive;
991
992#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000993 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100994 // Use "current_line" from "cmd_loop_cookie", it may have been
995 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000996 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997#endif
998
999 if (next_cmdline == NULL)
1000 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001001 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001002
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 /*
1004 * If the command was typed, remember it for the ':' register.
1005 * Do this AFTER executing the command to make :@: work.
1006 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001007 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 && new_last_cmdline != NULL)
1009 {
1010 vim_free(last_cmdline);
1011 last_cmdline = new_last_cmdline;
1012 new_last_cmdline = NULL;
1013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 }
1015 else
1016 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001017 // need to copy the command after the '|' to cmdline_copy, for the
1018 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001019 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 next_cmdline = cmdline_copy;
1021 }
1022
1023
1024#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001025 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001027 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 && !func_has_abort(real_cookie))
1029 did_emsg = FALSE;
1030
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001031 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 {
1033 ++current_line;
1034
1035 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001036 * An ":endwhile", ":endfor" and ":continue" is handled here.
1037 * If we were executing commands, jump back to the ":while" or
1038 * ":for".
1039 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001041 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001043 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001045 // Jump back to the matching ":while" or ":for". Be careful
1046 // not to use a cs_line[] from an entry that isn't a ":while"
1047 // or ":for": It would make "current_line" invalid and can
1048 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 if (!did_emsg && !got_int && !did_throw
1050 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001051 && (cstack.cs_flags[cstack.cs_idx]
1052 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 && cstack.cs_line[cstack.cs_idx] >= 0
1054 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1055 {
1056 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001057 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001058 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001059 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001061 // Check for the next breakpoint at or after the ":while"
1062 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 if (breakpoint != NULL)
1064 {
1065 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001066 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 fname,
1068 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1069 *dbg_tick = debug_tick;
1070 }
1071 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001072 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001074 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001076 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1077 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 }
1079 }
1080
1081 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001084 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001086 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001087 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 }
1089 }
1090
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001091 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001092 if (breakpoint != NULL && has_watchexpr())
1093 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001094 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001095 *dbg_tick = debug_tick;
1096 }
1097
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 /*
1099 * When not inside any ":while" loop, clear remembered lines.
1100 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001101 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 {
1103 if (lines_ga.ga_len > 0)
1104 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001105 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1107 free_cmdlines(&lines_ga);
1108 }
1109 current_line = 0;
1110 }
1111
1112 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001113 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1114 * being restored at the ":endtry". Reset them here and set the
1115 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1116 * This includes the case where a missing ":endif", ":endwhile" or
1117 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001119 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001121 cstack.cs_lflags &= ~CSL_HAD_FINA;
1122 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1123 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 did_throw ? (void *)current_exception : NULL);
1125 did_emsg = got_int = did_throw = FALSE;
1126 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1127 }
1128
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001129 // Update global "trylevel" for recursive calls to do_cmdline() from
1130 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 trylevel = initial_trylevel + cstack.cs_trylevel;
1132
1133 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001134 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 * files) is aborted because of an error, an interrupt, or an uncaught
1136 * exception, cancel everything. If it is left normally, reset
1137 * force_abort to get the non-EH compatible abortion behavior for
1138 * the rest of the script.
1139 */
1140 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1141 force_abort = FALSE;
1142
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001143 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001145#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146
1147 }
1148 /*
1149 * Continue executing command lines when:
1150 * - no CTRL-C typed, no aborting error, no exception thrown or try
1151 * conditionals need to be checked for executing finally clauses or
1152 * catching an interrupt exception
1153 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001154 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 * looping for ":source" command or function call.
1156 */
1157 while (!((got_int
1158#ifdef FEAT_EVAL
1159 || (did_emsg && force_abort) || did_throw
1160#endif
1161 )
1162#ifdef FEAT_EVAL
1163 && cstack.cs_trylevel == 0
1164#endif
1165 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001166 && !(did_emsg
1167#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001168 // Keep going when inside try/catch, so that the error can be
1169 // deal with, except when it is a syntax error, it may cause
1170 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001171 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1172#endif
1173 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001174 && (getline_equal(fgetline, cookie, getexmodeline)
1175 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 && (next_cmdline != NULL
1177#ifdef FEAT_EVAL
1178 || cstack.cs_idx >= 0
1179#endif
1180 || (flags & DOCMD_REPEAT)));
1181
1182 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001183 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184#ifdef FEAT_EVAL
1185 free_cmdlines(&lines_ga);
1186 ga_clear(&lines_ga);
1187
1188 if (cstack.cs_idx >= 0)
1189 {
1190 /*
1191 * If a sourced file or executed function ran to its end, report the
1192 * unclosed conditional.
1193 */
1194 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001195 && ((getline_equal(fgetline, cookie, getsourceline)
1196 && !source_finished(fgetline, cookie))
1197 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 && !func_has_ended(real_cookie))))
1199 {
1200 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001201 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001203 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001204 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001205 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001207 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 }
1209
1210 /*
1211 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1212 * ":endtry" in a sourced file or executed function. If the try
1213 * conditional is in its finally clause, ignore anything pending.
1214 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001215 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 */
1217 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001218 {
1219 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1220
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001221 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001222 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001223 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1224 &cstack.cs_looplevel);
1225 }
1226 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 trylevel = initial_trylevel;
1228 }
1229
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001230 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1231 // lack was reported above and the error message is to be converted to an
1232 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001233 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 ? (char_u *)"endfunction" : (char_u *)NULL);
1235
1236 if (trylevel == 0)
1237 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001238 // Just in case did_throw got set but current_exception wasn't.
1239 if (current_exception == NULL)
1240 did_throw = FALSE;
1241
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 /*
1243 * When an exception is being thrown out of the outermost try
1244 * conditional, discard the uncaught exception, disable the conversion
1245 * of interrupts or errors to exceptions, and ensure that no more
1246 * commands are executed.
1247 */
1248 if (did_throw)
1249 {
1250 void *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001251 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252
1253 /*
1254 * If the uncaught exception is a user exception, report it as an
1255 * error. If it is an error exception, display the saved error
1256 * message now. For an interrupt exception, do nothing; the
1257 * interrupt message is given elsewhere.
1258 */
1259 switch (current_exception->type)
1260 {
1261 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001262 vim_snprintf((char *)IObuff, IOSIZE,
1263 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 current_exception->value);
1265 p = vim_strsave(IObuff);
1266 break;
1267 case ET_ERROR:
1268 messages = current_exception->messages;
1269 current_exception->messages = NULL;
1270 break;
1271 case ET_INTERRUPT:
1272 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 }
1274
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001275 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1276 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001277 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 current_exception->throw_name = NULL;
1279
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001280 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 suppress_errthrow = TRUE;
1282 force_abort = TRUE;
1283
1284 if (messages != NULL)
1285 {
1286 do
1287 {
1288 next = messages->next;
1289 emsg(messages->msg);
1290 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001291 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 vim_free(messages);
1293 messages = next;
1294 }
1295 while (messages != NULL);
1296 }
1297 else if (p != NULL)
1298 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001299 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 vim_free(p);
1301 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001302 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001303 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001304 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 }
1306
1307 /*
1308 * On an interrupt or an aborting error not converted to an exception,
1309 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001310 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 * when force_abort is set and did_emsg unset in case of an interrupt
1312 * from a finally clause after an error.
1313 */
1314 else if (got_int || (did_emsg && force_abort))
1315 suppress_errthrow = TRUE;
1316 }
1317
1318 /*
1319 * The current cstack will be freed when do_cmdline() returns. An uncaught
1320 * exception will have to be rethrown in the previous cstack. If a function
1321 * has just returned or a script file was just finished and the previous
1322 * cstack belongs to the same function or, respectively, script file, it
1323 * will have to be checked for finally clauses to be executed due to the
1324 * ":return" or ":finish". This is done in do_one_cmd().
1325 */
1326 if (did_throw)
1327 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001328 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001330 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 && ex_nesting_level > func_level(real_cookie) + 1))
1332 {
1333 if (!did_throw)
1334 check_cstack = TRUE;
1335 }
1336 else
1337 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001338 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001339 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 --ex_nesting_level;
1341 /*
1342 * Go to debug mode when returning from a function in which we are
1343 * single-stepping.
1344 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001345 if ((getline_equal(fgetline, cookie, getsourceline)
1346 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001348 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 ? (char_u *)_("End of sourced file")
1350 : (char_u *)_("End of function"));
1351 }
1352
1353 /*
1354 * Restore the exception environment (done after returning from the
1355 * debugger).
1356 */
1357 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001358 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359
1360 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001361
1362 // Cleanup if "cs_emsg_silent_list" remains.
1363 if (cstack.cs_emsg_silent_list != NULL)
1364 {
1365 eslist_T *elem, *temp;
1366
1367 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1368 {
1369 temp = elem->next;
1370 vim_free(elem);
1371 }
1372 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001373#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374
1375 /*
1376 * If there was too much output to fit on the command line, ask the user to
1377 * hit return before redrawing the screen. With the ":global" command we do
1378 * this only once after the command is finished.
1379 */
1380 if (did_inc)
1381 {
1382 --RedrawingDisabled;
1383 --no_wait_return;
1384 msg_scroll = FALSE;
1385
1386 /*
1387 * When just finished an ":if"-":else" which was typed, no need to
1388 * wait for hit-return. Also for an error situation.
1389 */
1390 if (retval == FAIL
1391#ifdef FEAT_EVAL
1392 || (did_endif && KeyTyped && !did_emsg)
1393#endif
1394 )
1395 {
1396 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001397 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 }
1399 else if (need_wait_return)
1400 {
1401 /*
1402 * The msg_start() above clears msg_didout. The wait_return we do
1403 * here should not overwrite the command that may be shown before
1404 * doing that.
1405 */
1406 msg_didout |= msg_didout_before_start;
1407 wait_return(FALSE);
1408 }
1409 }
1410
Bram Moolenaar2a942252012-11-28 23:03:07 +01001411#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001412 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001413#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 /*
1415 * Reset if_level, in case a sourced script file contains more ":if" than
1416 * ":endif" (could be ":if x | foo | endif").
1417 */
1418 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001419#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001420
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 --call_depth;
1422 return retval;
1423}
1424
1425#ifdef FEAT_EVAL
1426/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001427 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 */
1429 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001430get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001432 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 wcmd_T *wp;
1434 char_u *line;
1435
1436 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1437 {
1438 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001439 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001441 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001443 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001445 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001446 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 ++cp->current_line;
1448
1449 return line;
1450 }
1451
1452 KeyTyped = FALSE;
1453 ++cp->current_line;
1454 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001455 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 return vim_strsave(wp->line);
1457}
1458
1459/*
1460 * Store a line in "gap" so that a ":while" loop can execute it again.
1461 */
1462 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001463store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
1465 if (ga_grow(gap, 1) == FAIL)
1466 return FAIL;
1467 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001468 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 return OK;
1471}
1472
1473/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001474 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 */
1476 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001477free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478{
1479 while (gap->ga_len > 0)
1480 {
1481 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1482 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 }
1484}
1485#endif
1486
1487/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001488 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1489 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001492getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001493 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001494 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001495 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496{
1497#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001498 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001499 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001501 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1502 // function that's originally used to obtain the lines. This may be
1503 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001504 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001505 cp = (struct loop_cookie *)cookie;
1506 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 {
1508 gp = cp->getline;
1509 cp = cp->cookie;
1510 }
1511 return gp == func;
1512#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001513 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514#endif
1515}
1516
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001518 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 * getline function. Otherwise return "cookie".
1520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001522getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001523 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001524 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525{
Bram Moolenaar13505972019-01-24 15:04:48 +01001526#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001527 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001528 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001530 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1531 // cookie that's originally used to obtain the lines. This may be nested
1532 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001533 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001534 cp = (struct loop_cookie *)cookie;
1535 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 {
1537 gp = cp->getline;
1538 cp = cp->cookie;
1539 }
1540 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001541#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001544}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545
Bram Moolenaard5053d02020-06-28 15:51:16 +02001546#if defined(FEAT_EVAL) || defined(PROT)
1547/*
1548 * Get the next line source line without advancing.
1549 */
1550 char_u *
1551getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001552 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001553 void *cookie) // argument for fgetline()
1554{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001555 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001556 struct loop_cookie *cp;
1557 wcmd_T *wp;
1558
1559 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1560 // cookie that's originally used to obtain the lines. This may be nested
1561 // several levels.
1562 gp = fgetline;
1563 cp = (struct loop_cookie *)cookie;
1564 while (gp == get_loop_line)
1565 {
1566 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1567 {
1568 // executing lines a second time, use the stored copy
1569 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1570 return wp->line;
1571 }
1572 gp = cp->getline;
1573 cp = cp->cookie;
1574 }
1575 if (gp == getsourceline)
1576 return source_nextline(cp);
1577 return NULL;
1578}
1579#endif
1580
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001581
1582/*
1583 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1584 * ":bwipeout", etc.
1585 * Returns the buffer number.
1586 */
1587 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001588compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001589{
1590 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001591 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001592 int count = offset;
1593
1594 buf = firstbuf;
1595 while (buf->b_next != NULL && buf->b_fnum < lnum)
1596 buf = buf->b_next;
1597 while (count != 0)
1598 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001599 count += (offset < 0) ? 1 : -1;
1600 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1601 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001602 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001603 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001604 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001605 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001606 while (buf->b_ml.ml_mfp == NULL)
1607 {
1608 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1609 if (nextbuf == NULL)
1610 break;
1611 buf = nextbuf;
1612 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001613 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001614 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001615 if (addr_type == ADDR_LOADED_BUFFERS)
1616 while (buf->b_ml.ml_mfp == NULL)
1617 {
1618 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1619 if (nextbuf == NULL)
1620 break;
1621 buf = nextbuf;
1622 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001623 return buf->b_fnum;
1624}
1625
Bram Moolenaarb7316892019-05-01 18:08:42 +02001626/*
1627 * Return the window number of "win".
1628 * When "win" is NULL return the number of windows.
1629 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001630 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001631current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001632{
1633 win_T *wp;
1634 int nr = 0;
1635
Bram Moolenaar29323592016-07-24 22:04:11 +02001636 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001637 {
1638 ++nr;
1639 if (wp == win)
1640 break;
1641 }
1642 return nr;
1643}
1644
1645 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001646current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001647{
1648 tabpage_T *tp;
1649 int nr = 0;
1650
Bram Moolenaar29323592016-07-24 22:04:11 +02001651 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001652 {
1653 ++nr;
1654 if (tp == tab)
1655 break;
1656 }
1657 return nr;
1658}
1659
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001660 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001661comment_start(char_u *p, int starts_with_colon UNUSED)
1662{
1663#ifdef FEAT_EVAL
1664 if (in_vim9script())
1665 return p[0] == '#' && p[1] != '{' && !starts_with_colon;
1666#endif
1667 return *p == '"';
1668}
1669
Bram Moolenaarf240e182014-11-27 18:33:02 +01001670# define CURRENT_WIN_NR current_win_nr(curwin)
1671# define LAST_WIN_NR current_win_nr(NULL)
1672# define CURRENT_TAB_NR current_tab_nr(curtab)
1673# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001674
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675/*
1676 * Execute one Ex command.
1677 *
1678 * If 'sourcing' is TRUE, the command will be included in the error message.
1679 *
1680 * 1. skip comment lines and leading space
1681 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001682 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001683 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001684 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001685 * 6. parse arguments
1686 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001688 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 *
1690 * This function may be called recursively!
1691 */
1692#if (_MSC_VER == 1200)
1693/*
Bram Moolenaared203462004-06-16 11:19:22 +00001694 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001696 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697#endif
1698 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001699do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001700 char_u **cmdlinep,
1701 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001703 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001705 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001706 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001708 char_u *p;
1709 linenr_T lnum;
1710 long n;
1711 char *errormsg = NULL; // error message
1712 char_u *after_modifier = NULL;
1713 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001714 cmdmod_T save_cmdmod;
1715 int save_reg_executing = reg_executing;
1716 int ni; // set when Not Implemented
1717 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001718 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001719#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001720 int may_have_range;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001721 int vim9script = in_vim9script();
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001722 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724
Bram Moolenaara80faa82020-04-12 19:37:17 +02001725 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 ea.line1 = 1;
1727 ea.line2 = 1;
1728#ifdef FEAT_EVAL
1729 ++ex_nesting_level;
1730#endif
1731
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001732 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 if (quitmore
1734#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001735 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001736 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001737#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001738 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001739 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 --quitmore;
1741
1742 /*
1743 * Reset browse, confirm, etc.. They are restored when returning, for
1744 * recursive calls.
1745 */
1746 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001748 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001749 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1750 goto doend;
1751
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001752/*
1753 * 1. Skip comment lines and leading white space and colons.
1754 * 2. Handle command modifiers.
1755 */
1756 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001758 ea.cmdlinep = cmdlinep;
1759 ea.getline = fgetline;
1760 ea.cookie = cookie;
1761#ifdef FEAT_EVAL
1762 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001763 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001765 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001766 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001767 apply_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001769 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770
1771#ifdef FEAT_EVAL
1772 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1773 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1774#else
1775 ea.skip = (if_level > 0);
1776#endif
1777
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001779 * 3. Skip over the range to find the command. Let "p" point to after it.
1780 *
1781 * We need the command to know what kind of range it uses.
1782 */
1783 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001784#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001785 // In Vim9 script a colon is required before the range. This may also be
1786 // after command modifiers.
1787 if (vim9script)
1788 {
1789 may_have_range = FALSE;
1790 for (p = ea.cmd; p >= *cmdlinep; --p)
1791 {
1792 if (*p == ':')
1793 may_have_range = TRUE;
1794 if (p < ea.cmd && !VIM_ISWHITE(*p))
1795 break;
1796 }
1797 }
1798 else
1799 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001800 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001801#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001802 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803
1804#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001805 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001806 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001807 if (ea.cmd == cmd + 1 && *cmd == '$')
1808 // should be "$VAR = val"
1809 --ea.cmd;
1810 else if (ea.cmd > cmd)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001811 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001812 emsg(_(e_colon_required_before_a_range));
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001813 goto doend;
1814 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001816 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 else
1818#endif
1819 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001820
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001821#ifdef FEAT_EVAL
1822# ifdef FEAT_PROFILE
1823 // Count this line for profiling if skip is TRUE.
1824 if (do_profiling == PROF_YES
1825 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1826 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1827 {
1828 int skip = did_emsg || got_int || did_throw;
1829
1830 if (ea.cmdidx == CMD_catch)
1831 skip = !skip && !(cstack->cs_idx >= 0
1832 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1833 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1834 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1835 skip = skip || !(cstack->cs_idx >= 0
1836 && !(cstack->cs_flags[cstack->cs_idx]
1837 & (CSF_ACTIVE | CSF_TRUE)));
1838 else if (ea.cmdidx == CMD_finally)
1839 skip = FALSE;
1840 else if (ea.cmdidx != CMD_endif
1841 && ea.cmdidx != CMD_endfor
1842 && ea.cmdidx != CMD_endtry
1843 && ea.cmdidx != CMD_endwhile)
1844 skip = ea.skip;
1845
1846 if (!skip)
1847 {
1848 if (getline_equal(fgetline, cookie, get_func_line))
1849 func_line_exec(getline_cookie(fgetline, cookie));
1850 else if (getline_equal(fgetline, cookie, getsourceline))
1851 script_line_exec();
1852 }
1853 }
1854# endif
1855
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001856 // May go to debug mode. If this happens and the ">quit" debug command is
1857 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001858 dbg_check_breakpoint(&ea);
1859 if (!ea.skip && got_int)
1860 {
1861 ea.skip = TRUE;
1862 (void)do_intthrow(cstack);
1863 }
1864#endif
1865
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001866/*
1867 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 *
1869 * where 'addr' is:
1870 *
1871 * % (entire file)
1872 * $ [+-NUM]
1873 * 'x [+-NUM] (where x denotes a currently defined mark)
1874 * . [+-NUM]
1875 * [+-NUM]..
1876 * NUM
1877 *
1878 * The ea.cmd pointer is updated to point to the first character following the
1879 * range spec. If an initial address is found, but no second, the upper bound
1880 * is equal to the lower.
1881 */
1882
Bram Moolenaar25190db2019-05-04 15:05:28 +02001883 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001884 if (!IS_USER_CMDIDX(ea.cmdidx))
1885 {
1886 if (ea.cmdidx != CMD_SIZE)
1887 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1888 else
1889 ea.addr_type = ADDR_LINES;
1890
Bram Moolenaar25190db2019-05-04 15:05:28 +02001891 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001892 if (ea.cmdidx == CMD_wincmd && p != NULL)
1893 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001894#ifdef FEAT_QUICKFIX
1895 // :.cc in quickfix window uses line number
1896 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1897 ea.addr_type = ADDR_OTHER;
1898#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001899 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001900
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001901 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001902#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001903 if (!may_have_range)
1904 ea.line1 = ea.line2 = default_address(&ea);
1905 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001906#endif
1907 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1908 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001911 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 */
1913
1914 /*
1915 * Skip ':' and any white space
1916 */
1917 ea.cmd = skipwhite(ea.cmd);
1918 while (*ea.cmd == ':')
1919 ea.cmd = skipwhite(ea.cmd + 1);
1920
1921 /*
1922 * If we got a line, but no command, then go to the line.
1923 * If we find a '|' or '\n' we set ea.nextcmd.
1924 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001925 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1926 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 {
1928 /*
1929 * strange vi behaviour:
1930 * ":3" jumps to line 3
1931 * ":3|..." prints line 3
1932 * ":|" prints current line
1933 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001934 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001936 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 {
1938 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001939 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 if ((errormsg = invalid_range(&ea)) == NULL)
1941 {
1942 correct_range(&ea);
1943 ex_print(&ea);
1944 }
1945 }
1946 else if (ea.addr_count != 0)
1947 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001948 if (ea.line2 > curbuf->b_ml.ml_line_count)
1949 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001950 // With '-' in 'cpoptions' a line number past the file is an
1951 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001952 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1953 ea.line2 = -1;
1954 else
1955 ea.line2 = curbuf->b_ml.ml_line_count;
1956 }
1957
1958 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001959 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 else
1961 {
1962 if (ea.line2 == 0)
1963 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 else
1965 curwin->w_cursor.lnum = ea.line2;
1966 beginline(BL_SOL | BL_FIX);
1967 }
1968 }
1969 goto doend;
1970 }
1971
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001972 // If this looks like an undefined user command and there are CmdUndefined
1973 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001974 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1975 && ASCII_ISUPPER(*ea.cmd)
1976 && has_cmdundefined())
1977 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001978 int ret;
1979
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001980 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001981 while (ASCII_ISALNUM(*p))
1982 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001983 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001984 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1985 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001986 // If the autocommands did something and didn't cause an error, try
1987 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001988 p = (ret
1989#ifdef FEAT_EVAL
1990 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001991#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001993 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001994
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 if (p == NULL)
1996 {
1997 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001998 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 goto doend;
2000 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002001 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002002 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2003 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 {
2005 errormsg = uc_fun_cmd();
2006 goto doend;
2007 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002008
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 if (ea.cmdidx == CMD_SIZE)
2010 {
2011 if (!ea.skip)
2012 {
2013 STRCPY(IObuff, _("E492: Not an editor command"));
2014 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002015 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002016 // If the modifier was parsed OK the error must be in the
2017 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002018 if (after_modifier != NULL)
2019 append_command(after_modifier);
2020 else
2021 append_command(*cmdlinep);
2022 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002023 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002024 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 }
2026 goto doend;
2027 }
2028
Bram Moolenaar958636c2014-10-21 20:01:58 +02002029 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2030 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002031#ifdef HAVE_EX_SCRIPT_NI
2032 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2033#endif
2034 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035
2036#ifndef FEAT_EVAL
2037 /*
2038 * When the expression evaluation is disabled, recognize the ":if" and
2039 * ":endif" commands and ignore everything in between it.
2040 */
2041 if (ea.cmdidx == CMD_if)
2042 ++if_level;
2043 if (if_level)
2044 {
2045 if (ea.cmdidx == CMD_endif)
2046 --if_level;
2047 goto doend;
2048 }
2049
2050#endif
2051
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002052 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002053 if (*p == '!' && ea.cmdidx != CMD_substitute
2054 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 {
2056 ++p;
2057 ea.forceit = TRUE;
2058 }
2059 else
2060 ea.forceit = FALSE;
2061
2062/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002063 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002065 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002066 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067
2068 if (!ea.skip)
2069 {
2070#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002071 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002073 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002074 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 goto doend;
2076 }
2077#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002078 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002079 {
2080 errormsg = _("E981: Command not allowed in rvim");
2081 goto doend;
2082 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002083 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002085 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002086 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 goto doend;
2088 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002089
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002090 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002092#ifdef FEAT_CMDWIN
2093 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2094 {
2095 // Command not allowed in the command line window
2096 errormsg = _(e_cmdwin);
2097 goto doend;
2098 }
2099#endif
2100 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2101 {
2102 // Command not allowed when text is locked
2103 errormsg = _(get_text_locked_msg());
2104 goto doend;
2105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002107
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002108 // Disallow editing another buffer when "curbuf_lock" is set.
2109 // Do allow ":checktime" (it is postponed).
2110 // Do allow ":edit" (check for an argument later).
2111 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002112 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002113 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002114 && ea.cmdidx != CMD_edit
2115 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002116 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002117 && curbuf_locked())
2118 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002120 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002122 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002123 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 goto doend;
2125 }
2126 }
2127
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002128 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002130 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 goto doend;
2132 }
2133
2134 /*
2135 * Don't complain about the range if it is not used
2136 * (could happen if line_count is accidentally set to 0).
2137 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002138 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 {
2140 /*
2141 * If the range is backwards, ask for confirmation and, if given, swap
2142 * ea.line1 & ea.line2 so it's forwards again.
2143 * When global command is busy, don't ask, will fail below.
2144 */
2145 if (!global_busy && ea.line1 > ea.line2)
2146 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002147 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002149 if (sourcing || exmode_active)
2150 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002151 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002152 goto doend;
2153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 if (ask_yesno((char_u *)
2155 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002156 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 }
2158 lnum = ea.line1;
2159 ea.line1 = ea.line2;
2160 ea.line2 = lnum;
2161 }
2162 if ((errormsg = invalid_range(&ea)) != NULL)
2163 goto doend;
2164 }
2165
Bram Moolenaarb7316892019-05-01 18:08:42 +02002166 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2167 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 ea.line2 = 1;
2169
2170 correct_range(&ea);
2171
2172#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002173 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002174 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002176 // Put the first line at the start of a closed fold, put the last line
2177 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 (void)hasFolding(ea.line1, &ea.line1, NULL);
2179 (void)hasFolding(ea.line2, NULL, &ea.line2);
2180 }
2181#endif
2182
2183#ifdef FEAT_QUICKFIX
2184 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002185 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 * option here, so things like % get expanded.
2187 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002188 p = replace_makeprg(&ea, p, cmdlinep);
2189 if (p == NULL)
2190 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191#endif
2192
2193 /*
2194 * Skip to start of argument.
2195 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2196 */
2197 if (ea.cmdidx == CMD_bang)
2198 ea.arg = p;
2199 else
2200 ea.arg = skipwhite(p);
2201
Bram Moolenaar379fb762018-08-30 15:58:28 +02002202 // ":file" cannot be run with an argument when "curbuf_lock" is set
2203 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2204 goto doend;
2205
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 /*
2207 * Check for "++opt=val" argument.
2208 * Must be first, allow ":w ++enc=utf8 !cmd"
2209 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002210 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2212 if (getargopt(&ea) == FAIL && !ni)
2213 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002214 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 goto doend;
2216 }
2217
2218 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2219 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002220 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002222 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002224 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 goto doend;
2226 }
2227 ea.arg = skipwhite(ea.arg + 1);
2228 ea.append = TRUE;
2229 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002230 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 {
2232 ++ea.arg;
2233 ea.usefilter = TRUE;
2234 }
2235 }
2236
2237 if (ea.cmdidx == CMD_read)
2238 {
2239 if (ea.forceit)
2240 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002241 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 ea.forceit = FALSE;
2243 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002244 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 {
2246 ++ea.arg;
2247 ea.usefilter = TRUE;
2248 }
2249 }
2250
2251 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2252 {
2253 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002254 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 {
2256 ++ea.arg;
2257 ++ea.amount;
2258 }
2259 ea.arg = skipwhite(ea.arg);
2260 }
2261
2262 /*
2263 * Check for "+command" argument, before checking for next command.
2264 * Don't do this for ":read !cmd" and ":write !cmd".
2265 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002266 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2268
2269 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002270 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 * Don't do this for ":read !cmd" and ":write !cmd".
2272 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002273 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 separate_nextcmd(&ea);
2275
2276 /*
2277 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002278 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2279 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002281 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002282 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002283 || ea.cmdidx == CMD_global
2284 || ea.cmdidx == CMD_vglobal
2285 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 {
2287 for (p = ea.arg; *p; ++p)
2288 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002289 // Remove one backslash before a newline, so that it's possible to
2290 // pass a newline to the shell and also a newline that is preceded
2291 // with a backslash. This makes it impossible to end a shell
2292 // command in a backslash, but that doesn't appear useful.
2293 // Halving the number of backslashes is incompatible with previous
2294 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002296 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 else if (*p == '\n')
2298 {
2299 ea.nextcmd = p + 1;
2300 *p = NUL;
2301 break;
2302 }
2303 }
2304 }
2305
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002306 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002307 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002309 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002310 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002312 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002313 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002314 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002316#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002317 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002318 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002320 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002321 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 }
2323#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002324 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2325 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002326 {
2327 ea.regname = *ea.arg++;
2328#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002329 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002330 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2331 {
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002332 set_expr_line(vim_strsave(ea.arg), &ea);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002333 ea.arg += STRLEN(ea.arg);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002334 did_set_expr_line = TRUE;
Bram Moolenaar85de2062011-05-05 14:26:41 +02002335 }
2336#endif
2337 ea.arg = skipwhite(ea.arg);
2338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 }
2340
2341 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002342 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 * count, it's a buffer name.
2344 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002345 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2346 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002347 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
2349 n = getdigits(&ea.arg);
2350 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002351 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002353 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 goto doend;
2355 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002356 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
2358 ea.line2 = n;
2359 if (ea.addr_count == 0)
2360 ea.addr_count = 1;
2361 }
2362 else
2363 {
2364 ea.line1 = ea.line2;
2365 ea.line2 += n - 1;
2366 ++ea.addr_count;
2367 /*
2368 * Be vi compatible: no error message for out of range.
2369 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002370 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 ea.line2 = curbuf->b_ml.ml_line_count;
2372 }
2373 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002374
2375 /*
2376 * Check for flags: 'l', 'p' and '#'.
2377 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002378 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002379 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002380 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2381 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002383 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002384 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 goto doend;
2386 }
2387
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002388 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002390 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 goto doend;
2392 }
2393
2394#ifdef FEAT_EVAL
2395 /*
2396 * Skip the command when it's not going to be executed.
2397 * The commands like :if, :endif, etc. always need to be executed.
2398 * Also make an exception for commands that handle a trailing command
2399 * themselves.
2400 */
2401 if (ea.skip)
2402 {
2403 switch (ea.cmdidx)
2404 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002405 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 case CMD_while:
2407 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002408 case CMD_for:
2409 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 case CMD_if:
2411 case CMD_elseif:
2412 case CMD_else:
2413 case CMD_endif:
2414 case CMD_try:
2415 case CMD_catch:
2416 case CMD_finally:
2417 case CMD_endtry:
2418 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002419 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 break;
2421
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002422 // Commands that handle '|' themselves. Check: A command should
2423 // either have the EX_TRLBAR flag, appear in this list or appear in
2424 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 case CMD_aboveleft:
2426 case CMD_and:
2427 case CMD_belowright:
2428 case CMD_botright:
2429 case CMD_browse:
2430 case CMD_call:
2431 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002432 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 case CMD_delfunction:
2434 case CMD_djump:
2435 case CMD_dlist:
2436 case CMD_dsearch:
2437 case CMD_dsplit:
2438 case CMD_echo:
2439 case CMD_echoerr:
2440 case CMD_echomsg:
2441 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002442 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002444 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002445 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 case CMD_help:
2447 case CMD_hide:
2448 case CMD_ijump:
2449 case CMD_ilist:
2450 case CMD_isearch:
2451 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002452 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 case CMD_keepjumps:
2454 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002455 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 case CMD_leftabove:
2457 case CMD_let:
2458 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002459 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002460 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002462 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002463 case CMD_noautocmd:
2464 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 case CMD_perl:
2466 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002467 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002468 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002469 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 case CMD_return:
2471 case CMD_rightbelow:
2472 case CMD_ruby:
2473 case CMD_silent:
2474 case CMD_smagic:
2475 case CMD_snomagic:
2476 case CMD_substitute:
2477 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002478 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 case CMD_tcl:
2480 case CMD_throw:
2481 case CMD_tilde:
2482 case CMD_topleft:
2483 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002484 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002485 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 case CMD_verbose:
2487 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002488 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 break;
2490
2491 default: goto doend;
2492 }
2493 }
2494#endif
2495
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002496 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 {
2498 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2499 goto doend;
2500 }
2501
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 /*
2503 * Accept buffer name. Cannot be used at the same time with a buffer
2504 * number. Don't do this for a user command.
2505 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002506 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002507 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 {
2509 /*
2510 * :bdelete, :bwipeout and :bunload take several arguments, separated
2511 * by spaces: find next space (skipping over escaped characters).
2512 * The others take one argument: ignore trailing spaces.
2513 */
2514 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2515 || ea.cmdidx == CMD_bunload)
2516 p = skiptowhite_esc(ea.arg);
2517 else
2518 {
2519 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002520 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 --p;
2522 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002523 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002524 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002525 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 goto doend;
2527 ea.addr_count = 1;
2528 ea.arg = skipwhite(p);
2529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002531 // The :try command saves the emsg_silent flag, reset it here when
2532 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002533 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002534 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002535 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002536 if (emsg_silent < 0)
2537 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002538 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002539 }
2540
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002542 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
Bram Moolenaar958636c2014-10-21 20:01:58 +02002545 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 {
2547 /*
2548 * Execute a user-defined command.
2549 */
2550 do_ucmd(&ea);
2551 }
2552 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 {
2554 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002555 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 */
2557 ea.errmsg = NULL;
2558 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2559 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002560 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 }
2562
2563#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002565 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2566 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002567 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 /*
2570 * If the command just executed called do_cmdline(), any throw or ":return"
2571 * or ":finish" encountered there must also check the cstack of the still
2572 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2573 * exception, or reanimate a returned function or finished script file and
2574 * return or finish it again.
2575 */
2576 if (need_rethrow)
2577 do_throw(cstack);
2578 else if (check_cstack)
2579 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002580 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002582 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 && current_func_returned())
2584 do_return(&ea, TRUE, FALSE, NULL);
2585 }
2586 need_rethrow = check_cstack = FALSE;
2587#endif
2588
2589doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002590 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002591 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002593 curwin->w_cursor.col = 0;
2594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595
2596 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2597 {
2598 if (sourcing)
2599 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002600 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 {
2602 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002603 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002605 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 }
2607 emsg(errormsg);
2608 }
2609#ifdef FEAT_EVAL
2610 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002611 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2612 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002613
2614 if (did_set_expr_line)
2615 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616#endif
2617
Bram Moolenaare1004402020-10-24 20:49:43 +02002618 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002620 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002622 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 ea.nextcmd = NULL;
2624
2625#ifdef FEAT_EVAL
2626 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002627 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628#endif
2629
2630 return ea.nextcmd;
2631}
2632#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002633 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634#endif
2635
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002636static char ex_error_buf[MSG_BUF_LEN];
2637
2638/*
2639 * Return an error message with argument included.
2640 * Uses a static buffer, only the last error will be kept.
2641 * "msg" will be translated, caller should use N_().
2642 */
2643 char *
2644ex_errmsg(char *msg, char_u *arg)
2645{
2646 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2647 return ex_error_buf;
2648}
2649
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002651 * Parse and skip over command modifiers:
2652 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002653 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002654 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002655 * When "skip_only" is TRUE the global variables are not changed, except for
2656 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002657 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2658 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002659 * Call apply_cmdmod() to get the side effects of the modifiers:
2660 * - Increment "sandbox" for ":sandbox"
2661 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002662 * - set msg_silent for ":silent"
2663 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002664 * Return FAIL when the command is not to be executed.
2665 * May set "errormsg" to an error message.
2666 */
2667 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002668parse_command_modifiers(
2669 exarg_T *eap,
2670 char **errormsg,
2671 cmdmod_T *cmod,
2672 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002673{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002674 char_u *p;
2675 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002676
Bram Moolenaare1004402020-10-24 20:49:43 +02002677 CLEAR_POINTER(cmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002678
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002679 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002680 for (;;)
2681 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002682 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002683 {
2684 if (*eap->cmd == ':')
2685 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002686 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002687 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002688
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002689 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002690 if (*eap->cmd == NUL && exmode_active
2691 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2692 || getline_equal(eap->getline, eap->cookie, getexline))
2693 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2694 {
2695 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002696 if (!skip_only)
2697 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002698 }
2699
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002700 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002701 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002702 return FAIL;
2703 if (*eap->cmd == NUL)
2704 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002705 if (!skip_only)
2706 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002707 return FAIL;
2708 }
2709
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002710 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002711 switch (*p)
2712 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002713 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002714 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2715 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002716 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002717 continue;
2718
2719 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2720 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002721 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002722 continue;
2723 }
2724 if (checkforcmd(&eap->cmd, "browse", 3))
2725 {
2726#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002727 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002728#endif
2729 continue;
2730 }
2731 if (!checkforcmd(&eap->cmd, "botright", 2))
2732 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002733 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002734 continue;
2735
2736 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2737 break;
2738#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002739 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002740#endif
2741 continue;
2742
2743 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2744 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002745 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002746 continue;
2747 }
2748 if (checkforcmd(&eap->cmd, "keepalt", 5))
2749 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002750 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002751 continue;
2752 }
2753 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2754 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002755 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002756 continue;
2757 }
2758 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2759 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002760 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002761 continue;
2762
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002763 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002764 {
2765 char_u *reg_pat;
2766
2767 if (!checkforcmd(&p, "filter", 4)
2768 || *p == NUL || ends_excmd(*p))
2769 break;
2770 if (*p == '!')
2771 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002772 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002773 p = skipwhite(p + 1);
2774 if (*p == NUL || ends_excmd(*p))
2775 break;
2776 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002777#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002778 // Avoid that "filter(arg)" is recognized.
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +01002779 if (in_vim9script() && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002780 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002781#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002782 if (skip_only)
2783 p = skip_vimgrep_pat(p, NULL, NULL);
2784 else
2785 // NOTE: This puts a NUL after the pattern.
2786 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002787 if (p == NULL || *p == NUL)
2788 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002789 if (!skip_only)
2790 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002791 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002792 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002793 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002794 break;
2795 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002796 eap->cmd = p;
2797 continue;
2798 }
2799
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002800 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002801 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2802 || *p == NUL || ends_excmd(*p))
2803 break;
2804 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002805 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002806 continue;
2807
2808 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2809 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002810 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002811 continue;
2812 }
2813
2814 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2815 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002816 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002817 continue;
2818
2819 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2820 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002821 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002822 continue;
2823 }
2824 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2825 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002826 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002827 continue;
2828
2829 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2830 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002831 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002832 continue;
2833
2834 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2835 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002836 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02002837 continue;
2838 }
2839 if (!checkforcmd(&eap->cmd, "silent", 3))
2840 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002841 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002842 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2843 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002844 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002845 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02002846 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002847 }
2848 continue;
2849
2850 case 't': if (checkforcmd(&p, "tab", 3))
2851 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002852 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002853 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002854 long tabnr = get_address(eap, &eap->cmd,
2855 ADDR_TABS, eap->skip,
2856 skip_only, FALSE, 1);
2857 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02002858 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01002859 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002860 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002861 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2862 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002863 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002864 return FAIL;
2865 }
Bram Moolenaare1004402020-10-24 20:49:43 +02002866 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002867 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002868 }
2869 eap->cmd = p;
2870 continue;
2871 }
2872 if (!checkforcmd(&eap->cmd, "topleft", 2))
2873 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002874 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02002875 continue;
2876
2877 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2878 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002879 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002880 continue;
2881
2882 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2883 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002884 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002885 continue;
2886 }
2887 if (!checkforcmd(&p, "verbose", 4))
2888 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002889 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02002890 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002891 else
Bram Moolenaare1004402020-10-24 20:49:43 +02002892 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002893 eap->cmd = p;
2894 continue;
2895 }
2896 break;
2897 }
2898
2899 return OK;
2900}
2901
2902/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002903 * Apply the command modifiers. Saves current state in "cmdmod", call
2904 * undo_cmdmod() later.
2905 */
2906 void
2907apply_cmdmod(cmdmod_T *cmod)
2908{
2909#ifdef HAVE_SANDBOX
2910 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
2911 {
2912 ++sandbox;
2913 cmod->cmod_did_sandbox = TRUE;
2914 }
2915#endif
2916 if (cmod->cmod_verbose > 0)
2917 {
2918 if (cmod->cmod_verbose_save == 0)
2919 cmod->cmod_verbose_save = p_verbose + 1;
2920 p_verbose = cmod->cmod_verbose;
2921 }
2922
2923 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
2924 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02002925 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002926 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02002927 cmod->cmod_save_msg_scroll = msg_scroll;
2928 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002929 if (cmod->cmod_flags & CMOD_SILENT)
2930 ++msg_silent;
2931 if (cmod->cmod_flags & CMOD_UNSILENT)
2932 msg_silent = 0;
2933
2934 if (cmod->cmod_flags & CMOD_ERRSILENT)
2935 {
2936 ++emsg_silent;
2937 ++cmod->cmod_did_esilent;
2938 }
2939
Bram Moolenaare1004402020-10-24 20:49:43 +02002940 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002941 {
2942 // Set 'eventignore' to "all".
2943 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02002944 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002945 set_string_option_direct((char_u *)"ei", -1,
2946 (char_u *)"all", OPT_FREE, SID_NONE);
2947 }
2948}
2949
2950/*
Bram Moolenaare1004402020-10-24 20:49:43 +02002951 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002952 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002953 void
Bram Moolenaare1004402020-10-24 20:49:43 +02002954undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002955{
Bram Moolenaare1004402020-10-24 20:49:43 +02002956 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002957 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002958 p_verbose = cmod->cmod_verbose_save - 1;
2959 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002960 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002961
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002962#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02002963 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002964 {
2965 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02002966 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002967 }
2968#endif
2969
Bram Moolenaare1004402020-10-24 20:49:43 +02002970 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002971 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002972 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02002973 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
2974 OPT_FREE, SID_NONE);
2975 free_string_option(cmod->cmod_save_ei);
2976 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002977 }
2978
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01002979 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002980
Bram Moolenaare1004402020-10-24 20:49:43 +02002981 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002982 {
2983 // messages could be enabled for a serious error, need to check if the
2984 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02002985 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
2986 msg_silent = cmod->cmod_save_msg_silent - 1;
2987 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002988 if (emsg_silent < 0)
2989 emsg_silent = 0;
2990 // Restore msg_scroll, it's set by file I/O commands, even when no
2991 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02002992 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002993
2994 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2995 // somewhere in the line. Put it back in the first column.
2996 if (redirecting())
2997 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002998
Bram Moolenaare1004402020-10-24 20:49:43 +02002999 cmod->cmod_save_msg_silent = 0;
3000 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003001 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003002}
3003
3004/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003005 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003006 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003007 * Return FAIL and set "errormsg" or return OK.
3008 */
3009 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003010parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003011{
3012 int address_count = 1;
3013 linenr_T lnum;
3014
3015 // Repeat for all ',' or ';' separated addresses.
3016 for (;;)
3017 {
3018 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003019 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003020 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003021 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003022 eap->addr_count == 0, address_count++);
3023 if (eap->cmd == NULL) // error detected
3024 return FAIL;
3025 if (lnum == MAXLNUM)
3026 {
3027 if (*eap->cmd == '%') // '%' - all lines
3028 {
3029 ++eap->cmd;
3030 switch (eap->addr_type)
3031 {
3032 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003033 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003034 eap->line1 = 1;
3035 eap->line2 = curbuf->b_ml.ml_line_count;
3036 break;
3037 case ADDR_LOADED_BUFFERS:
3038 {
3039 buf_T *buf = firstbuf;
3040
3041 while (buf->b_next != NULL
3042 && buf->b_ml.ml_mfp == NULL)
3043 buf = buf->b_next;
3044 eap->line1 = buf->b_fnum;
3045 buf = lastbuf;
3046 while (buf->b_prev != NULL
3047 && buf->b_ml.ml_mfp == NULL)
3048 buf = buf->b_prev;
3049 eap->line2 = buf->b_fnum;
3050 break;
3051 }
3052 case ADDR_BUFFERS:
3053 eap->line1 = firstbuf->b_fnum;
3054 eap->line2 = lastbuf->b_fnum;
3055 break;
3056 case ADDR_WINDOWS:
3057 case ADDR_TABS:
3058 if (IS_USER_CMDIDX(eap->cmdidx))
3059 {
3060 eap->line1 = 1;
3061 eap->line2 = eap->addr_type == ADDR_WINDOWS
3062 ? LAST_WIN_NR : LAST_TAB_NR;
3063 }
3064 else
3065 {
3066 // there is no Vim command which uses '%' and
3067 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003068 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003069 return FAIL;
3070 }
3071 break;
3072 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003073 case ADDR_UNSIGNED:
3074 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003075 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003076 return FAIL;
3077 case ADDR_ARGUMENTS:
3078 if (ARGCOUNT == 0)
3079 eap->line1 = eap->line2 = 0;
3080 else
3081 {
3082 eap->line1 = 1;
3083 eap->line2 = ARGCOUNT;
3084 }
3085 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003086 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003087#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003088 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003089 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003090 if (eap->line2 == 0)
3091 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003092#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003093 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003094 case ADDR_NONE:
3095 // Will give an error later if a range is found.
3096 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003097 }
3098 ++eap->addr_count;
3099 }
3100 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3101 {
3102 pos_T *fp;
3103
3104 // '*' - visual area
3105 if (eap->addr_type != ADDR_LINES)
3106 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003107 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003108 return FAIL;
3109 }
3110
3111 ++eap->cmd;
3112 if (!eap->skip)
3113 {
3114 fp = getmark('<', FALSE);
3115 if (check_mark(fp) == FAIL)
3116 return FAIL;
3117 eap->line1 = fp->lnum;
3118 fp = getmark('>', FALSE);
3119 if (check_mark(fp) == FAIL)
3120 return FAIL;
3121 eap->line2 = fp->lnum;
3122 ++eap->addr_count;
3123 }
3124 }
3125 }
3126 else
3127 eap->line2 = lnum;
3128 eap->addr_count++;
3129
3130 if (*eap->cmd == ';')
3131 {
3132 if (!eap->skip)
3133 {
3134 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003135 // Don't leave the cursor on an illegal line or column, but do
3136 // accept zero as address, so 0;/PATTERN/ works correctly.
3137 if (eap->line2 > 0)
3138 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003139 }
3140 }
3141 else if (*eap->cmd != ',')
3142 break;
3143 ++eap->cmd;
3144 }
3145
3146 // One address given: set start and end lines.
3147 if (eap->addr_count == 1)
3148 {
3149 eap->line1 = eap->line2;
3150 // ... but only implicit: really no address given
3151 if (lnum == MAXLNUM)
3152 eap->addr_count = 0;
3153 }
3154 return OK;
3155}
3156
3157/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003158 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 * If there is a match advance "pp" to the argument and return TRUE.
3160 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003161 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003162checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003163 char_u **pp, // start of command
3164 char *cmd, // name of command
3165 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166{
3167 int i;
3168
3169 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003170 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 break;
Bram Moolenaar7a66a172020-10-16 19:56:12 +02003172 if (i >= len && !isalpha((*pp)[i]) && (*pp)[i] != '_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 {
3174 *pp = skipwhite(*pp + i);
3175 return TRUE;
3176 }
3177 return FALSE;
3178}
3179
3180/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003181 * Append "cmd" to the error message in IObuff.
3182 * Takes care of limiting the length and handling 0xa0, which would be
3183 * invisible otherwise.
3184 */
3185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003186append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003187{
3188 char_u *s = cmd;
3189 char_u *d;
3190
3191 STRCAT(IObuff, ": ");
3192 d = IObuff + STRLEN(IObuff);
3193 while (*s != NUL && d - IObuff < IOSIZE - 7)
3194 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003195 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003196 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003197 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003198 STRCPY(d, "<a0>");
3199 d += 4;
3200 }
3201 else
3202 MB_COPY_CHAR(s, d);
3203 }
3204 *d = NUL;
3205}
3206
3207/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003208 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3209 * the name. Otherwise just return "start".
3210 */
3211 char_u *
3212skip_option_env_lead(char_u *start)
3213{
3214 char_u *name = start;
3215
3216 if (*start == '&')
3217 {
3218 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3219 name += 3;
3220 else
3221 name += 1;
3222 }
3223 else if (*start == '$')
3224 name += 1;
3225 return name;
3226}
3227
3228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003230 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003231 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003232 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 *
3234 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3235 * assignment without "let". Sets eap->cmdidx to the command while returning
3236 * "eap->cmd".
3237 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 * Returns NULL for an ambiguous user command.
3239 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 char_u *
3241find_ex_command(
3242 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003243 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003244 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003245 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246{
3247 int len;
3248 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003249 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251#ifdef FEAT_EVAL
3252 /*
3253 * Recognize a Vim9 script function/method call and assignment:
3254 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3255 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003256 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003257 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003259 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260
Bram Moolenaar83144542020-08-02 20:40:43 +02003261 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003262 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003264 int oplen;
3265 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003267 if (
3268 // "(..." is an expression.
3269 // "funcname(" is always a function call.
3270 *p == '('
3271 || (p == eap->cmd
3272 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003273 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003274 *eap->cmd == '{'
3275 // "'string'->func()" is an expression.
3276 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003277 // '"string"->func()' is an expression.
3278 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003279 // "g:varname" is an expression.
3280 || eap->cmd[1] == ':'
3281 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003282 // "varname->func()" is an expression.
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003283 : (*p == '-' && p[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003284 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003285 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3286 {
3287 // "{" by itself is the start of a block.
3288 eap->cmdidx = CMD_block;
3289 return eap->cmd + 1;
3290 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003291 eap->cmdidx = CMD_eval;
3292 return eap->cmd;
3293 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003294
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003295 if (p != eap->cmd && (
3296 // "varname[]" is an expression.
3297 *p == '['
3298 // "varname.key" is an expression.
3299 || (*p == '.' && ASCII_ISALPHA(p[1]))))
3300 {
3301 char_u *after = p;
3302
3303 // When followed by "=" or "+=" then it is an assignment.
3304 ++emsg_silent;
Bram Moolenaar683581e2020-10-22 21:22:58 +02003305 if (skip_expr(&after, NULL) == OK
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003306 && (*after == '='
3307 || (*after != NUL && after[1] == '=')))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003308 eap->cmdidx = CMD_var;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003309 else
3310 eap->cmdidx = CMD_eval;
3311 --emsg_silent;
3312 return eap->cmd;
3313 }
3314
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003315 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3316 // an assignment.
3317 // If there is no line break inside the "[...]" then "p" is
3318 // advanced to after the "]" by to_name_const_end(): check if a "="
3319 // follows.
3320 // If "[...]" has a line break "p" still points at the "[" and it
3321 // can't be an assignment.
3322 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003323 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003324 p = to_name_const_end(eap->cmd);
3325 if (p == eap->cmd || *skipwhite(p) != '=')
3326 {
3327 eap->cmdidx = CMD_eval;
3328 return eap->cmd;
3329 }
3330 if (p > eap->cmd && *skipwhite(p) == '=')
3331 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003332 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003333 return eap->cmd;
3334 }
3335 }
3336
3337 // Recognize an assignment if we recognize the variable name:
3338 // "g:var = expr"
3339 // "var = expr" where "var" is a local var name.
Bram Moolenaar83144542020-08-02 20:40:43 +02003340 if (*eap->cmd == '@')
3341 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003342 oplen = assignment_len(skipwhite(p), &heredoc);
3343 if (oplen > 0)
3344 {
3345 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3346 || *eap->cmd == '&'
3347 || *eap->cmd == '$'
3348 || *eap->cmd == '@'
3349 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
3350 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003351 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003352 return eap->cmd;
3353 }
3354 }
3355
3356 // Recognize using a type for a w:, b:, t: or g: variable:
3357 // "w:varname: number = 123".
3358 if (eap->cmd[1] == ':' && *p == ':')
3359 {
3360 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003361 return eap->cmd;
3362 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003363 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364 }
3365#endif
3366
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 /*
3368 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003369 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 * - the 'k' command can directly be followed by any character.
3371 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003372 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003374 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 */
3376 p = eap->cmd;
3377 if (*p == 'k')
3378 {
3379 eap->cmdidx = CMD_k;
3380 ++p;
3381 }
3382 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003383 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3384 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 || p[1] == 'g'
3386 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3387 || p[1] == 'I'
3388 || (p[1] == 'r' && p[2] != 'e')))
3389 {
3390 eap->cmdidx = CMD_substitute;
3391 ++p;
3392 }
3393 else
3394 {
3395 while (ASCII_ISALPHA(*p))
3396 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003397 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003398 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003400 while (ASCII_ISALNUM(*p))
3401 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 }
3403 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3404 {
3405 // include "9" for "vim9script"
3406 ++p;
3407 while (ASCII_ISALPHA(*p))
3408 ++p;
3409 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003410
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003411 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003412 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 ++p;
3414 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003415 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3416 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003417 // Check for ":dl", ":dell", etc. to ":deletel": that's
3418 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003419 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003420 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003421 break;
3422 if (i == len - 1)
3423 {
3424 --len;
3425 if (p[-1] == 'l')
3426 eap->flags |= EXFLAG_LIST;
3427 else
3428 eap->flags |= EXFLAG_PRINT;
3429 }
3430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003432 if (ASCII_ISLOWER(eap->cmd[0]))
3433 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003434 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003435 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003436
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003437 if (command_count != (int)CMD_SIZE)
3438 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003439 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003440 getout(1);
3441 }
3442
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003443 // Use a precomputed index for fast look-up in cmdnames[]
3444 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003445 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3446 if (ASCII_ISLOWER(c2))
3447 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3448 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003449 else if (ASCII_ISUPPER(eap->cmd[0]))
3450 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003452 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
3454 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3455 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3456 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3457 (size_t)len) == 0)
3458 {
3459#ifdef FEAT_EVAL
3460 if (full != NULL
3461 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3462 *full = TRUE;
3463#endif
3464 break;
3465 }
3466
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003467 // Look for a user defined command as a last resort. Let ":Print" be
3468 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003469 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3470 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003472 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 while (ASCII_ISALNUM(*p))
3474 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003475 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003477 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 eap->cmdidx = CMD_SIZE;
3479 }
3480
Bram Moolenaar373863e2020-09-26 17:20:53 +02003481 // ":fina" means ":finally" for backwards compatibility.
3482 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3483 eap->cmdidx = CMD_finally;
3484
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 return p;
3486}
3487
3488#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003489static struct cmdmod
3490{
3491 char *name;
3492 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003493 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003494} cmdmods[] = {
3495 {"aboveleft", 3, FALSE},
3496 {"belowright", 3, FALSE},
3497 {"botright", 2, FALSE},
3498 {"browse", 3, FALSE},
3499 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003500 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003501 {"hide", 3, FALSE},
3502 {"keepalt", 5, FALSE},
3503 {"keepjumps", 5, FALSE},
3504 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003505 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003506 {"leftabove", 5, FALSE},
3507 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003508 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003509 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003510 {"rightbelow", 6, FALSE},
3511 {"sandbox", 3, FALSE},
3512 {"silent", 3, FALSE},
3513 {"tab", 3, TRUE},
3514 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003515 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003516 {"verbose", 4, TRUE},
3517 {"vertical", 4, FALSE},
3518};
3519
3520/*
3521 * Return length of a command modifier (including optional count).
3522 * Return zero when it's not a modifier.
3523 */
3524 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003525modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003526{
3527 int i, j;
3528 char_u *p = cmd;
3529
3530 if (VIM_ISDIGIT(*cmd))
3531 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003532 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003533 {
3534 for (j = 0; p[j] != NUL; ++j)
3535 if (p[j] != cmdmods[i].name[j])
3536 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003537 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003538 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003539 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003540 }
3541 return 0;
3542}
3543
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544/*
3545 * Return > 0 if an Ex command "name" exists.
3546 * Return 2 if there is an exact match.
3547 * Return 3 if there is an ambiguous match.
3548 */
3549 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003550cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551{
3552 exarg_T ea;
3553 int full = FALSE;
3554 int i;
3555 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003556 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003558 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003559 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 {
3561 for (j = 0; name[j] != NUL; ++j)
3562 if (name[j] != cmdmods[i].name[j])
3563 break;
3564 if (name[j] == NUL && j >= cmdmods[i].minlen)
3565 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3566 }
3567
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003568 // Check built-in commands and user defined commands.
3569 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003570 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003573 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003575 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3576 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003577 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003578 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3580}
3581#endif
3582
Bram Moolenaard0190392019-08-23 21:17:35 +02003583 cmdidx_T
3584excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585{
Bram Moolenaard0190392019-08-23 21:17:35 +02003586 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587
Bram Moolenaard0190392019-08-23 21:17:35 +02003588 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3589 idx = (cmdidx_T)((int)idx + 1))
3590 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 break;
3592
Bram Moolenaard0190392019-08-23 21:17:35 +02003593 return idx;
3594}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
Bram Moolenaard0190392019-08-23 21:17:35 +02003596 long
3597excmd_get_argt(cmdidx_T idx)
3598{
3599 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600}
3601
3602/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003603 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 *
3605 * Backslashed delimiters after / or ? will be skipped, and commands will
3606 * not be expanded between /'s and ?'s or after "'".
3607 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003608 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 * Returns the "cmd" pointer advanced to beyond the range.
3610 */
3611 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003612skip_range(
3613 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003614 int skip_star, // skip "*" used for Visual range
3615 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003617 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003619 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003621 if (*cmd == '\\')
3622 {
3623 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3624 ++cmd;
3625 else
3626 break;
3627 }
3628 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 {
3630 if (*++cmd == NUL && ctx != NULL)
3631 *ctx = EXPAND_NOTHING;
3632 }
3633 else if (*cmd == '/' || *cmd == '?')
3634 {
3635 delim = *cmd++;
3636 while (*cmd != NUL && *cmd != delim)
3637 if (*cmd++ == '\\' && *cmd != NUL)
3638 ++cmd;
3639 if (*cmd == NUL && ctx != NULL)
3640 *ctx = EXPAND_NOTHING;
3641 }
3642 if (*cmd != NUL)
3643 ++cmd;
3644 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003645
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003646 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003647 while (*cmd == ':')
3648 cmd = skipwhite(cmd + 1);
3649
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003650 // Skip "*" used for Visual range.
3651 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3652 cmd = skipwhite(cmd + 1);
3653
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 return cmd;
3655}
3656
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003657 static void
3658addr_error(cmd_addr_T addr_type)
3659{
3660 if (addr_type == ADDR_NONE)
3661 emsg(_(e_norange));
3662 else
3663 emsg(_(e_invrange));
3664}
3665
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003667 * Return the default address for an address type.
3668 */
3669 static linenr_T
3670default_address(exarg_T *eap)
3671{
3672 linenr_T lnum = 0;
3673
3674 switch (eap->addr_type)
3675 {
3676 case ADDR_LINES:
3677 case ADDR_OTHER:
3678 // Default is the cursor line number. Avoid using an invalid
3679 // line number though.
3680 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3681 lnum = curbuf->b_ml.ml_line_count;
3682 else
3683 lnum = curwin->w_cursor.lnum;
3684 break;
3685 case ADDR_WINDOWS:
3686 lnum = CURRENT_WIN_NR;
3687 break;
3688 case ADDR_ARGUMENTS:
3689 lnum = curwin->w_arg_idx + 1;
3690 if (lnum > ARGCOUNT)
3691 lnum = ARGCOUNT;
3692 break;
3693 case ADDR_LOADED_BUFFERS:
3694 case ADDR_BUFFERS:
3695 lnum = curbuf->b_fnum;
3696 break;
3697 case ADDR_TABS:
3698 lnum = CURRENT_TAB_NR;
3699 break;
3700 case ADDR_TABS_RELATIVE:
3701 case ADDR_UNSIGNED:
3702 lnum = 1;
3703 break;
3704 case ADDR_QUICKFIX:
3705#ifdef FEAT_QUICKFIX
3706 lnum = qf_get_cur_idx(eap);
3707#endif
3708 break;
3709 case ADDR_QUICKFIX_VALID:
3710#ifdef FEAT_QUICKFIX
3711 lnum = qf_get_cur_valid_idx(eap);
3712#endif
3713 break;
3714 case ADDR_NONE:
3715 // Will give an error later if a range is found.
3716 break;
3717 }
3718 return lnum;
3719}
3720
3721/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003722 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 *
3724 * Set ptr to the next character after the part that was interpreted.
3725 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003726 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 *
3728 * Return MAXLNUM when no Ex address was found.
3729 */
3730 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003731get_address(
3732 exarg_T *eap UNUSED,
3733 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003734 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003735 int skip, // only skip the address, don't use it
3736 int silent, // no errors or side effects
3737 int to_other_file, // flag: may jump to other file
3738 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739{
3740 int c;
3741 int i;
3742 long n;
3743 char_u *cmd;
3744 pos_T pos;
3745 pos_T *fp;
3746 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003747 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748
3749 cmd = skipwhite(*ptr);
3750 lnum = MAXLNUM;
3751 do
3752 {
3753 switch (*cmd)
3754 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003755 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003756 ++cmd;
3757 switch (addr_type)
3758 {
3759 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003760 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 lnum = curwin->w_cursor.lnum;
3762 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003763 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003764 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003765 break;
3766 case ADDR_ARGUMENTS:
3767 lnum = curwin->w_arg_idx + 1;
3768 break;
3769 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003770 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003771 lnum = curbuf->b_fnum;
3772 break;
3773 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003774 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003775 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003776 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003777 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003778 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003779 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003780 cmd = NULL;
3781 goto error;
3782 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003783 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003784#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003785 lnum = qf_get_cur_idx(eap);
3786#endif
3787 break;
3788 case ADDR_QUICKFIX_VALID:
3789#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003790 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003791#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003792 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003793 }
3794 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003796 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003797 ++cmd;
3798 switch (addr_type)
3799 {
3800 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003801 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 lnum = curbuf->b_ml.ml_line_count;
3803 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003804 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003805 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003806 break;
3807 case ADDR_ARGUMENTS:
3808 lnum = ARGCOUNT;
3809 break;
3810 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003811 buf = lastbuf;
3812 while (buf->b_ml.ml_mfp == NULL)
3813 {
3814 if (buf->b_prev == NULL)
3815 break;
3816 buf = buf->b_prev;
3817 }
3818 lnum = buf->b_fnum;
3819 break;
3820 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003821 lnum = lastbuf->b_fnum;
3822 break;
3823 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003824 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003825 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003826 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003827 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003828 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003829 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003830 cmd = NULL;
3831 goto error;
3832 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003833 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003834#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003835 lnum = qf_get_size(eap);
3836 if (lnum == 0)
3837 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003838#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003839 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003840 case ADDR_QUICKFIX_VALID:
3841#ifdef FEAT_QUICKFIX
3842 lnum = qf_get_valid_size(eap);
3843 if (lnum == 0)
3844 lnum = 1;
3845#endif
3846 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003847 }
3848 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003850 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003851 if (*++cmd == NUL)
3852 {
3853 cmd = NULL;
3854 goto error;
3855 }
3856 if (addr_type != ADDR_LINES)
3857 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003858 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003859 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003860 goto error;
3861 }
3862 if (skip)
3863 ++cmd;
3864 else
3865 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003866 // Only accept a mark in another file when it is
3867 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003868 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3869 ++cmd;
3870 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003871 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003872 lnum = curwin->w_cursor.lnum;
3873 else
3874 {
3875 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 {
3877 cmd = NULL;
3878 goto error;
3879 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003880 lnum = fp->lnum;
3881 }
3882 }
3883 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884
3885 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003886 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003887 c = *cmd++;
3888 if (addr_type != ADDR_LINES)
3889 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003890 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003891 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003892 goto error;
3893 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003894 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003895 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003896 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003897 if (*cmd == c)
3898 ++cmd;
3899 }
3900 else
3901 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003902 int flags;
3903
3904 pos = curwin->w_cursor; // save curwin->w_cursor
3905
3906 // When '/' or '?' follows another address, start from
3907 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003908 if (lnum != MAXLNUM)
3909 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003910
3911 // Start a forward search at the end of the line (unless
3912 // before the first line).
3913 // Start a backward search at the start of the line.
3914 // This makes sure we never match in the current
3915 // line, and can match anywhere in the
3916 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003917 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003918 curwin->w_cursor.col = MAXCOL;
3919 else
3920 curwin->w_cursor.col = 0;
3921 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003922 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003923 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003924 {
3925 curwin->w_cursor = pos;
3926 cmd = NULL;
3927 goto error;
3928 }
3929 lnum = curwin->w_cursor.lnum;
3930 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003931 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003932 cmd += searchcmdlen;
3933 }
3934 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003936 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003937 ++cmd;
3938 if (addr_type != ADDR_LINES)
3939 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003940 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003941 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003942 goto error;
3943 }
3944 if (*cmd == '&')
3945 i = RE_SUBST;
3946 else if (*cmd == '?' || *cmd == '/')
3947 i = RE_SEARCH;
3948 else
3949 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003950 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003951 cmd = NULL;
3952 goto error;
3953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003955 if (!skip)
3956 {
3957 /*
3958 * When search follows another address, start from
3959 * there.
3960 */
3961 if (lnum != MAXLNUM)
3962 pos.lnum = lnum;
3963 else
3964 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003966 /*
3967 * Start the search just like for the above
3968 * do_search().
3969 */
3970 if (*cmd != '?')
3971 pos.col = MAXCOL;
3972 else
3973 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003974 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003975 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003976 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003977 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003978 lnum = pos.lnum;
3979 else
3980 {
3981 cmd = NULL;
3982 goto error;
3983 }
3984 }
3985 ++cmd;
3986 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987
3988 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003989 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003990 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 }
3992
3993 for (;;)
3994 {
3995 cmd = skipwhite(cmd);
3996 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3997 break;
3998
3999 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004000 {
4001 switch (addr_type)
4002 {
4003 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004004 case ADDR_OTHER:
4005 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004006 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004007 break;
4008 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004009 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004010 break;
4011 case ADDR_ARGUMENTS:
4012 lnum = curwin->w_arg_idx + 1;
4013 break;
4014 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004015 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004016 lnum = curbuf->b_fnum;
4017 break;
4018 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004019 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004020 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004021 case ADDR_TABS_RELATIVE:
4022 lnum = 1;
4023 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004024 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004025#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004026 lnum = qf_get_cur_idx(eap);
4027#endif
4028 break;
4029 case ADDR_QUICKFIX_VALID:
4030#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004031 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004032#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004033 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004034 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004035 case ADDR_UNSIGNED:
4036 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004037 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004038 }
4039 }
4040
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004042 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 else
4044 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004045 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 n = 1;
4047 else
4048 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004049
4050 if (addr_type == ADDR_TABS_RELATIVE)
4051 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004052 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004053 cmd = NULL;
4054 goto error;
4055 }
4056 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004057 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004058 lnum = compute_buffer_local_count(
4059 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004061 {
4062#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004063 // Relative line addressing, need to adjust for folded lines
4064 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004065 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4066 && address_count >= 2)
4067 (void)hasFolding(lnum, NULL, &lnum);
4068#endif
4069 if (i == '-')
4070 lnum -= n;
4071 else
4072 lnum += n;
4073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 }
4075 } while (*cmd == '/' || *cmd == '?');
4076
4077error:
4078 *ptr = cmd;
4079 return lnum;
4080}
4081
4082/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004083 * Set eap->line1 and eap->line2 to the whole range.
4084 * Used for commands with the EX_DFLALL flag and no range given.
4085 */
4086 static void
4087address_default_all(exarg_T *eap)
4088{
4089 eap->line1 = 1;
4090 switch (eap->addr_type)
4091 {
4092 case ADDR_LINES:
4093 case ADDR_OTHER:
4094 eap->line2 = curbuf->b_ml.ml_line_count;
4095 break;
4096 case ADDR_LOADED_BUFFERS:
4097 {
4098 buf_T *buf = firstbuf;
4099
4100 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4101 buf = buf->b_next;
4102 eap->line1 = buf->b_fnum;
4103 buf = lastbuf;
4104 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4105 buf = buf->b_prev;
4106 eap->line2 = buf->b_fnum;
4107 }
4108 break;
4109 case ADDR_BUFFERS:
4110 eap->line1 = firstbuf->b_fnum;
4111 eap->line2 = lastbuf->b_fnum;
4112 break;
4113 case ADDR_WINDOWS:
4114 eap->line2 = LAST_WIN_NR;
4115 break;
4116 case ADDR_TABS:
4117 eap->line2 = LAST_TAB_NR;
4118 break;
4119 case ADDR_TABS_RELATIVE:
4120 eap->line2 = 1;
4121 break;
4122 case ADDR_ARGUMENTS:
4123 if (ARGCOUNT == 0)
4124 eap->line1 = eap->line2 = 0;
4125 else
4126 eap->line2 = ARGCOUNT;
4127 break;
4128 case ADDR_QUICKFIX_VALID:
4129#ifdef FEAT_QUICKFIX
4130 eap->line2 = qf_get_valid_size(eap);
4131 if (eap->line2 == 0)
4132 eap->line2 = 1;
4133#endif
4134 break;
4135 case ADDR_NONE:
4136 case ADDR_UNSIGNED:
4137 case ADDR_QUICKFIX:
4138 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4139 break;
4140 }
4141}
4142
4143
4144/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004145 * Get flags from an Ex command argument.
4146 */
4147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004148get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004149{
4150 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4151 {
4152 if (*eap->arg == 'l')
4153 eap->flags |= EXFLAG_LIST;
4154 else if (*eap->arg == 'p')
4155 eap->flags |= EXFLAG_PRINT;
4156 else
4157 eap->flags |= EXFLAG_NR;
4158 eap->arg = skipwhite(eap->arg + 1);
4159 }
4160}
4161
4162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 * Function called for command which is Not Implemented. NI!
4164 */
4165 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004166ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167{
4168 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004169 eap->errmsg =
4170 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171}
4172
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004173#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174/*
4175 * Function called for script command which is Not Implemented. NI!
4176 * Skips over ":perl <<EOF" constructs.
4177 */
4178 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004179ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180{
4181 if (!eap->skip)
4182 ex_ni(eap);
4183 else
4184 vim_free(script_get(eap, eap->arg));
4185}
4186#endif
4187
4188/*
4189 * Check range in Ex command for validity.
4190 * Return NULL when valid, error message when invalid.
4191 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004192 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004193invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004195 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004196
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 if ( eap->line1 < 0
4198 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004199 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004200 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004201
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004202 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004203 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004204 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004205 {
4206 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004207 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004208#ifdef FEAT_DIFF
4209 + (eap->cmdidx == CMD_diffget)
4210#endif
4211 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004212 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004213 break;
4214 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004215 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004216 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004217 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004218 break;
4219 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004220 // Only a boundary check, not whether the buffers actually
4221 // exist.
4222 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004223 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004224 break;
4225 case ADDR_LOADED_BUFFERS:
4226 buf = firstbuf;
4227 while (buf->b_ml.ml_mfp == NULL)
4228 {
4229 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004230 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004231 buf = buf->b_next;
4232 }
4233 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004234 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004235 buf = lastbuf;
4236 while (buf->b_ml.ml_mfp == NULL)
4237 {
4238 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004239 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004240 buf = buf->b_prev;
4241 }
4242 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004243 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004244 break;
4245 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004246 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004247 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004248 break;
4249 case ADDR_TABS:
4250 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004251 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004252 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004253 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004254 case ADDR_OTHER:
4255 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004256 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004257 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004258#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004259 // No error for value that is too big, will use the last entry.
4260 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004261 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004262#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004263 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004264 case ADDR_QUICKFIX_VALID:
4265#ifdef FEAT_QUICKFIX
4266 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4267 || eap->line2 < 0)
4268 return _(e_invrange);
4269#endif
4270 break;
4271 case ADDR_UNSIGNED:
4272 if (eap->line2 < 0)
4273 return _(e_invrange);
4274 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004275 case ADDR_NONE:
4276 // Will give an error elsewhere.
4277 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004278 }
4279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 return NULL;
4281}
4282
4283/*
4284 * Correct the range for zero line number, if required.
4285 */
4286 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004287correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004289 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 {
4291 if (eap->line1 == 0)
4292 eap->line1 = 1;
4293 if (eap->line2 == 0)
4294 eap->line2 = 1;
4295 }
4296}
4297
Bram Moolenaar748bf032005-02-02 23:04:36 +00004298#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004299/*
4300 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4301 * pattern. Otherwise return eap->arg.
4302 */
4303 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004304skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004305{
4306 char_u *p = eap->arg;
4307
Bram Moolenaara37420f2006-02-04 22:37:47 +00004308 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4309 || eap->cmdidx == CMD_vimgrepadd
4310 || eap->cmdidx == CMD_lvimgrepadd
4311 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004312 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004313 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004314 if (p == NULL)
4315 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004316 }
4317 return p;
4318}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004319
4320/*
4321 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4322 * in the command line, so that things like % get expanded.
4323 */
4324 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004325replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004326{
4327 char_u *new_cmdline;
4328 char_u *program;
4329 char_u *pos;
4330 char_u *ptr;
4331 int len;
4332 int i;
4333
4334 /*
4335 * Don't do it when ":vimgrep" is used for ":grep".
4336 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004337 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4338 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4339 || eap->cmdidx == CMD_grepadd
4340 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004341 && !grep_internal(eap->cmdidx))
4342 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004343 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4344 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004345 {
4346 if (*curbuf->b_p_gp == NUL)
4347 program = p_gp;
4348 else
4349 program = curbuf->b_p_gp;
4350 }
4351 else
4352 {
4353 if (*curbuf->b_p_mp == NUL)
4354 program = p_mp;
4355 else
4356 program = curbuf->b_p_mp;
4357 }
4358
4359 p = skipwhite(p);
4360
4361 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4362 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004363 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004364 i = 1;
4365 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4366 ++i;
4367 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004368 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004369 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004370 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004371 ptr = new_cmdline;
4372 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4373 {
4374 i = (int)(pos - program);
4375 STRNCPY(ptr, program, i);
4376 STRCPY(ptr += i, p);
4377 ptr += len;
4378 program = pos + 2;
4379 }
4380 STRCPY(ptr, program);
4381 }
4382 else
4383 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004384 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004385 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004386 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004387 STRCPY(new_cmdline, program);
4388 STRCAT(new_cmdline, " ");
4389 STRCAT(new_cmdline, p);
4390 }
4391 msg_make(p);
4392
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004393 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004394 vim_free(*cmdlinep);
4395 *cmdlinep = new_cmdline;
4396 p = new_cmdline;
4397 }
4398 return p;
4399}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004400#endif
4401
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402/*
4403 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004404 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 * Return FAIL for failure, OK otherwise.
4406 */
4407 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004408expand_filename(
4409 exarg_T *eap,
4410 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004411 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004413 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 char_u *repl;
4415 int srclen;
4416 char_u *p;
4417 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004418 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419
Bram Moolenaar748bf032005-02-02 23:04:36 +00004420#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004421 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004422 p = skip_grep_pat(eap);
4423#else
4424 p = eap->arg;
4425#endif
4426
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 /*
4428 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4429 * the file name contains a wildcard it should not cause expanding.
4430 * (it will be expanded anyway if there is a wildcard before replacing).
4431 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004432 has_wildcards = mch_has_wildcard(p);
4433 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004435#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004436 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004437 if (p[0] == '`' && p[1] == '=')
4438 {
4439 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004440 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004441 if (*p == '`')
4442 ++p;
4443 continue;
4444 }
4445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 /*
4447 * Quick check if this cannot be the start of a special string.
4448 * Also removes backslash before '%', '#' and '<'.
4449 */
4450 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4451 {
4452 ++p;
4453 continue;
4454 }
4455
4456 /*
4457 * Try to find a match at this position.
4458 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004459 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4460 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004461 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004463 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 {
4465 p += srclen;
4466 continue;
4467 }
4468
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004469 // Wildcards won't be expanded below, the replacement is taken
4470 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004471 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4472 {
4473 char_u *l = repl;
4474
4475 repl = expand_env_save(repl);
4476 vim_free(l);
4477 }
4478
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004479 // Need to escape white space et al. with a backslash.
4480 // Don't do this for:
4481 // - replacement that already has been escaped: "##"
4482 // - shell commands (may have to use quotes instead).
4483 // - non-unix systems when there is a single argument (spaces don't
4484 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004486 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 && eap->cmdidx != CMD_grep
4489 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004490 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004491 && eap->cmdidx != CMD_lgrep
4492 && eap->cmdidx != CMD_lgrepadd
4493 && eap->cmdidx != CMD_lmake
4494 && eap->cmdidx != CMD_make
4495 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004497 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498#endif
4499 )
4500 {
4501 char_u *l;
4502#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004503 // Don't escape a backslash here, because rem_backslash() doesn't
4504 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 static char_u *nobslash = (char_u *)" \t\"|";
4506# define ESCAPE_CHARS nobslash
4507#else
4508# define ESCAPE_CHARS escape_chars
4509#endif
4510
4511 for (l = repl; *l; ++l)
4512 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4513 {
4514 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4515 if (l != NULL)
4516 {
4517 vim_free(repl);
4518 repl = l;
4519 }
4520 break;
4521 }
4522 }
4523
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004524 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004525 if ((eap->usefilter || eap->cmdidx == CMD_bang
4526 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004527 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 {
4529 char_u *l;
4530
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004531 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 if (l != NULL)
4533 {
4534 vim_free(repl);
4535 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 }
4537 }
4538
4539 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4540 vim_free(repl);
4541 if (p == NULL)
4542 return FAIL;
4543 }
4544
4545 /*
4546 * One file argument: Expand wildcards.
4547 * Don't do this with ":r !command" or ":w !command".
4548 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004549 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 {
4551 /*
4552 * May do this twice:
4553 * 1. Replace environment variables.
4554 * 2. Replace any other wildcards, remove backslashes.
4555 */
4556 for (n = 1; n <= 2; ++n)
4557 {
4558 if (n == 2)
4559 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 /*
4561 * Halve the number of backslashes (this is Vi compatible).
4562 * For Unix and OS/2, when wildcards are expanded, this is
4563 * done by ExpandOne() below.
4564 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004565#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 if (!has_wildcards)
4567#endif
4568 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 }
4570
4571 if (has_wildcards)
4572 {
4573 if (n == 1)
4574 {
4575 /*
4576 * First loop: May expand environment variables. This
4577 * can be done much faster with expand_env() than with
4578 * something else (e.g., calling a shell).
4579 * After expanding environment variables, check again
4580 * if there are still wildcards present.
4581 */
4582 if (vim_strchr(eap->arg, '$') != NULL
4583 || vim_strchr(eap->arg, '~') != NULL)
4584 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004585 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004586 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 has_wildcards = mch_has_wildcard(NameBuff);
4588 p = NameBuff;
4589 }
4590 else
4591 p = NULL;
4592 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004593 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 {
4595 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004596 int options = WILD_LIST_NOTFOUND
4597 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598
4599 ExpandInit(&xpc);
4600 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004601 if (p_wic)
4602 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004604 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 if (p == NULL)
4606 return FAIL;
4607 }
4608 if (p != NULL)
4609 {
4610 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4611 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004612 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 vim_free(p);
4614 }
4615 }
4616 }
4617 }
4618 return OK;
4619}
4620
4621/*
4622 * Replace part of the command line, keeping eap->cmd, eap->arg and
4623 * eap->nextcmd correct.
4624 * "src" points to the part that is to be replaced, of length "srclen".
4625 * "repl" is the replacement string.
4626 * Returns a pointer to the character after the replaced string.
4627 * Returns NULL for failure.
4628 */
4629 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004630repl_cmdline(
4631 exarg_T *eap,
4632 char_u *src,
4633 int srclen,
4634 char_u *repl,
4635 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636{
4637 int len;
4638 int i;
4639 char_u *new_cmdline;
4640
4641 /*
4642 * The new command line is build in new_cmdline[].
4643 * First allocate it.
4644 * Careful: a "+cmd" argument may have been NUL terminated.
4645 */
4646 len = (int)STRLEN(repl);
4647 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004648 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004649 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004650 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004651 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652
4653 /*
4654 * Copy the stuff before the expanded part.
4655 * Copy the expanded stuff.
4656 * Copy what came after the expanded part.
4657 * Copy the next commands, if there are any.
4658 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004659 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004661
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004663 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004665 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004667 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 {
4669 i = (int)STRLEN(new_cmdline) + 1;
4670 STRCPY(new_cmdline + i, eap->nextcmd);
4671 eap->nextcmd = new_cmdline + i;
4672 }
4673 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4674 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4675 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4676 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4677 vim_free(*cmdlinep);
4678 *cmdlinep = new_cmdline;
4679
4680 return src;
4681}
4682
4683/*
4684 * Check for '|' to separate commands and '"' to start comments.
4685 */
4686 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004687separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688{
4689 char_u *p;
4690
Bram Moolenaar86b68352004-12-27 21:59:20 +00004691#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004692 p = skip_grep_pat(eap);
4693#else
4694 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004695#endif
4696
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004697 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 {
4699 if (*p == Ctrl_V)
4700 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004701 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004702 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004704 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004705 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004706 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 break;
4708 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004709
4710#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004711 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004712 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004713 {
4714 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004715 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01004716 if (*p == NUL) // stop at NUL after CTRL-V
4717 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004718 }
4719#endif
4720
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004721 // Check for '"': start of comment or '|': next command
4722 // :@" and :*" do not start a comment!
4723 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004724 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004725#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004726 && !in_vim9script()
4727#endif
4728 && !(eap->argt & EX_NOTRLCOM)
4729 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4730 || p != eap->arg)
4731 && (eap->cmdidx != CMD_redir
4732 || p != eap->arg + 1 || p[-1] != '@'))
4733#ifdef FEAT_EVAL
4734 || (*p == '#'
4735 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004736 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004737 && p[1] != '{'
4738 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 || *p == '|' || *p == '\n')
4741 {
4742 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004743 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 * AND 'b' is present in 'cpoptions'.
4745 */
4746 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004747 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004749 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 --p;
4751 }
4752 else
4753 {
4754 eap->nextcmd = check_nextcmd(p);
4755 *p = NUL;
4756 break;
4757 }
4758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004760
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004761 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 del_trailing_spaces(eap->arg);
4763}
4764
4765/*
4766 * get + command from ex argument
4767 */
4768 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004769getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770{
4771 char_u *arg = *argp;
4772 char_u *command = NULL;
4773
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004774 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 {
4776 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004777 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 command = dollar_command;
4779 else
4780 {
4781 command = arg;
4782 arg = skip_cmd_arg(command, TRUE);
4783 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004784 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 }
4786
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004787 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 *argp = arg;
4789 }
4790 return command;
4791}
4792
4793/*
4794 * Find end of "+command" argument. Skip over "\ " and "\\".
4795 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004796 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004797skip_cmd_arg(
4798 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004799 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800{
4801 while (*p && !vim_isspace(*p))
4802 {
4803 if (*p == '\\' && p[1] != NUL)
4804 {
4805 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004806 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 else
4808 ++p;
4809 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004810 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 }
4812 return p;
4813}
4814
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004815 int
4816get_bad_opt(char_u *p, exarg_T *eap)
4817{
4818 if (STRICMP(p, "keep") == 0)
4819 eap->bad_char = BAD_KEEP;
4820 else if (STRICMP(p, "drop") == 0)
4821 eap->bad_char = BAD_DROP;
4822 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4823 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004824 else
4825 return FAIL;
4826 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004827}
4828
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829/*
4830 * Get "++opt=arg" argument.
4831 * Return FAIL or OK.
4832 */
4833 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004834getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835{
4836 char_u *arg = eap->arg + 2;
4837 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004838 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004841 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4843 {
4844 if (*arg == 'n')
4845 {
4846 arg += 2;
4847 eap->force_bin = FORCE_NOBIN;
4848 }
4849 else
4850 eap->force_bin = FORCE_BIN;
4851 if (!checkforcmd(&arg, "binary", 3))
4852 return FAIL;
4853 eap->arg = skipwhite(arg);
4854 return OK;
4855 }
4856
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004857 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004858 if (STRNCMP(arg, "edit", 4) == 0)
4859 {
4860 eap->read_edit = TRUE;
4861 eap->arg = skipwhite(arg + 4);
4862 return OK;
4863 }
4864
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 if (STRNCMP(arg, "ff", 2) == 0)
4866 {
4867 arg += 2;
4868 pp = &eap->force_ff;
4869 }
4870 else if (STRNCMP(arg, "fileformat", 10) == 0)
4871 {
4872 arg += 10;
4873 pp = &eap->force_ff;
4874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 else if (STRNCMP(arg, "enc", 3) == 0)
4876 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004877 if (STRNCMP(arg, "encoding", 8) == 0)
4878 arg += 8;
4879 else
4880 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 pp = &eap->force_enc;
4882 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004883 else if (STRNCMP(arg, "bad", 3) == 0)
4884 {
4885 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004886 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888
4889 if (pp == NULL || *arg != '=')
4890 return FAIL;
4891
4892 ++arg;
4893 *pp = (int)(arg - eap->cmd);
4894 arg = skip_cmd_arg(arg, FALSE);
4895 eap->arg = skipwhite(arg);
4896 *arg = NUL;
4897
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 if (pp == &eap->force_ff)
4899 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4901 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004902 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004904 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004906 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4908 *p = TOLOWER_ASC(*p);
4909 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004910 else
4911 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004912 // Check ++bad= argument. Must be a single-byte character, "keep" or
4913 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004914 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004915 return FAIL;
4916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917
4918 return OK;
4919}
4920
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004922ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923{
4924 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004925 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 * directory for security reasons.
4927 */
4928 if (secure)
4929 {
4930 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004931 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
4933 else if (eap->cmdidx == CMD_autocmd)
4934 do_autocmd(eap->arg, eap->forceit);
4935 else
4936 do_augroup(eap->arg, eap->forceit);
4937}
4938
4939/*
4940 * ":doautocmd": Apply the automatic commands to the current buffer.
4941 */
4942 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004943ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004945 char_u *arg = eap->arg;
4946 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004947 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004948
Bram Moolenaar1610d052016-06-09 22:53:01 +02004949 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004950 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004951 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004952 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955/*
4956 * :[N]bunload[!] [N] [bufname] unload buffer
4957 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4958 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4959 */
4960 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004961ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004963 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004964 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 eap->errmsg = do_bufdel(
4966 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4967 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4968 : DOBUF_UNLOAD, eap->arg,
4969 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4970}
4971
4972/*
4973 * :[N]buffer [N] to buffer N
4974 * :[N]sbuffer [N] to buffer N
4975 */
4976 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004977ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004979 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004980 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004982 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 else
4984 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004985 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4987 else
4988 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004989 if (eap->do_ecmd_cmd != NULL)
4990 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
4992}
4993
4994/*
4995 * :[N]bmodified [N] to next mod. buffer
4996 * :[N]sbmodified [N] to next mod. buffer
4997 */
4998 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004999ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000{
5001 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005002 if (eap->do_ecmd_cmd != NULL)
5003 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004}
5005
5006/*
5007 * :[N]bnext [N] to next buffer
5008 * :[N]sbnext [N] split and to next buffer
5009 */
5010 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005011ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005013 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005014 return;
5015
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005017 if (eap->do_ecmd_cmd != NULL)
5018 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019}
5020
5021/*
5022 * :[N]bNext [N] to previous buffer
5023 * :[N]bprevious [N] to previous buffer
5024 * :[N]sbNext [N] split and to previous buffer
5025 * :[N]sbprevious [N] split and to previous buffer
5026 */
5027 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005028ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005030 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005031 return;
5032
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005034 if (eap->do_ecmd_cmd != NULL)
5035 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036}
5037
5038/*
5039 * :brewind to first buffer
5040 * :bfirst to first buffer
5041 * :sbrewind split and to first buffer
5042 * :sbfirst split and to first buffer
5043 */
5044 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005045ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005047 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005048 return;
5049
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005051 if (eap->do_ecmd_cmd != NULL)
5052 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053}
5054
5055/*
5056 * :blast to last buffer
5057 * :sblast split and to last buffer
5058 */
5059 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005060ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005062 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005063 return;
5064
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005066 if (eap->do_ecmd_cmd != NULL)
5067 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069
Bram Moolenaar7a092242020-04-16 22:10:49 +02005070/*
5071 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02005072 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005073 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005075ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005077 int comment_char = '"';
5078
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005079#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005080 if (in_vim9script())
5081 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005082#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005083 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005084}
5085
5086/*
5087 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5088 * to "cmd_start" or has a white space character before it.
5089 */
5090 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005091ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005092{
5093 int c = *cmd;
5094
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005095 if (c == NUL || c == '|' || c == '\n')
5096 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005097#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005098 if (in_vim9script())
5099 return c == '#' && cmd[1] != '{'
5100 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005101#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005102 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103}
5104
5105#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5106 || defined(PROTO)
5107/*
5108 * Return the next command, after the first '|' or '\n'.
5109 * Return NULL if not found.
5110 */
5111 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005112find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113{
5114 while (*p != '|' && *p != '\n')
5115 {
5116 if (*p == NUL)
5117 return NULL;
5118 ++p;
5119 }
5120 return (p + 1);
5121}
5122#endif
5123
5124/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005125 * Check if *p is a separator between Ex commands, skipping over white space.
5126 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 */
5128 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005129check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005131 char_u *s = skipwhite(p);
5132
5133 if (*s == '|' || *s == '\n')
5134 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 else
5136 return NULL;
5137}
5138
5139/*
5140 * - if there are more files to edit
5141 * - and this is the last window
5142 * - and forceit not used
5143 * - and not repeated twice on a row
5144 * return FAIL and give error message if 'message' TRUE
5145 * return OK otherwise
5146 */
5147 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005148check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005149 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005150 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151{
5152 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5153
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005154 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005155 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 {
5157 if (message)
5158 {
5159#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005160 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5161 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005163 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005165 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5166 NGETTEXT("%d more file to edit. Quit anyway?",
5167 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5169 return OK;
5170 return FAIL;
5171 }
5172#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005173 semsg(NGETTEXT("E173: %d more file to edit",
5174 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005175 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 }
5177 return FAIL;
5178 }
5179 return OK;
5180}
5181
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182/*
5183 * Function given to ExpandGeneric() to obtain the list of command names.
5184 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005186get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187{
5188 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 return cmdnames[idx].cmd_name;
5191}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005194ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195{
Bram Moolenaare6850792010-05-14 15:28:44 +02005196 if (*eap->arg == NUL)
5197 {
5198#ifdef FEAT_EVAL
5199 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5200 char_u *p = NULL;
5201
5202 if (expr != NULL)
5203 {
5204 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005205 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005206 --emsg_off;
5207 vim_free(expr);
5208 }
5209 if (p != NULL)
5210 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005211 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005212 vim_free(p);
5213 }
5214 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005215 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005216#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005217 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005218#endif
5219 }
5220 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005221 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005222
5223#ifdef FEAT_VTP
5224 else if (has_vtp_working())
5225 {
5226 // background color change requires clear + redraw
5227 update_screen(CLEAR);
5228 redrawcmd();
5229 }
5230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231}
5232
5233 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005234ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235{
5236 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005237 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 do_highlight(eap->arg, eap->forceit, FALSE);
5239}
5240
5241
5242/*
5243 * Call this function if we thought we were going to exit, but we won't
5244 * (because of an error). May need to restore the terminal mode.
5245 */
5246 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005247not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248{
5249 exiting = FALSE;
5250 settmode(TMODE_RAW);
5251}
5252
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005253 static int
5254before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5255{
5256 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5257
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005258 // Bail out when autocommands closed the window.
5259 // Refuse to quit when the buffer in the last window is being closed (can
5260 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005261 if (!win_valid(wp)
5262 || curbuf_locked()
5263 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5264 return TRUE;
5265
5266 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5267 {
5268 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005269 // Refuse to quit when locked or when the window was closed or the
5270 // buffer in the last window is being closed (can only happen in
5271 // autocommands).
5272 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005273 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5274 return TRUE;
5275 }
5276
5277 return FALSE;
5278}
5279
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005281 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005282 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005283 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005285 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005286ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005288 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005289
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290#ifdef FEAT_CMDWIN
5291 if (cmdwin_type != 0)
5292 {
5293 cmdwin_result = Ctrl_C;
5294 return;
5295 }
5296#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005297 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005298 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005299 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005300 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005301 return;
5302 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005303 if (eap->addr_count > 0)
5304 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005305 int wnr = eap->line2;
5306
5307 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5308 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005309 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005310 }
5311 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005312 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005313
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005314 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005315 if (curbuf_locked())
5316 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005317
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005318 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005319 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005320 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321
5322#ifdef FEAT_NETBEANS_INTG
5323 netbeansForcedQuit = eap->forceit;
5324#endif
5325
5326 /*
5327 * If there are more files or windows we won't exit.
5328 */
5329 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5330 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005331 if ((!buf_hide(wp->w_buffer)
5332 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005333 | (eap->forceit ? CCGD_FORCEIT : 0)
5334 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005336 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 {
5338 not_exiting();
5339 }
5340 else
5341 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005342 // quit last window
5343 // Note: only_one_window() returns true, even so a help window is
5344 // still open. In that case only quit, if no address has been
5345 // specified. Example:
5346 // :h|wincmd w|1q - don't quit
5347 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005348 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005350 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005351#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005353#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005354 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005355 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 }
5357}
5358
5359/*
5360 * ":cquit".
5361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005363ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005365 // this does not always pass on the exit code to the Manx compiler. why?
5366 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367}
5368
5369/*
5370 * ":qall": try to quit all windows
5371 */
5372 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005373ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374{
5375# ifdef FEAT_CMDWIN
5376 if (cmdwin_type != 0)
5377 {
5378 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005379 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 else
5381 cmdwin_result = K_XF2;
5382 return;
5383 }
5384# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005385
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005386 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005387 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005388 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005389 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005390 return;
5391 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005392
5393 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005394 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005395
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005397 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 getout(0);
5399 not_exiting();
5400}
5401
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402/*
5403 * ":close": close current window, unless it is the last one
5404 */
5405 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005406ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005408 win_T *win;
5409 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005410#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005412 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005414#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005415 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005416 {
5417 if (eap->addr_count == 0)
5418 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005419 else
5420 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005421 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005422 {
5423 winnr++;
5424 if (winnr == eap->line2)
5425 break;
5426 }
5427 if (win == NULL)
5428 win = lastwin;
5429 ex_win_close(eap->forceit, win, NULL);
5430 }
5431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432}
5433
Bram Moolenaar4033c552017-09-16 20:54:51 +02005434#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005435/*
5436 * ":pclose": Close any preview window.
5437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005439ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005440{
5441 win_T *win;
5442
Bram Moolenaar79648732019-07-18 21:43:07 +02005443 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005444 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005445 if (win->w_p_pvw)
5446 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005447 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005448 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005449 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005450# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005451 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005452 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005453# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005454}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005455#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005456
Bram Moolenaarf740b292006-02-16 22:11:02 +00005457/*
5458 * Close window "win" and take care of handling closing the last window for a
5459 * modified buffer.
5460 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005462ex_win_close(
5463 int forceit,
5464 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005465 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466{
5467 int need_hide;
5468 buf_T *buf = win->w_buffer;
5469
Bram Moolenaarcf844172020-06-26 19:44:06 +02005470 // Never close the autocommand window.
5471 if (win == aucmd_win)
5472 {
5473 emsg(_(e_autocmd_close));
5474 return;
5475 }
5476
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005478 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005480#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005481 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005483 bufref_T bufref;
5484
5485 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005487 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 return;
5489 need_hide = FALSE;
5490 }
5491 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005494 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 return;
5496 }
5497 }
5498
Bram Moolenaar4033c552017-09-16 20:54:51 +02005499#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005501#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005502
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005503 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005504 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005505 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005506 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005507 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508}
5509
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005511 * Handle the argument for a tabpage related ex command.
5512 * Returns a tabpage number.
5513 * When an error is encountered then eap->errmsg is set.
5514 */
5515 static int
5516get_tabpage_arg(exarg_T *eap)
5517{
5518 int tab_number;
5519 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5520
5521 if (eap->arg && *eap->arg != NUL)
5522 {
5523 char_u *p = eap->arg;
5524 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005525 int relative = 0; // argument +N/-N means: go to N places to the
5526 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005527
5528 if (*p == '-')
5529 {
5530 relative = -1;
5531 p++;
5532 }
5533 else if (*p == '+')
5534 {
5535 relative = 1;
5536 p++;
5537 }
5538
5539 p_save = p;
5540 tab_number = getdigits(&p);
5541
5542 if (relative == 0)
5543 {
5544 if (STRCMP(p, "$") == 0)
5545 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005546 else if (STRCMP(p, "#") == 0)
5547 if (valid_tabpage(lastused_tabpage))
5548 tab_number = tabpage_index(lastused_tabpage);
5549 else
5550 {
5551 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5552 tab_number = 0;
5553 goto theend;
5554 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005555 else if (p == p_save || *p_save == '-' || *p != NUL
5556 || tab_number > LAST_TAB_NR)
5557 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005558 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005559 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005560 goto theend;
5561 }
5562 }
5563 else
5564 {
5565 if (*p_save == NUL)
5566 tab_number = 1;
5567 else if (p == p_save || *p_save == '-' || *p != NUL
5568 || tab_number == 0)
5569 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005570 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005571 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005572 goto theend;
5573 }
5574 tab_number = tab_number * relative + tabpage_index(curtab);
5575 if (!unaccept_arg0 && relative == -1)
5576 --tab_number;
5577 }
5578 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005579 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005580 }
5581 else if (eap->addr_count > 0)
5582 {
5583 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005584 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005585 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005586 tab_number = 0;
5587 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005588 else
5589 {
5590 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005591 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005592 {
5593 --tab_number;
5594 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005595 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005596 }
5597 }
5598 }
5599 else
5600 {
5601 switch (eap->cmdidx)
5602 {
5603 case CMD_tabnext:
5604 tab_number = tabpage_index(curtab) + 1;
5605 if (tab_number > LAST_TAB_NR)
5606 tab_number = 1;
5607 break;
5608 case CMD_tabmove:
5609 tab_number = LAST_TAB_NR;
5610 break;
5611 default:
5612 tab_number = tabpage_index(curtab);
5613 }
5614 }
5615
5616theend:
5617 return tab_number;
5618}
5619
5620/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005621 * ":tabclose": close current tab page, unless it is the last one.
5622 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 */
5624 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005625ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005627 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005628 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005629
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005630# ifdef FEAT_CMDWIN
5631 if (cmdwin_type != 0)
5632 cmdwin_result = K_IGNORE;
5633 else
5634# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005635 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005636 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005637 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005639 tab_number = get_tabpage_arg(eap);
5640 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005641 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005642 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005643 if (tp == NULL)
5644 {
5645 beep_flush();
5646 return;
5647 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005648 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005649 {
5650 tabpage_close_other(tp, eap->forceit);
5651 return;
5652 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005653 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005654 tabpage_close(eap->forceit);
5655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005657}
5658
5659/*
5660 * ":tabonly": close all tab pages except the current one
5661 */
5662 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005663ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005664{
5665 tabpage_T *tp;
5666 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005667 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005668
5669# ifdef FEAT_CMDWIN
5670 if (cmdwin_type != 0)
5671 cmdwin_result = K_IGNORE;
5672 else
5673# endif
5674 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005675 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005676 else
5677 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005678 tab_number = get_tabpage_arg(eap);
5679 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005680 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005681 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005682 // Repeat this up to a 1000 times, because autocommands may
5683 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005684 for (done = 0; done < 1000; ++done)
5685 {
5686 FOR_ALL_TABPAGES(tp)
5687 if (tp->tp_topframe != topframe)
5688 {
5689 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005690 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005691 if (valid_tabpage(tp))
5692 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005693 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005694 break;
5695 }
5696 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005697 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005698 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005699 }
5700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702
5703/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005704 * Close the current tab page.
5705 */
5706 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005707tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005708{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005709 // First close all the windows but the current one. If that worked then
5710 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005711 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005712 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005713 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005714 ex_win_close(forceit, curwin, NULL);
5715# ifdef FEAT_GUI
5716 need_mouse_correct = TRUE;
5717# endif
5718}
5719
5720/*
5721 * Close tab page "tp", which is not the current tab page.
5722 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005723 * Also takes care of the tab pages line disappearing when closing the
5724 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005725 */
5726 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005727tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005728{
5729 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005730 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005731 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005732
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005733 // Limit to 1000 windows, autocommands may add a window while we close
5734 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005735 while (++done < 1000)
5736 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005737 wp = tp->tp_firstwin;
5738 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005739
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005740 // Autocommands may delete the tab page under our fingers and we may
5741 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005742 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005743 break;
5744 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005745
Bram Moolenaar29323592016-07-24 22:04:11 +02005746 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005747
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005748 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005749 if (h != tabline_height())
5750 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005751}
5752
5753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 * ":only".
5755 */
5756 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005757ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005759 win_T *wp;
5760 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761# ifdef FEAT_GUI
5762 need_mouse_correct = TRUE;
5763# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005764 if (eap->addr_count > 0)
5765 {
5766 wnr = eap->line2;
5767 for (wp = firstwin; --wnr > 0; )
5768 {
5769 if (wp->w_next == NULL)
5770 break;
5771 else
5772 wp = wp->w_next;
5773 }
5774 win_goto(wp);
5775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 close_others(TRUE, eap->forceit);
5777}
5778
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005780ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005782 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005783 if (!eap->skip)
5784 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005785#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005786 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005787#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005788 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005789 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005790 else
5791 {
5792 int winnr = 0;
5793 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005794
Bram Moolenaar2256c992016-11-15 21:17:07 +01005795 FOR_ALL_WINDOWS(win)
5796 {
5797 winnr++;
5798 if (winnr == eap->line2)
5799 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005800 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005801 if (win == NULL)
5802 win = lastwin;
5803 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 }
5806}
5807
5808/*
5809 * ":stop" and ":suspend": Suspend Vim.
5810 */
5811 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005812ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813{
5814 /*
5815 * Disallow suspending for "rvim".
5816 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005817 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 {
5819 if (!eap->forceit)
5820 autowrite_all();
5821 windgoto((int)Rows - 1, 0);
5822 out_char('\n');
5823 out_flush();
5824 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005825 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005827 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005829 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830#ifdef FEAT_TITLE
5831 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005832 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833#endif
5834 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005835 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005837 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 }
5839}
5840
5841/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005842 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 */
5844 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005845ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846{
Bram Moolenaar461f2122020-07-28 20:25:47 +02005847#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02005848 if (not_in_vim9(eap) == FAIL)
5849 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02005850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851#ifdef FEAT_CMDWIN
5852 if (cmdwin_type != 0)
5853 {
5854 cmdwin_result = Ctrl_C;
5855 return;
5856 }
5857#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005858 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005859 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005860 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005861 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005862 return;
5863 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005864
5865 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005866 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867
5868 /*
5869 * if more files or windows we won't exit
5870 */
5871 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5872 exiting = TRUE;
5873 if ( ((eap->cmdidx == CMD_wq
5874 || curbufIsChanged())
5875 && do_write(eap) == FAIL)
5876 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005877 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 {
5879 not_exiting();
5880 }
5881 else
5882 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005883 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005885 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886# ifdef FEAT_GUI
5887 need_mouse_correct = TRUE;
5888# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005889 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005890 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 }
5892}
5893
5894/*
5895 * ":print", ":list", ":number".
5896 */
5897 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005898ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005900 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005901 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005902 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005904 for ( ;!got_int; ui_breakcheck())
5905 {
5906 print_line(eap->line1,
5907 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5908 || (eap->flags & EXFLAG_NR)),
5909 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5910 if (++eap->line1 > eap->line2)
5911 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005912 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005913 }
5914 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005915 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005916 curwin->w_cursor.lnum = eap->line2;
5917 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 }
5919
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921}
5922
5923#ifdef FEAT_BYTEOFF
5924 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005925ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926{
5927 goto_byte(eap->line2);
5928}
5929#endif
5930
5931/*
5932 * ":shell".
5933 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005935ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936{
5937 do_shell(NULL, 0);
5938}
5939
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005940#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005942static int drop_busy = FALSE;
5943static int drop_filec;
5944static char_u **drop_filev = NULL;
5945static int drop_split;
5946static void (*drop_callback)(void *);
5947static void *drop_cookie;
5948
5949 static void
5950handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951{
5952 exarg_T ea;
5953 int save_msg_scroll = msg_scroll;
5954
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005955 // Setting the argument list may cause screen updates and being called
5956 // recursively. Avoid that by setting drop_busy.
5957 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005959 // Check whether the current buffer is changed. If so, we will need
5960 // to split the current window or data could be lost.
5961 // We don't need to check if the 'hidden' option is set, as in this
5962 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005963 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 {
5965 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005966 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 --emsg_off;
5968 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005969 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 if (win_split(0, 0) == FAIL)
5972 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005973 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005975 // When splitting the window, create a new alist. Otherwise the
5976 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 alist_unlink(curwin->w_alist);
5978 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 }
5980
5981 /*
5982 * Set up the new argument list.
5983 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005984 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985
5986 /*
5987 * Move to the first file.
5988 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005989 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005990 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 ea.cmd = (char_u *)"next";
5992 do_argfile(&ea, 0);
5993
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005994 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5995 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 need_start_insertmode = FALSE;
5997
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005998 // Restore msg_scroll, otherwise a following command may cause scrolling
5999 // unexpectedly. The screen will be redrawn by the caller, thus
6000 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006002
6003 if (drop_callback != NULL)
6004 drop_callback(drop_cookie);
6005
6006 drop_filev = NULL;
6007 drop_busy = FALSE;
6008}
6009
6010/*
6011 * Handle a file drop. The code is here because a drop is *nearly* like an
6012 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006013 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006014 * code is very similar to :args and hence needs access to a lot of the static
6015 * functions in this file.
6016 *
6017 * The "filev" list must have been allocated using alloc(), as should each item
6018 * in the list. This function takes over responsibility for freeing the "filev"
6019 * list.
6020 */
6021 void
6022handle_drop(
6023 int filec, // the number of files dropped
6024 char_u **filev, // the list of files dropped
6025 int split, // force splitting the window
6026 void (*callback)(void *), // to be called after setting the argument
6027 // list
6028 void *cookie) // argument for "callback" (allocated)
6029{
6030 // Cannot handle recursive drops, finish the pending one.
6031 if (drop_busy)
6032 {
6033 FreeWild(filec, filev);
6034 vim_free(cookie);
6035 return;
6036 }
6037
6038 // When calling handle_drop() more than once in a row we only use the last
6039 // one.
6040 if (drop_filev != NULL)
6041 {
6042 FreeWild(drop_filec, drop_filev);
6043 vim_free(drop_cookie);
6044 }
6045
6046 drop_filec = filec;
6047 drop_filev = filev;
6048 drop_split = split;
6049 drop_callback = callback;
6050 drop_cookie = cookie;
6051
6052 // Postpone this when:
6053 // - editing the command line
6054 // - not possible to change the current buffer
6055 // - updating the screen
6056 // As it may change buffers and window structures that are in use and cause
6057 // freed memory to be used.
6058 if (text_locked() || curbuf_locked() || updating_screen)
6059 return;
6060
6061 handle_drop_internal();
6062}
6063
6064/*
6065 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6066 * reset: Handle a postponed drop.
6067 */
6068 void
6069handle_any_postponed_drop(void)
6070{
6071 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006072 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006073 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074}
6075#endif
6076
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 * ":preserve".
6079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006081ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006083 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 ml_preserve(curbuf, TRUE);
6085}
6086
6087/*
6088 * ":recover".
6089 */
6090 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006091ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006093 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006095 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6096 | CCGD_MULTWIN
6097 | (eap->forceit ? CCGD_FORCEIT : 0)
6098 | CCGD_EXCMD)
6099
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 && (*eap->arg == NUL
6101 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006102 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 recoverymode = FALSE;
6104}
6105
6106/*
6107 * Command modifier used in a wrong way.
6108 */
6109 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006110ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006112 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113}
6114
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115/*
6116 * :sview [+command] file split window with new file, read-only
6117 * :split [[+command] file] split window with current or new file
6118 * :vsplit [[+command] file] split window vertically with current or new file
6119 * :new [[+command] file] split window with no or new file
6120 * :vnew [[+command] file] split vertically window with no or new file
6121 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006122 *
6123 * :tabedit open new Tab page with empty window
6124 * :tabedit [+command] file open new Tab page and edit "file"
6125 * :tabnew [[+command] file] just like :tabedit
6126 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 */
6128 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006129ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006131 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006132#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006134#endif
6135#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006136 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006137 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006138#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006139 int use_tab = eap->cmdidx == CMD_tabedit
6140 || eap->cmdidx == CMD_tabfind
6141 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006143 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006144 return;
6145
Bram Moolenaar4033c552017-09-16 20:54:51 +02006146#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149
Bram Moolenaar4033c552017-09-16 20:54:51 +02006150#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006151 // A ":split" in the quickfix window works like ":new". Don't want two
6152 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006153 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 {
6155 if (eap->cmdidx == CMD_split)
6156 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 if (eap->cmdidx == CMD_vsplit)
6158 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161
Bram Moolenaar4033c552017-09-16 20:54:51 +02006162#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006163 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 {
6165 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6166 FNAME_MESS, TRUE, curbuf->b_ffname);
6167 if (fname == NULL)
6168 goto theend;
6169 eap->arg = fname;
6170 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006171# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006172 else
6173# endif
6174#endif
6175#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006176 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 && eap->cmdidx != CMD_new)
6179 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006180 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006181# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006182 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006183# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006184 au_has_group((char_u *)"FileExplorer"))
6185 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006186 // No browsing supported but we do have the file explorer:
6187 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006188 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006189 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006190 }
6191 else
6192 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006193 fname = do_browse(0, (char_u *)(use_tab
6194 ? _("Edit File in new tab page")
6195 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006197 if (fname == NULL)
6198 goto theend;
6199 eap->arg = fname;
6200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006202 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006205 /*
6206 * Either open new tab page or split the window.
6207 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006208 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006209 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006210 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006211 : eap->addr_count == 0 ? 0
6212 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006213 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006214 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006215
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006216 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006217 if (curwin != old_curwin
6218 && win_valid(old_curwin)
6219 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006220 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006221 old_curwin->w_alt_fnum = curbuf->b_fnum;
6222 }
6223 }
6224 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6226 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006227 // Reset 'scrollbind' when editing another file, but keep it when
6228 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006229 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006230 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 else
6232 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 do_exedit(eap, old_curwin);
6234 }
6235
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006236# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006237 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006238# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006240# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241theend:
6242 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006243# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006245
6246/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006247 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006248 */
6249 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006250tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006251{
6252 exarg_T ea;
6253
Bram Moolenaara80faa82020-04-12 19:37:17 +02006254 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006255 ea.cmdidx = CMD_tabnew;
6256 ea.cmd = (char_u *)"tabn";
6257 ea.arg = (char_u *)"";
6258 ex_splitview(&ea);
6259}
6260
6261/*
6262 * :tabnext command
6263 */
6264 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006265ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006266{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006267 int tab_number;
6268
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006269 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006270 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006271 switch (eap->cmdidx)
6272 {
6273 case CMD_tabfirst:
6274 case CMD_tabrewind:
6275 goto_tabpage(1);
6276 break;
6277 case CMD_tablast:
6278 goto_tabpage(9999);
6279 break;
6280 case CMD_tabprevious:
6281 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006282 if (eap->arg && *eap->arg != NUL)
6283 {
6284 char_u *p = eap->arg;
6285 char_u *p_save = p;
6286
6287 tab_number = getdigits(&p);
6288 if (p == p_save || *p_save == '-' || *p != NUL
6289 || tab_number == 0)
6290 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006291 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006292 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006293 return;
6294 }
6295 }
6296 else
6297 {
6298 if (eap->addr_count == 0)
6299 tab_number = 1;
6300 else
6301 {
6302 tab_number = eap->line2;
6303 if (tab_number < 1)
6304 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006305 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006306 return;
6307 }
6308 }
6309 }
6310 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006311 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006312 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006313 tab_number = get_tabpage_arg(eap);
6314 if (eap->errmsg == NULL)
6315 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006316 break;
6317 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006318}
6319
6320/*
6321 * :tabmove command
6322 */
6323 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006324ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006325{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006326 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006327
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006328 tab_number = get_tabpage_arg(eap);
6329 if (eap->errmsg == NULL)
6330 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006331}
6332
6333/*
6334 * :tabs command: List tabs and their contents.
6335 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006336 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006337ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006338{
6339 tabpage_T *tp;
6340 win_T *wp;
6341 int tabcount = 1;
6342
6343 msg_start();
6344 msg_scroll = TRUE;
6345 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6346 {
6347 msg_putchar('\n');
6348 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006349 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006350 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006351 ui_breakcheck();
6352
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006353 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006354 wp = firstwin;
6355 else
6356 wp = tp->tp_firstwin;
6357 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6358 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006359 msg_putchar('\n');
6360 msg_putchar(wp == curwin ? '>' : ' ');
6361 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006362 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6363 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006364 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006365 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006366 else
6367 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6368 IObuff, IOSIZE, TRUE);
6369 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006370 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006371 ui_breakcheck();
6372 }
6373 }
6374}
6375
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376/*
6377 * ":mode": Set screen mode.
6378 * If no argument given, just get the screen size and redraw.
6379 */
6380 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006381ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382{
6383 if (*eap->arg == NUL)
6384 shell_resized();
6385 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006386 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387}
6388
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389/*
6390 * ":resize".
6391 * set, increment or decrement current window height
6392 */
6393 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006394ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395{
6396 int n;
6397 win_T *wp = curwin;
6398
6399 if (eap->addr_count > 0)
6400 {
6401 n = eap->line2;
6402 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6403 ;
6404 }
6405
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006406# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006408# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006410 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 {
6412 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006413 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006414 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 n = 9999;
6416 win_setwidth_win((int)n, wp);
6417 }
6418 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 {
6420 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006421 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006422 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 n = 9999;
6424 win_setheight_win((int)n, wp);
6425 }
6426}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427
6428/*
6429 * ":find [+command] <file>" command.
6430 */
6431 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006432ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433{
6434#ifdef FEAT_SEARCHPATH
6435 char_u *fname;
6436 int count;
6437
6438 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6439 TRUE, curbuf->b_ffname);
6440 if (eap->addr_count > 0)
6441 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006442 // Repeat finding the file "count" times. This matters when it
6443 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 count = eap->line2;
6445 while (fname != NULL && --count > 0)
6446 {
6447 vim_free(fname);
6448 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6449 FALSE, curbuf->b_ffname);
6450 }
6451 }
6452
6453 if (fname != NULL)
6454 {
6455 eap->arg = fname;
6456#endif
6457 do_exedit(eap, NULL);
6458#ifdef FEAT_SEARCHPATH
6459 vim_free(fname);
6460 }
6461#endif
6462}
6463
6464/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006465 * ":open" simulation: for now just work like ":visual".
6466 */
6467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006468ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006469{
6470 regmatch_T regmatch;
6471 char_u *p;
6472
6473 curwin->w_cursor.lnum = eap->line2;
6474 beginline(BL_SOL | BL_FIX);
6475 if (*eap->arg == '/')
6476 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006477 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006478 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006479 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006480 *p = NUL;
6481 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6482 if (regmatch.regprog != NULL)
6483 {
6484 regmatch.rm_ic = p_ic;
6485 p = ml_get_curline();
6486 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006487 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006488 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006489 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006490 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006491 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006492 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006493 eap->arg += STRLEN(eap->arg);
6494 }
6495 check_cursor();
6496
6497 eap->cmdidx = CMD_visual;
6498 do_exedit(eap, NULL);
6499}
6500
6501/*
6502 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 */
6504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006505ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506{
6507 do_exedit(eap, NULL);
6508}
6509
6510/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006511 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006514do_exedit(
6515 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006516 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517{
6518 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006520 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006522 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6523 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006524 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 /*
6526 * ":vi" command ends Ex mode.
6527 */
6528 if (exmode_active && (eap->cmdidx == CMD_visual
6529 || eap->cmdidx == CMD_view))
6530 {
6531 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006532 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006534 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006535 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006536 if (global_busy)
6537 {
6538 int rd = RedrawingDisabled;
6539 int nwr = no_wait_return;
6540 int ms = msg_scroll;
6541#ifdef FEAT_GUI
6542 int he = hold_gui_events;
6543#endif
6544
6545 if (eap->nextcmd != NULL)
6546 {
6547 stuffReadbuff(eap->nextcmd);
6548 eap->nextcmd = NULL;
6549 }
6550
6551 if (exmode_was != EXMODE_VIM)
6552 settmode(TMODE_RAW);
6553 RedrawingDisabled = 0;
6554 no_wait_return = 0;
6555 need_wait_return = FALSE;
6556 msg_scroll = 0;
6557#ifdef FEAT_GUI
6558 hold_gui_events = 0;
6559#endif
6560 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006561 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006562
6563 main_loop(FALSE, TRUE);
6564
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006565 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006566 RedrawingDisabled = rd;
6567 no_wait_return = nwr;
6568 msg_scroll = ms;
6569#ifdef FEAT_GUI
6570 hold_gui_events = he;
6571#endif
6572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 }
6576
6577 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006578 || eap->cmdidx == CMD_tabnew
6579 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006580 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006582 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 setpcmark();
6584 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006585 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6586 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006588 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 || *eap->arg != NUL
6590#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006591 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592#endif
6593 )
6594 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006595 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6596 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006597 if (*eap->arg != NUL && curbuf_locked())
6598 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006599
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 n = readonlymode;
6601 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6602 readonlymode = TRUE;
6603 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006604 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6605 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 setpcmark();
6607 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6608 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006609 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6611 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6612 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006613 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006615 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006616 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006618 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006620 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 if (old_curwin != NULL)
6622 {
6623 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006624 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006626#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006627 cleanup_T cs;
6628
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006629 // Reset the error/interrupt/exception state here so that
6630 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006631 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006632#endif
6633#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006635#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006636 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006637
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006638#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006639 // Restore the error/interrupt/exception state if not
6640 // discarded by a new aborting error, interrupt, or
6641 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006642 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 }
6645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 }
6647 else if (readonlymode && curbuf->b_nwindows == 1)
6648 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006649 // When editing an already visited buffer, 'readonly' won't be set
6650 // but the previous value is kept. With ":view" and ":sview" we
6651 // want the file to be readonly, except when another window is
6652 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 curbuf->b_p_ro = TRUE;
6654 }
6655 readonlymode = n;
6656 }
6657 else
6658 {
6659 if (eap->do_ecmd_cmd != NULL)
6660 do_cmdline_cmd(eap->do_ecmd_cmd);
6661#ifdef FEAT_TITLE
6662 n = curwin->w_arg_idx_invalid;
6663#endif
6664 check_arg_idx(curwin);
6665#ifdef FEAT_TITLE
6666 if (n != curwin->w_arg_idx_invalid)
6667 maketitle();
6668#endif
6669 }
6670
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 /*
6672 * if ":split file" worked, set alternate file name in old window to new
6673 * file
6674 */
6675 if (old_curwin != NULL
6676 && *eap->arg != NUL
6677 && curwin != old_curwin
6678 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006679 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006680 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682
6683 ex_no_reprint = TRUE;
6684}
6685
6686#ifndef FEAT_GUI
6687/*
6688 * ":gui" and ":gvim" when there is no GUI.
6689 */
6690 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006691ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006693 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694}
6695#endif
6696
Bram Moolenaar4f974752019-02-17 17:44:42 +01006697#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006699ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700{
6701 gui_make_tearoff(eap->arg);
6702}
6703#endif
6704
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006705#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6706 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006708ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006710# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6711 if (gui.in_use)
6712 gui_make_popup(eap->arg, eap->forceit);
6713# ifdef FEAT_TERM_POPUP_MENU
6714 else
6715# endif
6716# endif
6717# ifdef FEAT_TERM_POPUP_MENU
6718 pum_make_popup(eap->arg, eap->forceit);
6719# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720}
6721#endif
6722
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006724ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725{
6726 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006727 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006729 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730}
6731
6732/*
6733 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6734 * offset.
6735 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006738ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006741 win_T *save_curwin = curwin;
6742 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 long topline;
6744 long y;
6745 linenr_T old_linenr = curwin->w_cursor.lnum;
6746
6747 setpcmark();
6748
6749 /*
6750 * determine max topline
6751 */
6752 if (curwin->w_p_scb)
6753 {
6754 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006755 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 {
6757 if (wp->w_p_scb && wp->w_buffer)
6758 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006759 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 if (topline > y)
6761 topline = y;
6762 }
6763 }
6764 if (topline < 1)
6765 topline = 1;
6766 }
6767 else
6768 {
6769 topline = 1;
6770 }
6771
6772
6773 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006774 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006776 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 {
6778 if (curwin->w_p_scb)
6779 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006780 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 y = topline - curwin->w_topline;
6782 if (y > 0)
6783 scrollup(y, TRUE);
6784 else
6785 scrolldown(-y, TRUE);
6786 curwin->w_scbind_pos = topline;
6787 redraw_later(VALID);
6788 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 }
6791 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006792 curwin = save_curwin;
6793 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 if (curwin->w_p_scb)
6795 {
6796 did_syncbind = TRUE;
6797 checkpcmark();
6798 if (old_linenr != curwin->w_cursor.lnum)
6799 {
6800 char_u ctrl_o[2];
6801
6802 ctrl_o[0] = Ctrl_O;
6803 ctrl_o[1] = 0;
6804 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6805 }
6806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807}
6808
6809
6810 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006811ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006813 int i;
6814 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6815 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006817 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 do_bang(1, eap, FALSE, FALSE, TRUE);
6819 else
6820 {
6821 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6822 return;
6823
6824#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006825 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 {
6827 char_u *browseFile;
6828
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006829 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 NULL, NULL, NULL, curbuf);
6831 if (browseFile != NULL)
6832 {
6833 i = readfile(browseFile, NULL,
6834 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6835 vim_free(browseFile);
6836 }
6837 else
6838 i = OK;
6839 }
6840 else
6841#endif
6842 if (*eap->arg == NUL)
6843 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006844 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 return;
6846 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6847 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6848 }
6849 else
6850 {
6851 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6852 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6853 i = readfile(eap->arg, NULL,
6854 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6855
6856 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006857 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006859#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 if (!aborting())
6861#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006862 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 }
6864 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006865 {
6866 if (empty && exmode_active)
6867 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006868 // Delete the empty line that remains. Historically ex does
6869 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006870 if (eap->line2 == 0)
6871 lnum = curbuf->b_ml.ml_line_count;
6872 else
6873 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006874 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006875 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006876 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006877 if (curwin->w_cursor.lnum > 1
6878 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006879 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006880 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006881 }
6882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 }
6886}
6887
Bram Moolenaarea408852005-06-25 22:49:46 +00006888static char_u *prev_dir = NULL;
6889
6890#if defined(EXITFREE) || defined(PROTO)
6891 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006892free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006893{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006894 VIM_CLEAR(prev_dir);
6895 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006896}
6897#endif
6898
Bram Moolenaarf4258302013-06-02 18:20:17 +02006899/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006900 * Get the previous directory for the given chdir scope.
6901 */
6902 static char_u *
6903get_prevdir(cdscope_T scope)
6904{
6905 if (scope == CDSCOPE_WINDOW)
6906 return curwin->w_prevdir;
6907 else if (scope == CDSCOPE_TABPAGE)
6908 return curtab->tp_prevdir;
6909 return prev_dir;
6910}
6911
6912/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006913 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006914 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6915 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006916 */
6917 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006918post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006919{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006920 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006921 // Clear tab local directory for both :cd and :tcd
6922 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006923 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006924 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006925 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006926 char_u *pdir = get_prevdir(scope);
6927
6928 // If still in the global directory, need to remember current
6929 // directory as the global directory.
6930 if (globaldir == NULL && pdir != NULL)
6931 globaldir = vim_strsave(pdir);
6932
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006933 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006934 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006935 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006936 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006937 curtab->tp_localdir = vim_strsave(NameBuff);
6938 else
6939 curwin->w_localdir = vim_strsave(NameBuff);
6940 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006941 }
6942 else
6943 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006944 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006945 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006946 }
6947
6948 shorten_fnames(TRUE);
6949}
6950
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006951/*
6952 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006953 * chdir() function.
6954 * scope == CDSCOPE_WINDOW: changes the window-local directory
6955 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6956 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006957 * Returns TRUE if the directory is successfully changed.
6958 */
6959 int
6960changedir_func(
6961 char_u *new_dir,
6962 int forceit,
6963 cdscope_T scope)
6964{
6965 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006966 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006967 int dir_differs;
6968 int retval = FALSE;
6969
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006970 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006971 return FALSE;
6972
6973 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6974 {
6975 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6976 return FALSE;
6977 }
6978
6979 // ":cd -": Change to previous directory
6980 if (STRCMP(new_dir, "-") == 0)
6981 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006982 pdir = get_prevdir(scope);
6983 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006984 {
6985 emsg(_("E186: No previous directory"));
6986 return FALSE;
6987 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006988 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006989 }
6990
Bram Moolenaar002bc792020-06-05 22:33:42 +02006991 // Free the previous directory
6992 tofree = get_prevdir(scope);
6993
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006994 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006995 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006996 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006997 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006998 pdir = NULL;
6999 if (scope == CDSCOPE_WINDOW)
7000 curwin->w_prevdir = pdir;
7001 else if (scope == CDSCOPE_TABPAGE)
7002 curtab->tp_prevdir = pdir;
7003 else
7004 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007005
7006#if defined(UNIX) || defined(VMS)
7007 // for UNIX ":cd" means: go to home directory
7008 if (*new_dir == NUL)
7009 {
7010 // use NameBuff for home directory name
7011# ifdef VMS
7012 char_u *p;
7013
7014 p = mch_getenv((char_u *)"SYS$LOGIN");
7015 if (p == NULL || *p == NUL) // empty is the same as not set
7016 NameBuff[0] = NUL;
7017 else
7018 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7019# else
7020 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7021# endif
7022 new_dir = NameBuff;
7023 }
7024#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02007025 dir_differs = new_dir == NULL || pdir == NULL
7026 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007027 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7028 emsg(_(e_failed));
7029 else
7030 {
7031 char_u *acmd_fname;
7032
7033 post_chdir(scope);
7034
7035 if (dir_differs)
7036 {
7037 if (scope == CDSCOPE_WINDOW)
7038 acmd_fname = (char_u *)"window";
7039 else if (scope == CDSCOPE_TABPAGE)
7040 acmd_fname = (char_u *)"tabpage";
7041 else
7042 acmd_fname = (char_u *)"global";
7043 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7044 curbuf);
7045 }
7046 retval = TRUE;
7047 }
7048 vim_free(tofree);
7049
7050 return retval;
7051}
Bram Moolenaarea408852005-06-25 22:49:46 +00007052
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007054 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007056 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007057ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007059 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060
7061 new_dir = eap->arg;
7062#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007063 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 if (*new_dir == NUL)
7065 ex_pwd(NULL);
7066 else
7067#endif
7068 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007069 cdscope_T scope = CDSCOPE_GLOBAL;
7070
7071 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7072 scope = CDSCOPE_WINDOW;
7073 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7074 scope = CDSCOPE_TABPAGE;
7075
7076 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007077 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007078 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007079 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 ex_pwd(eap);
7081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 }
7083}
7084
7085/*
7086 * ":pwd".
7087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007089ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090{
7091 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7092 {
7093#ifdef BACKSLASH_IN_FILENAME
7094 slash_adjust(NameBuff);
7095#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007096 if (p_verbose > 0)
7097 {
7098 char *context = "global";
7099
7100 if (curwin->w_localdir != NULL)
7101 context = "window";
7102 else if (curtab->tp_localdir != NULL)
7103 context = "tabpage";
7104 smsg("[%s] %s", context, (char *)NameBuff);
7105 }
7106 else
7107 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 }
7109 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007110 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111}
7112
7113/*
7114 * ":=".
7115 */
7116 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007117ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007119 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007120 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121}
7122
7123 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007124ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007126 int n;
7127 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128
7129 if (cursor_valid())
7130 {
7131 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7132 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007133 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007135
7136 len = eap->line2;
7137 switch (*eap->arg)
7138 {
7139 case 'm': break;
7140 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007141 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007142 }
7143 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144}
7145
7146/*
7147 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7148 */
7149 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007150do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151{
Bram Moolenaar52953192019-08-16 10:27:13 +02007152 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007153 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007154# ifdef ELAPSED_FUNC
7155 elapsed_T start_tv;
7156
7157 // Remember at what time we started, so that we know how much longer we
7158 // should wait after waiting for a bit.
7159 ELAPSED_INIT(start_tv);
7160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161
7162 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007163 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007164 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007166 wait_now = msec - done > 1000L ? 1000L : msec - done;
7167#ifdef FEAT_TIMERS
7168 {
7169 long due_time = check_due_timer();
7170
7171 if (due_time > 0 && due_time < wait_now)
7172 wait_now = due_time;
7173 }
7174#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007175#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007176 if (has_any_channel() && wait_now > 20L)
7177 wait_now = 20L;
7178#endif
7179#ifdef FEAT_SOUND
7180 if (has_any_sound_callback() && wait_now > 20L)
7181 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007182#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007183 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007184
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007185#ifdef FEAT_JOB_CHANNEL
7186 if (has_any_channel())
7187 ui_breakcheck_force(TRUE);
7188 else
7189#endif
7190 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007191#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007192 // Process the netbeans and clientserver messages that may have been
7193 // received in the call to ui_breakcheck() when the GUI is in use. This
7194 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007195 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007196#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007197
7198# ifdef ELAPSED_FUNC
7199 // actual time passed
7200 done = ELAPSED_FUNC(start_tv);
7201# else
7202 // guestimate time passed (will actually be more)
7203 done += wait_now;
7204# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007206
7207 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7208 // input buffer, otherwise a following call to input() fails.
7209 if (got_int)
7210 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211}
7212
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213/*
7214 * ":winsize" command (obsolete).
7215 */
7216 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007217ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218{
7219 int w, h;
7220 char_u *arg = eap->arg;
7221 char_u *p;
7222
7223 w = getdigits(&arg);
7224 arg = skipwhite(arg);
7225 p = arg;
7226 h = getdigits(&arg);
7227 if (*p != NUL && *arg == NUL)
7228 set_shellsize(w, h, TRUE);
7229 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007230 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231}
7232
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007234ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235{
7236 int xchar = NUL;
7237 char_u *p;
7238
7239 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7240 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007241 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 if (eap->arg[1] == NUL)
7243 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007244 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 return;
7246 }
7247 xchar = eap->arg[1];
7248 p = eap->arg + 2;
7249 }
7250 else
7251 p = eap->arg + 1;
7252
7253 eap->nextcmd = check_nextcmd(p);
7254 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007255 if (*p != NUL && *p != (
7256#ifdef FEAT_EVAL
7257 in_vim9script() ? '#' :
7258#endif
7259 '"')
7260 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007261 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007262 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007264 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007265 postponed_split_flags = cmdmod.cmod_split;
7266 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7268 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007269 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 }
7271}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272
Bram Moolenaar843ee412004-06-30 16:16:41 +00007273#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274/*
7275 * ":winpos".
7276 */
7277 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007278ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279{
7280 int x, y;
7281 char_u *arg = eap->arg;
7282 char_u *p;
7283
7284 if (*arg == NUL)
7285 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007286# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007287# ifdef VIMDLL
7288 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7289 mch_get_winpos(&x, &y) != FAIL)
7290# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007292# else
7293 if (mch_get_winpos(&x, &y) != FAIL)
7294# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 {
7296 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007297 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 }
7299 else
7300# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007301 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 }
7303 else
7304 {
7305 x = getdigits(&arg);
7306 arg = skipwhite(arg);
7307 p = arg;
7308 y = getdigits(&arg);
7309 if (*p == NUL || *arg != NUL)
7310 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007311 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 return;
7313 }
7314# ifdef FEAT_GUI
7315 if (gui.in_use)
7316 gui_mch_set_winpos(x, y);
7317 else if (gui.starting)
7318 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007319 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 gui_win_x = x;
7321 gui_win_y = y;
7322 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007323# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 else
7325# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007326# endif
7327# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007328 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329# endif
7330# ifdef HAVE_TGETENT
7331 if (*T_CWP)
7332 term_set_winpos(x, y);
7333# endif
7334 }
7335}
7336#endif
7337
7338/*
7339 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7340 */
7341 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007342ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343{
7344 oparg_T oa;
7345
7346 clear_oparg(&oa);
7347 oa.regname = eap->regname;
7348 oa.start.lnum = eap->line1;
7349 oa.end.lnum = eap->line2;
7350 oa.line_count = eap->line2 - eap->line1 + 1;
7351 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007353 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 {
7355 setpcmark();
7356 curwin->w_cursor.lnum = eap->line1;
7357 beginline(BL_SOL | BL_FIX);
7358 }
7359
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007360 if (VIsual_active)
7361 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007362
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 switch (eap->cmdidx)
7364 {
7365 case CMD_delete:
7366 oa.op_type = OP_DELETE;
7367 op_delete(&oa);
7368 break;
7369
7370 case CMD_yank:
7371 oa.op_type = OP_YANK;
7372 (void)op_yank(&oa, FALSE, TRUE);
7373 break;
7374
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007375 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007376 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007378 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7379#else
7380 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007382 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 oa.op_type = OP_RSHIFT;
7384 else
7385 oa.op_type = OP_LSHIFT;
7386 op_shift(&oa, FALSE, eap->amount);
7387 break;
7388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007390 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391}
7392
7393/*
7394 * ":put".
7395 */
7396 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007397ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007399 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 if (eap->line2 == 0)
7401 {
7402 eap->line2 = 1;
7403 eap->forceit = TRUE;
7404 }
7405 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007406 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007407 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408}
7409
7410/*
7411 * Handle ":copy" and ":move".
7412 */
7413 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007414ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415{
7416 long n;
7417
Bram Moolenaar491799b2020-08-01 19:23:43 +02007418#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007419 if (not_in_vim9(eap) == FAIL)
7420 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007421#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007422 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007423 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 {
7425 eap->nextcmd = NULL;
7426 return;
7427 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007428 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429
7430 /*
7431 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7432 */
7433 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7434 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007435 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 return;
7437 }
7438
7439 if (eap->cmdidx == CMD_move)
7440 {
7441 if (do_move(eap->line1, eap->line2, n) == FAIL)
7442 return;
7443 }
7444 else
7445 ex_copy(eap->line1, eap->line2, n);
7446 u_clearline();
7447 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007448 ex_may_print(eap);
7449}
7450
7451/*
7452 * Print the current line if flags were given to the Ex command.
7453 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007454 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007455ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007456{
7457 if (eap->flags != 0)
7458 {
7459 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7460 (eap->flags & EXFLAG_LIST));
7461 ex_no_reprint = TRUE;
7462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463}
7464
7465/*
7466 * ":smagic" and ":snomagic".
7467 */
7468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007469ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470{
7471 int magic_save = p_magic;
7472
7473 p_magic = (eap->cmdidx == CMD_smagic);
Bram Moolenaar66e00142020-08-12 21:58:12 +02007474 ex_substitute(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 p_magic = magic_save;
7476}
7477
7478/*
7479 * ":join".
7480 */
7481 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007482ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483{
7484 curwin->w_cursor.lnum = eap->line1;
7485 if (eap->line1 == eap->line2)
7486 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007487 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 return;
7489 if (eap->line2 == curbuf->b_ml.ml_line_count)
7490 {
7491 beep_flush();
7492 return;
7493 }
7494 ++eap->line2;
7495 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007496 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007498 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499}
7500
7501/*
7502 * ":[addr]@r" or ":[addr]*r": execute register
7503 */
7504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007505ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506{
7507 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007508 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509
7510 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007511 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512
7513#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007514 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515#endif
7516
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007517 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 c = *eap->arg;
7519 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7520 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007521 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007522 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7523 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007524 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 else
7528 {
7529 int save_efr = exec_from_reg;
7530
7531 exec_from_reg = TRUE;
7532
7533 /*
7534 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007535 * Continue until the stuff buffer is empty and all added characters
7536 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007538 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7540
7541 exec_from_reg = save_efr;
7542 }
7543}
7544
7545/*
7546 * ":!".
7547 */
7548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007549ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550{
7551 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7552}
7553
7554/*
7555 * ":undo".
7556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007558ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007560 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007561 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007562 else
7563 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564}
7565
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007566#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007568ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007569{
7570 char_u hash[UNDO_HASH_SIZE];
7571
7572 u_compute_hash(hash);
7573 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7574}
7575
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007577ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007578{
7579 char_u hash[UNDO_HASH_SIZE];
7580
7581 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007582 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007583}
7584#endif
7585
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586/*
7587 * ":redo".
7588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007590ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591{
7592 u_redo(1);
7593}
7594
7595/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007596 * ":earlier" and ":later".
7597 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007599ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007600{
7601 long count = 0;
7602 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007603 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007604 char_u *p = eap->arg;
7605
7606 if (*p == NUL)
7607 count = 1;
7608 else if (isdigit(*p))
7609 {
7610 count = getdigits(&p);
7611 switch (*p)
7612 {
7613 case 's': ++p; sec = TRUE; break;
7614 case 'm': ++p; sec = TRUE; count *= 60; break;
7615 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007616 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7617 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007618 }
7619 }
7620
7621 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007622 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007623 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007624 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7625 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007626}
7627
7628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 * ":redir": start/stop redirection.
7630 */
7631 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007632ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633{
7634 char *mode;
7635 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007636 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637
Bram Moolenaarba768492016-07-08 15:32:54 +02007638#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007639 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007640 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007641 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007642 return;
7643 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007644#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007645
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 if (STRICMP(eap->arg, "END") == 0)
7647 close_redir();
7648 else
7649 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007650 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007652 ++arg;
7653 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007655 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 mode = "a";
7657 }
7658 else
7659 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007660 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661
7662 close_redir();
7663
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007664 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007665 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 if (fname == NULL)
7667 return;
7668#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007669 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 {
7671 char_u *browseFile;
7672
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007673 browseFile = do_browse(BROWSE_SAVE,
7674 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007675 fname, NULL, NULL,
7676 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007678 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 vim_free(fname);
7680 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007681 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 }
7683#endif
7684
7685 redir_fd = open_exfile(fname, eap->forceit, mode);
7686 vim_free(fname);
7687 }
7688#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007689 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007691 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007693 ++arg;
7694 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007696 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007697 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007699 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007701 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007702 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007703 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007704 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007706 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007707 if (*arg == '>')
7708 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007709 // Make register empty when not using @A-@Z and the
7710 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007711 if (*arg == NUL && !isupper(redir_reg))
7712 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007714 }
7715 if (*arg != NUL)
7716 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007717 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007718 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007719 }
7720 }
7721 else if (*arg == '=' && arg[1] == '>')
7722 {
7723 int append;
7724
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007725 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007726 close_redir();
7727 arg += 2;
7728
7729 if (*arg == '>')
7730 {
7731 ++arg;
7732 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 }
7734 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007735 append = FALSE;
7736
7737 if (var_redir_start(skipwhite(arg), append) == OK)
7738 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 }
7740#endif
7741
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007742 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007745 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007747
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007748 // Make sure redirection is not off. Can happen for cmdline completion
7749 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007750 if (redir_fd != NULL
7751#ifdef FEAT_EVAL
7752 || redir_reg || redir_vname
7753#endif
7754 )
7755 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756}
7757
7758/*
7759 * ":redraw": force redraw
7760 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007761 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007762ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763{
7764 int r = RedrawingDisabled;
7765 int p = p_lz;
7766
7767 RedrawingDisabled = 0;
7768 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007769 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007771 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772#ifdef FEAT_TITLE
7773 if (need_maketitle)
7774 maketitle();
7775#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007776#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7777# ifdef VIMDLL
7778 if (!gui.in_use)
7779# endif
7780 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 RedrawingDisabled = r;
7783 p_lz = p;
7784
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007785 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 msg_didout = FALSE;
7787 msg_col = 0;
7788
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007789 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007790 need_wait_return = FALSE;
7791
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 out_flush();
7793}
7794
7795/*
7796 * ":redrawstatus": force redraw of status line(s)
7797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007799ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 int r = RedrawingDisabled;
7802 int p = p_lz;
7803
7804 RedrawingDisabled = 0;
7805 p_lz = FALSE;
7806 if (eap->forceit)
7807 status_redraw_all();
7808 else
7809 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007810 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 RedrawingDisabled = r;
7812 p_lz = p;
7813 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814}
7815
Bram Moolenaare12bab32019-01-08 22:02:56 +01007816/*
7817 * ":redrawtabline": force redraw of the tabline
7818 */
7819 static void
7820ex_redrawtabline(exarg_T *eap UNUSED)
7821{
7822 int r = RedrawingDisabled;
7823 int p = p_lz;
7824
7825 RedrawingDisabled = 0;
7826 p_lz = FALSE;
7827
7828 draw_tabline();
7829
7830 RedrawingDisabled = r;
7831 p_lz = p;
7832 out_flush();
7833}
7834
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007836close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837{
7838 if (redir_fd != NULL)
7839 {
7840 fclose(redir_fd);
7841 redir_fd = NULL;
7842 }
7843#ifdef FEAT_EVAL
7844 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007845 if (redir_vname)
7846 {
7847 var_redir_stop();
7848 redir_vname = 0;
7849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850#endif
7851}
7852
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007853#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007854 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007855vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007856{
7857 if (vim_mkdir(name, prot) != 0)
7858 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007859 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007860 return FAIL;
7861 }
7862 return OK;
7863}
7864#endif
7865
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866/*
7867 * Open a file for writing for an Ex command, with some checks.
7868 * Return file descriptor, or NULL on failure.
7869 */
7870 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007871open_exfile(
7872 char_u *fname,
7873 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007874 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875{
7876 FILE *fd;
7877
7878#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007879 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 if (mch_isdir(fname))
7881 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007882 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 return NULL;
7884 }
7885#endif
7886 if (!forceit && *mode != 'a' && vim_fexists(fname))
7887 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007888 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 return NULL;
7890 }
7891
7892 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007893 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894
7895 return fd;
7896}
7897
7898/*
7899 * ":mark" and ":k".
7900 */
7901 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007902ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903{
7904 pos_T pos;
7905
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007906 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007907 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007908 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02007909 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 else
7911 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007912 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 curwin->w_cursor.lnum = eap->line2;
7914 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007915 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007916 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007917 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 }
7919}
7920
7921/*
7922 * Update w_topline, w_leftcol and the cursor position.
7923 */
7924 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007925update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007927 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 update_topline();
7929 if (!curwin->w_p_wrap)
7930 validate_cursor();
7931 update_curswant();
7932}
7933
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007935 * Save the current State and go to Normal mode.
7936 * Return TRUE if the typeahead could be saved.
7937 */
7938 int
7939save_current_state(save_state_T *sst)
7940{
7941 sst->save_msg_scroll = msg_scroll;
7942 sst->save_restart_edit = restart_edit;
7943 sst->save_msg_didout = msg_didout;
7944 sst->save_State = State;
7945 sst->save_insertmode = p_im;
7946 sst->save_finish_op = finish_op;
7947 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007948 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007949
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007950 msg_scroll = FALSE; // no msg scrolling in Normal mode
7951 restart_edit = 0; // don't go to Insert mode
7952 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007953
7954 /*
7955 * Save the current typeahead. This is required to allow using ":normal"
7956 * from an event handler and makes sure we don't hang when the argument
7957 * ends with half a command.
7958 */
7959 save_typeahead(&sst->tabuf);
7960 return sst->tabuf.typebuf_valid;
7961}
7962
7963 void
7964restore_current_state(save_state_T *sst)
7965{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007966 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007967 restore_typeahead(&sst->tabuf);
7968
7969 msg_scroll = sst->save_msg_scroll;
7970 restart_edit = sst->save_restart_edit;
7971 p_im = sst->save_insertmode;
7972 finish_op = sst->save_finish_op;
7973 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007974 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007975 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007976
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007977 // Restore the state (needed when called from a function executed for
7978 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007979 State = sst->save_State;
7980#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007981 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007982#endif
7983}
7984
7985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 * ":normal[!] {commands}": Execute normal mode commands.
7987 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007988 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007989ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007991 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 char_u *arg = NULL;
7993 int l;
7994 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007996 if (ex_normal_lock > 0)
7997 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007998 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007999 return;
8000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 if (ex_normal_busy >= p_mmd)
8002 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008003 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 return;
8005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 /*
8008 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8009 * this for the K_SPECIAL leading byte, otherwise special keys will not
8010 * work.
8011 */
8012 if (has_mbyte)
8013 {
8014 int len = 0;
8015
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008016 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 for (p = eap->arg; *p != NUL; ++p)
8018 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008019#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008020 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008022#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008023 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008024 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008025#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 )
8029 len += 2;
8030 }
8031 if (len > 0)
8032 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008033 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 if (arg != NULL)
8035 {
8036 len = 0;
8037 for (p = eap->arg; *p != NUL; ++p)
8038 {
8039 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008040#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 if (*p == CSI)
8042 {
8043 arg[len++] = KS_EXTRA;
8044 arg[len++] = (int)KE_CSI;
8045 }
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 Moolenaar071d4272004-06-13 20:20:40 +00008048 {
8049 arg[len++] = *++p;
8050 if (*p == K_SPECIAL)
8051 {
8052 arg[len++] = KS_SPECIAL;
8053 arg[len++] = KE_FILLER;
8054 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008055#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 else if (*p == CSI)
8057 {
8058 arg[len++] = KS_EXTRA;
8059 arg[len++] = (int)KE_CSI;
8060 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 }
8063 arg[len] = NUL;
8064 }
8065 }
8066 }
8067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008069 ++ex_normal_busy;
8070 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {
8072 /*
8073 * Repeat the :normal command for each line in the range. When no
8074 * range given, execute it just once, without positioning the cursor
8075 * first.
8076 */
8077 do
8078 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 if (eap->addr_count != 0)
8080 {
8081 curwin->w_cursor.lnum = eap->line1++;
8082 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008083 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 }
8085
Bram Moolenaar13505972019-01-24 15:04:48 +01008086 exec_normal_cmd(arg != NULL
8087 ? arg
8088 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 }
8090 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8091 }
8092
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008093 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 update_topline_cursor();
8095
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008096 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008098 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008099#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008100 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008101#endif
8102
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104}
8105
8106/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008107 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 */
8109 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008110ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008112 if (eap->forceit)
8113 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008114 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008115 if (!curwin->w_cursor.lnum)
8116 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008117 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008118 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008119#ifdef FEAT_TERMINAL
8120 // Ignore this when running in an active terminal.
8121 if (term_job_running(curbuf->b_term))
8122 return;
8123#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008124
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008125 // Ignore the command when already in Insert mode. Inserting an
8126 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008127 if (State & INSERT)
8128 return;
8129
Bram Moolenaar64969662005-12-14 21:59:55 +00008130 if (eap->cmdidx == CMD_startinsert)
8131 restart_edit = 'a';
8132 else if (eap->cmdidx == CMD_startreplace)
8133 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008135 restart_edit = 'V';
8136
8137 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008139 if (eap->cmdidx == CMD_startinsert)
8140 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008141 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 }
8143}
8144
8145/*
8146 * ":stopinsert"
8147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008149ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150{
8151 restart_edit = 0;
8152 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008153 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008156/*
8157 * Execute normal mode command "cmd".
8158 * "remap" can be REMAP_NONE or REMAP_YES.
8159 */
8160 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008161exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008162{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008163 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008164 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008165 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008166}
Bram Moolenaar25281632016-01-21 23:32:32 +01008167
Bram Moolenaar25281632016-01-21 23:32:32 +01008168/*
8169 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008170 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008171 */
8172 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008173exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008174{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008175 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008176 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008177
Bram Moolenaar905dd902019-04-07 14:21:47 +02008178 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8179 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008180 clear_oparg(&oa);
8181 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008182 while ((!stuff_empty()
8183 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008184 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008185 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008186 {
8187 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008188#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008189 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008190 && oa.op_type == OP_NOP && oa.regname == NUL
8191 && !VIsual_active)
8192 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008193 // If terminal_loop() returns OK we got a key that is handled
8194 // in Normal model. With FAIL we first need to position the
8195 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008196 if (terminal_loop(TRUE) == OK)
8197 normal_cmd(&oa, TRUE);
8198 }
8199 else
8200#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008201 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008202 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008203 }
8204}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008205
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206#ifdef FEAT_FIND_ID
8207 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008208ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209{
8210 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8211 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8212 (linenr_T)1, (linenr_T)MAXLNUM);
8213}
8214
Bram Moolenaar4033c552017-09-16 20:54:51 +02008215#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216/*
8217 * ":psearch"
8218 */
8219 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008220ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221{
8222 g_do_tagpreview = p_pvh;
8223 ex_findpat(eap);
8224 g_do_tagpreview = 0;
8225}
8226#endif
8227
8228 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008229ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230{
8231 int whole = TRUE;
8232 long n;
8233 char_u *p;
8234 int action;
8235
8236 switch (cmdnames[eap->cmdidx].cmd_name[2])
8237 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008238 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8240 action = ACTION_GOTO;
8241 else
8242 action = ACTION_SHOW;
8243 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008244 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 action = ACTION_SHOW_ALL;
8246 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008247 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 action = ACTION_GOTO;
8249 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008250 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 action = ACTION_SPLIT;
8252 break;
8253 }
8254
8255 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008256 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 {
8258 n = getdigits(&eap->arg);
8259 eap->arg = skipwhite(eap->arg);
8260 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008261 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 {
8263 whole = FALSE;
8264 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008265 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 if (*p)
8267 {
8268 *p++ = NUL;
8269 p = skipwhite(p);
8270
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008271 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008272 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008273 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 else
8275 eap->nextcmd = check_nextcmd(p);
8276 }
8277 }
8278 if (!eap->skip)
8279 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8280 whole, !eap->forceit,
8281 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8282 n, action, eap->line1, eap->line2);
8283}
8284#endif
8285
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286
Bram Moolenaar4033c552017-09-16 20:54:51 +02008287#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288/*
8289 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8290 */
8291 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008292ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008294 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8296}
8297
8298/*
8299 * ":pedit"
8300 */
8301 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008302ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303{
8304 win_T *curwin_save = curwin;
8305
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008306 if (ERROR_IF_ANY_POPUP_WINDOW)
8307 return;
8308
Bram Moolenaar026587b2019-08-17 15:08:00 +02008309 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008311 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008312
Bram Moolenaar026587b2019-08-17 15:08:00 +02008313 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008315
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 if (curwin != curwin_save && win_valid(curwin_save))
8317 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008318 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 validate_cursor();
8320 redraw_later(VALID);
8321 win_enter(curwin_save, TRUE);
8322 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008323# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008324 else if (WIN_IS_POPUP(curwin))
8325 {
8326 // can't keep focus in popup window
8327 win_enter(firstwin, TRUE);
8328 }
8329# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 g_do_tagpreview = 0;
8331}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333
8334/*
8335 * ":stag", ":stselect" and ":stjump".
8336 */
8337 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008338ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339{
8340 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008341 postponed_split_flags = cmdmod.cmod_split;
8342 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8344 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008345 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347
8348/*
8349 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8350 */
8351 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008352ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353{
8354 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8355}
8356
8357 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008358ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359{
8360 int cmd;
8361
8362 switch (name[1])
8363 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008364 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008366 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008368 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008370 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008372 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008374 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008376 case 'f': // ":tfirst"
8377 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008379 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008381 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008383 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008385 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 return;
8387 }
8388#endif
8389 cmd = DT_TAG;
8390 break;
8391 }
8392
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008393 if (name[0] == 'l')
8394 {
8395#ifndef FEAT_QUICKFIX
8396 ex_ni(eap);
8397 return;
8398#else
8399 cmd = DT_LTAG;
8400#endif
8401 }
8402
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8404 eap->forceit, TRUE);
8405}
8406
8407/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008408 * Check "str" for starting with a special cmdline variable.
8409 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8410 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8411 */
8412 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008413find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008414{
8415 int len;
8416 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008417 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008418 "%",
8419#define SPEC_PERC 0
8420 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008421#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008422 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008423#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008424 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008425#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008426 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008427#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008428 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008429#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008430 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008431#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008432 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008433#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008434 "<stack>", // call stack
8435#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008436 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008437#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008438 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008439#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008440 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008441#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008442 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008443#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008444 "<SID>", // script ID: <SNR>123_
8445#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008446#ifdef FEAT_CLIENTSERVER
8447 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008448# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008449#endif
8450 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008451
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008452 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008453 {
8454 len = (int)STRLEN(spec_str[i]);
8455 if (STRNCMP(src, spec_str[i], len) == 0)
8456 {
8457 *usedlen = len;
8458 return i;
8459 }
8460 }
8461 return -1;
8462}
8463
8464/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 * Evaluate cmdline variables.
8466 *
8467 * change '%' to curbuf->b_ffname
8468 * '#' to curwin->w_altfile
8469 * '<cword>' to word under the cursor
8470 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008471 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 * '<cfile>' to path name under the cursor
8473 * '<sfile>' to sourced file name
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008474 * '<stack>' to call stack
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008475 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 * '<afile>' to file name for autocommand
8477 * '<abuf>' to buffer number for autocommand
8478 * '<amatch>' to matching name for autocommand
8479 *
8480 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8481 * "" for error without a message) and NULL is returned.
8482 * Returns an allocated string if a valid match was found.
8483 * Returns NULL if no match was found. "usedlen" then still contains the
8484 * number of characters to skip.
8485 */
8486 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008487eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008488 char_u *src, // pointer into commandline
8489 char_u *srcstart, // beginning of valid memory for src
8490 int *usedlen, // characters after src that are used
8491 linenr_T *lnump, // line number for :e command, or NULL
8492 char **errormsg, // pointer to error message
8493 int *escaped) // return value has escaped white space (can
8494 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495{
8496 int i;
8497 char_u *s;
8498 char_u *result;
8499 char_u *resultbuf = NULL;
8500 int resultlen;
8501 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008502 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008504 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507
8508 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008509 if (escaped != NULL)
8510 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511
8512 /*
8513 * Check if there is something to do.
8514 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008515 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008516 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 {
8518 *usedlen = 1;
8519 return NULL;
8520 }
8521
8522 /*
8523 * Skip when preceded with a backslash "\%" and "\#".
8524 * Note: In "\\%" the % is also not recognized!
8525 */
8526 if (src > srcstart && src[-1] == '\\')
8527 {
8528 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008529 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 return NULL;
8531 }
8532
8533 /*
8534 * word or WORD under cursor
8535 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008536 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8537 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008539 resultlen = find_ident_under_cursor(&result,
8540 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8541 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8542 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 if (resultlen == 0)
8544 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008545 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 return NULL;
8547 }
8548 }
8549
8550 /*
8551 * '#': Alternate file name
8552 * '%': Current file name
8553 * File name under the cursor
8554 * File name for autocommand
8555 * and following modifiers
8556 */
8557 else
8558 {
8559 switch (spec_idx)
8560 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008561 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 if (curbuf->b_fname == NULL)
8563 {
8564 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008565 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 }
8567 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008568 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008570 tilde_file = STRCMP(result, "~") == 0;
8571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 break;
8573
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008574 case SPEC_HASH: // '#' or "#99": alternate file
8575 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 {
8577 result = arg_all();
8578 resultbuf = result;
8579 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008580 if (escaped != NULL)
8581 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 break;
8584 }
8585 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008586 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008587 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008589 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008590 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008591 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008592 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008594 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008596 if (*usedlen < 2)
8597 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008598 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008599 *usedlen = 1;
8600 return NULL;
8601 }
8602#ifdef FEAT_EVAL
8603 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8604 (long)i);
8605 if (result == NULL)
8606 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008607 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008608 return NULL;
8609 }
8610#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008611 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 }
8615 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008616 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008617 if (i == 0 && src[1] == '<' && *usedlen > 1)
8618 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008619 buf = buflist_findnr(i);
8620 if (buf == NULL)
8621 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008622 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008623 return NULL;
8624 }
8625 if (lnump != NULL)
8626 *lnump = ECMD_LAST;
8627 if (buf->b_fname == NULL)
8628 {
8629 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008630 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008631 }
8632 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008633 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008634 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008635 tilde_file = STRCMP(result, "~") == 0;
8636 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 break;
8639
8640#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008641 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008642 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 if (result == NULL)
8644 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008645 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 return NULL;
8647 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008648 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 break;
8650#endif
8651
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008652 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008654 if (result != NULL && !autocmd_fname_full)
8655 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008656 // Still need to turn the fname into a full path. It is
8657 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008658 autocmd_fname_full = TRUE;
8659 result = FullName_save(autocmd_fname, FALSE);
8660 vim_free(autocmd_fname);
8661 autocmd_fname = result;
8662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 if (result == NULL)
8664 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008665 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 return NULL;
8667 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008668 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 break;
8670
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008671 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 if (autocmd_bufnr <= 0)
8673 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008674 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 return NULL;
8676 }
8677 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8678 result = strbuf;
8679 break;
8680
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008681 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 result = autocmd_match;
8683 if (result == NULL)
8684 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008685 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 return NULL;
8687 }
8688 break;
8689
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008690 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008691 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008692 result = estack_sfile(spec_idx == SPEC_SFILE
8693 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 if (result == NULL)
8695 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008696 *errormsg = spec_idx == SPEC_SFILE
8697 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8698 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 return NULL;
8700 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008701 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008703
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008704 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008705 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008706 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008707 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008708 return NULL;
8709 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008710 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008711 result = strbuf;
8712 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008713
8714#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008715 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008716 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008717 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008718 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008719 return NULL;
8720 }
8721 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008722 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008723 result = strbuf;
8724 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008725
Bram Moolenaar90944302020-08-01 20:45:11 +02008726 case SPEC_SID:
8727 if (current_sctx.sc_sid <= 0)
8728 {
8729 *errormsg = _(e_usingsid);
8730 return NULL;
8731 }
8732 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
8733 result = strbuf;
8734 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02008735#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02008736
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008737#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008738 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008739 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8740 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 result = strbuf;
8742 break;
8743#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008744
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008745 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008746 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008747 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 }
8749
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008750 resultlen = (int)STRLEN(result); // length of new string
8751 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 {
8753 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 resultlen = (int)(s - result);
8756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 else if (!skip_mod)
8758 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008759 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 &resultlen);
8761 if (result == NULL)
8762 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008763 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 return NULL;
8765 }
8766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 }
8768
8769 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8770 {
8771 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008772 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008773 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008775 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 result = NULL;
8777 }
8778 else
8779 result = vim_strnsave(result, resultlen);
8780 vim_free(resultbuf);
8781 return result;
8782}
8783
8784/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 * Expand the <sfile> string in "arg".
8786 *
8787 * Returns an allocated string, or NULL for any error.
8788 */
8789 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008790expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008792 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 int len;
8794 char_u *result;
8795 char_u *newres;
8796 char_u *repl;
8797 int srclen;
8798 char_u *p;
8799
8800 result = vim_strsave(arg);
8801 if (result == NULL)
8802 return NULL;
8803
8804 for (p = result; *p; )
8805 {
8806 if (STRNCMP(p, "<sfile>", 7) != 0)
8807 ++p;
8808 else
8809 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008810 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008811 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 if (errormsg != NULL)
8813 {
8814 if (*errormsg)
8815 emsg(errormsg);
8816 vim_free(result);
8817 return NULL;
8818 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008819 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 {
8821 p += srclen;
8822 continue;
8823 }
8824 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8825 newres = alloc(len);
8826 if (newres == NULL)
8827 {
8828 vim_free(repl);
8829 vim_free(result);
8830 return NULL;
8831 }
8832 mch_memmove(newres, result, (size_t)(p - result));
8833 STRCPY(newres + (p - result), repl);
8834 len = (int)STRLEN(newres);
8835 STRCAT(newres, p + srclen);
8836 vim_free(repl);
8837 vim_free(result);
8838 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008839 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 }
8841 }
8842
8843 return result;
8844}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008847/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008848 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008849 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008852dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 if (fname == NULL)
8855 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008856 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857}
8858#endif
8859
8860/*
8861 * ":behave {mswin,xterm}"
8862 */
8863 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008864ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865{
8866 if (STRCMP(eap->arg, "mswin") == 0)
8867 {
8868 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8869 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8870 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8871 set_option_value((char_u *)"keymodel", 0L,
8872 (char_u *)"startsel,stopsel", 0);
8873 }
8874 else if (STRCMP(eap->arg, "xterm") == 0)
8875 {
8876 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8877 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8878 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8879 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8880 }
8881 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008882 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883}
8884
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885static int filetype_detect = FALSE;
8886static int filetype_plugin = FALSE;
8887static int filetype_indent = FALSE;
8888
8889/*
8890 * ":filetype [plugin] [indent] {on,off,detect}"
8891 * on: Load the filetype.vim file to install autocommands for file types.
8892 * off: Load the ftoff.vim file to remove all autocommands for file types.
8893 * plugin on: load filetype.vim and ftplugin.vim
8894 * plugin off: load ftplugof.vim
8895 * indent on: load filetype.vim and indent.vim
8896 * indent off: load indoff.vim
8897 */
8898 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008899ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900{
8901 char_u *arg = eap->arg;
8902 int plugin = FALSE;
8903 int indent = FALSE;
8904
8905 if (*eap->arg == NUL)
8906 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008907 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008908 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 filetype_detect ? "ON" : "OFF",
8910 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8911 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8912 return;
8913 }
8914
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008915 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 for (;;)
8917 {
8918 if (STRNCMP(arg, "plugin", 6) == 0)
8919 {
8920 plugin = TRUE;
8921 arg = skipwhite(arg + 6);
8922 continue;
8923 }
8924 if (STRNCMP(arg, "indent", 6) == 0)
8925 {
8926 indent = TRUE;
8927 arg = skipwhite(arg + 6);
8928 continue;
8929 }
8930 break;
8931 }
8932 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8933 {
8934 if (*arg == 'o' || !filetype_detect)
8935 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008936 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 filetype_detect = TRUE;
8938 if (plugin)
8939 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008940 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 filetype_plugin = TRUE;
8942 }
8943 if (indent)
8944 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008945 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 filetype_indent = TRUE;
8947 }
8948 }
8949 if (*arg == 'd')
8950 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008951 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008952 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 }
8954 }
8955 else if (STRCMP(arg, "off") == 0)
8956 {
8957 if (plugin || indent)
8958 {
8959 if (plugin)
8960 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008961 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 filetype_plugin = FALSE;
8963 }
8964 if (indent)
8965 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008966 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 filetype_indent = FALSE;
8968 }
8969 }
8970 else
8971 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008972 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 filetype_detect = FALSE;
8974 }
8975 }
8976 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008977 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978}
8979
8980/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008981 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 */
8983 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008984ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985{
8986 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008987 {
8988 char_u *arg = eap->arg;
8989
8990 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8991 arg += 9;
8992
8993 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8994 if (arg != eap->arg)
8995 did_filetype = FALSE;
8996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009000ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001{
9002#ifdef FEAT_DIGRAPHS
9003 if (*eap->arg != NUL)
9004 putdigraph(eap->arg);
9005 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009006 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009008 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009#endif
9010}
9011
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009012#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9013 void
9014set_no_hlsearch(int flag)
9015{
9016 no_hlsearch = flag;
9017# ifdef FEAT_EVAL
9018 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9019# endif
9020}
9021
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022/*
9023 * ":nohlsearch"
9024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009026ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009028 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009029 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031#endif
9032
9033#ifdef FEAT_CRYPT
9034/*
9035 * ":X": Get crypt key
9036 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009038ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009040 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009041 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042}
9043#endif
9044
9045#ifdef FEAT_FOLDING
9046 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009047ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048{
9049 if (foldManualAllowed(TRUE))
9050 foldCreate(eap->line1, eap->line2);
9051}
9052
9053 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009054ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055{
9056 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9057 eap->forceit, FALSE);
9058}
9059
9060 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009061ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062{
9063 linenr_T lnum;
9064
Bram Moolenaar4facea32019-10-12 20:17:40 +02009065# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009066 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009067# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009068
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009069 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9071 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9072 ml_setmarked(lnum);
9073
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009074 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009076 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009077# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009078 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009079# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080}
9081#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009082
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009083#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009084/*
9085 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9086 * instead of a quickfix command.
9087 */
9088 int
9089is_loclist_cmd(int cmdidx)
9090{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009091 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009092 return FALSE;
9093 return cmdnames[cmdidx].cmd_name[0] == 'l';
9094}
9095#endif
9096
Bram Moolenaar4facea32019-10-12 20:17:40 +02009097#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009098 int
9099get_pressedreturn(void)
9100{
9101 return ex_pressedreturn;
9102}
9103
9104 void
9105set_pressedreturn(int val)
9106{
9107 ex_pressedreturn = val;
9108}
9109#endif