blob: f58415327e3f83951605a5e8dbd600ccef85dca5 [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 Moolenaare4ce8252019-08-04 15:30:16 +0200266# define ex_break ex_ni
267# define ex_breakadd ex_ni
268# define ex_breakdel ex_ni
269# define ex_breaklist ex_ni
270# define ex_call ex_ni
271# define ex_catch ex_ni
272# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200273# define ex_continue ex_ni
274# define ex_debug ex_ni
275# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200276# define ex_def ex_ni
277# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200278# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_echo ex_ni
281# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282# define ex_else ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200283# define ex_endfunction ex_ni
284# define ex_endif ex_ni
285# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200287# define ex_eval ex_ni
288# define ex_execute ex_ni
289# define ex_finally ex_ni
290# define ex_finish ex_ni
291# define ex_function ex_ni
292# define ex_if ex_ni
293# define ex_let ex_ni
294# define ex_lockvar ex_ni
295# define ex_oldfiles ex_ni
296# define ex_options ex_ni
297# define ex_packadd ex_ni
298# define ex_packloadall ex_ni
299# define ex_return ex_ni
300# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# define ex_throw ex_ni
302# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000304# define ex_unlockvar ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305# define ex_vim9script ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200306# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100307# define ex_import ex_ni
308# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200310#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311# define ex_loadview ex_ni
312#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200313#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314# define ex_viminfo ex_ni
315#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100316static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_filetype(exarg_T *eap);
318static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000320# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321# define ex_diffpatch ex_ni
322# define ex_diffgetput ex_ni
323# define ex_diffsplit ex_ni
324# define ex_diffthis ex_ni
325# define ex_diffupdate ex_ni
326#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100329static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#else
331# define ex_nohlsearch ex_ni
332# define ex_match ex_ni
333#endif
334#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100335static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336#else
337# define ex_X ex_ni
338#endif
339#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100340static void ex_fold(exarg_T *eap);
341static void ex_foldopen(exarg_T *eap);
342static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#else
344# define ex_fold ex_ni
345# define ex_foldopen ex_ni
346# define ex_folddo ex_ni
347#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100348#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349# define ex_language ex_ni
350#endif
351#ifndef FEAT_SIGNS
352# define ex_sign ex_ni
353#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000354#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200355# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200357# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360#ifndef FEAT_JUMPLIST
361# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200362# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363# define ex_changes ex_ni
364#endif
365
Bram Moolenaar05159a02005-02-26 23:04:13 +0000366#ifndef FEAT_PROFILE
367# define ex_profile ex_ni
368#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200369#ifndef FEAT_TERMINAL
370# define ex_terminal ex_ni
371#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200372#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
373# define ex_xrestore ex_ni
374#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100375#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200376# define ex_popupclear ex_ni
377#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000378
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379/*
380 * Declare cmdnames[].
381 */
382#define DO_DECLARE_EXCMD
383#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200384#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100385
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386static char_u dollar_command[2] = {'$', 0};
387
388
389#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100390// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391typedef struct
392{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100393 char_u *line; // command line
394 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395} wcmd_T;
396
397/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000398 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000402struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100404 garray_T *lines_gap; // growarray with line info
405 int current_line; // last read line from growarray
406 int repeating; // TRUE when looping a second time
407 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200408 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 void *cookie;
410};
411
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200412static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100413static int store_loop_line(garray_T *gap, char_u *line);
414static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000415
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100416// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000417struct dbg_stuff
418{
419 int trylevel;
420 int force_abort;
421 except_T *caught_stack;
422 char_u *vv_exception;
423 char_u *vv_throwpoint;
424 int did_emsg;
425 int got_int;
426 int did_throw;
427 int need_rethrow;
428 int check_cstack;
429 except_T *current_exception;
430};
431
Bram Moolenaared203462004-06-16 11:19:22 +0000432 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100433save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000434{
435 dsp->trylevel = trylevel; trylevel = 0;
436 dsp->force_abort = force_abort; force_abort = FALSE;
437 dsp->caught_stack = caught_stack; caught_stack = NULL;
438 dsp->vv_exception = v_exception(NULL);
439 dsp->vv_throwpoint = v_throwpoint(NULL);
440
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100441 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000442 dsp->did_emsg = did_emsg; did_emsg = FALSE;
443 dsp->got_int = got_int; got_int = FALSE;
444 dsp->did_throw = did_throw; did_throw = FALSE;
445 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
446 dsp->check_cstack = check_cstack; check_cstack = FALSE;
447 dsp->current_exception = current_exception; current_exception = NULL;
448}
449
450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100451restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000452{
453 suppress_errthrow = FALSE;
454 trylevel = dsp->trylevel;
455 force_abort = dsp->force_abort;
456 caught_stack = dsp->caught_stack;
457 (void)v_exception(dsp->vv_exception);
458 (void)v_throwpoint(dsp->vv_throwpoint);
459 did_emsg = dsp->did_emsg;
460 got_int = dsp->got_int;
461 did_throw = dsp->did_throw;
462 need_rethrow = dsp->need_rethrow;
463 check_cstack = dsp->check_cstack;
464 current_exception = dsp->current_exception;
465}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466#endif
467
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468/*
469 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
470 * command is given.
471 */
472 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100473do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100474 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475{
476 int save_msg_scroll;
477 int prev_msg_row;
478 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100479 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000480
481 if (improved)
482 exmode_active = EXMODE_VIM;
483 else
484 exmode_active = EXMODE_NORMAL;
485 State = NORMAL;
486
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100487 // When using ":global /pat/ visual" and then "Q" we return to continue
488 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000489 if (global_busy)
490 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491
492 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100493 ++RedrawingDisabled; // don't redisplay the window
494 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100496 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 ++hold_gui_events;
498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499
Bram Moolenaar32526b32019-01-19 17:43:09 +0100500 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 while (exmode_active)
502 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100503 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000504 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
505 {
506 exmode_active = FALSE;
507 break;
508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 msg_scroll = TRUE;
510 need_wait_return = FALSE;
511 ex_pressedreturn = FALSE;
512 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100513 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 prev_msg_row = msg_row;
515 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 if (improved)
517 {
518 cmdline_row = msg_row;
519 do_cmdline(NULL, getexline, NULL, 0);
520 }
521 else
522 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
523 lines_left = Rows - 1;
524
Bram Moolenaardf177f62005-02-22 08:39:57 +0000525 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100526 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100529 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000530 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000532 if (ex_pressedreturn)
533 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100534 // go up one line, to overwrite the ":<CR>" line, so the
535 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000536 msg_row = prev_msg_row;
537 if (prev_msg_row == Rows - 1)
538 msg_row--;
539 }
540 msg_col = 0;
541 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
542 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100545 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000546 {
547 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100548 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100550 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 }
553
554#ifdef FEAT_GUI
555 --hold_gui_events;
556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 --RedrawingDisabled;
558 --no_wait_return;
559 update_screen(CLEAR);
560 need_wait_return = FALSE;
561 msg_scroll = save_msg_scroll;
562}
563
564/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200565 * Print the executed command for when 'verbose' is set.
566 * When "lnum" is 0 only print the command.
567 */
568 static void
569msg_verbose_cmd(linenr_T lnum, char_u *cmd)
570{
571 ++no_wait_return;
572 verbose_enter_scroll();
573
574 if (lnum == 0)
575 smsg(_("Executing: %s"), cmd);
576 else
577 smsg(_("line %ld: %s"), (long)lnum, cmd);
578 if (msg_silent == 0)
579 msg_puts("\n"); // don't overwrite this
580
581 verbose_leave_scroll();
582 --no_wait_return;
583}
584
585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 * Execute a simple command line. Used for translated commands like "*".
587 */
588 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100589do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590{
591 return do_cmdline(cmd, NULL, NULL,
592 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
593}
594
595/*
596 * do_cmdline(): execute one Ex command line
597 *
598 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100599 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 * 2. Split up in parts separated with '|'.
601 *
602 * This function can be called recursively!
603 *
604 * flags:
605 * DOCMD_VERBOSE - The command will be included in the error message.
606 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100607 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 * DOCMD_KEYTYPED - Don't reset KeyTyped.
609 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
610 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
611 *
612 * return FAIL if cmdline could not be executed, OK otherwise
613 */
614 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100615do_cmdline(
616 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200617 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100618 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100619 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100621 char_u *next_cmdline; // next cmd to execute
622 char_u *cmdline_copy = NULL; // copy of cmd line
623 int used_getline = FALSE; // used "fgetline" to obtain command
624 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100626 int count = 0; // line number count
627 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 int retval = OK;
629#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100630 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100631 garray_T lines_ga; // keep lines for ":while"/":for"
632 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200633 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100634 char_u *fname = NULL; // function or script name
635 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
636 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
637 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200639 msglist_T **saved_msg_list = NULL;
640 msglist_T *private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100642 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200643 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000645 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000647 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100649# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650# define cmd_cookie cookie
651#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100652 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200653#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100654 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100656 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
657 // location for storing error messages to be converted to an exception.
658 // This ensures that the do_errthrow() call in do_one_cmd() does not
659 // combine the messages stored by an earlier invocation of do_one_cmd()
660 // with the command name of the later one. This would happen when
661 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 saved_msg_list = msg_list;
663 msg_list = &private_msg_list;
664 private_msg_list = NULL;
665#endif
666
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100667 // It's possible to create an endless loop with ":execute", catch that
668 // here. The value of 200 allows nested function calls, ":source", etc.
669 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100670 if (call_depth >= 200
671#ifdef FEAT_EVAL
672 && call_depth >= p_mfd
673#endif
674 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100676 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100678 // When converting to an exception, we do not include the command name
679 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100680 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 msg_list = saved_msg_list;
682#endif
683 return FAIL;
684 }
685 ++call_depth;
686
687#ifdef FEAT_EVAL
688 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000689 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 cstack.cs_trylevel = 0;
691 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000692 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
694
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100695 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100697 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100698 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000699 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 ++ex_nesting_level;
701
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100702 // Get the function or script name and the address where the next breakpoint
703 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000704 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {
706 fname = func_name(real_cookie);
707 breakpoint = func_breakpoint(real_cookie);
708 dbg_tick = func_dbg_tick(real_cookie);
709 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100710 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100712 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 breakpoint = source_breakpoint(real_cookie);
714 dbg_tick = source_dbg_tick(real_cookie);
715 }
716
717 /*
718 * Initialize "force_abort" and "suppress_errthrow" at the top level.
719 */
720 if (!recursive)
721 {
722 force_abort = FALSE;
723 suppress_errthrow = FALSE;
724 }
725
726 /*
727 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000728 * exception handling (used when debugging). Otherwise clear it to avoid
729 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000731 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000732 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000733 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200734 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735
736 initial_trylevel = trylevel;
737
738 /*
739 * "did_throw" will be set to TRUE when an exception is being thrown.
740 */
741 did_throw = FALSE;
742#endif
743 /*
744 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000745 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 * If force_abort is set, we cancel everything.
747 */
748 did_emsg = FALSE;
749
750 /*
751 * KeyTyped is only set when calling vgetc(). Reset it here when not
752 * calling vgetc() (sourced command lines).
753 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100754 if (!(flags & DOCMD_KEYTYPED)
755 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 KeyTyped = FALSE;
757
758 /*
759 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000760 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 * - for multiple commands on one line, separated with '|'
762 * - when repeating until there are no more lines (for ":source")
763 */
764 next_cmdline = cmdline;
765 do
766 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000767#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100768 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000769#endif
770
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100771 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 if (next_cmdline == NULL
773#ifdef FEAT_EVAL
774 && !force_abort
775 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000776 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777#endif
778 )
779 did_emsg = FALSE;
780
781 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000782 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100783 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 * 3. If a line is given: Make a copy, so we can mess with it.
785 */
786
787#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100788 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000789 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100791 // Each '|' separated command is stored separately in lines_ga, to
792 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100793 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100795 // Check if a function has returned or, unless it has an unclosed
796 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000797 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000799# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000800 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000801 func_line_end(real_cookie);
802# endif
803 if (func_has_ended(real_cookie))
804 {
805 retval = FAIL;
806 break;
807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000809#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000810 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100811 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812 script_line_end();
813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100815 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100816 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 {
818 retval = FAIL;
819 break;
820 }
821
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100822 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 if (breakpoint != NULL && dbg_tick != NULL
824 && *dbg_tick != debug_tick)
825 {
826 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100828 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 *dbg_tick = debug_tick;
830 }
831
832 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100833 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100835 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100837 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100839 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100840 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100842 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100843 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 *dbg_tick = debug_tick;
845 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000846# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000847 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000848 {
849 if (getline_is_func)
850 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100851 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000852 script_line_start();
853 }
854# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856#endif
857
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100858 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 if (next_cmdline == NULL)
860 {
861 /*
862 * Need to set msg_didout for the first line after an ":if",
863 * otherwise the ":if" will be overwritten.
864 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100865 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100867 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868#ifdef FEAT_EVAL
869 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
870#else
871 0
872#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200873 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100875 // Don't call wait_return for aborted command line. The NULL
876 // returned for the end of a sourced file or executed function
877 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 if (KeyTyped && !(flags & DOCMD_REPEAT))
879 need_wait_return = FALSE;
880 retval = FAIL;
881 break;
882 }
883 used_getline = TRUE;
884
885 /*
886 * Keep the first typed line. Clear it when more lines are typed.
887 */
888 if (flags & DOCMD_KEEPLINE)
889 {
890 vim_free(repeat_cmdline);
891 if (count == 0)
892 repeat_cmdline = vim_strsave(next_cmdline);
893 else
894 repeat_cmdline = NULL;
895 }
896 }
897
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100898 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 else if (cmdline_copy == NULL)
900 {
901 next_cmdline = vim_strsave(next_cmdline);
902 if (next_cmdline == NULL)
903 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100904 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 retval = FAIL;
906 break;
907 }
908 }
909 cmdline_copy = next_cmdline;
910
911#ifdef FEAT_EVAL
912 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200913 * Inside a while/for loop, and when the command looks like a ":while"
914 * or ":for", the line is stored, because we may need it later when
915 * looping.
916 *
917 * When there is a '|' and another command, it is stored separately,
918 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000919 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200920 *
921 * Pass a different "fgetline" function to do_one_cmd() below,
922 * that it stores lines in or reads them from "lines_ga". Makes it
923 * possible to define a function inside a while/for loop and handles
924 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200926 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200928 cmd_getline = get_loop_line;
929 cmd_cookie = (void *)&cmd_loop_cookie;
930 cmd_loop_cookie.lines_gap = &lines_ga;
931 cmd_loop_cookie.current_line = current_line;
932 cmd_loop_cookie.getline = fgetline;
933 cmd_loop_cookie.cookie = cookie;
934 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
935
936 // Save the current line when encountering it the first time.
937 if (current_line == lines_ga.ga_len
938 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 {
940 retval = FAIL;
941 break;
942 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200943 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200945 else
946 {
947 cmd_getline = fgetline;
948 cmd_cookie = cookie;
949 }
950
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 did_endif = FALSE;
952#endif
953
954 if (count++ == 0)
955 {
956 /*
957 * All output from the commands is put below each other, without
958 * waiting for a return. Don't do this when executing commands
959 * from a script or when being called recursive (e.g. for ":e
960 * +command file").
961 */
962 if (!(flags & DOCMD_NOWAIT) && !recursive)
963 {
964 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100965 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100967 msg_scroll = TRUE; // put messages below each other
968 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 ++RedrawingDisabled;
970 did_inc = TRUE;
971 }
972 }
973
Bram Moolenaar823654b2020-05-29 23:03:09 +0200974 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100975 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976
977 /*
978 * 2. Execute one '|' separated command.
979 * do_one_cmd() will return NULL if there is no trailing '|'.
980 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
981 */
982 ++recursive;
983 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
984#ifdef FEAT_EVAL
985 &cstack,
986#endif
987 cmd_getline, cmd_cookie);
988 --recursive;
989
990#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000991 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100992 // Use "current_line" from "cmd_loop_cookie", it may have been
993 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000994 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995#endif
996
997 if (next_cmdline == NULL)
998 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100999 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 /*
1002 * If the command was typed, remember it for the ':' register.
1003 * Do this AFTER executing the command to make :@: work.
1004 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001005 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 && new_last_cmdline != NULL)
1007 {
1008 vim_free(last_cmdline);
1009 last_cmdline = new_last_cmdline;
1010 new_last_cmdline = NULL;
1011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 }
1013 else
1014 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001015 // need to copy the command after the '|' to cmdline_copy, for the
1016 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001017 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 next_cmdline = cmdline_copy;
1019 }
1020
1021
1022#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001023 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001025 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 && !func_has_abort(real_cookie))
1027 did_emsg = FALSE;
1028
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001029 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 {
1031 ++current_line;
1032
1033 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001034 * An ":endwhile", ":endfor" and ":continue" is handled here.
1035 * If we were executing commands, jump back to the ":while" or
1036 * ":for".
1037 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001039 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001041 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001043 // Jump back to the matching ":while" or ":for". Be careful
1044 // not to use a cs_line[] from an entry that isn't a ":while"
1045 // or ":for": It would make "current_line" invalid and can
1046 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 if (!did_emsg && !got_int && !did_throw
1048 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001049 && (cstack.cs_flags[cstack.cs_idx]
1050 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 && cstack.cs_line[cstack.cs_idx] >= 0
1052 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1053 {
1054 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001055 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001056 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001057 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001059 // Check for the next breakpoint at or after the ":while"
1060 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 if (breakpoint != NULL)
1062 {
1063 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001064 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 fname,
1066 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1067 *dbg_tick = debug_tick;
1068 }
1069 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001070 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001072 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001074 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1075 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 }
1077 }
1078
1079 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001080 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001084 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001085 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 }
1087 }
1088
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001089 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001090 if (breakpoint != NULL && has_watchexpr())
1091 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001092 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001093 *dbg_tick = debug_tick;
1094 }
1095
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 /*
1097 * When not inside any ":while" loop, clear remembered lines.
1098 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001099 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 {
1101 if (lines_ga.ga_len > 0)
1102 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001103 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1105 free_cmdlines(&lines_ga);
1106 }
1107 current_line = 0;
1108 }
1109
1110 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001111 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1112 * being restored at the ":endtry". Reset them here and set the
1113 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1114 * This includes the case where a missing ":endif", ":endwhile" or
1115 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001117 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001119 cstack.cs_lflags &= ~CSL_HAD_FINA;
1120 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1121 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 did_throw ? (void *)current_exception : NULL);
1123 did_emsg = got_int = did_throw = FALSE;
1124 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1125 }
1126
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001127 // Update global "trylevel" for recursive calls to do_cmdline() from
1128 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 trylevel = initial_trylevel + cstack.cs_trylevel;
1130
1131 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001132 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 * files) is aborted because of an error, an interrupt, or an uncaught
1134 * exception, cancel everything. If it is left normally, reset
1135 * force_abort to get the non-EH compatible abortion behavior for
1136 * the rest of the script.
1137 */
1138 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1139 force_abort = FALSE;
1140
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001141 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001143#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144
1145 }
1146 /*
1147 * Continue executing command lines when:
1148 * - no CTRL-C typed, no aborting error, no exception thrown or try
1149 * conditionals need to be checked for executing finally clauses or
1150 * catching an interrupt exception
1151 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001152 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 * looping for ":source" command or function call.
1154 */
1155 while (!((got_int
1156#ifdef FEAT_EVAL
1157 || (did_emsg && force_abort) || did_throw
1158#endif
1159 )
1160#ifdef FEAT_EVAL
1161 && cstack.cs_trylevel == 0
1162#endif
1163 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001164 && !(did_emsg
1165#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001166 // Keep going when inside try/catch, so that the error can be
1167 // deal with, except when it is a syntax error, it may cause
1168 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001169 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1170#endif
1171 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001172 && (getline_equal(fgetline, cookie, getexmodeline)
1173 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 && (next_cmdline != NULL
1175#ifdef FEAT_EVAL
1176 || cstack.cs_idx >= 0
1177#endif
1178 || (flags & DOCMD_REPEAT)));
1179
1180 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001181 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182#ifdef FEAT_EVAL
1183 free_cmdlines(&lines_ga);
1184 ga_clear(&lines_ga);
1185
1186 if (cstack.cs_idx >= 0)
1187 {
1188 /*
1189 * If a sourced file or executed function ran to its end, report the
1190 * unclosed conditional.
1191 */
1192 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001193 && ((getline_equal(fgetline, cookie, getsourceline)
1194 && !source_finished(fgetline, cookie))
1195 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 && !func_has_ended(real_cookie))))
1197 {
1198 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001199 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001201 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001202 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001203 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001205 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 }
1207
1208 /*
1209 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1210 * ":endtry" in a sourced file or executed function. If the try
1211 * conditional is in its finally clause, ignore anything pending.
1212 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001213 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 */
1215 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001216 {
1217 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1218
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001219 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001220 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001221 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1222 &cstack.cs_looplevel);
1223 }
1224 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 trylevel = initial_trylevel;
1226 }
1227
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001228 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1229 // lack was reported above and the error message is to be converted to an
1230 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001231 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 ? (char_u *)"endfunction" : (char_u *)NULL);
1233
1234 if (trylevel == 0)
1235 {
1236 /*
1237 * When an exception is being thrown out of the outermost try
1238 * conditional, discard the uncaught exception, disable the conversion
1239 * of interrupts or errors to exceptions, and ensure that no more
1240 * commands are executed.
1241 */
1242 if (did_throw)
1243 {
1244 void *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001245 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246
1247 /*
1248 * If the uncaught exception is a user exception, report it as an
1249 * error. If it is an error exception, display the saved error
1250 * message now. For an interrupt exception, do nothing; the
1251 * interrupt message is given elsewhere.
1252 */
1253 switch (current_exception->type)
1254 {
1255 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001256 vim_snprintf((char *)IObuff, IOSIZE,
1257 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 current_exception->value);
1259 p = vim_strsave(IObuff);
1260 break;
1261 case ET_ERROR:
1262 messages = current_exception->messages;
1263 current_exception->messages = NULL;
1264 break;
1265 case ET_INTERRUPT:
1266 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 }
1268
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001269 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1270 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001271 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 current_exception->throw_name = NULL;
1273
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001274 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 suppress_errthrow = TRUE;
1276 force_abort = TRUE;
1277
1278 if (messages != NULL)
1279 {
1280 do
1281 {
1282 next = messages->next;
1283 emsg(messages->msg);
1284 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001285 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 vim_free(messages);
1287 messages = next;
1288 }
1289 while (messages != NULL);
1290 }
1291 else if (p != NULL)
1292 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001293 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 vim_free(p);
1295 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001296 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001297 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001298 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 }
1300
1301 /*
1302 * On an interrupt or an aborting error not converted to an exception,
1303 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001304 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 * when force_abort is set and did_emsg unset in case of an interrupt
1306 * from a finally clause after an error.
1307 */
1308 else if (got_int || (did_emsg && force_abort))
1309 suppress_errthrow = TRUE;
1310 }
1311
1312 /*
1313 * The current cstack will be freed when do_cmdline() returns. An uncaught
1314 * exception will have to be rethrown in the previous cstack. If a function
1315 * has just returned or a script file was just finished and the previous
1316 * cstack belongs to the same function or, respectively, script file, it
1317 * will have to be checked for finally clauses to be executed due to the
1318 * ":return" or ":finish". This is done in do_one_cmd().
1319 */
1320 if (did_throw)
1321 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001322 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001324 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 && ex_nesting_level > func_level(real_cookie) + 1))
1326 {
1327 if (!did_throw)
1328 check_cstack = TRUE;
1329 }
1330 else
1331 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001332 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001333 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 --ex_nesting_level;
1335 /*
1336 * Go to debug mode when returning from a function in which we are
1337 * single-stepping.
1338 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001339 if ((getline_equal(fgetline, cookie, getsourceline)
1340 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001342 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 ? (char_u *)_("End of sourced file")
1344 : (char_u *)_("End of function"));
1345 }
1346
1347 /*
1348 * Restore the exception environment (done after returning from the
1349 * debugger).
1350 */
1351 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001352 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353
1354 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001355
1356 // Cleanup if "cs_emsg_silent_list" remains.
1357 if (cstack.cs_emsg_silent_list != NULL)
1358 {
1359 eslist_T *elem, *temp;
1360
1361 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1362 {
1363 temp = elem->next;
1364 vim_free(elem);
1365 }
1366 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001367#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368
1369 /*
1370 * If there was too much output to fit on the command line, ask the user to
1371 * hit return before redrawing the screen. With the ":global" command we do
1372 * this only once after the command is finished.
1373 */
1374 if (did_inc)
1375 {
1376 --RedrawingDisabled;
1377 --no_wait_return;
1378 msg_scroll = FALSE;
1379
1380 /*
1381 * When just finished an ":if"-":else" which was typed, no need to
1382 * wait for hit-return. Also for an error situation.
1383 */
1384 if (retval == FAIL
1385#ifdef FEAT_EVAL
1386 || (did_endif && KeyTyped && !did_emsg)
1387#endif
1388 )
1389 {
1390 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001391 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 }
1393 else if (need_wait_return)
1394 {
1395 /*
1396 * The msg_start() above clears msg_didout. The wait_return we do
1397 * here should not overwrite the command that may be shown before
1398 * doing that.
1399 */
1400 msg_didout |= msg_didout_before_start;
1401 wait_return(FALSE);
1402 }
1403 }
1404
Bram Moolenaar2a942252012-11-28 23:03:07 +01001405#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001406 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001407#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 /*
1409 * Reset if_level, in case a sourced script file contains more ":if" than
1410 * ":endif" (could be ":if x | foo | endif").
1411 */
1412 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001413#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001414
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 --call_depth;
1416 return retval;
1417}
1418
1419#ifdef FEAT_EVAL
1420/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001421 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 */
1423 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001424get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001426 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 wcmd_T *wp;
1428 char_u *line;
1429
1430 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1431 {
1432 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001433 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001435 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001437 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001439 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001440 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 ++cp->current_line;
1442
1443 return line;
1444 }
1445
1446 KeyTyped = FALSE;
1447 ++cp->current_line;
1448 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001449 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 return vim_strsave(wp->line);
1451}
1452
1453/*
1454 * Store a line in "gap" so that a ":while" loop can execute it again.
1455 */
1456 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001457store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458{
1459 if (ga_grow(gap, 1) == FAIL)
1460 return FAIL;
1461 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001462 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 return OK;
1465}
1466
1467/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001468 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 */
1470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001471free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472{
1473 while (gap->ga_len > 0)
1474 {
1475 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1476 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 }
1478}
1479#endif
1480
1481/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001482 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1483 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001486getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001487 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001488 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001489 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
1491#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001492 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001493 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001495 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1496 // function that's originally used to obtain the lines. This may be
1497 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001498 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001499 cp = (struct loop_cookie *)cookie;
1500 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 {
1502 gp = cp->getline;
1503 cp = cp->cookie;
1504 }
1505 return gp == func;
1506#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001507 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508#endif
1509}
1510
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001512 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 * getline function. Otherwise return "cookie".
1514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001516getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001517 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001518 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
Bram Moolenaar13505972019-01-24 15:04:48 +01001520#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001521 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001522 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001524 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1525 // cookie that's originally used to obtain the lines. This may be nested
1526 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001527 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001528 cp = (struct loop_cookie *)cookie;
1529 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 {
1531 gp = cp->getline;
1532 cp = cp->cookie;
1533 }
1534 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001535#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001538}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539
Bram Moolenaard5053d02020-06-28 15:51:16 +02001540#if defined(FEAT_EVAL) || defined(PROT)
1541/*
1542 * Get the next line source line without advancing.
1543 */
1544 char_u *
1545getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001546 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001547 void *cookie) // argument for fgetline()
1548{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001549 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001550 struct loop_cookie *cp;
1551 wcmd_T *wp;
1552
1553 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1554 // cookie that's originally used to obtain the lines. This may be nested
1555 // several levels.
1556 gp = fgetline;
1557 cp = (struct loop_cookie *)cookie;
1558 while (gp == get_loop_line)
1559 {
1560 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1561 {
1562 // executing lines a second time, use the stored copy
1563 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1564 return wp->line;
1565 }
1566 gp = cp->getline;
1567 cp = cp->cookie;
1568 }
1569 if (gp == getsourceline)
1570 return source_nextline(cp);
1571 return NULL;
1572}
1573#endif
1574
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001575
1576/*
1577 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1578 * ":bwipeout", etc.
1579 * Returns the buffer number.
1580 */
1581 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001582compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001583{
1584 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001585 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001586 int count = offset;
1587
1588 buf = firstbuf;
1589 while (buf->b_next != NULL && buf->b_fnum < lnum)
1590 buf = buf->b_next;
1591 while (count != 0)
1592 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001593 count += (offset < 0) ? 1 : -1;
1594 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1595 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001596 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001597 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001598 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001599 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001600 while (buf->b_ml.ml_mfp == NULL)
1601 {
1602 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1603 if (nextbuf == NULL)
1604 break;
1605 buf = nextbuf;
1606 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001607 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001608 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001609 if (addr_type == ADDR_LOADED_BUFFERS)
1610 while (buf->b_ml.ml_mfp == NULL)
1611 {
1612 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1613 if (nextbuf == NULL)
1614 break;
1615 buf = nextbuf;
1616 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001617 return buf->b_fnum;
1618}
1619
Bram Moolenaarb7316892019-05-01 18:08:42 +02001620/*
1621 * Return the window number of "win".
1622 * When "win" is NULL return the number of windows.
1623 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001624 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001625current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001626{
1627 win_T *wp;
1628 int nr = 0;
1629
Bram Moolenaar29323592016-07-24 22:04:11 +02001630 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001631 {
1632 ++nr;
1633 if (wp == win)
1634 break;
1635 }
1636 return nr;
1637}
1638
1639 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001640current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001641{
1642 tabpage_T *tp;
1643 int nr = 0;
1644
Bram Moolenaar29323592016-07-24 22:04:11 +02001645 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001646 {
1647 ++nr;
1648 if (tp == tab)
1649 break;
1650 }
1651 return nr;
1652}
1653
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001654 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001655comment_start(char_u *p, int starts_with_colon UNUSED)
1656{
1657#ifdef FEAT_EVAL
1658 if (in_vim9script())
1659 return p[0] == '#' && p[1] != '{' && !starts_with_colon;
1660#endif
1661 return *p == '"';
1662}
1663
Bram Moolenaarf240e182014-11-27 18:33:02 +01001664# define CURRENT_WIN_NR current_win_nr(curwin)
1665# define LAST_WIN_NR current_win_nr(NULL)
1666# define CURRENT_TAB_NR current_tab_nr(curtab)
1667# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001668
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669/*
1670 * Execute one Ex command.
1671 *
1672 * If 'sourcing' is TRUE, the command will be included in the error message.
1673 *
1674 * 1. skip comment lines and leading space
1675 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001676 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001677 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001678 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001679 * 6. parse arguments
1680 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001682 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 *
1684 * This function may be called recursively!
1685 */
1686#if (_MSC_VER == 1200)
1687/*
Bram Moolenaared203462004-06-16 11:19:22 +00001688 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001690 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691#endif
1692 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001693do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001694 char_u **cmdlinep,
1695 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001697 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001699 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001700 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001702 char_u *p;
1703 linenr_T lnum;
1704 long n;
1705 char *errormsg = NULL; // error message
1706 char_u *after_modifier = NULL;
1707 exarg_T ea; // Ex command arguments
1708 int save_msg_scroll = msg_scroll;
1709 cmdmod_T save_cmdmod;
1710 int save_reg_executing = reg_executing;
1711 int ni; // set when Not Implemented
1712 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001713 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001714#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001715 int may_have_range;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001716 int vim9script = in_vim9script();
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718
Bram Moolenaara80faa82020-04-12 19:37:17 +02001719 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 ea.line1 = 1;
1721 ea.line2 = 1;
1722#ifdef FEAT_EVAL
1723 ++ex_nesting_level;
1724#endif
1725
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001726 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 if (quitmore
1728#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001729 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001730 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001731#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001732 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001733 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 --quitmore;
1735
1736 /*
1737 * Reset browse, confirm, etc.. They are restored when returning, for
1738 * recursive calls.
1739 */
1740 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001742 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001743 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1744 goto doend;
1745
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001746/*
1747 * 1. Skip comment lines and leading white space and colons.
1748 * 2. Handle command modifiers.
1749 */
1750 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001752 ea.cmdlinep = cmdlinep;
1753 ea.getline = fgetline;
1754 ea.cookie = cookie;
1755#ifdef FEAT_EVAL
1756 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001757 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001759 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001760 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001762 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763
1764#ifdef FEAT_EVAL
1765 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1766 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1767#else
1768 ea.skip = (if_level > 0);
1769#endif
1770
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001772 * 3. Skip over the range to find the command. Let "p" point to after it.
1773 *
1774 * We need the command to know what kind of range it uses.
1775 */
1776 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001777#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001778 // In Vim9 script a colon is required before the range.
1779 may_have_range = !vim9script || starts_with_colon;
1780 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001781#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001782 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783
1784#ifdef FEAT_EVAL
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001785 if (vim9script && !starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001786 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001787 if (ea.cmd == cmd + 1 && *cmd == '$')
1788 // should be "$VAR = val"
1789 --ea.cmd;
1790 else if (ea.cmd > cmd)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001791 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001792 emsg(_(e_colon_required_before_a_range));
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001793 goto doend;
1794 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001796 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 else
1798#endif
1799 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001800
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001801#ifdef FEAT_EVAL
1802# ifdef FEAT_PROFILE
1803 // Count this line for profiling if skip is TRUE.
1804 if (do_profiling == PROF_YES
1805 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1806 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1807 {
1808 int skip = did_emsg || got_int || did_throw;
1809
1810 if (ea.cmdidx == CMD_catch)
1811 skip = !skip && !(cstack->cs_idx >= 0
1812 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1813 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1814 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1815 skip = skip || !(cstack->cs_idx >= 0
1816 && !(cstack->cs_flags[cstack->cs_idx]
1817 & (CSF_ACTIVE | CSF_TRUE)));
1818 else if (ea.cmdidx == CMD_finally)
1819 skip = FALSE;
1820 else if (ea.cmdidx != CMD_endif
1821 && ea.cmdidx != CMD_endfor
1822 && ea.cmdidx != CMD_endtry
1823 && ea.cmdidx != CMD_endwhile)
1824 skip = ea.skip;
1825
1826 if (!skip)
1827 {
1828 if (getline_equal(fgetline, cookie, get_func_line))
1829 func_line_exec(getline_cookie(fgetline, cookie));
1830 else if (getline_equal(fgetline, cookie, getsourceline))
1831 script_line_exec();
1832 }
1833 }
1834# endif
1835
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001836 // May go to debug mode. If this happens and the ">quit" debug command is
1837 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001838 dbg_check_breakpoint(&ea);
1839 if (!ea.skip && got_int)
1840 {
1841 ea.skip = TRUE;
1842 (void)do_intthrow(cstack);
1843 }
1844#endif
1845
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001846/*
1847 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 *
1849 * where 'addr' is:
1850 *
1851 * % (entire file)
1852 * $ [+-NUM]
1853 * 'x [+-NUM] (where x denotes a currently defined mark)
1854 * . [+-NUM]
1855 * [+-NUM]..
1856 * NUM
1857 *
1858 * The ea.cmd pointer is updated to point to the first character following the
1859 * range spec. If an initial address is found, but no second, the upper bound
1860 * is equal to the lower.
1861 */
1862
Bram Moolenaar25190db2019-05-04 15:05:28 +02001863 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001864 if (!IS_USER_CMDIDX(ea.cmdidx))
1865 {
1866 if (ea.cmdidx != CMD_SIZE)
1867 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1868 else
1869 ea.addr_type = ADDR_LINES;
1870
Bram Moolenaar25190db2019-05-04 15:05:28 +02001871 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001872 if (ea.cmdidx == CMD_wincmd && p != NULL)
1873 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001874#ifdef FEAT_QUICKFIX
1875 // :.cc in quickfix window uses line number
1876 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1877 ea.addr_type = ADDR_OTHER;
1878#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001879 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001880
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001881 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001882#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001883 if (!may_have_range)
1884 ea.line1 = ea.line2 = default_address(&ea);
1885 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001886#endif
1887 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1888 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001891 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 */
1893
1894 /*
1895 * Skip ':' and any white space
1896 */
1897 ea.cmd = skipwhite(ea.cmd);
1898 while (*ea.cmd == ':')
1899 ea.cmd = skipwhite(ea.cmd + 1);
1900
1901 /*
1902 * If we got a line, but no command, then go to the line.
1903 * If we find a '|' or '\n' we set ea.nextcmd.
1904 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001905 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1906 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 {
1908 /*
1909 * strange vi behaviour:
1910 * ":3" jumps to line 3
1911 * ":3|..." prints line 3
1912 * ":|" prints current line
1913 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001914 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001916 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 {
1918 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001919 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 if ((errormsg = invalid_range(&ea)) == NULL)
1921 {
1922 correct_range(&ea);
1923 ex_print(&ea);
1924 }
1925 }
1926 else if (ea.addr_count != 0)
1927 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001928 if (ea.line2 > curbuf->b_ml.ml_line_count)
1929 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001930 // With '-' in 'cpoptions' a line number past the file is an
1931 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001932 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1933 ea.line2 = -1;
1934 else
1935 ea.line2 = curbuf->b_ml.ml_line_count;
1936 }
1937
1938 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001939 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 else
1941 {
1942 if (ea.line2 == 0)
1943 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 else
1945 curwin->w_cursor.lnum = ea.line2;
1946 beginline(BL_SOL | BL_FIX);
1947 }
1948 }
1949 goto doend;
1950 }
1951
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001952 // If this looks like an undefined user command and there are CmdUndefined
1953 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001954 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1955 && ASCII_ISUPPER(*ea.cmd)
1956 && has_cmdundefined())
1957 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001958 int ret;
1959
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001960 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001961 while (ASCII_ISALNUM(*p))
1962 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001963 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001964 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1965 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001966 // If the autocommands did something and didn't cause an error, try
1967 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001968 p = (ret
1969#ifdef FEAT_EVAL
1970 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001971#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001972 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001973 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001974
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 if (p == NULL)
1976 {
1977 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001978 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 goto doend;
1980 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001981 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001982 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1983 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 {
1985 errormsg = uc_fun_cmd();
1986 goto doend;
1987 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001988
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 if (ea.cmdidx == CMD_SIZE)
1990 {
1991 if (!ea.skip)
1992 {
1993 STRCPY(IObuff, _("E492: Not an editor command"));
1994 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001995 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001996 // If the modifier was parsed OK the error must be in the
1997 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001998 if (after_modifier != NULL)
1999 append_command(after_modifier);
2000 else
2001 append_command(*cmdlinep);
2002 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002003 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002004 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 }
2006 goto doend;
2007 }
2008
Bram Moolenaar958636c2014-10-21 20:01:58 +02002009 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2010 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002011#ifdef HAVE_EX_SCRIPT_NI
2012 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2013#endif
2014 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015
2016#ifndef FEAT_EVAL
2017 /*
2018 * When the expression evaluation is disabled, recognize the ":if" and
2019 * ":endif" commands and ignore everything in between it.
2020 */
2021 if (ea.cmdidx == CMD_if)
2022 ++if_level;
2023 if (if_level)
2024 {
2025 if (ea.cmdidx == CMD_endif)
2026 --if_level;
2027 goto doend;
2028 }
2029
2030#endif
2031
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002032 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002033 if (*p == '!' && ea.cmdidx != CMD_substitute
2034 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 {
2036 ++p;
2037 ea.forceit = TRUE;
2038 }
2039 else
2040 ea.forceit = FALSE;
2041
2042/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002043 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002045 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002046 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047
2048 if (!ea.skip)
2049 {
2050#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002051 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002053 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002054 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 goto doend;
2056 }
2057#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002058 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002059 {
2060 errormsg = _("E981: Command not allowed in rvim");
2061 goto doend;
2062 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002063 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002065 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002066 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 goto doend;
2068 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002069
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002070 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002072#ifdef FEAT_CMDWIN
2073 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2074 {
2075 // Command not allowed in the command line window
2076 errormsg = _(e_cmdwin);
2077 goto doend;
2078 }
2079#endif
2080 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2081 {
2082 // Command not allowed when text is locked
2083 errormsg = _(get_text_locked_msg());
2084 goto doend;
2085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002087
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002088 // Disallow editing another buffer when "curbuf_lock" is set.
2089 // Do allow ":checktime" (it is postponed).
2090 // Do allow ":edit" (check for an argument later).
2091 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002092 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002093 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002094 && ea.cmdidx != CMD_edit
2095 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002096 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002097 && curbuf_locked())
2098 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002100 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002102 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002103 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 goto doend;
2105 }
2106 }
2107
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002108 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002110 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 goto doend;
2112 }
2113
2114 /*
2115 * Don't complain about the range if it is not used
2116 * (could happen if line_count is accidentally set to 0).
2117 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002118 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 {
2120 /*
2121 * If the range is backwards, ask for confirmation and, if given, swap
2122 * ea.line1 & ea.line2 so it's forwards again.
2123 * When global command is busy, don't ask, will fail below.
2124 */
2125 if (!global_busy && ea.line1 > ea.line2)
2126 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002127 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002129 if (sourcing || exmode_active)
2130 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002131 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002132 goto doend;
2133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 if (ask_yesno((char_u *)
2135 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002136 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 }
2138 lnum = ea.line1;
2139 ea.line1 = ea.line2;
2140 ea.line2 = lnum;
2141 }
2142 if ((errormsg = invalid_range(&ea)) != NULL)
2143 goto doend;
2144 }
2145
Bram Moolenaarb7316892019-05-01 18:08:42 +02002146 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2147 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 ea.line2 = 1;
2149
2150 correct_range(&ea);
2151
2152#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002153 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002154 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002156 // Put the first line at the start of a closed fold, put the last line
2157 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 (void)hasFolding(ea.line1, &ea.line1, NULL);
2159 (void)hasFolding(ea.line2, NULL, &ea.line2);
2160 }
2161#endif
2162
2163#ifdef FEAT_QUICKFIX
2164 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002165 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 * option here, so things like % get expanded.
2167 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002168 p = replace_makeprg(&ea, p, cmdlinep);
2169 if (p == NULL)
2170 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171#endif
2172
2173 /*
2174 * Skip to start of argument.
2175 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2176 */
2177 if (ea.cmdidx == CMD_bang)
2178 ea.arg = p;
2179 else
2180 ea.arg = skipwhite(p);
2181
Bram Moolenaar379fb762018-08-30 15:58:28 +02002182 // ":file" cannot be run with an argument when "curbuf_lock" is set
2183 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2184 goto doend;
2185
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 /*
2187 * Check for "++opt=val" argument.
2188 * Must be first, allow ":w ++enc=utf8 !cmd"
2189 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002190 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2192 if (getargopt(&ea) == FAIL && !ni)
2193 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002194 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 goto doend;
2196 }
2197
2198 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2199 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002200 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002202 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002204 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 goto doend;
2206 }
2207 ea.arg = skipwhite(ea.arg + 1);
2208 ea.append = TRUE;
2209 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002210 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 {
2212 ++ea.arg;
2213 ea.usefilter = TRUE;
2214 }
2215 }
2216
2217 if (ea.cmdidx == CMD_read)
2218 {
2219 if (ea.forceit)
2220 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002221 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 ea.forceit = FALSE;
2223 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002224 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 {
2226 ++ea.arg;
2227 ea.usefilter = TRUE;
2228 }
2229 }
2230
2231 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2232 {
2233 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002234 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 {
2236 ++ea.arg;
2237 ++ea.amount;
2238 }
2239 ea.arg = skipwhite(ea.arg);
2240 }
2241
2242 /*
2243 * Check for "+command" argument, before checking for next command.
2244 * Don't do this for ":read !cmd" and ":write !cmd".
2245 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002246 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2248
2249 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002250 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 * Don't do this for ":read !cmd" and ":write !cmd".
2252 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002253 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 separate_nextcmd(&ea);
2255
2256 /*
2257 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002258 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2259 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002261 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002262 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002263 || ea.cmdidx == CMD_global
2264 || ea.cmdidx == CMD_vglobal
2265 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 {
2267 for (p = ea.arg; *p; ++p)
2268 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002269 // Remove one backslash before a newline, so that it's possible to
2270 // pass a newline to the shell and also a newline that is preceded
2271 // with a backslash. This makes it impossible to end a shell
2272 // command in a backslash, but that doesn't appear useful.
2273 // Halving the number of backslashes is incompatible with previous
2274 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002276 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 else if (*p == '\n')
2278 {
2279 ea.nextcmd = p + 1;
2280 *p = NUL;
2281 break;
2282 }
2283 }
2284 }
2285
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002286 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002287 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002289 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002290 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002292 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002293 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002294 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002296#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002297 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002298 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002300 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002301 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
2303#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002304 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2305 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002306 {
2307 ea.regname = *ea.arg++;
2308#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002309 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002310 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2311 {
2312 set_expr_line(vim_strsave(ea.arg));
2313 ea.arg += STRLEN(ea.arg);
2314 }
2315#endif
2316 ea.arg = skipwhite(ea.arg);
2317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
2319
2320 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002321 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 * count, it's a buffer name.
2323 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002324 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2325 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002326 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
2328 n = getdigits(&ea.arg);
2329 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002330 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002332 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 goto doend;
2334 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002335 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
2337 ea.line2 = n;
2338 if (ea.addr_count == 0)
2339 ea.addr_count = 1;
2340 }
2341 else
2342 {
2343 ea.line1 = ea.line2;
2344 ea.line2 += n - 1;
2345 ++ea.addr_count;
2346 /*
2347 * Be vi compatible: no error message for out of range.
2348 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002349 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 ea.line2 = curbuf->b_ml.ml_line_count;
2351 }
2352 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002353
2354 /*
2355 * Check for flags: 'l', 'p' and '#'.
2356 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002357 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002358 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002359 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2360 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002362 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002363 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 goto doend;
2365 }
2366
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002367 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002369 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 goto doend;
2371 }
2372
2373#ifdef FEAT_EVAL
2374 /*
2375 * Skip the command when it's not going to be executed.
2376 * The commands like :if, :endif, etc. always need to be executed.
2377 * Also make an exception for commands that handle a trailing command
2378 * themselves.
2379 */
2380 if (ea.skip)
2381 {
2382 switch (ea.cmdidx)
2383 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002384 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 case CMD_while:
2386 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002387 case CMD_for:
2388 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 case CMD_if:
2390 case CMD_elseif:
2391 case CMD_else:
2392 case CMD_endif:
2393 case CMD_try:
2394 case CMD_catch:
2395 case CMD_finally:
2396 case CMD_endtry:
2397 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002398 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 break;
2400
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002401 // Commands that handle '|' themselves. Check: A command should
2402 // either have the EX_TRLBAR flag, appear in this list or appear in
2403 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 case CMD_aboveleft:
2405 case CMD_and:
2406 case CMD_belowright:
2407 case CMD_botright:
2408 case CMD_browse:
2409 case CMD_call:
2410 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002411 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 case CMD_delfunction:
2413 case CMD_djump:
2414 case CMD_dlist:
2415 case CMD_dsearch:
2416 case CMD_dsplit:
2417 case CMD_echo:
2418 case CMD_echoerr:
2419 case CMD_echomsg:
2420 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002421 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002423 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002424 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 case CMD_help:
2426 case CMD_hide:
2427 case CMD_ijump:
2428 case CMD_ilist:
2429 case CMD_isearch:
2430 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002431 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 case CMD_keepjumps:
2433 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002434 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 case CMD_leftabove:
2436 case CMD_let:
2437 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002438 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002439 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002441 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002442 case CMD_noautocmd:
2443 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 case CMD_perl:
2445 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002446 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002447 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002448 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 case CMD_return:
2450 case CMD_rightbelow:
2451 case CMD_ruby:
2452 case CMD_silent:
2453 case CMD_smagic:
2454 case CMD_snomagic:
2455 case CMD_substitute:
2456 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002457 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 case CMD_tcl:
2459 case CMD_throw:
2460 case CMD_tilde:
2461 case CMD_topleft:
2462 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002463 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002464 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 case CMD_verbose:
2466 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002467 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 break;
2469
2470 default: goto doend;
2471 }
2472 }
2473#endif
2474
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002475 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 {
2477 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2478 goto doend;
2479 }
2480
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 /*
2482 * Accept buffer name. Cannot be used at the same time with a buffer
2483 * number. Don't do this for a user command.
2484 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002485 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002486 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 {
2488 /*
2489 * :bdelete, :bwipeout and :bunload take several arguments, separated
2490 * by spaces: find next space (skipping over escaped characters).
2491 * The others take one argument: ignore trailing spaces.
2492 */
2493 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2494 || ea.cmdidx == CMD_bunload)
2495 p = skiptowhite_esc(ea.arg);
2496 else
2497 {
2498 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002499 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 --p;
2501 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002502 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002503 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002504 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 goto doend;
2506 ea.addr_count = 1;
2507 ea.arg = skipwhite(p);
2508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002510 // The :try command saves the emsg_silent flag, reset it here when
2511 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002512 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002513 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002514 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002515 if (emsg_silent < 0)
2516 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002517 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002518 }
2519
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002521 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523
Bram Moolenaar958636c2014-10-21 20:01:58 +02002524 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 {
2526 /*
2527 * Execute a user-defined command.
2528 */
2529 do_ucmd(&ea);
2530 }
2531 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 {
2533 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002534 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 */
2536 ea.errmsg = NULL;
2537 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2538 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002539 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 }
2541
2542#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002544 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2545 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002546 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 /*
2549 * If the command just executed called do_cmdline(), any throw or ":return"
2550 * or ":finish" encountered there must also check the cstack of the still
2551 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2552 * exception, or reanimate a returned function or finished script file and
2553 * return or finish it again.
2554 */
2555 if (need_rethrow)
2556 do_throw(cstack);
2557 else if (check_cstack)
2558 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002559 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002561 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 && current_func_returned())
2563 do_return(&ea, TRUE, FALSE, NULL);
2564 }
2565 need_rethrow = check_cstack = FALSE;
2566#endif
2567
2568doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002569 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002570 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002572 curwin->w_cursor.col = 0;
2573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574
2575 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2576 {
2577 if (sourcing)
2578 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002579 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 {
2581 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002582 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002584 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 }
2586 emsg(errormsg);
2587 }
2588#ifdef FEAT_EVAL
2589 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002590 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2591 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592#endif
2593
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002594 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002596 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002598#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002599 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002600 --sandbox;
2601#endif
2602
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002603 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 ea.nextcmd = NULL;
2605
2606#ifdef FEAT_EVAL
2607 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002608 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609#endif
2610
2611 return ea.nextcmd;
2612}
2613#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002614 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615#endif
2616
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002617static char ex_error_buf[MSG_BUF_LEN];
2618
2619/*
2620 * Return an error message with argument included.
2621 * Uses a static buffer, only the last error will be kept.
2622 * "msg" will be translated, caller should use N_().
2623 */
2624 char *
2625ex_errmsg(char *msg, char_u *arg)
2626{
2627 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2628 return ex_error_buf;
2629}
2630
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002632 * Parse and skip over command modifiers:
2633 * - update eap->cmd
2634 * - store flags in "cmdmod".
2635 * - Set ex_pressedreturn for an empty command line.
2636 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002637 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002638 * - set p_verbose for ":verbose"
2639 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002640 * When "skip_only" is TRUE the global variables are not changed, except for
2641 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002642 * Return FAIL when the command is not to be executed.
2643 * May set "errormsg" to an error message.
2644 */
2645 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002646parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002647{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002648 char_u *p;
2649 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002650
Bram Moolenaara80faa82020-04-12 19:37:17 +02002651 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002652 eap->verbose_save = -1;
2653 eap->save_msg_silent = -1;
2654
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002655 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002656 for (;;)
2657 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002658 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002659 {
2660 if (*eap->cmd == ':')
2661 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002662 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002663 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002664
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002665 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002666 if (*eap->cmd == NUL && exmode_active
2667 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2668 || getline_equal(eap->getline, eap->cookie, getexline))
2669 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2670 {
2671 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002672 if (!skip_only)
2673 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002674 }
2675
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002676 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002677 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002678 return FAIL;
2679 if (*eap->cmd == NUL)
2680 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002681 if (!skip_only)
2682 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002683 return FAIL;
2684 }
2685
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002686 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002687 switch (*p)
2688 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002689 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002690 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2691 break;
2692 cmdmod.split |= WSP_ABOVE;
2693 continue;
2694
2695 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2696 {
2697 cmdmod.split |= WSP_BELOW;
2698 continue;
2699 }
2700 if (checkforcmd(&eap->cmd, "browse", 3))
2701 {
2702#ifdef FEAT_BROWSE_CMD
2703 cmdmod.browse = TRUE;
2704#endif
2705 continue;
2706 }
2707 if (!checkforcmd(&eap->cmd, "botright", 2))
2708 break;
2709 cmdmod.split |= WSP_BOT;
2710 continue;
2711
2712 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2713 break;
2714#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2715 cmdmod.confirm = TRUE;
2716#endif
2717 continue;
2718
2719 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2720 {
2721 cmdmod.keepmarks = TRUE;
2722 continue;
2723 }
2724 if (checkforcmd(&eap->cmd, "keepalt", 5))
2725 {
2726 cmdmod.keepalt = TRUE;
2727 continue;
2728 }
2729 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2730 {
2731 cmdmod.keeppatterns = TRUE;
2732 continue;
2733 }
2734 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2735 break;
2736 cmdmod.keepjumps = TRUE;
2737 continue;
2738
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002739 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002740 {
2741 char_u *reg_pat;
2742
2743 if (!checkforcmd(&p, "filter", 4)
2744 || *p == NUL || ends_excmd(*p))
2745 break;
2746 if (*p == '!')
2747 {
2748 cmdmod.filter_force = TRUE;
2749 p = skipwhite(p + 1);
2750 if (*p == NUL || ends_excmd(*p))
2751 break;
2752 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002753#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002754 // Avoid that "filter(arg)" is recognized.
2755 if (in_vim9script() && !VIM_ISWHITE(*p))
2756 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002757#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002758 if (skip_only)
2759 p = skip_vimgrep_pat(p, NULL, NULL);
2760 else
2761 // NOTE: This puts a NUL after the pattern.
2762 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002763 if (p == NULL || *p == NUL)
2764 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002765 if (!skip_only)
2766 {
2767 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002768 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002769 if (cmdmod.filter_regmatch.regprog == NULL)
2770 break;
2771 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002772 eap->cmd = p;
2773 continue;
2774 }
2775
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002776 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002777 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2778 || *p == NUL || ends_excmd(*p))
2779 break;
2780 eap->cmd = p;
2781 cmdmod.hide = TRUE;
2782 continue;
2783
2784 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2785 {
2786 cmdmod.lockmarks = TRUE;
2787 continue;
2788 }
2789
2790 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2791 break;
2792 cmdmod.split |= WSP_ABOVE;
2793 continue;
2794
2795 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2796 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002797 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002798 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002799 // Set 'eventignore' to "all". Restore the
2800 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002801 cmdmod.save_ei = vim_strsave(p_ei);
2802 set_string_option_direct((char_u *)"ei", -1,
2803 (char_u *)"all", OPT_FREE, SID_NONE);
2804 }
2805 continue;
2806 }
2807 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2808 break;
2809 cmdmod.noswapfile = TRUE;
2810 continue;
2811
2812 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2813 break;
2814 cmdmod.split |= WSP_BELOW;
2815 continue;
2816
2817 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2818 {
2819#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002820 if (!skip_only)
2821 {
2822 if (!eap->did_sandbox)
2823 ++sandbox;
2824 eap->did_sandbox = TRUE;
2825 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002826#endif
2827 continue;
2828 }
2829 if (!checkforcmd(&eap->cmd, "silent", 3))
2830 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002831 if (!skip_only)
2832 {
2833 if (eap->save_msg_silent == -1)
2834 eap->save_msg_silent = msg_silent;
2835 ++msg_silent;
2836 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002837 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2838 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002839 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002840 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002841 if (!skip_only)
2842 {
2843 ++emsg_silent;
2844 ++eap->did_esilent;
2845 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002846 }
2847 continue;
2848
2849 case 't': if (checkforcmd(&p, "tab", 3))
2850 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002851 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002852 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002853 long tabnr = get_address(eap, &eap->cmd,
2854 ADDR_TABS, eap->skip,
2855 skip_only, FALSE, 1);
2856 if (tabnr == MAXLNUM)
2857 cmdmod.tab = tabpage_index(curtab) + 1;
2858 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002859 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002860 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2861 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002862 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002863 return FAIL;
2864 }
2865 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002866 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002867 }
2868 eap->cmd = p;
2869 continue;
2870 }
2871 if (!checkforcmd(&eap->cmd, "topleft", 2))
2872 break;
2873 cmdmod.split |= WSP_TOP;
2874 continue;
2875
2876 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2877 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002878 if (!skip_only)
2879 {
2880 if (eap->save_msg_silent == -1)
2881 eap->save_msg_silent = msg_silent;
2882 msg_silent = 0;
2883 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002884 continue;
2885
2886 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2887 {
2888 cmdmod.split |= WSP_VERT;
2889 continue;
2890 }
2891 if (!checkforcmd(&p, "verbose", 4))
2892 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002893 if (!skip_only)
2894 {
2895 if (eap->verbose_save < 0)
2896 eap->verbose_save = p_verbose;
2897 if (vim_isdigit(*eap->cmd))
2898 p_verbose = atoi((char *)eap->cmd);
2899 else
2900 p_verbose = 1;
2901 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002902 eap->cmd = p;
2903 continue;
2904 }
2905 break;
2906 }
2907
2908 return OK;
2909}
2910
2911/*
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002912 * Undo and free contents of "cmdmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002913 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002914 void
2915undo_cmdmod(exarg_T *eap, int save_msg_scroll)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002916{
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002917 if (eap->verbose_save >= 0)
2918 p_verbose = eap->verbose_save;
2919
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002920 if (cmdmod.save_ei != NULL)
2921 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002922 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002923 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2924 OPT_FREE, SID_NONE);
2925 free_string_option(cmdmod.save_ei);
2926 }
2927
2928 if (cmdmod.filter_regmatch.regprog != NULL)
2929 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002930
2931 if (eap->save_msg_silent != -1)
2932 {
2933 // messages could be enabled for a serious error, need to check if the
2934 // counters don't become negative
2935 if (!did_emsg || msg_silent > eap->save_msg_silent)
2936 msg_silent = eap->save_msg_silent;
2937 emsg_silent -= eap->did_esilent;
2938 if (emsg_silent < 0)
2939 emsg_silent = 0;
2940 // Restore msg_scroll, it's set by file I/O commands, even when no
2941 // message is actually displayed.
2942 msg_scroll = save_msg_scroll;
2943
2944 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2945 // somewhere in the line. Put it back in the first column.
2946 if (redirecting())
2947 msg_col = 0;
2948 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002949}
2950
2951/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002952 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002953 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002954 * Return FAIL and set "errormsg" or return OK.
2955 */
2956 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002957parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002958{
2959 int address_count = 1;
2960 linenr_T lnum;
2961
2962 // Repeat for all ',' or ';' separated addresses.
2963 for (;;)
2964 {
2965 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002966 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002967 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002968 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002969 eap->addr_count == 0, address_count++);
2970 if (eap->cmd == NULL) // error detected
2971 return FAIL;
2972 if (lnum == MAXLNUM)
2973 {
2974 if (*eap->cmd == '%') // '%' - all lines
2975 {
2976 ++eap->cmd;
2977 switch (eap->addr_type)
2978 {
2979 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002980 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002981 eap->line1 = 1;
2982 eap->line2 = curbuf->b_ml.ml_line_count;
2983 break;
2984 case ADDR_LOADED_BUFFERS:
2985 {
2986 buf_T *buf = firstbuf;
2987
2988 while (buf->b_next != NULL
2989 && buf->b_ml.ml_mfp == NULL)
2990 buf = buf->b_next;
2991 eap->line1 = buf->b_fnum;
2992 buf = lastbuf;
2993 while (buf->b_prev != NULL
2994 && buf->b_ml.ml_mfp == NULL)
2995 buf = buf->b_prev;
2996 eap->line2 = buf->b_fnum;
2997 break;
2998 }
2999 case ADDR_BUFFERS:
3000 eap->line1 = firstbuf->b_fnum;
3001 eap->line2 = lastbuf->b_fnum;
3002 break;
3003 case ADDR_WINDOWS:
3004 case ADDR_TABS:
3005 if (IS_USER_CMDIDX(eap->cmdidx))
3006 {
3007 eap->line1 = 1;
3008 eap->line2 = eap->addr_type == ADDR_WINDOWS
3009 ? LAST_WIN_NR : LAST_TAB_NR;
3010 }
3011 else
3012 {
3013 // there is no Vim command which uses '%' and
3014 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003015 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003016 return FAIL;
3017 }
3018 break;
3019 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003020 case ADDR_UNSIGNED:
3021 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003022 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003023 return FAIL;
3024 case ADDR_ARGUMENTS:
3025 if (ARGCOUNT == 0)
3026 eap->line1 = eap->line2 = 0;
3027 else
3028 {
3029 eap->line1 = 1;
3030 eap->line2 = ARGCOUNT;
3031 }
3032 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003033 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003034#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003035 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003036 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003037 if (eap->line2 == 0)
3038 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003039#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003040 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003041 case ADDR_NONE:
3042 // Will give an error later if a range is found.
3043 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003044 }
3045 ++eap->addr_count;
3046 }
3047 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3048 {
3049 pos_T *fp;
3050
3051 // '*' - visual area
3052 if (eap->addr_type != ADDR_LINES)
3053 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003054 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003055 return FAIL;
3056 }
3057
3058 ++eap->cmd;
3059 if (!eap->skip)
3060 {
3061 fp = getmark('<', FALSE);
3062 if (check_mark(fp) == FAIL)
3063 return FAIL;
3064 eap->line1 = fp->lnum;
3065 fp = getmark('>', FALSE);
3066 if (check_mark(fp) == FAIL)
3067 return FAIL;
3068 eap->line2 = fp->lnum;
3069 ++eap->addr_count;
3070 }
3071 }
3072 }
3073 else
3074 eap->line2 = lnum;
3075 eap->addr_count++;
3076
3077 if (*eap->cmd == ';')
3078 {
3079 if (!eap->skip)
3080 {
3081 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003082 // Don't leave the cursor on an illegal line or column, but do
3083 // accept zero as address, so 0;/PATTERN/ works correctly.
3084 if (eap->line2 > 0)
3085 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003086 }
3087 }
3088 else if (*eap->cmd != ',')
3089 break;
3090 ++eap->cmd;
3091 }
3092
3093 // One address given: set start and end lines.
3094 if (eap->addr_count == 1)
3095 {
3096 eap->line1 = eap->line2;
3097 // ... but only implicit: really no address given
3098 if (lnum == MAXLNUM)
3099 eap->addr_count = 0;
3100 }
3101 return OK;
3102}
3103
3104/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003105 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 * If there is a match advance "pp" to the argument and return TRUE.
3107 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003108 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003109checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003110 char_u **pp, // start of command
3111 char *cmd, // name of command
3112 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
3114 int i;
3115
3116 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003117 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 break;
3119 if (i >= len && !isalpha((*pp)[i]))
3120 {
3121 *pp = skipwhite(*pp + i);
3122 return TRUE;
3123 }
3124 return FALSE;
3125}
3126
3127/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003128 * Append "cmd" to the error message in IObuff.
3129 * Takes care of limiting the length and handling 0xa0, which would be
3130 * invisible otherwise.
3131 */
3132 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003133append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003134{
3135 char_u *s = cmd;
3136 char_u *d;
3137
3138 STRCAT(IObuff, ": ");
3139 d = IObuff + STRLEN(IObuff);
3140 while (*s != NUL && d - IObuff < IOSIZE - 7)
3141 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003142 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003143 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003144 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003145 STRCPY(d, "<a0>");
3146 d += 4;
3147 }
3148 else
3149 MB_COPY_CHAR(s, d);
3150 }
3151 *d = NUL;
3152}
3153
3154/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003155 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3156 * the name. Otherwise just return "start".
3157 */
3158 char_u *
3159skip_option_env_lead(char_u *start)
3160{
3161 char_u *name = start;
3162
3163 if (*start == '&')
3164 {
3165 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3166 name += 3;
3167 else
3168 name += 1;
3169 }
3170 else if (*start == '$')
3171 name += 1;
3172 return name;
3173}
3174
3175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003177 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003178 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003179 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 *
3181 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3182 * assignment without "let". Sets eap->cmdidx to the command while returning
3183 * "eap->cmd".
3184 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 * Returns NULL for an ambiguous user command.
3186 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 char_u *
3188find_ex_command(
3189 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003190 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003191 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003192 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193{
3194 int len;
3195 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003196 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198#ifdef FEAT_EVAL
3199 /*
3200 * Recognize a Vim9 script function/method call and assignment:
3201 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3202 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003203 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003204 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003206 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207
Bram Moolenaar83144542020-08-02 20:40:43 +02003208 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003209 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003211 int oplen;
3212 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003214 if (
3215 // "(..." is an expression.
3216 // "funcname(" is always a function call.
3217 *p == '('
3218 || (p == eap->cmd
3219 ? (
3220 // "{..." is an dict expression.
3221 *eap->cmd == '{'
3222 // "'string'->func()" is an expression.
3223 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003224 // '"string"->func()' is an expression.
3225 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003226 // "g:varname" is an expression.
3227 || eap->cmd[1] == ':'
3228 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003229 // "varname->func()" is an expression.
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003230 : (*p == '-' && p[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003231 {
3232 eap->cmdidx = CMD_eval;
3233 return eap->cmd;
3234 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003235
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003236 if (p != eap->cmd && (
3237 // "varname[]" is an expression.
3238 *p == '['
3239 // "varname.key" is an expression.
3240 || (*p == '.' && ASCII_ISALPHA(p[1]))))
3241 {
3242 char_u *after = p;
3243
3244 // When followed by "=" or "+=" then it is an assignment.
3245 ++emsg_silent;
3246 if (skip_expr(&after) == OK
3247 && (*after == '='
3248 || (*after != NUL && after[1] == '=')))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003249 eap->cmdidx = CMD_var;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003250 else
3251 eap->cmdidx = CMD_eval;
3252 --emsg_silent;
3253 return eap->cmd;
3254 }
3255
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003256 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3257 // an assignment.
3258 // If there is no line break inside the "[...]" then "p" is
3259 // advanced to after the "]" by to_name_const_end(): check if a "="
3260 // follows.
3261 // If "[...]" has a line break "p" still points at the "[" and it
3262 // can't be an assignment.
3263 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003264 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003265 p = to_name_const_end(eap->cmd);
3266 if (p == eap->cmd || *skipwhite(p) != '=')
3267 {
3268 eap->cmdidx = CMD_eval;
3269 return eap->cmd;
3270 }
3271 if (p > eap->cmd && *skipwhite(p) == '=')
3272 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003273 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003274 return eap->cmd;
3275 }
3276 }
3277
3278 // Recognize an assignment if we recognize the variable name:
3279 // "g:var = expr"
3280 // "var = expr" where "var" is a local var name.
Bram Moolenaar83144542020-08-02 20:40:43 +02003281 if (*eap->cmd == '@')
3282 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003283 oplen = assignment_len(skipwhite(p), &heredoc);
3284 if (oplen > 0)
3285 {
3286 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3287 || *eap->cmd == '&'
3288 || *eap->cmd == '$'
3289 || *eap->cmd == '@'
3290 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
3291 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003292 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003293 return eap->cmd;
3294 }
3295 }
3296
3297 // Recognize using a type for a w:, b:, t: or g: variable:
3298 // "w:varname: number = 123".
3299 if (eap->cmd[1] == ':' && *p == ':')
3300 {
3301 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003302 return eap->cmd;
3303 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003304 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 }
3306#endif
3307
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 /*
3309 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003310 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 * - the 'k' command can directly be followed by any character.
3312 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003313 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003315 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 */
3317 p = eap->cmd;
3318 if (*p == 'k')
3319 {
3320 eap->cmdidx = CMD_k;
3321 ++p;
3322 }
3323 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003324 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3325 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 || p[1] == 'g'
3327 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3328 || p[1] == 'I'
3329 || (p[1] == 'r' && p[2] != 'e')))
3330 {
3331 eap->cmdidx = CMD_substitute;
3332 ++p;
3333 }
3334 else
3335 {
3336 while (ASCII_ISALPHA(*p))
3337 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003338 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003339 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003341 while (ASCII_ISALNUM(*p))
3342 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 }
3344 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3345 {
3346 // include "9" for "vim9script"
3347 ++p;
3348 while (ASCII_ISALPHA(*p))
3349 ++p;
3350 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003351
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003352 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3354 ++p;
3355 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003356 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3357 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003358 // Check for ":dl", ":dell", etc. to ":deletel": that's
3359 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003360 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003361 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003362 break;
3363 if (i == len - 1)
3364 {
3365 --len;
3366 if (p[-1] == 'l')
3367 eap->flags |= EXFLAG_LIST;
3368 else
3369 eap->flags |= EXFLAG_PRINT;
3370 }
3371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003373 if (ASCII_ISLOWER(eap->cmd[0]))
3374 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003375 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003376 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003377
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003378 if (command_count != (int)CMD_SIZE)
3379 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003380 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003381 getout(1);
3382 }
3383
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003384 // Use a precomputed index for fast look-up in cmdnames[]
3385 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003386 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3387 if (ASCII_ISLOWER(c2))
3388 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3389 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003390 else if (ASCII_ISUPPER(eap->cmd[0]))
3391 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003393 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
3395 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3396 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3397 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3398 (size_t)len) == 0)
3399 {
3400#ifdef FEAT_EVAL
3401 if (full != NULL
3402 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3403 *full = TRUE;
3404#endif
3405 break;
3406 }
3407
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003408 // Look for a user defined command as a last resort. Let ":Print" be
3409 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003410 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3411 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003413 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 while (ASCII_ISALNUM(*p))
3415 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003416 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003418 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 eap->cmdidx = CMD_SIZE;
3420 }
3421
3422 return p;
3423}
3424
3425#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003426static struct cmdmod
3427{
3428 char *name;
3429 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003430 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003431} cmdmods[] = {
3432 {"aboveleft", 3, FALSE},
3433 {"belowright", 3, FALSE},
3434 {"botright", 2, FALSE},
3435 {"browse", 3, FALSE},
3436 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003437 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003438 {"hide", 3, FALSE},
3439 {"keepalt", 5, FALSE},
3440 {"keepjumps", 5, FALSE},
3441 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003442 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003443 {"leftabove", 5, FALSE},
3444 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003445 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003446 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003447 {"rightbelow", 6, FALSE},
3448 {"sandbox", 3, FALSE},
3449 {"silent", 3, FALSE},
3450 {"tab", 3, TRUE},
3451 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003452 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003453 {"verbose", 4, TRUE},
3454 {"vertical", 4, FALSE},
3455};
3456
3457/*
3458 * Return length of a command modifier (including optional count).
3459 * Return zero when it's not a modifier.
3460 */
3461 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003462modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003463{
3464 int i, j;
3465 char_u *p = cmd;
3466
3467 if (VIM_ISDIGIT(*cmd))
3468 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003469 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003470 {
3471 for (j = 0; p[j] != NUL; ++j)
3472 if (p[j] != cmdmods[i].name[j])
3473 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003474 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003475 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003476 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003477 }
3478 return 0;
3479}
3480
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481/*
3482 * Return > 0 if an Ex command "name" exists.
3483 * Return 2 if there is an exact match.
3484 * Return 3 if there is an ambiguous match.
3485 */
3486 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003487cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488{
3489 exarg_T ea;
3490 int full = FALSE;
3491 int i;
3492 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003493 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003495 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003496 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 {
3498 for (j = 0; name[j] != NUL; ++j)
3499 if (name[j] != cmdmods[i].name[j])
3500 break;
3501 if (name[j] == NUL && j >= cmdmods[i].minlen)
3502 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3503 }
3504
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003505 // Check built-in commands and user defined commands.
3506 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003507 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003510 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003512 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3513 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003514 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003515 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3517}
3518#endif
3519
Bram Moolenaard0190392019-08-23 21:17:35 +02003520 cmdidx_T
3521excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522{
Bram Moolenaard0190392019-08-23 21:17:35 +02003523 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524
Bram Moolenaard0190392019-08-23 21:17:35 +02003525 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3526 idx = (cmdidx_T)((int)idx + 1))
3527 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 break;
3529
Bram Moolenaard0190392019-08-23 21:17:35 +02003530 return idx;
3531}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532
Bram Moolenaard0190392019-08-23 21:17:35 +02003533 long
3534excmd_get_argt(cmdidx_T idx)
3535{
3536 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537}
3538
3539/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003540 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 *
3542 * Backslashed delimiters after / or ? will be skipped, and commands will
3543 * not be expanded between /'s and ?'s or after "'".
3544 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003545 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 * Returns the "cmd" pointer advanced to beyond the range.
3547 */
3548 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003549skip_range(
3550 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003551 int skip_star, // skip "*" used for Visual range
3552 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003554 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003556 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003558 if (*cmd == '\\')
3559 {
3560 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3561 ++cmd;
3562 else
3563 break;
3564 }
3565 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 {
3567 if (*++cmd == NUL && ctx != NULL)
3568 *ctx = EXPAND_NOTHING;
3569 }
3570 else if (*cmd == '/' || *cmd == '?')
3571 {
3572 delim = *cmd++;
3573 while (*cmd != NUL && *cmd != delim)
3574 if (*cmd++ == '\\' && *cmd != NUL)
3575 ++cmd;
3576 if (*cmd == NUL && ctx != NULL)
3577 *ctx = EXPAND_NOTHING;
3578 }
3579 if (*cmd != NUL)
3580 ++cmd;
3581 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003582
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003583 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003584 while (*cmd == ':')
3585 cmd = skipwhite(cmd + 1);
3586
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003587 // Skip "*" used for Visual range.
3588 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3589 cmd = skipwhite(cmd + 1);
3590
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 return cmd;
3592}
3593
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003594 static void
3595addr_error(cmd_addr_T addr_type)
3596{
3597 if (addr_type == ADDR_NONE)
3598 emsg(_(e_norange));
3599 else
3600 emsg(_(e_invrange));
3601}
3602
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003604 * Return the default address for an address type.
3605 */
3606 static linenr_T
3607default_address(exarg_T *eap)
3608{
3609 linenr_T lnum = 0;
3610
3611 switch (eap->addr_type)
3612 {
3613 case ADDR_LINES:
3614 case ADDR_OTHER:
3615 // Default is the cursor line number. Avoid using an invalid
3616 // line number though.
3617 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3618 lnum = curbuf->b_ml.ml_line_count;
3619 else
3620 lnum = curwin->w_cursor.lnum;
3621 break;
3622 case ADDR_WINDOWS:
3623 lnum = CURRENT_WIN_NR;
3624 break;
3625 case ADDR_ARGUMENTS:
3626 lnum = curwin->w_arg_idx + 1;
3627 if (lnum > ARGCOUNT)
3628 lnum = ARGCOUNT;
3629 break;
3630 case ADDR_LOADED_BUFFERS:
3631 case ADDR_BUFFERS:
3632 lnum = curbuf->b_fnum;
3633 break;
3634 case ADDR_TABS:
3635 lnum = CURRENT_TAB_NR;
3636 break;
3637 case ADDR_TABS_RELATIVE:
3638 case ADDR_UNSIGNED:
3639 lnum = 1;
3640 break;
3641 case ADDR_QUICKFIX:
3642#ifdef FEAT_QUICKFIX
3643 lnum = qf_get_cur_idx(eap);
3644#endif
3645 break;
3646 case ADDR_QUICKFIX_VALID:
3647#ifdef FEAT_QUICKFIX
3648 lnum = qf_get_cur_valid_idx(eap);
3649#endif
3650 break;
3651 case ADDR_NONE:
3652 // Will give an error later if a range is found.
3653 break;
3654 }
3655 return lnum;
3656}
3657
3658/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003659 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 *
3661 * Set ptr to the next character after the part that was interpreted.
3662 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003663 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 *
3665 * Return MAXLNUM when no Ex address was found.
3666 */
3667 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003668get_address(
3669 exarg_T *eap UNUSED,
3670 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003671 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003672 int skip, // only skip the address, don't use it
3673 int silent, // no errors or side effects
3674 int to_other_file, // flag: may jump to other file
3675 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676{
3677 int c;
3678 int i;
3679 long n;
3680 char_u *cmd;
3681 pos_T pos;
3682 pos_T *fp;
3683 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003684 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685
3686 cmd = skipwhite(*ptr);
3687 lnum = MAXLNUM;
3688 do
3689 {
3690 switch (*cmd)
3691 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003692 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003693 ++cmd;
3694 switch (addr_type)
3695 {
3696 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003697 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 lnum = curwin->w_cursor.lnum;
3699 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003700 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003701 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003702 break;
3703 case ADDR_ARGUMENTS:
3704 lnum = curwin->w_arg_idx + 1;
3705 break;
3706 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003707 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003708 lnum = curbuf->b_fnum;
3709 break;
3710 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003711 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003712 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003713 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003714 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003715 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003716 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003717 cmd = NULL;
3718 goto error;
3719 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003720 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003721#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003722 lnum = qf_get_cur_idx(eap);
3723#endif
3724 break;
3725 case ADDR_QUICKFIX_VALID:
3726#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003727 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003728#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003729 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003730 }
3731 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003733 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003734 ++cmd;
3735 switch (addr_type)
3736 {
3737 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003738 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 lnum = curbuf->b_ml.ml_line_count;
3740 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003741 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003742 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003743 break;
3744 case ADDR_ARGUMENTS:
3745 lnum = ARGCOUNT;
3746 break;
3747 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003748 buf = lastbuf;
3749 while (buf->b_ml.ml_mfp == NULL)
3750 {
3751 if (buf->b_prev == NULL)
3752 break;
3753 buf = buf->b_prev;
3754 }
3755 lnum = buf->b_fnum;
3756 break;
3757 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003758 lnum = lastbuf->b_fnum;
3759 break;
3760 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003761 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003762 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003763 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003764 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003765 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003766 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003767 cmd = NULL;
3768 goto error;
3769 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003770 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003771#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003772 lnum = qf_get_size(eap);
3773 if (lnum == 0)
3774 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003775#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003776 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003777 case ADDR_QUICKFIX_VALID:
3778#ifdef FEAT_QUICKFIX
3779 lnum = qf_get_valid_size(eap);
3780 if (lnum == 0)
3781 lnum = 1;
3782#endif
3783 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003784 }
3785 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003787 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003788 if (*++cmd == NUL)
3789 {
3790 cmd = NULL;
3791 goto error;
3792 }
3793 if (addr_type != ADDR_LINES)
3794 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003795 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003796 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003797 goto error;
3798 }
3799 if (skip)
3800 ++cmd;
3801 else
3802 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003803 // Only accept a mark in another file when it is
3804 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003805 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3806 ++cmd;
3807 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003808 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003809 lnum = curwin->w_cursor.lnum;
3810 else
3811 {
3812 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 {
3814 cmd = NULL;
3815 goto error;
3816 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003817 lnum = fp->lnum;
3818 }
3819 }
3820 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821
3822 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003823 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003824 c = *cmd++;
3825 if (addr_type != ADDR_LINES)
3826 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003827 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003828 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003829 goto error;
3830 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003831 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003832 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003833 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003834 if (*cmd == c)
3835 ++cmd;
3836 }
3837 else
3838 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003839 int flags;
3840
3841 pos = curwin->w_cursor; // save curwin->w_cursor
3842
3843 // When '/' or '?' follows another address, start from
3844 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003845 if (lnum != MAXLNUM)
3846 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003847
3848 // Start a forward search at the end of the line (unless
3849 // before the first line).
3850 // Start a backward search at the start of the line.
3851 // This makes sure we never match in the current
3852 // line, and can match anywhere in the
3853 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003854 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003855 curwin->w_cursor.col = MAXCOL;
3856 else
3857 curwin->w_cursor.col = 0;
3858 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003859 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003860 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003861 {
3862 curwin->w_cursor = pos;
3863 cmd = NULL;
3864 goto error;
3865 }
3866 lnum = curwin->w_cursor.lnum;
3867 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003868 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003869 cmd += searchcmdlen;
3870 }
3871 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003873 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003874 ++cmd;
3875 if (addr_type != ADDR_LINES)
3876 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003877 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003878 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003879 goto error;
3880 }
3881 if (*cmd == '&')
3882 i = RE_SUBST;
3883 else if (*cmd == '?' || *cmd == '/')
3884 i = RE_SEARCH;
3885 else
3886 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003887 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003888 cmd = NULL;
3889 goto error;
3890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003892 if (!skip)
3893 {
3894 /*
3895 * When search follows another address, start from
3896 * there.
3897 */
3898 if (lnum != MAXLNUM)
3899 pos.lnum = lnum;
3900 else
3901 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003903 /*
3904 * Start the search just like for the above
3905 * do_search().
3906 */
3907 if (*cmd != '?')
3908 pos.col = MAXCOL;
3909 else
3910 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003911 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003912 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003913 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003914 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003915 lnum = pos.lnum;
3916 else
3917 {
3918 cmd = NULL;
3919 goto error;
3920 }
3921 }
3922 ++cmd;
3923 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
3925 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003926 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003927 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 }
3929
3930 for (;;)
3931 {
3932 cmd = skipwhite(cmd);
3933 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3934 break;
3935
3936 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003937 {
3938 switch (addr_type)
3939 {
3940 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003941 case ADDR_OTHER:
3942 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003943 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003944 break;
3945 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003946 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003947 break;
3948 case ADDR_ARGUMENTS:
3949 lnum = curwin->w_arg_idx + 1;
3950 break;
3951 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003952 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003953 lnum = curbuf->b_fnum;
3954 break;
3955 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003956 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003957 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003958 case ADDR_TABS_RELATIVE:
3959 lnum = 1;
3960 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003961 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003962#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003963 lnum = qf_get_cur_idx(eap);
3964#endif
3965 break;
3966 case ADDR_QUICKFIX_VALID:
3967#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003968 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003969#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003970 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003971 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003972 case ADDR_UNSIGNED:
3973 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003974 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003975 }
3976 }
3977
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003979 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 else
3981 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003982 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 n = 1;
3984 else
3985 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003986
3987 if (addr_type == ADDR_TABS_RELATIVE)
3988 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003989 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003990 cmd = NULL;
3991 goto error;
3992 }
3993 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003994 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003995 lnum = compute_buffer_local_count(
3996 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003998 {
3999#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004000 // Relative line addressing, need to adjust for folded lines
4001 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004002 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4003 && address_count >= 2)
4004 (void)hasFolding(lnum, NULL, &lnum);
4005#endif
4006 if (i == '-')
4007 lnum -= n;
4008 else
4009 lnum += n;
4010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 }
4012 } while (*cmd == '/' || *cmd == '?');
4013
4014error:
4015 *ptr = cmd;
4016 return lnum;
4017}
4018
4019/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004020 * Set eap->line1 and eap->line2 to the whole range.
4021 * Used for commands with the EX_DFLALL flag and no range given.
4022 */
4023 static void
4024address_default_all(exarg_T *eap)
4025{
4026 eap->line1 = 1;
4027 switch (eap->addr_type)
4028 {
4029 case ADDR_LINES:
4030 case ADDR_OTHER:
4031 eap->line2 = curbuf->b_ml.ml_line_count;
4032 break;
4033 case ADDR_LOADED_BUFFERS:
4034 {
4035 buf_T *buf = firstbuf;
4036
4037 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4038 buf = buf->b_next;
4039 eap->line1 = buf->b_fnum;
4040 buf = lastbuf;
4041 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4042 buf = buf->b_prev;
4043 eap->line2 = buf->b_fnum;
4044 }
4045 break;
4046 case ADDR_BUFFERS:
4047 eap->line1 = firstbuf->b_fnum;
4048 eap->line2 = lastbuf->b_fnum;
4049 break;
4050 case ADDR_WINDOWS:
4051 eap->line2 = LAST_WIN_NR;
4052 break;
4053 case ADDR_TABS:
4054 eap->line2 = LAST_TAB_NR;
4055 break;
4056 case ADDR_TABS_RELATIVE:
4057 eap->line2 = 1;
4058 break;
4059 case ADDR_ARGUMENTS:
4060 if (ARGCOUNT == 0)
4061 eap->line1 = eap->line2 = 0;
4062 else
4063 eap->line2 = ARGCOUNT;
4064 break;
4065 case ADDR_QUICKFIX_VALID:
4066#ifdef FEAT_QUICKFIX
4067 eap->line2 = qf_get_valid_size(eap);
4068 if (eap->line2 == 0)
4069 eap->line2 = 1;
4070#endif
4071 break;
4072 case ADDR_NONE:
4073 case ADDR_UNSIGNED:
4074 case ADDR_QUICKFIX:
4075 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4076 break;
4077 }
4078}
4079
4080
4081/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004082 * Get flags from an Ex command argument.
4083 */
4084 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004085get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004086{
4087 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4088 {
4089 if (*eap->arg == 'l')
4090 eap->flags |= EXFLAG_LIST;
4091 else if (*eap->arg == 'p')
4092 eap->flags |= EXFLAG_PRINT;
4093 else
4094 eap->flags |= EXFLAG_NR;
4095 eap->arg = skipwhite(eap->arg + 1);
4096 }
4097}
4098
4099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 * Function called for command which is Not Implemented. NI!
4101 */
4102 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004103ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104{
4105 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004106 eap->errmsg =
4107 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108}
4109
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004110#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111/*
4112 * Function called for script command which is Not Implemented. NI!
4113 * Skips over ":perl <<EOF" constructs.
4114 */
4115 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004116ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117{
4118 if (!eap->skip)
4119 ex_ni(eap);
4120 else
4121 vim_free(script_get(eap, eap->arg));
4122}
4123#endif
4124
4125/*
4126 * Check range in Ex command for validity.
4127 * Return NULL when valid, error message when invalid.
4128 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004129 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004130invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004132 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004133
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 if ( eap->line1 < 0
4135 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004136 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004137 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004138
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004139 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004140 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004141 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004142 {
4143 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004144 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004145#ifdef FEAT_DIFF
4146 + (eap->cmdidx == CMD_diffget)
4147#endif
4148 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004149 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004150 break;
4151 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004152 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004153 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004154 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004155 break;
4156 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004157 // Only a boundary check, not whether the buffers actually
4158 // exist.
4159 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004160 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004161 break;
4162 case ADDR_LOADED_BUFFERS:
4163 buf = firstbuf;
4164 while (buf->b_ml.ml_mfp == NULL)
4165 {
4166 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004167 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004168 buf = buf->b_next;
4169 }
4170 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004171 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004172 buf = lastbuf;
4173 while (buf->b_ml.ml_mfp == NULL)
4174 {
4175 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004176 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004177 buf = buf->b_prev;
4178 }
4179 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004180 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004181 break;
4182 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004183 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004184 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004185 break;
4186 case ADDR_TABS:
4187 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004188 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004189 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004190 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004191 case ADDR_OTHER:
4192 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004193 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004194 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004195#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004196 // No error for value that is too big, will use the last entry.
4197 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004198 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004199#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004200 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004201 case ADDR_QUICKFIX_VALID:
4202#ifdef FEAT_QUICKFIX
4203 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4204 || eap->line2 < 0)
4205 return _(e_invrange);
4206#endif
4207 break;
4208 case ADDR_UNSIGNED:
4209 if (eap->line2 < 0)
4210 return _(e_invrange);
4211 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004212 case ADDR_NONE:
4213 // Will give an error elsewhere.
4214 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004215 }
4216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 return NULL;
4218}
4219
4220/*
4221 * Correct the range for zero line number, if required.
4222 */
4223 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004224correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004226 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 {
4228 if (eap->line1 == 0)
4229 eap->line1 = 1;
4230 if (eap->line2 == 0)
4231 eap->line2 = 1;
4232 }
4233}
4234
Bram Moolenaar748bf032005-02-02 23:04:36 +00004235#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004236/*
4237 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4238 * pattern. Otherwise return eap->arg.
4239 */
4240 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004241skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004242{
4243 char_u *p = eap->arg;
4244
Bram Moolenaara37420f2006-02-04 22:37:47 +00004245 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4246 || eap->cmdidx == CMD_vimgrepadd
4247 || eap->cmdidx == CMD_lvimgrepadd
4248 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004249 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004250 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004251 if (p == NULL)
4252 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004253 }
4254 return p;
4255}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004256
4257/*
4258 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4259 * in the command line, so that things like % get expanded.
4260 */
4261 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004262replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004263{
4264 char_u *new_cmdline;
4265 char_u *program;
4266 char_u *pos;
4267 char_u *ptr;
4268 int len;
4269 int i;
4270
4271 /*
4272 * Don't do it when ":vimgrep" is used for ":grep".
4273 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004274 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4275 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4276 || eap->cmdidx == CMD_grepadd
4277 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004278 && !grep_internal(eap->cmdidx))
4279 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004280 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4281 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004282 {
4283 if (*curbuf->b_p_gp == NUL)
4284 program = p_gp;
4285 else
4286 program = curbuf->b_p_gp;
4287 }
4288 else
4289 {
4290 if (*curbuf->b_p_mp == NUL)
4291 program = p_mp;
4292 else
4293 program = curbuf->b_p_mp;
4294 }
4295
4296 p = skipwhite(p);
4297
4298 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4299 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004300 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004301 i = 1;
4302 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4303 ++i;
4304 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004305 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004306 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004307 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004308 ptr = new_cmdline;
4309 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4310 {
4311 i = (int)(pos - program);
4312 STRNCPY(ptr, program, i);
4313 STRCPY(ptr += i, p);
4314 ptr += len;
4315 program = pos + 2;
4316 }
4317 STRCPY(ptr, program);
4318 }
4319 else
4320 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004321 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004322 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004323 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004324 STRCPY(new_cmdline, program);
4325 STRCAT(new_cmdline, " ");
4326 STRCAT(new_cmdline, p);
4327 }
4328 msg_make(p);
4329
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004330 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004331 vim_free(*cmdlinep);
4332 *cmdlinep = new_cmdline;
4333 p = new_cmdline;
4334 }
4335 return p;
4336}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004337#endif
4338
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339/*
4340 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004341 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 * Return FAIL for failure, OK otherwise.
4343 */
4344 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004345expand_filename(
4346 exarg_T *eap,
4347 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004348 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004350 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 char_u *repl;
4352 int srclen;
4353 char_u *p;
4354 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004355 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356
Bram Moolenaar748bf032005-02-02 23:04:36 +00004357#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004358 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004359 p = skip_grep_pat(eap);
4360#else
4361 p = eap->arg;
4362#endif
4363
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 /*
4365 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4366 * the file name contains a wildcard it should not cause expanding.
4367 * (it will be expanded anyway if there is a wildcard before replacing).
4368 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004369 has_wildcards = mch_has_wildcard(p);
4370 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004372#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004373 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004374 if (p[0] == '`' && p[1] == '=')
4375 {
4376 p += 2;
4377 (void)skip_expr(&p);
4378 if (*p == '`')
4379 ++p;
4380 continue;
4381 }
4382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 /*
4384 * Quick check if this cannot be the start of a special string.
4385 * Also removes backslash before '%', '#' and '<'.
4386 */
4387 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4388 {
4389 ++p;
4390 continue;
4391 }
4392
4393 /*
4394 * Try to find a match at this position.
4395 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004396 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4397 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004398 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004400 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 {
4402 p += srclen;
4403 continue;
4404 }
4405
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004406 // Wildcards won't be expanded below, the replacement is taken
4407 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004408 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4409 {
4410 char_u *l = repl;
4411
4412 repl = expand_env_save(repl);
4413 vim_free(l);
4414 }
4415
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004416 // Need to escape white space et al. with a backslash.
4417 // Don't do this for:
4418 // - replacement that already has been escaped: "##"
4419 // - shell commands (may have to use quotes instead).
4420 // - non-unix systems when there is a single argument (spaces don't
4421 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004423 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 && eap->cmdidx != CMD_grep
4426 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004427 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004428 && eap->cmdidx != CMD_lgrep
4429 && eap->cmdidx != CMD_lgrepadd
4430 && eap->cmdidx != CMD_lmake
4431 && eap->cmdidx != CMD_make
4432 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004434 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435#endif
4436 )
4437 {
4438 char_u *l;
4439#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004440 // Don't escape a backslash here, because rem_backslash() doesn't
4441 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 static char_u *nobslash = (char_u *)" \t\"|";
4443# define ESCAPE_CHARS nobslash
4444#else
4445# define ESCAPE_CHARS escape_chars
4446#endif
4447
4448 for (l = repl; *l; ++l)
4449 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4450 {
4451 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4452 if (l != NULL)
4453 {
4454 vim_free(repl);
4455 repl = l;
4456 }
4457 break;
4458 }
4459 }
4460
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004461 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004462 if ((eap->usefilter || eap->cmdidx == CMD_bang
4463 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004464 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 {
4466 char_u *l;
4467
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004468 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 if (l != NULL)
4470 {
4471 vim_free(repl);
4472 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
4474 }
4475
4476 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4477 vim_free(repl);
4478 if (p == NULL)
4479 return FAIL;
4480 }
4481
4482 /*
4483 * One file argument: Expand wildcards.
4484 * Don't do this with ":r !command" or ":w !command".
4485 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004486 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 {
4488 /*
4489 * May do this twice:
4490 * 1. Replace environment variables.
4491 * 2. Replace any other wildcards, remove backslashes.
4492 */
4493 for (n = 1; n <= 2; ++n)
4494 {
4495 if (n == 2)
4496 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 /*
4498 * Halve the number of backslashes (this is Vi compatible).
4499 * For Unix and OS/2, when wildcards are expanded, this is
4500 * done by ExpandOne() below.
4501 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004502#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 if (!has_wildcards)
4504#endif
4505 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 }
4507
4508 if (has_wildcards)
4509 {
4510 if (n == 1)
4511 {
4512 /*
4513 * First loop: May expand environment variables. This
4514 * can be done much faster with expand_env() than with
4515 * something else (e.g., calling a shell).
4516 * After expanding environment variables, check again
4517 * if there are still wildcards present.
4518 */
4519 if (vim_strchr(eap->arg, '$') != NULL
4520 || vim_strchr(eap->arg, '~') != NULL)
4521 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004522 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004523 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 has_wildcards = mch_has_wildcard(NameBuff);
4525 p = NameBuff;
4526 }
4527 else
4528 p = NULL;
4529 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004530 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 {
4532 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004533 int options = WILD_LIST_NOTFOUND
4534 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
4536 ExpandInit(&xpc);
4537 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004538 if (p_wic)
4539 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004541 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 if (p == NULL)
4543 return FAIL;
4544 }
4545 if (p != NULL)
4546 {
4547 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4548 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004549 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 vim_free(p);
4551 }
4552 }
4553 }
4554 }
4555 return OK;
4556}
4557
4558/*
4559 * Replace part of the command line, keeping eap->cmd, eap->arg and
4560 * eap->nextcmd correct.
4561 * "src" points to the part that is to be replaced, of length "srclen".
4562 * "repl" is the replacement string.
4563 * Returns a pointer to the character after the replaced string.
4564 * Returns NULL for failure.
4565 */
4566 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004567repl_cmdline(
4568 exarg_T *eap,
4569 char_u *src,
4570 int srclen,
4571 char_u *repl,
4572 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573{
4574 int len;
4575 int i;
4576 char_u *new_cmdline;
4577
4578 /*
4579 * The new command line is build in new_cmdline[].
4580 * First allocate it.
4581 * Careful: a "+cmd" argument may have been NUL terminated.
4582 */
4583 len = (int)STRLEN(repl);
4584 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004585 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004586 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004587 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004588 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589
4590 /*
4591 * Copy the stuff before the expanded part.
4592 * Copy the expanded stuff.
4593 * Copy what came after the expanded part.
4594 * Copy the next commands, if there are any.
4595 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004596 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004598
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004600 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004602 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004604 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
4606 i = (int)STRLEN(new_cmdline) + 1;
4607 STRCPY(new_cmdline + i, eap->nextcmd);
4608 eap->nextcmd = new_cmdline + i;
4609 }
4610 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4611 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4612 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4613 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4614 vim_free(*cmdlinep);
4615 *cmdlinep = new_cmdline;
4616
4617 return src;
4618}
4619
4620/*
4621 * Check for '|' to separate commands and '"' to start comments.
4622 */
4623 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004624separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625{
4626 char_u *p;
4627
Bram Moolenaar86b68352004-12-27 21:59:20 +00004628#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004629 p = skip_grep_pat(eap);
4630#else
4631 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004632#endif
4633
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004634 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 {
4636 if (*p == Ctrl_V)
4637 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004638 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004639 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004641 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004642 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004643 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 break;
4645 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004646
4647#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004648 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004649 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004650 {
4651 p += 2;
4652 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004653 }
4654#endif
4655
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004656 // Check for '"': start of comment or '|': next command
4657 // :@" and :*" do not start a comment!
4658 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004659 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004660#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004661 && !in_vim9script()
4662#endif
4663 && !(eap->argt & EX_NOTRLCOM)
4664 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4665 || p != eap->arg)
4666 && (eap->cmdidx != CMD_redir
4667 || p != eap->arg + 1 || p[-1] != '@'))
4668#ifdef FEAT_EVAL
4669 || (*p == '#'
4670 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004671 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004672 && p[1] != '{'
4673 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 || *p == '|' || *p == '\n')
4676 {
4677 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004678 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 * AND 'b' is present in 'cpoptions'.
4680 */
4681 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004682 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004684 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 --p;
4686 }
4687 else
4688 {
4689 eap->nextcmd = check_nextcmd(p);
4690 *p = NUL;
4691 break;
4692 }
4693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004695
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004696 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 del_trailing_spaces(eap->arg);
4698}
4699
4700/*
4701 * get + command from ex argument
4702 */
4703 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004704getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705{
4706 char_u *arg = *argp;
4707 char_u *command = NULL;
4708
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004709 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 {
4711 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004712 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 command = dollar_command;
4714 else
4715 {
4716 command = arg;
4717 arg = skip_cmd_arg(command, TRUE);
4718 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004719 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 }
4721
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004722 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 *argp = arg;
4724 }
4725 return command;
4726}
4727
4728/*
4729 * Find end of "+command" argument. Skip over "\ " and "\\".
4730 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004731 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004732skip_cmd_arg(
4733 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004734 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735{
4736 while (*p && !vim_isspace(*p))
4737 {
4738 if (*p == '\\' && p[1] != NUL)
4739 {
4740 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004741 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 else
4743 ++p;
4744 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004745 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 }
4747 return p;
4748}
4749
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004750 int
4751get_bad_opt(char_u *p, exarg_T *eap)
4752{
4753 if (STRICMP(p, "keep") == 0)
4754 eap->bad_char = BAD_KEEP;
4755 else if (STRICMP(p, "drop") == 0)
4756 eap->bad_char = BAD_DROP;
4757 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4758 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004759 else
4760 return FAIL;
4761 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004762}
4763
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764/*
4765 * Get "++opt=arg" argument.
4766 * Return FAIL or OK.
4767 */
4768 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004769getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770{
4771 char_u *arg = eap->arg + 2;
4772 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004773 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004776 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4778 {
4779 if (*arg == 'n')
4780 {
4781 arg += 2;
4782 eap->force_bin = FORCE_NOBIN;
4783 }
4784 else
4785 eap->force_bin = FORCE_BIN;
4786 if (!checkforcmd(&arg, "binary", 3))
4787 return FAIL;
4788 eap->arg = skipwhite(arg);
4789 return OK;
4790 }
4791
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004792 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004793 if (STRNCMP(arg, "edit", 4) == 0)
4794 {
4795 eap->read_edit = TRUE;
4796 eap->arg = skipwhite(arg + 4);
4797 return OK;
4798 }
4799
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 if (STRNCMP(arg, "ff", 2) == 0)
4801 {
4802 arg += 2;
4803 pp = &eap->force_ff;
4804 }
4805 else if (STRNCMP(arg, "fileformat", 10) == 0)
4806 {
4807 arg += 10;
4808 pp = &eap->force_ff;
4809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 else if (STRNCMP(arg, "enc", 3) == 0)
4811 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004812 if (STRNCMP(arg, "encoding", 8) == 0)
4813 arg += 8;
4814 else
4815 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 pp = &eap->force_enc;
4817 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004818 else if (STRNCMP(arg, "bad", 3) == 0)
4819 {
4820 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004821 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823
4824 if (pp == NULL || *arg != '=')
4825 return FAIL;
4826
4827 ++arg;
4828 *pp = (int)(arg - eap->cmd);
4829 arg = skip_cmd_arg(arg, FALSE);
4830 eap->arg = skipwhite(arg);
4831 *arg = NUL;
4832
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 if (pp == &eap->force_ff)
4834 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4836 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004837 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004839 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004841 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4843 *p = TOLOWER_ASC(*p);
4844 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004845 else
4846 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004847 // Check ++bad= argument. Must be a single-byte character, "keep" or
4848 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004849 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004850 return FAIL;
4851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852
4853 return OK;
4854}
4855
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004857ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858{
4859 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004860 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 * directory for security reasons.
4862 */
4863 if (secure)
4864 {
4865 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004866 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 }
4868 else if (eap->cmdidx == CMD_autocmd)
4869 do_autocmd(eap->arg, eap->forceit);
4870 else
4871 do_augroup(eap->arg, eap->forceit);
4872}
4873
4874/*
4875 * ":doautocmd": Apply the automatic commands to the current buffer.
4876 */
4877 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004878ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004880 char_u *arg = eap->arg;
4881 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004882 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004883
Bram Moolenaar1610d052016-06-09 22:53:01 +02004884 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004885 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004886 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004887 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890/*
4891 * :[N]bunload[!] [N] [bufname] unload buffer
4892 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4893 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4894 */
4895 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004896ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004898 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004899 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 eap->errmsg = do_bufdel(
4901 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4902 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4903 : DOBUF_UNLOAD, eap->arg,
4904 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4905}
4906
4907/*
4908 * :[N]buffer [N] to buffer N
4909 * :[N]sbuffer [N] to buffer N
4910 */
4911 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004912ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004914 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004915 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004917 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 else
4919 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004920 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4922 else
4923 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004924 if (eap->do_ecmd_cmd != NULL)
4925 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 }
4927}
4928
4929/*
4930 * :[N]bmodified [N] to next mod. buffer
4931 * :[N]sbmodified [N] to next mod. buffer
4932 */
4933 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004934ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935{
4936 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004937 if (eap->do_ecmd_cmd != NULL)
4938 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939}
4940
4941/*
4942 * :[N]bnext [N] to next buffer
4943 * :[N]sbnext [N] split and to next buffer
4944 */
4945 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004946ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004948 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004949 return;
4950
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004952 if (eap->do_ecmd_cmd != NULL)
4953 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954}
4955
4956/*
4957 * :[N]bNext [N] to previous buffer
4958 * :[N]bprevious [N] to previous buffer
4959 * :[N]sbNext [N] split and to previous buffer
4960 * :[N]sbprevious [N] split and to previous buffer
4961 */
4962 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004963ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004965 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004966 return;
4967
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004969 if (eap->do_ecmd_cmd != NULL)
4970 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971}
4972
4973/*
4974 * :brewind to first buffer
4975 * :bfirst to first buffer
4976 * :sbrewind split and to first buffer
4977 * :sbfirst split and to first buffer
4978 */
4979 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004980ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004982 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004983 return;
4984
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004986 if (eap->do_ecmd_cmd != NULL)
4987 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988}
4989
4990/*
4991 * :blast to last buffer
4992 * :sblast split and to last buffer
4993 */
4994 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004995ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004997 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004998 return;
4999
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005001 if (eap->do_ecmd_cmd != NULL)
5002 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004
Bram Moolenaar7a092242020-04-16 22:10:49 +02005005/*
5006 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02005007 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005010ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005012 int comment_char = '"';
5013
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005014#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005015 if (in_vim9script())
5016 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005017#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005018 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005019}
5020
5021/*
5022 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5023 * to "cmd_start" or has a white space character before it.
5024 */
5025 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005026ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005027{
5028 int c = *cmd;
5029
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005030 if (c == NUL || c == '|' || c == '\n')
5031 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005032#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005033 if (in_vim9script())
5034 return c == '#' && cmd[1] != '{'
5035 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005036#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005037 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038}
5039
5040#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5041 || defined(PROTO)
5042/*
5043 * Return the next command, after the first '|' or '\n'.
5044 * Return NULL if not found.
5045 */
5046 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005047find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048{
5049 while (*p != '|' && *p != '\n')
5050 {
5051 if (*p == NUL)
5052 return NULL;
5053 ++p;
5054 }
5055 return (p + 1);
5056}
5057#endif
5058
5059/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005060 * Check if *p is a separator between Ex commands, skipping over white space.
5061 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 */
5063 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005064check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005066 char_u *s = skipwhite(p);
5067
5068 if (*s == '|' || *s == '\n')
5069 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 else
5071 return NULL;
5072}
5073
5074/*
5075 * - if there are more files to edit
5076 * - and this is the last window
5077 * - and forceit not used
5078 * - and not repeated twice on a row
5079 * return FAIL and give error message if 'message' TRUE
5080 * return OK otherwise
5081 */
5082 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005083check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005084 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005085 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086{
5087 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5088
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005089 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005090 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 {
5092 if (message)
5093 {
5094#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5095 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5096 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005097 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005099 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5100 NGETTEXT("%d more file to edit. Quit anyway?",
5101 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5103 return OK;
5104 return FAIL;
5105 }
5106#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005107 semsg(NGETTEXT("E173: %d more file to edit",
5108 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005109 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 }
5111 return FAIL;
5112 }
5113 return OK;
5114}
5115
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116/*
5117 * Function given to ExpandGeneric() to obtain the list of command names.
5118 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005120get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121{
5122 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 return cmdnames[idx].cmd_name;
5125}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005128ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129{
Bram Moolenaare6850792010-05-14 15:28:44 +02005130 if (*eap->arg == NUL)
5131 {
5132#ifdef FEAT_EVAL
5133 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5134 char_u *p = NULL;
5135
5136 if (expr != NULL)
5137 {
5138 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005139 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005140 --emsg_off;
5141 vim_free(expr);
5142 }
5143 if (p != NULL)
5144 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005145 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005146 vim_free(p);
5147 }
5148 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005149 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005150#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005151 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005152#endif
5153 }
5154 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005155 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005156
5157#ifdef FEAT_VTP
5158 else if (has_vtp_working())
5159 {
5160 // background color change requires clear + redraw
5161 update_screen(CLEAR);
5162 redrawcmd();
5163 }
5164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165}
5166
5167 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005168ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169{
5170 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005171 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 do_highlight(eap->arg, eap->forceit, FALSE);
5173}
5174
5175
5176/*
5177 * Call this function if we thought we were going to exit, but we won't
5178 * (because of an error). May need to restore the terminal mode.
5179 */
5180 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005181not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182{
5183 exiting = FALSE;
5184 settmode(TMODE_RAW);
5185}
5186
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005187 static int
5188before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5189{
5190 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5191
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005192 // Bail out when autocommands closed the window.
5193 // Refuse to quit when the buffer in the last window is being closed (can
5194 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005195 if (!win_valid(wp)
5196 || curbuf_locked()
5197 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5198 return TRUE;
5199
5200 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5201 {
5202 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005203 // Refuse to quit when locked or when the window was closed or the
5204 // buffer in the last window is being closed (can only happen in
5205 // autocommands).
5206 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005207 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5208 return TRUE;
5209 }
5210
5211 return FALSE;
5212}
5213
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005215 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005216 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005217 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005219 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005220ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005222 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005223
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224#ifdef FEAT_CMDWIN
5225 if (cmdwin_type != 0)
5226 {
5227 cmdwin_result = Ctrl_C;
5228 return;
5229 }
5230#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005231 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005232 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005233 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005234 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005235 return;
5236 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005237 if (eap->addr_count > 0)
5238 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005239 int wnr = eap->line2;
5240
5241 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5242 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005243 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005244 }
5245 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005246 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005247
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005248 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005249 if (curbuf_locked())
5250 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005251
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005252 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005253 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005254 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255
5256#ifdef FEAT_NETBEANS_INTG
5257 netbeansForcedQuit = eap->forceit;
5258#endif
5259
5260 /*
5261 * If there are more files or windows we won't exit.
5262 */
5263 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5264 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005265 if ((!buf_hide(wp->w_buffer)
5266 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005267 | (eap->forceit ? CCGD_FORCEIT : 0)
5268 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005270 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 {
5272 not_exiting();
5273 }
5274 else
5275 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005276 // quit last window
5277 // Note: only_one_window() returns true, even so a help window is
5278 // still open. In that case only quit, if no address has been
5279 // specified. Example:
5280 // :h|wincmd w|1q - don't quit
5281 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005282 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005284 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005285#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005287#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005288 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005289 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 }
5291}
5292
5293/*
5294 * ":cquit".
5295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005297ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005299 // this does not always pass on the exit code to the Manx compiler. why?
5300 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301}
5302
5303/*
5304 * ":qall": try to quit all windows
5305 */
5306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005307ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308{
5309# ifdef FEAT_CMDWIN
5310 if (cmdwin_type != 0)
5311 {
5312 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005313 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 else
5315 cmdwin_result = K_XF2;
5316 return;
5317 }
5318# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005319
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005320 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005321 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005322 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005323 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005324 return;
5325 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005326
5327 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005328 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005329
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005331 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 getout(0);
5333 not_exiting();
5334}
5335
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336/*
5337 * ":close": close current window, unless it is the last one
5338 */
5339 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005340ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005342 win_T *win;
5343 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005344#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005346 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005348#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005349 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005350 {
5351 if (eap->addr_count == 0)
5352 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005353 else
5354 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005355 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005356 {
5357 winnr++;
5358 if (winnr == eap->line2)
5359 break;
5360 }
5361 if (win == NULL)
5362 win = lastwin;
5363 ex_win_close(eap->forceit, win, NULL);
5364 }
5365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366}
5367
Bram Moolenaar4033c552017-09-16 20:54:51 +02005368#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005369/*
5370 * ":pclose": Close any preview window.
5371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005373ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005374{
5375 win_T *win;
5376
Bram Moolenaar79648732019-07-18 21:43:07 +02005377 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005378 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005379 if (win->w_p_pvw)
5380 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005381 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005382 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005383 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005384# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005385 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005386 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005387# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005388}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005389#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005390
Bram Moolenaarf740b292006-02-16 22:11:02 +00005391/*
5392 * Close window "win" and take care of handling closing the last window for a
5393 * modified buffer.
5394 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005395 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005396ex_win_close(
5397 int forceit,
5398 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005399 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400{
5401 int need_hide;
5402 buf_T *buf = win->w_buffer;
5403
Bram Moolenaarcf844172020-06-26 19:44:06 +02005404 // Never close the autocommand window.
5405 if (win == aucmd_win)
5406 {
5407 emsg(_(e_autocmd_close));
5408 return;
5409 }
5410
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005412 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005414#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 if ((p_confirm || cmdmod.confirm) && p_write)
5416 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005417 bufref_T bufref;
5418
5419 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005421 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 return;
5423 need_hide = FALSE;
5424 }
5425 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005428 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 return;
5430 }
5431 }
5432
Bram Moolenaar4033c552017-09-16 20:54:51 +02005433#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005435#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005436
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005437 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005438 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005439 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005440 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005441 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442}
5443
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005445 * Handle the argument for a tabpage related ex command.
5446 * Returns a tabpage number.
5447 * When an error is encountered then eap->errmsg is set.
5448 */
5449 static int
5450get_tabpage_arg(exarg_T *eap)
5451{
5452 int tab_number;
5453 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5454
5455 if (eap->arg && *eap->arg != NUL)
5456 {
5457 char_u *p = eap->arg;
5458 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005459 int relative = 0; // argument +N/-N means: go to N places to the
5460 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005461
5462 if (*p == '-')
5463 {
5464 relative = -1;
5465 p++;
5466 }
5467 else if (*p == '+')
5468 {
5469 relative = 1;
5470 p++;
5471 }
5472
5473 p_save = p;
5474 tab_number = getdigits(&p);
5475
5476 if (relative == 0)
5477 {
5478 if (STRCMP(p, "$") == 0)
5479 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005480 else if (STRCMP(p, "#") == 0)
5481 if (valid_tabpage(lastused_tabpage))
5482 tab_number = tabpage_index(lastused_tabpage);
5483 else
5484 {
5485 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5486 tab_number = 0;
5487 goto theend;
5488 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005489 else if (p == p_save || *p_save == '-' || *p != NUL
5490 || tab_number > LAST_TAB_NR)
5491 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005492 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005493 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005494 goto theend;
5495 }
5496 }
5497 else
5498 {
5499 if (*p_save == NUL)
5500 tab_number = 1;
5501 else if (p == p_save || *p_save == '-' || *p != NUL
5502 || tab_number == 0)
5503 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005504 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005505 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005506 goto theend;
5507 }
5508 tab_number = tab_number * relative + tabpage_index(curtab);
5509 if (!unaccept_arg0 && relative == -1)
5510 --tab_number;
5511 }
5512 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005513 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005514 }
5515 else if (eap->addr_count > 0)
5516 {
5517 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005518 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005519 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005520 tab_number = 0;
5521 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005522 else
5523 {
5524 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005525 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005526 {
5527 --tab_number;
5528 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005529 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005530 }
5531 }
5532 }
5533 else
5534 {
5535 switch (eap->cmdidx)
5536 {
5537 case CMD_tabnext:
5538 tab_number = tabpage_index(curtab) + 1;
5539 if (tab_number > LAST_TAB_NR)
5540 tab_number = 1;
5541 break;
5542 case CMD_tabmove:
5543 tab_number = LAST_TAB_NR;
5544 break;
5545 default:
5546 tab_number = tabpage_index(curtab);
5547 }
5548 }
5549
5550theend:
5551 return tab_number;
5552}
5553
5554/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005555 * ":tabclose": close current tab page, unless it is the last one.
5556 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 */
5558 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005559ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005561 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005562 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005563
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005564# ifdef FEAT_CMDWIN
5565 if (cmdwin_type != 0)
5566 cmdwin_result = K_IGNORE;
5567 else
5568# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005569 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005570 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005571 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005573 tab_number = get_tabpage_arg(eap);
5574 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005575 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005576 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005577 if (tp == NULL)
5578 {
5579 beep_flush();
5580 return;
5581 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005582 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005583 {
5584 tabpage_close_other(tp, eap->forceit);
5585 return;
5586 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005587 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005588 tabpage_close(eap->forceit);
5589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005591}
5592
5593/*
5594 * ":tabonly": close all tab pages except the current one
5595 */
5596 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005597ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005598{
5599 tabpage_T *tp;
5600 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005601 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005602
5603# ifdef FEAT_CMDWIN
5604 if (cmdwin_type != 0)
5605 cmdwin_result = K_IGNORE;
5606 else
5607# endif
5608 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005609 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005610 else
5611 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005612 tab_number = get_tabpage_arg(eap);
5613 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005614 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005615 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005616 // Repeat this up to a 1000 times, because autocommands may
5617 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005618 for (done = 0; done < 1000; ++done)
5619 {
5620 FOR_ALL_TABPAGES(tp)
5621 if (tp->tp_topframe != topframe)
5622 {
5623 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005624 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005625 if (valid_tabpage(tp))
5626 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005627 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005628 break;
5629 }
5630 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005631 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005632 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005633 }
5634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636
5637/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005638 * Close the current tab page.
5639 */
5640 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005641tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005642{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005643 // First close all the windows but the current one. If that worked then
5644 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005645 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005646 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005647 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005648 ex_win_close(forceit, curwin, NULL);
5649# ifdef FEAT_GUI
5650 need_mouse_correct = TRUE;
5651# endif
5652}
5653
5654/*
5655 * Close tab page "tp", which is not the current tab page.
5656 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005657 * Also takes care of the tab pages line disappearing when closing the
5658 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005659 */
5660 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005661tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005662{
5663 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005664 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005665 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005666
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005667 // Limit to 1000 windows, autocommands may add a window while we close
5668 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005669 while (++done < 1000)
5670 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005671 wp = tp->tp_firstwin;
5672 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005673
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005674 // Autocommands may delete the tab page under our fingers and we may
5675 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005676 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005677 break;
5678 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005679
Bram Moolenaar29323592016-07-24 22:04:11 +02005680 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005681
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005682 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005683 if (h != tabline_height())
5684 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005685}
5686
5687/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 * ":only".
5689 */
5690 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005691ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005693 win_T *wp;
5694 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695# ifdef FEAT_GUI
5696 need_mouse_correct = TRUE;
5697# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005698 if (eap->addr_count > 0)
5699 {
5700 wnr = eap->line2;
5701 for (wp = firstwin; --wnr > 0; )
5702 {
5703 if (wp->w_next == NULL)
5704 break;
5705 else
5706 wp = wp->w_next;
5707 }
5708 win_goto(wp);
5709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 close_others(TRUE, eap->forceit);
5711}
5712
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005714ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005716 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005717 if (!eap->skip)
5718 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005719#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005720 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005721#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005722 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005723 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005724 else
5725 {
5726 int winnr = 0;
5727 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005728
Bram Moolenaar2256c992016-11-15 21:17:07 +01005729 FOR_ALL_WINDOWS(win)
5730 {
5731 winnr++;
5732 if (winnr == eap->line2)
5733 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005734 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005735 if (win == NULL)
5736 win = lastwin;
5737 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 }
5740}
5741
5742/*
5743 * ":stop" and ":suspend": Suspend Vim.
5744 */
5745 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005746ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747{
5748 /*
5749 * Disallow suspending for "rvim".
5750 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005751 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 {
5753 if (!eap->forceit)
5754 autowrite_all();
5755 windgoto((int)Rows - 1, 0);
5756 out_char('\n');
5757 out_flush();
5758 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005759 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005761 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005763 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764#ifdef FEAT_TITLE
5765 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005766 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767#endif
5768 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005769 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005771 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 }
5773}
5774
5775/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005776 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 */
5778 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005779ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780{
Bram Moolenaar461f2122020-07-28 20:25:47 +02005781#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02005782 if (not_in_vim9(eap) == FAIL)
5783 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02005784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785#ifdef FEAT_CMDWIN
5786 if (cmdwin_type != 0)
5787 {
5788 cmdwin_result = Ctrl_C;
5789 return;
5790 }
5791#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005792 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005793 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005794 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005795 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005796 return;
5797 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005798
5799 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005800 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801
5802 /*
5803 * if more files or windows we won't exit
5804 */
5805 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5806 exiting = TRUE;
5807 if ( ((eap->cmdidx == CMD_wq
5808 || curbufIsChanged())
5809 && do_write(eap) == FAIL)
5810 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005811 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 {
5813 not_exiting();
5814 }
5815 else
5816 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005817 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005819 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820# ifdef FEAT_GUI
5821 need_mouse_correct = TRUE;
5822# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005823 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005824 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 }
5826}
5827
5828/*
5829 * ":print", ":list", ":number".
5830 */
5831 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005832ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005834 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005835 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005836 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005838 for ( ;!got_int; ui_breakcheck())
5839 {
5840 print_line(eap->line1,
5841 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5842 || (eap->flags & EXFLAG_NR)),
5843 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5844 if (++eap->line1 > eap->line2)
5845 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005846 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005847 }
5848 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005849 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005850 curwin->w_cursor.lnum = eap->line2;
5851 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 }
5853
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855}
5856
5857#ifdef FEAT_BYTEOFF
5858 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005859ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860{
5861 goto_byte(eap->line2);
5862}
5863#endif
5864
5865/*
5866 * ":shell".
5867 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005869ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870{
5871 do_shell(NULL, 0);
5872}
5873
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005874#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005876static int drop_busy = FALSE;
5877static int drop_filec;
5878static char_u **drop_filev = NULL;
5879static int drop_split;
5880static void (*drop_callback)(void *);
5881static void *drop_cookie;
5882
5883 static void
5884handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885{
5886 exarg_T ea;
5887 int save_msg_scroll = msg_scroll;
5888
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005889 // Setting the argument list may cause screen updates and being called
5890 // recursively. Avoid that by setting drop_busy.
5891 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005893 // Check whether the current buffer is changed. If so, we will need
5894 // to split the current window or data could be lost.
5895 // We don't need to check if the 'hidden' option is set, as in this
5896 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005897 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 {
5899 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005900 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 --emsg_off;
5902 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005903 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 if (win_split(0, 0) == FAIL)
5906 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005907 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005909 // When splitting the window, create a new alist. Otherwise the
5910 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 alist_unlink(curwin->w_alist);
5912 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 }
5914
5915 /*
5916 * Set up the new argument list.
5917 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005918 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919
5920 /*
5921 * Move to the first file.
5922 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005923 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005924 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 ea.cmd = (char_u *)"next";
5926 do_argfile(&ea, 0);
5927
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005928 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5929 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 need_start_insertmode = FALSE;
5931
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005932 // Restore msg_scroll, otherwise a following command may cause scrolling
5933 // unexpectedly. The screen will be redrawn by the caller, thus
5934 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005936
5937 if (drop_callback != NULL)
5938 drop_callback(drop_cookie);
5939
5940 drop_filev = NULL;
5941 drop_busy = FALSE;
5942}
5943
5944/*
5945 * Handle a file drop. The code is here because a drop is *nearly* like an
5946 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005947 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005948 * code is very similar to :args and hence needs access to a lot of the static
5949 * functions in this file.
5950 *
5951 * The "filev" list must have been allocated using alloc(), as should each item
5952 * in the list. This function takes over responsibility for freeing the "filev"
5953 * list.
5954 */
5955 void
5956handle_drop(
5957 int filec, // the number of files dropped
5958 char_u **filev, // the list of files dropped
5959 int split, // force splitting the window
5960 void (*callback)(void *), // to be called after setting the argument
5961 // list
5962 void *cookie) // argument for "callback" (allocated)
5963{
5964 // Cannot handle recursive drops, finish the pending one.
5965 if (drop_busy)
5966 {
5967 FreeWild(filec, filev);
5968 vim_free(cookie);
5969 return;
5970 }
5971
5972 // When calling handle_drop() more than once in a row we only use the last
5973 // one.
5974 if (drop_filev != NULL)
5975 {
5976 FreeWild(drop_filec, drop_filev);
5977 vim_free(drop_cookie);
5978 }
5979
5980 drop_filec = filec;
5981 drop_filev = filev;
5982 drop_split = split;
5983 drop_callback = callback;
5984 drop_cookie = cookie;
5985
5986 // Postpone this when:
5987 // - editing the command line
5988 // - not possible to change the current buffer
5989 // - updating the screen
5990 // As it may change buffers and window structures that are in use and cause
5991 // freed memory to be used.
5992 if (text_locked() || curbuf_locked() || updating_screen)
5993 return;
5994
5995 handle_drop_internal();
5996}
5997
5998/*
5999 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6000 * reset: Handle a postponed drop.
6001 */
6002 void
6003handle_any_postponed_drop(void)
6004{
6005 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006006 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006007 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008}
6009#endif
6010
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 * ":preserve".
6013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006015ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006017 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 ml_preserve(curbuf, TRUE);
6019}
6020
6021/*
6022 * ":recover".
6023 */
6024 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006025ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006027 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006029 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6030 | CCGD_MULTWIN
6031 | (eap->forceit ? CCGD_FORCEIT : 0)
6032 | CCGD_EXCMD)
6033
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 && (*eap->arg == NUL
6035 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006036 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 recoverymode = FALSE;
6038}
6039
6040/*
6041 * Command modifier used in a wrong way.
6042 */
6043 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006044ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006046 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047}
6048
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049/*
6050 * :sview [+command] file split window with new file, read-only
6051 * :split [[+command] file] split window with current or new file
6052 * :vsplit [[+command] file] split window vertically with current or new file
6053 * :new [[+command] file] split window with no or new file
6054 * :vnew [[+command] file] split vertically window with no or new file
6055 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006056 *
6057 * :tabedit open new Tab page with empty window
6058 * :tabedit [+command] file open new Tab page and edit "file"
6059 * :tabnew [[+command] file] just like :tabedit
6060 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 */
6062 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006063ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006065 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006066#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006068#endif
6069#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006071#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006072 int use_tab = eap->cmdidx == CMD_tabedit
6073 || eap->cmdidx == CMD_tabfind
6074 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006076 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006077 return;
6078
Bram Moolenaar4033c552017-09-16 20:54:51 +02006079#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082
Bram Moolenaar4033c552017-09-16 20:54:51 +02006083#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006084 // A ":split" in the quickfix window works like ":new". Don't want two
6085 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006086 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 {
6088 if (eap->cmdidx == CMD_split)
6089 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 if (eap->cmdidx == CMD_vsplit)
6091 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094
Bram Moolenaar4033c552017-09-16 20:54:51 +02006095#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006096 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 {
6098 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6099 FNAME_MESS, TRUE, curbuf->b_ffname);
6100 if (fname == NULL)
6101 goto theend;
6102 eap->arg = fname;
6103 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006104# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006105 else
6106# endif
6107#endif
6108#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 && eap->cmdidx != CMD_new)
6112 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006113 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006114# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006115 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006116# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006117 au_has_group((char_u *)"FileExplorer"))
6118 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006119 // No browsing supported but we do have the file explorer:
6120 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006121 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6122 eap->arg = (char_u *)".";
6123 }
6124 else
6125 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006126 fname = do_browse(0, (char_u *)(use_tab
6127 ? _("Edit File in new tab page")
6128 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006130 if (fname == NULL)
6131 goto theend;
6132 eap->arg = fname;
6133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006135 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006138 /*
6139 * Either open new tab page or split the window.
6140 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006141 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006142 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006143 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6144 : eap->addr_count == 0 ? 0
6145 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006146 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006147 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006148
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006149 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006150 if (curwin != old_curwin
6151 && win_valid(old_curwin)
6152 && old_curwin->w_buffer != curbuf
6153 && !cmdmod.keepalt)
6154 old_curwin->w_alt_fnum = curbuf->b_fnum;
6155 }
6156 }
6157 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6159 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006160 // Reset 'scrollbind' when editing another file, but keep it when
6161 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006163# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006165# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006167 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 else
6169 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 do_exedit(eap, old_curwin);
6171 }
6172
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006173# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006175# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006177# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178theend:
6179 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006182
6183/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006184 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006185 */
6186 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006187tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006188{
6189 exarg_T ea;
6190
Bram Moolenaara80faa82020-04-12 19:37:17 +02006191 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006192 ea.cmdidx = CMD_tabnew;
6193 ea.cmd = (char_u *)"tabn";
6194 ea.arg = (char_u *)"";
6195 ex_splitview(&ea);
6196}
6197
6198/*
6199 * :tabnext command
6200 */
6201 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006202ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006203{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006204 int tab_number;
6205
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006206 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006207 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006208 switch (eap->cmdidx)
6209 {
6210 case CMD_tabfirst:
6211 case CMD_tabrewind:
6212 goto_tabpage(1);
6213 break;
6214 case CMD_tablast:
6215 goto_tabpage(9999);
6216 break;
6217 case CMD_tabprevious:
6218 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006219 if (eap->arg && *eap->arg != NUL)
6220 {
6221 char_u *p = eap->arg;
6222 char_u *p_save = p;
6223
6224 tab_number = getdigits(&p);
6225 if (p == p_save || *p_save == '-' || *p != NUL
6226 || tab_number == 0)
6227 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006228 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006229 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006230 return;
6231 }
6232 }
6233 else
6234 {
6235 if (eap->addr_count == 0)
6236 tab_number = 1;
6237 else
6238 {
6239 tab_number = eap->line2;
6240 if (tab_number < 1)
6241 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006242 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006243 return;
6244 }
6245 }
6246 }
6247 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006248 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006249 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006250 tab_number = get_tabpage_arg(eap);
6251 if (eap->errmsg == NULL)
6252 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006253 break;
6254 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006255}
6256
6257/*
6258 * :tabmove command
6259 */
6260 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006261ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006262{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006263 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006264
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006265 tab_number = get_tabpage_arg(eap);
6266 if (eap->errmsg == NULL)
6267 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006268}
6269
6270/*
6271 * :tabs command: List tabs and their contents.
6272 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006273 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006274ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006275{
6276 tabpage_T *tp;
6277 win_T *wp;
6278 int tabcount = 1;
6279
6280 msg_start();
6281 msg_scroll = TRUE;
6282 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6283 {
6284 msg_putchar('\n');
6285 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006286 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006287 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006288 ui_breakcheck();
6289
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006290 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006291 wp = firstwin;
6292 else
6293 wp = tp->tp_firstwin;
6294 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6295 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006296 msg_putchar('\n');
6297 msg_putchar(wp == curwin ? '>' : ' ');
6298 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006299 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6300 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006301 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006302 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006303 else
6304 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6305 IObuff, IOSIZE, TRUE);
6306 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006307 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006308 ui_breakcheck();
6309 }
6310 }
6311}
6312
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313/*
6314 * ":mode": Set screen mode.
6315 * If no argument given, just get the screen size and redraw.
6316 */
6317 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006318ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319{
6320 if (*eap->arg == NUL)
6321 shell_resized();
6322 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006323 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324}
6325
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326/*
6327 * ":resize".
6328 * set, increment or decrement current window height
6329 */
6330 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006331ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332{
6333 int n;
6334 win_T *wp = curwin;
6335
6336 if (eap->addr_count > 0)
6337 {
6338 n = eap->line2;
6339 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6340 ;
6341 }
6342
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006343# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006345# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 if (cmdmod.split & WSP_VERT)
6348 {
6349 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006350 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006351 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 n = 9999;
6353 win_setwidth_win((int)n, wp);
6354 }
6355 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 {
6357 if (*eap->arg == '-' || *eap->arg == '+')
6358 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006359 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 n = 9999;
6361 win_setheight_win((int)n, wp);
6362 }
6363}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364
6365/*
6366 * ":find [+command] <file>" command.
6367 */
6368 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006369ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370{
6371#ifdef FEAT_SEARCHPATH
6372 char_u *fname;
6373 int count;
6374
6375 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6376 TRUE, curbuf->b_ffname);
6377 if (eap->addr_count > 0)
6378 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006379 // Repeat finding the file "count" times. This matters when it
6380 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 count = eap->line2;
6382 while (fname != NULL && --count > 0)
6383 {
6384 vim_free(fname);
6385 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6386 FALSE, curbuf->b_ffname);
6387 }
6388 }
6389
6390 if (fname != NULL)
6391 {
6392 eap->arg = fname;
6393#endif
6394 do_exedit(eap, NULL);
6395#ifdef FEAT_SEARCHPATH
6396 vim_free(fname);
6397 }
6398#endif
6399}
6400
6401/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006402 * ":open" simulation: for now just work like ":visual".
6403 */
6404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006405ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006406{
6407 regmatch_T regmatch;
6408 char_u *p;
6409
6410 curwin->w_cursor.lnum = eap->line2;
6411 beginline(BL_SOL | BL_FIX);
6412 if (*eap->arg == '/')
6413 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006414 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006415 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006416 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006417 *p = NUL;
6418 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6419 if (regmatch.regprog != NULL)
6420 {
6421 regmatch.rm_ic = p_ic;
6422 p = ml_get_curline();
6423 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006424 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006425 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006426 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006427 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006428 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006429 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006430 eap->arg += STRLEN(eap->arg);
6431 }
6432 check_cursor();
6433
6434 eap->cmdidx = CMD_visual;
6435 do_exedit(eap, NULL);
6436}
6437
6438/*
6439 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 */
6441 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006442ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443{
6444 do_exedit(eap, NULL);
6445}
6446
6447/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006448 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006451do_exedit(
6452 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006453 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454{
6455 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006457 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006459 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6460 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006461 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 /*
6463 * ":vi" command ends Ex mode.
6464 */
6465 if (exmode_active && (eap->cmdidx == CMD_visual
6466 || eap->cmdidx == CMD_view))
6467 {
6468 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006469 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006471 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006472 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006473 if (global_busy)
6474 {
6475 int rd = RedrawingDisabled;
6476 int nwr = no_wait_return;
6477 int ms = msg_scroll;
6478#ifdef FEAT_GUI
6479 int he = hold_gui_events;
6480#endif
6481
6482 if (eap->nextcmd != NULL)
6483 {
6484 stuffReadbuff(eap->nextcmd);
6485 eap->nextcmd = NULL;
6486 }
6487
6488 if (exmode_was != EXMODE_VIM)
6489 settmode(TMODE_RAW);
6490 RedrawingDisabled = 0;
6491 no_wait_return = 0;
6492 need_wait_return = FALSE;
6493 msg_scroll = 0;
6494#ifdef FEAT_GUI
6495 hold_gui_events = 0;
6496#endif
6497 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006498 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006499
6500 main_loop(FALSE, TRUE);
6501
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006502 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006503 RedrawingDisabled = rd;
6504 no_wait_return = nwr;
6505 msg_scroll = ms;
6506#ifdef FEAT_GUI
6507 hold_gui_events = he;
6508#endif
6509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 }
6513
6514 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006515 || eap->cmdidx == CMD_tabnew
6516 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006517 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006519 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 setpcmark();
6521 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006522 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6523 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006525 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 || *eap->arg != NUL
6527#ifdef FEAT_BROWSE
6528 || cmdmod.browse
6529#endif
6530 )
6531 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006532 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6533 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006534 if (*eap->arg != NUL && curbuf_locked())
6535 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006536
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 n = readonlymode;
6538 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6539 readonlymode = TRUE;
6540 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006541 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6542 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 setpcmark();
6544 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6545 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006546 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6548 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6549 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006550 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006552 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006553 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006555 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006557 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 if (old_curwin != NULL)
6559 {
6560 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006561 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006563#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006564 cleanup_T cs;
6565
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006566 // Reset the error/interrupt/exception state here so that
6567 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006568 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006569#endif
6570#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006572#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006573 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006574
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006575#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006576 // Restore the error/interrupt/exception state if not
6577 // discarded by a new aborting error, interrupt, or
6578 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006579 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 }
6582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 }
6584 else if (readonlymode && curbuf->b_nwindows == 1)
6585 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006586 // When editing an already visited buffer, 'readonly' won't be set
6587 // but the previous value is kept. With ":view" and ":sview" we
6588 // want the file to be readonly, except when another window is
6589 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 curbuf->b_p_ro = TRUE;
6591 }
6592 readonlymode = n;
6593 }
6594 else
6595 {
6596 if (eap->do_ecmd_cmd != NULL)
6597 do_cmdline_cmd(eap->do_ecmd_cmd);
6598#ifdef FEAT_TITLE
6599 n = curwin->w_arg_idx_invalid;
6600#endif
6601 check_arg_idx(curwin);
6602#ifdef FEAT_TITLE
6603 if (n != curwin->w_arg_idx_invalid)
6604 maketitle();
6605#endif
6606 }
6607
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 /*
6609 * if ":split file" worked, set alternate file name in old window to new
6610 * file
6611 */
6612 if (old_curwin != NULL
6613 && *eap->arg != NUL
6614 && curwin != old_curwin
6615 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006616 && old_curwin->w_buffer != curbuf
6617 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619
6620 ex_no_reprint = TRUE;
6621}
6622
6623#ifndef FEAT_GUI
6624/*
6625 * ":gui" and ":gvim" when there is no GUI.
6626 */
6627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006628ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006630 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631}
6632#endif
6633
Bram Moolenaar4f974752019-02-17 17:44:42 +01006634#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006636ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637{
6638 gui_make_tearoff(eap->arg);
6639}
6640#endif
6641
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006642#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6643 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006645ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006647# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6648 if (gui.in_use)
6649 gui_make_popup(eap->arg, eap->forceit);
6650# ifdef FEAT_TERM_POPUP_MENU
6651 else
6652# endif
6653# endif
6654# ifdef FEAT_TERM_POPUP_MENU
6655 pum_make_popup(eap->arg, eap->forceit);
6656# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657}
6658#endif
6659
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006661ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662{
6663 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006664 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006666 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667}
6668
6669/*
6670 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6671 * offset.
6672 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006675ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006678 win_T *save_curwin = curwin;
6679 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 long topline;
6681 long y;
6682 linenr_T old_linenr = curwin->w_cursor.lnum;
6683
6684 setpcmark();
6685
6686 /*
6687 * determine max topline
6688 */
6689 if (curwin->w_p_scb)
6690 {
6691 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006692 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 {
6694 if (wp->w_p_scb && wp->w_buffer)
6695 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006696 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 if (topline > y)
6698 topline = y;
6699 }
6700 }
6701 if (topline < 1)
6702 topline = 1;
6703 }
6704 else
6705 {
6706 topline = 1;
6707 }
6708
6709
6710 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006711 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006713 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 {
6715 if (curwin->w_p_scb)
6716 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006717 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 y = topline - curwin->w_topline;
6719 if (y > 0)
6720 scrollup(y, TRUE);
6721 else
6722 scrolldown(-y, TRUE);
6723 curwin->w_scbind_pos = topline;
6724 redraw_later(VALID);
6725 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 }
6728 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006729 curwin = save_curwin;
6730 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 if (curwin->w_p_scb)
6732 {
6733 did_syncbind = TRUE;
6734 checkpcmark();
6735 if (old_linenr != curwin->w_cursor.lnum)
6736 {
6737 char_u ctrl_o[2];
6738
6739 ctrl_o[0] = Ctrl_O;
6740 ctrl_o[1] = 0;
6741 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6742 }
6743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744}
6745
6746
6747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006748ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006750 int i;
6751 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6752 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006754 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 do_bang(1, eap, FALSE, FALSE, TRUE);
6756 else
6757 {
6758 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6759 return;
6760
6761#ifdef FEAT_BROWSE
6762 if (cmdmod.browse)
6763 {
6764 char_u *browseFile;
6765
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006766 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 NULL, NULL, NULL, curbuf);
6768 if (browseFile != NULL)
6769 {
6770 i = readfile(browseFile, NULL,
6771 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6772 vim_free(browseFile);
6773 }
6774 else
6775 i = OK;
6776 }
6777 else
6778#endif
6779 if (*eap->arg == NUL)
6780 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006781 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 return;
6783 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6784 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6785 }
6786 else
6787 {
6788 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6789 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6790 i = readfile(eap->arg, NULL,
6791 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6792
6793 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006794 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006796#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 if (!aborting())
6798#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006799 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 }
6801 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006802 {
6803 if (empty && exmode_active)
6804 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006805 // Delete the empty line that remains. Historically ex does
6806 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006807 if (eap->line2 == 0)
6808 lnum = curbuf->b_ml.ml_line_count;
6809 else
6810 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006811 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006812 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006813 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006814 if (curwin->w_cursor.lnum > 1
6815 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006816 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006817 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006818 }
6819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 }
6823}
6824
Bram Moolenaarea408852005-06-25 22:49:46 +00006825static char_u *prev_dir = NULL;
6826
6827#if defined(EXITFREE) || defined(PROTO)
6828 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006829free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006830{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006831 VIM_CLEAR(prev_dir);
6832 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006833}
6834#endif
6835
Bram Moolenaarf4258302013-06-02 18:20:17 +02006836/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006837 * Get the previous directory for the given chdir scope.
6838 */
6839 static char_u *
6840get_prevdir(cdscope_T scope)
6841{
6842 if (scope == CDSCOPE_WINDOW)
6843 return curwin->w_prevdir;
6844 else if (scope == CDSCOPE_TABPAGE)
6845 return curtab->tp_prevdir;
6846 return prev_dir;
6847}
6848
6849/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006850 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006851 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6852 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006853 */
6854 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006855post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006856{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006857 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006858 // Clear tab local directory for both :cd and :tcd
6859 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006860 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006861 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006862 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006863 char_u *pdir = get_prevdir(scope);
6864
6865 // If still in the global directory, need to remember current
6866 // directory as the global directory.
6867 if (globaldir == NULL && pdir != NULL)
6868 globaldir = vim_strsave(pdir);
6869
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006870 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006871 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006872 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006873 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006874 curtab->tp_localdir = vim_strsave(NameBuff);
6875 else
6876 curwin->w_localdir = vim_strsave(NameBuff);
6877 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006878 }
6879 else
6880 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006881 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006882 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006883 }
6884
6885 shorten_fnames(TRUE);
6886}
6887
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006888/*
6889 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006890 * chdir() function.
6891 * scope == CDSCOPE_WINDOW: changes the window-local directory
6892 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6893 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006894 * Returns TRUE if the directory is successfully changed.
6895 */
6896 int
6897changedir_func(
6898 char_u *new_dir,
6899 int forceit,
6900 cdscope_T scope)
6901{
6902 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006903 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006904 int dir_differs;
6905 int retval = FALSE;
6906
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006907 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006908 return FALSE;
6909
6910 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6911 {
6912 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6913 return FALSE;
6914 }
6915
6916 // ":cd -": Change to previous directory
6917 if (STRCMP(new_dir, "-") == 0)
6918 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006919 pdir = get_prevdir(scope);
6920 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006921 {
6922 emsg(_("E186: No previous directory"));
6923 return FALSE;
6924 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006925 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006926 }
6927
Bram Moolenaar002bc792020-06-05 22:33:42 +02006928 // Free the previous directory
6929 tofree = get_prevdir(scope);
6930
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006931 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006932 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006933 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006934 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006935 pdir = NULL;
6936 if (scope == CDSCOPE_WINDOW)
6937 curwin->w_prevdir = pdir;
6938 else if (scope == CDSCOPE_TABPAGE)
6939 curtab->tp_prevdir = pdir;
6940 else
6941 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006942
6943#if defined(UNIX) || defined(VMS)
6944 // for UNIX ":cd" means: go to home directory
6945 if (*new_dir == NUL)
6946 {
6947 // use NameBuff for home directory name
6948# ifdef VMS
6949 char_u *p;
6950
6951 p = mch_getenv((char_u *)"SYS$LOGIN");
6952 if (p == NULL || *p == NUL) // empty is the same as not set
6953 NameBuff[0] = NUL;
6954 else
6955 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6956# else
6957 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6958# endif
6959 new_dir = NameBuff;
6960 }
6961#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006962 dir_differs = new_dir == NULL || pdir == NULL
6963 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006964 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6965 emsg(_(e_failed));
6966 else
6967 {
6968 char_u *acmd_fname;
6969
6970 post_chdir(scope);
6971
6972 if (dir_differs)
6973 {
6974 if (scope == CDSCOPE_WINDOW)
6975 acmd_fname = (char_u *)"window";
6976 else if (scope == CDSCOPE_TABPAGE)
6977 acmd_fname = (char_u *)"tabpage";
6978 else
6979 acmd_fname = (char_u *)"global";
6980 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6981 curbuf);
6982 }
6983 retval = TRUE;
6984 }
6985 vim_free(tofree);
6986
6987 return retval;
6988}
Bram Moolenaarea408852005-06-25 22:49:46 +00006989
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006991 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006993 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006994ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006996 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997
6998 new_dir = eap->arg;
6999#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007000 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 if (*new_dir == NUL)
7002 ex_pwd(NULL);
7003 else
7004#endif
7005 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007006 cdscope_T scope = CDSCOPE_GLOBAL;
7007
7008 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7009 scope = CDSCOPE_WINDOW;
7010 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7011 scope = CDSCOPE_TABPAGE;
7012
7013 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007014 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007015 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007016 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 ex_pwd(eap);
7018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 }
7020}
7021
7022/*
7023 * ":pwd".
7024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007026ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027{
7028 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7029 {
7030#ifdef BACKSLASH_IN_FILENAME
7031 slash_adjust(NameBuff);
7032#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007033 if (p_verbose > 0)
7034 {
7035 char *context = "global";
7036
7037 if (curwin->w_localdir != NULL)
7038 context = "window";
7039 else if (curtab->tp_localdir != NULL)
7040 context = "tabpage";
7041 smsg("[%s] %s", context, (char *)NameBuff);
7042 }
7043 else
7044 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 }
7046 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007047 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048}
7049
7050/*
7051 * ":=".
7052 */
7053 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007054ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007056 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007057 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058}
7059
7060 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007061ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007063 int n;
7064 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065
7066 if (cursor_valid())
7067 {
7068 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7069 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007070 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007072
7073 len = eap->line2;
7074 switch (*eap->arg)
7075 {
7076 case 'm': break;
7077 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007078 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007079 }
7080 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081}
7082
7083/*
7084 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7085 */
7086 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007087do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088{
Bram Moolenaar52953192019-08-16 10:27:13 +02007089 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007090 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007091# ifdef ELAPSED_FUNC
7092 elapsed_T start_tv;
7093
7094 // Remember at what time we started, so that we know how much longer we
7095 // should wait after waiting for a bit.
7096 ELAPSED_INIT(start_tv);
7097# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098
7099 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007100 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007101 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007103 wait_now = msec - done > 1000L ? 1000L : msec - done;
7104#ifdef FEAT_TIMERS
7105 {
7106 long due_time = check_due_timer();
7107
7108 if (due_time > 0 && due_time < wait_now)
7109 wait_now = due_time;
7110 }
7111#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007112#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007113 if (has_any_channel() && wait_now > 20L)
7114 wait_now = 20L;
7115#endif
7116#ifdef FEAT_SOUND
7117 if (has_any_sound_callback() && wait_now > 20L)
7118 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007119#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007120 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007121
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007122#ifdef FEAT_JOB_CHANNEL
7123 if (has_any_channel())
7124 ui_breakcheck_force(TRUE);
7125 else
7126#endif
7127 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007128#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007129 // Process the netbeans and clientserver messages that may have been
7130 // received in the call to ui_breakcheck() when the GUI is in use. This
7131 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007132 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007133#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007134
7135# ifdef ELAPSED_FUNC
7136 // actual time passed
7137 done = ELAPSED_FUNC(start_tv);
7138# else
7139 // guestimate time passed (will actually be more)
7140 done += wait_now;
7141# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007143
7144 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7145 // input buffer, otherwise a following call to input() fails.
7146 if (got_int)
7147 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148}
7149
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150/*
7151 * ":winsize" command (obsolete).
7152 */
7153 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007154ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155{
7156 int w, h;
7157 char_u *arg = eap->arg;
7158 char_u *p;
7159
7160 w = getdigits(&arg);
7161 arg = skipwhite(arg);
7162 p = arg;
7163 h = getdigits(&arg);
7164 if (*p != NUL && *arg == NUL)
7165 set_shellsize(w, h, TRUE);
7166 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007167 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168}
7169
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007171ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172{
7173 int xchar = NUL;
7174 char_u *p;
7175
7176 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7177 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007178 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 if (eap->arg[1] == NUL)
7180 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007181 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 return;
7183 }
7184 xchar = eap->arg[1];
7185 p = eap->arg + 2;
7186 }
7187 else
7188 p = eap->arg + 1;
7189
7190 eap->nextcmd = check_nextcmd(p);
7191 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007192 if (*p != NUL && *p != (
7193#ifdef FEAT_EVAL
7194 in_vim9script() ? '#' :
7195#endif
7196 '"')
7197 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007198 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007199 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007201 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007203 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7205 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007206 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 }
7208}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209
Bram Moolenaar843ee412004-06-30 16:16:41 +00007210#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211/*
7212 * ":winpos".
7213 */
7214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007215ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216{
7217 int x, y;
7218 char_u *arg = eap->arg;
7219 char_u *p;
7220
7221 if (*arg == NUL)
7222 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007223# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007224# ifdef VIMDLL
7225 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7226 mch_get_winpos(&x, &y) != FAIL)
7227# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007229# else
7230 if (mch_get_winpos(&x, &y) != FAIL)
7231# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 {
7233 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007234 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 }
7236 else
7237# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007238 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 }
7240 else
7241 {
7242 x = getdigits(&arg);
7243 arg = skipwhite(arg);
7244 p = arg;
7245 y = getdigits(&arg);
7246 if (*p == NUL || *arg != NUL)
7247 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007248 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 return;
7250 }
7251# ifdef FEAT_GUI
7252 if (gui.in_use)
7253 gui_mch_set_winpos(x, y);
7254 else if (gui.starting)
7255 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007256 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 gui_win_x = x;
7258 gui_win_y = y;
7259 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007260# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 else
7262# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007263# endif
7264# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007265 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266# endif
7267# ifdef HAVE_TGETENT
7268 if (*T_CWP)
7269 term_set_winpos(x, y);
7270# endif
7271 }
7272}
7273#endif
7274
7275/*
7276 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7277 */
7278 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007279ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280{
7281 oparg_T oa;
7282
7283 clear_oparg(&oa);
7284 oa.regname = eap->regname;
7285 oa.start.lnum = eap->line1;
7286 oa.end.lnum = eap->line2;
7287 oa.line_count = eap->line2 - eap->line1 + 1;
7288 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007290 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 {
7292 setpcmark();
7293 curwin->w_cursor.lnum = eap->line1;
7294 beginline(BL_SOL | BL_FIX);
7295 }
7296
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007297 if (VIsual_active)
7298 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007299
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 switch (eap->cmdidx)
7301 {
7302 case CMD_delete:
7303 oa.op_type = OP_DELETE;
7304 op_delete(&oa);
7305 break;
7306
7307 case CMD_yank:
7308 oa.op_type = OP_YANK;
7309 (void)op_yank(&oa, FALSE, TRUE);
7310 break;
7311
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007312 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007313 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007315 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7316#else
7317 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007319 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 oa.op_type = OP_RSHIFT;
7321 else
7322 oa.op_type = OP_LSHIFT;
7323 op_shift(&oa, FALSE, eap->amount);
7324 break;
7325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007327 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328}
7329
7330/*
7331 * ":put".
7332 */
7333 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007334ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007336 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 if (eap->line2 == 0)
7338 {
7339 eap->line2 = 1;
7340 eap->forceit = TRUE;
7341 }
7342 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007343 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007344 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345}
7346
7347/*
7348 * Handle ":copy" and ":move".
7349 */
7350 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007351ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352{
7353 long n;
7354
Bram Moolenaar491799b2020-08-01 19:23:43 +02007355#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007356 if (not_in_vim9(eap) == FAIL)
7357 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007358#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007359 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007360 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 {
7362 eap->nextcmd = NULL;
7363 return;
7364 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007365 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366
7367 /*
7368 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7369 */
7370 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7371 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007372 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 return;
7374 }
7375
7376 if (eap->cmdidx == CMD_move)
7377 {
7378 if (do_move(eap->line1, eap->line2, n) == FAIL)
7379 return;
7380 }
7381 else
7382 ex_copy(eap->line1, eap->line2, n);
7383 u_clearline();
7384 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007385 ex_may_print(eap);
7386}
7387
7388/*
7389 * Print the current line if flags were given to the Ex command.
7390 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007391 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007392ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007393{
7394 if (eap->flags != 0)
7395 {
7396 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7397 (eap->flags & EXFLAG_LIST));
7398 ex_no_reprint = TRUE;
7399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400}
7401
7402/*
7403 * ":smagic" and ":snomagic".
7404 */
7405 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007406ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407{
7408 int magic_save = p_magic;
7409
7410 p_magic = (eap->cmdidx == CMD_smagic);
Bram Moolenaar66e00142020-08-12 21:58:12 +02007411 ex_substitute(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 p_magic = magic_save;
7413}
7414
7415/*
7416 * ":join".
7417 */
7418 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007419ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420{
7421 curwin->w_cursor.lnum = eap->line1;
7422 if (eap->line1 == eap->line2)
7423 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007424 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 return;
7426 if (eap->line2 == curbuf->b_ml.ml_line_count)
7427 {
7428 beep_flush();
7429 return;
7430 }
7431 ++eap->line2;
7432 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007433 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007435 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436}
7437
7438/*
7439 * ":[addr]@r" or ":[addr]*r": execute register
7440 */
7441 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007442ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443{
7444 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007445 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446
7447 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007448 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449
7450#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007451 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452#endif
7453
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007454 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 c = *eap->arg;
7456 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7457 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007458 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007459 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7460 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007461 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 else
7465 {
7466 int save_efr = exec_from_reg;
7467
7468 exec_from_reg = TRUE;
7469
7470 /*
7471 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007472 * Continue until the stuff buffer is empty and all added characters
7473 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007475 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7477
7478 exec_from_reg = save_efr;
7479 }
7480}
7481
7482/*
7483 * ":!".
7484 */
7485 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007486ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487{
7488 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7489}
7490
7491/*
7492 * ":undo".
7493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007495ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007497 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007498 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007499 else
7500 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501}
7502
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007503#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007505ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007506{
7507 char_u hash[UNDO_HASH_SIZE];
7508
7509 u_compute_hash(hash);
7510 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7511}
7512
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007513 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007514ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007515{
7516 char_u hash[UNDO_HASH_SIZE];
7517
7518 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007519 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007520}
7521#endif
7522
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523/*
7524 * ":redo".
7525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007527ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528{
7529 u_redo(1);
7530}
7531
7532/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007533 * ":earlier" and ":later".
7534 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007536ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007537{
7538 long count = 0;
7539 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007540 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007541 char_u *p = eap->arg;
7542
7543 if (*p == NUL)
7544 count = 1;
7545 else if (isdigit(*p))
7546 {
7547 count = getdigits(&p);
7548 switch (*p)
7549 {
7550 case 's': ++p; sec = TRUE; break;
7551 case 'm': ++p; sec = TRUE; count *= 60; break;
7552 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007553 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7554 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007555 }
7556 }
7557
7558 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007559 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007560 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007561 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7562 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007563}
7564
7565/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 * ":redir": start/stop redirection.
7567 */
7568 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007569ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570{
7571 char *mode;
7572 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007573 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574
Bram Moolenaarba768492016-07-08 15:32:54 +02007575#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007576 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007577 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007578 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007579 return;
7580 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007581#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007582
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 if (STRICMP(eap->arg, "END") == 0)
7584 close_redir();
7585 else
7586 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007587 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007589 ++arg;
7590 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007592 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 mode = "a";
7594 }
7595 else
7596 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007597 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598
7599 close_redir();
7600
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007601 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007602 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 if (fname == NULL)
7604 return;
7605#ifdef FEAT_BROWSE
7606 if (cmdmod.browse)
7607 {
7608 char_u *browseFile;
7609
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007610 browseFile = do_browse(BROWSE_SAVE,
7611 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007612 fname, NULL, NULL,
7613 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007615 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 vim_free(fname);
7617 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007618 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 }
7620#endif
7621
7622 redir_fd = open_exfile(fname, eap->forceit, mode);
7623 vim_free(fname);
7624 }
7625#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007626 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007628 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007630 ++arg;
7631 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007633 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007634 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007636 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007638 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007639 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007640 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007641 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007643 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007644 if (*arg == '>')
7645 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007646 // Make register empty when not using @A-@Z and the
7647 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007648 if (*arg == NUL && !isupper(redir_reg))
7649 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007651 }
7652 if (*arg != NUL)
7653 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007654 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007655 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007656 }
7657 }
7658 else if (*arg == '=' && arg[1] == '>')
7659 {
7660 int append;
7661
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007662 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007663 close_redir();
7664 arg += 2;
7665
7666 if (*arg == '>')
7667 {
7668 ++arg;
7669 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 }
7671 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007672 append = FALSE;
7673
7674 if (var_redir_start(skipwhite(arg), append) == OK)
7675 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 }
7677#endif
7678
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007679 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007682 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007684
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007685 // Make sure redirection is not off. Can happen for cmdline completion
7686 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007687 if (redir_fd != NULL
7688#ifdef FEAT_EVAL
7689 || redir_reg || redir_vname
7690#endif
7691 )
7692 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693}
7694
7695/*
7696 * ":redraw": force redraw
7697 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007698 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007699ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700{
7701 int r = RedrawingDisabled;
7702 int p = p_lz;
7703
7704 RedrawingDisabled = 0;
7705 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007706 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007708 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709#ifdef FEAT_TITLE
7710 if (need_maketitle)
7711 maketitle();
7712#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007713#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7714# ifdef VIMDLL
7715 if (!gui.in_use)
7716# endif
7717 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 RedrawingDisabled = r;
7720 p_lz = p;
7721
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007722 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 msg_didout = FALSE;
7724 msg_col = 0;
7725
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007726 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007727 need_wait_return = FALSE;
7728
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 out_flush();
7730}
7731
7732/*
7733 * ":redrawstatus": force redraw of status line(s)
7734 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007736ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 int r = RedrawingDisabled;
7739 int p = p_lz;
7740
7741 RedrawingDisabled = 0;
7742 p_lz = FALSE;
7743 if (eap->forceit)
7744 status_redraw_all();
7745 else
7746 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007747 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 RedrawingDisabled = r;
7749 p_lz = p;
7750 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751}
7752
Bram Moolenaare12bab32019-01-08 22:02:56 +01007753/*
7754 * ":redrawtabline": force redraw of the tabline
7755 */
7756 static void
7757ex_redrawtabline(exarg_T *eap UNUSED)
7758{
7759 int r = RedrawingDisabled;
7760 int p = p_lz;
7761
7762 RedrawingDisabled = 0;
7763 p_lz = FALSE;
7764
7765 draw_tabline();
7766
7767 RedrawingDisabled = r;
7768 p_lz = p;
7769 out_flush();
7770}
7771
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007773close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774{
7775 if (redir_fd != NULL)
7776 {
7777 fclose(redir_fd);
7778 redir_fd = NULL;
7779 }
7780#ifdef FEAT_EVAL
7781 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007782 if (redir_vname)
7783 {
7784 var_redir_stop();
7785 redir_vname = 0;
7786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787#endif
7788}
7789
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007790#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007791 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007792vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007793{
7794 if (vim_mkdir(name, prot) != 0)
7795 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007796 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007797 return FAIL;
7798 }
7799 return OK;
7800}
7801#endif
7802
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803/*
7804 * Open a file for writing for an Ex command, with some checks.
7805 * Return file descriptor, or NULL on failure.
7806 */
7807 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007808open_exfile(
7809 char_u *fname,
7810 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007811 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812{
7813 FILE *fd;
7814
7815#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007816 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 if (mch_isdir(fname))
7818 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007819 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 return NULL;
7821 }
7822#endif
7823 if (!forceit && *mode != 'a' && vim_fexists(fname))
7824 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007825 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 return NULL;
7827 }
7828
7829 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007830 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831
7832 return fd;
7833}
7834
7835/*
7836 * ":mark" and ":k".
7837 */
7838 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007839ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840{
7841 pos_T pos;
7842
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007843 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007844 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007845 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02007846 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 else
7848 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007849 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 curwin->w_cursor.lnum = eap->line2;
7851 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007852 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007853 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007854 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 }
7856}
7857
7858/*
7859 * Update w_topline, w_leftcol and the cursor position.
7860 */
7861 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007862update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007864 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 update_topline();
7866 if (!curwin->w_p_wrap)
7867 validate_cursor();
7868 update_curswant();
7869}
7870
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007872 * Save the current State and go to Normal mode.
7873 * Return TRUE if the typeahead could be saved.
7874 */
7875 int
7876save_current_state(save_state_T *sst)
7877{
7878 sst->save_msg_scroll = msg_scroll;
7879 sst->save_restart_edit = restart_edit;
7880 sst->save_msg_didout = msg_didout;
7881 sst->save_State = State;
7882 sst->save_insertmode = p_im;
7883 sst->save_finish_op = finish_op;
7884 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007885 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007886
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007887 msg_scroll = FALSE; // no msg scrolling in Normal mode
7888 restart_edit = 0; // don't go to Insert mode
7889 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007890
7891 /*
7892 * Save the current typeahead. This is required to allow using ":normal"
7893 * from an event handler and makes sure we don't hang when the argument
7894 * ends with half a command.
7895 */
7896 save_typeahead(&sst->tabuf);
7897 return sst->tabuf.typebuf_valid;
7898}
7899
7900 void
7901restore_current_state(save_state_T *sst)
7902{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007903 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007904 restore_typeahead(&sst->tabuf);
7905
7906 msg_scroll = sst->save_msg_scroll;
7907 restart_edit = sst->save_restart_edit;
7908 p_im = sst->save_insertmode;
7909 finish_op = sst->save_finish_op;
7910 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007911 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007912 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007913
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007914 // Restore the state (needed when called from a function executed for
7915 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007916 State = sst->save_State;
7917#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007918 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007919#endif
7920}
7921
7922/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 * ":normal[!] {commands}": Execute normal mode commands.
7924 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007925 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007926ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007928 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 char_u *arg = NULL;
7930 int l;
7931 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007933 if (ex_normal_lock > 0)
7934 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007935 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007936 return;
7937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 if (ex_normal_busy >= p_mmd)
7939 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007940 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 return;
7942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 /*
7945 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7946 * this for the K_SPECIAL leading byte, otherwise special keys will not
7947 * work.
7948 */
7949 if (has_mbyte)
7950 {
7951 int len = 0;
7952
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007953 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 for (p = eap->arg; *p != NUL; ++p)
7955 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007956#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007957 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007959#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007960 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007961 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007962#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 )
7966 len += 2;
7967 }
7968 if (len > 0)
7969 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007970 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 if (arg != NULL)
7972 {
7973 len = 0;
7974 for (p = eap->arg; *p != NUL; ++p)
7975 {
7976 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007977#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 if (*p == CSI)
7979 {
7980 arg[len++] = KS_EXTRA;
7981 arg[len++] = (int)KE_CSI;
7982 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007983#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007984 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 {
7986 arg[len++] = *++p;
7987 if (*p == K_SPECIAL)
7988 {
7989 arg[len++] = KS_SPECIAL;
7990 arg[len++] = KE_FILLER;
7991 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007992#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 else if (*p == CSI)
7994 {
7995 arg[len++] = KS_EXTRA;
7996 arg[len++] = (int)KE_CSI;
7997 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 }
8000 arg[len] = NUL;
8001 }
8002 }
8003 }
8004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008006 ++ex_normal_busy;
8007 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 {
8009 /*
8010 * Repeat the :normal command for each line in the range. When no
8011 * range given, execute it just once, without positioning the cursor
8012 * first.
8013 */
8014 do
8015 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 if (eap->addr_count != 0)
8017 {
8018 curwin->w_cursor.lnum = eap->line1++;
8019 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008020 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 }
8022
Bram Moolenaar13505972019-01-24 15:04:48 +01008023 exec_normal_cmd(arg != NULL
8024 ? arg
8025 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 }
8027 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8028 }
8029
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008030 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 update_topline_cursor();
8032
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008033 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 --ex_normal_busy;
Bram Moolenaar189832b2020-09-23 12:29:11 +02008035#ifdef FEAT_PROP_POPUP
8036 if (ex_normal_busy == 0)
8037 ex_normal_busy_done = FALSE;
8038#endif
Bram Moolenaareda73602014-11-05 09:53:23 +01008039 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008040#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008041 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008042#endif
8043
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045}
8046
8047/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008048 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 */
8050 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008051ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008053 if (eap->forceit)
8054 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008055 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008056 if (!curwin->w_cursor.lnum)
8057 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008058 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008059 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008060#ifdef FEAT_TERMINAL
8061 // Ignore this when running in an active terminal.
8062 if (term_job_running(curbuf->b_term))
8063 return;
8064#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008065
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008066 // Ignore the command when already in Insert mode. Inserting an
8067 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008068 if (State & INSERT)
8069 return;
8070
Bram Moolenaar64969662005-12-14 21:59:55 +00008071 if (eap->cmdidx == CMD_startinsert)
8072 restart_edit = 'a';
8073 else if (eap->cmdidx == CMD_startreplace)
8074 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008076 restart_edit = 'V';
8077
8078 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008080 if (eap->cmdidx == CMD_startinsert)
8081 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008082 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 }
8084}
8085
8086/*
8087 * ":stopinsert"
8088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008090ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091{
8092 restart_edit = 0;
8093 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008094 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008097/*
8098 * Execute normal mode command "cmd".
8099 * "remap" can be REMAP_NONE or REMAP_YES.
8100 */
8101 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008102exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008103{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008104 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008105 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008106 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008107}
Bram Moolenaar25281632016-01-21 23:32:32 +01008108
Bram Moolenaar25281632016-01-21 23:32:32 +01008109/*
8110 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008111 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008112 */
8113 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008114exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008115{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008116 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008117 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008118
Bram Moolenaar905dd902019-04-07 14:21:47 +02008119 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8120 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008121 clear_oparg(&oa);
8122 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008123 while ((!stuff_empty()
8124 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008125 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008126 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008127 {
8128 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008129#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008130 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008131 && oa.op_type == OP_NOP && oa.regname == NUL
8132 && !VIsual_active)
8133 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008134 // If terminal_loop() returns OK we got a key that is handled
8135 // in Normal model. With FAIL we first need to position the
8136 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008137 if (terminal_loop(TRUE) == OK)
8138 normal_cmd(&oa, TRUE);
8139 }
8140 else
8141#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008142 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008143 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008144 }
8145}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008146
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147#ifdef FEAT_FIND_ID
8148 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008149ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150{
8151 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8152 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8153 (linenr_T)1, (linenr_T)MAXLNUM);
8154}
8155
Bram Moolenaar4033c552017-09-16 20:54:51 +02008156#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157/*
8158 * ":psearch"
8159 */
8160 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008161ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162{
8163 g_do_tagpreview = p_pvh;
8164 ex_findpat(eap);
8165 g_do_tagpreview = 0;
8166}
8167#endif
8168
8169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008170ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171{
8172 int whole = TRUE;
8173 long n;
8174 char_u *p;
8175 int action;
8176
8177 switch (cmdnames[eap->cmdidx].cmd_name[2])
8178 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008179 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8181 action = ACTION_GOTO;
8182 else
8183 action = ACTION_SHOW;
8184 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008185 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 action = ACTION_SHOW_ALL;
8187 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008188 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 action = ACTION_GOTO;
8190 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008191 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 action = ACTION_SPLIT;
8193 break;
8194 }
8195
8196 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008197 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {
8199 n = getdigits(&eap->arg);
8200 eap->arg = skipwhite(eap->arg);
8201 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008202 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 {
8204 whole = FALSE;
8205 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008206 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 if (*p)
8208 {
8209 *p++ = NUL;
8210 p = skipwhite(p);
8211
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008212 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008213 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008214 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 else
8216 eap->nextcmd = check_nextcmd(p);
8217 }
8218 }
8219 if (!eap->skip)
8220 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8221 whole, !eap->forceit,
8222 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8223 n, action, eap->line1, eap->line2);
8224}
8225#endif
8226
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227
Bram Moolenaar4033c552017-09-16 20:54:51 +02008228#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229/*
8230 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8231 */
8232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008233ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008235 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8237}
8238
8239/*
8240 * ":pedit"
8241 */
8242 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008243ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244{
8245 win_T *curwin_save = curwin;
8246
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008247 if (ERROR_IF_ANY_POPUP_WINDOW)
8248 return;
8249
Bram Moolenaar026587b2019-08-17 15:08:00 +02008250 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008252 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008253
Bram Moolenaar026587b2019-08-17 15:08:00 +02008254 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008256
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 if (curwin != curwin_save && win_valid(curwin_save))
8258 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008259 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 validate_cursor();
8261 redraw_later(VALID);
8262 win_enter(curwin_save, TRUE);
8263 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008264# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008265 else if (WIN_IS_POPUP(curwin))
8266 {
8267 // can't keep focus in popup window
8268 win_enter(firstwin, TRUE);
8269 }
8270# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 g_do_tagpreview = 0;
8272}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274
8275/*
8276 * ":stag", ":stselect" and ":stjump".
8277 */
8278 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008279ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280{
8281 postponed_split = -1;
8282 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008283 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8285 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008286 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288
8289/*
8290 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8291 */
8292 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008293ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294{
8295 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8296}
8297
8298 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008299ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300{
8301 int cmd;
8302
8303 switch (name[1])
8304 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008305 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008307 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008309 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008311 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008313 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008315 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008317 case 'f': // ":tfirst"
8318 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008320 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008322 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008324 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008326 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 return;
8328 }
8329#endif
8330 cmd = DT_TAG;
8331 break;
8332 }
8333
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008334 if (name[0] == 'l')
8335 {
8336#ifndef FEAT_QUICKFIX
8337 ex_ni(eap);
8338 return;
8339#else
8340 cmd = DT_LTAG;
8341#endif
8342 }
8343
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8345 eap->forceit, TRUE);
8346}
8347
8348/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008349 * Check "str" for starting with a special cmdline variable.
8350 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8351 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8352 */
8353 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008354find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008355{
8356 int len;
8357 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008358 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008359 "%",
8360#define SPEC_PERC 0
8361 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008362#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008363 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008364#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008365 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008366#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008367 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008368#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008369 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008370#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008371 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008372#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008373 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008374#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008375 "<stack>", // call stack
8376#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008377 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008378#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008379 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008380#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008381 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008382#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008383 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008384#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008385 "<SID>", // script ID: <SNR>123_
8386#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008387#ifdef FEAT_CLIENTSERVER
8388 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008389# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008390#endif
8391 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008392
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008393 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008394 {
8395 len = (int)STRLEN(spec_str[i]);
8396 if (STRNCMP(src, spec_str[i], len) == 0)
8397 {
8398 *usedlen = len;
8399 return i;
8400 }
8401 }
8402 return -1;
8403}
8404
8405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 * Evaluate cmdline variables.
8407 *
8408 * change '%' to curbuf->b_ffname
8409 * '#' to curwin->w_altfile
8410 * '<cword>' to word under the cursor
8411 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008412 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 * '<cfile>' to path name under the cursor
8414 * '<sfile>' to sourced file name
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008415 * '<stack>' to call stack
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008416 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 * '<afile>' to file name for autocommand
8418 * '<abuf>' to buffer number for autocommand
8419 * '<amatch>' to matching name for autocommand
8420 *
8421 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8422 * "" for error without a message) and NULL is returned.
8423 * Returns an allocated string if a valid match was found.
8424 * Returns NULL if no match was found. "usedlen" then still contains the
8425 * number of characters to skip.
8426 */
8427 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008428eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008429 char_u *src, // pointer into commandline
8430 char_u *srcstart, // beginning of valid memory for src
8431 int *usedlen, // characters after src that are used
8432 linenr_T *lnump, // line number for :e command, or NULL
8433 char **errormsg, // pointer to error message
8434 int *escaped) // return value has escaped white space (can
8435 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436{
8437 int i;
8438 char_u *s;
8439 char_u *result;
8440 char_u *resultbuf = NULL;
8441 int resultlen;
8442 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008443 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008445 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448
8449 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008450 if (escaped != NULL)
8451 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452
8453 /*
8454 * Check if there is something to do.
8455 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008456 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008457 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 {
8459 *usedlen = 1;
8460 return NULL;
8461 }
8462
8463 /*
8464 * Skip when preceded with a backslash "\%" and "\#".
8465 * Note: In "\\%" the % is also not recognized!
8466 */
8467 if (src > srcstart && src[-1] == '\\')
8468 {
8469 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008470 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 return NULL;
8472 }
8473
8474 /*
8475 * word or WORD under cursor
8476 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008477 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8478 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008480 resultlen = find_ident_under_cursor(&result,
8481 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8482 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8483 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 if (resultlen == 0)
8485 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008486 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 return NULL;
8488 }
8489 }
8490
8491 /*
8492 * '#': Alternate file name
8493 * '%': Current file name
8494 * File name under the cursor
8495 * File name for autocommand
8496 * and following modifiers
8497 */
8498 else
8499 {
8500 switch (spec_idx)
8501 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008502 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 if (curbuf->b_fname == NULL)
8504 {
8505 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008506 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 }
8508 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008509 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008511 tilde_file = STRCMP(result, "~") == 0;
8512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 break;
8514
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008515 case SPEC_HASH: // '#' or "#99": alternate file
8516 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 {
8518 result = arg_all();
8519 resultbuf = result;
8520 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008521 if (escaped != NULL)
8522 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 break;
8525 }
8526 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008527 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008528 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008530 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008531 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008532 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008533 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008535 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008537 if (*usedlen < 2)
8538 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008539 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008540 *usedlen = 1;
8541 return NULL;
8542 }
8543#ifdef FEAT_EVAL
8544 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8545 (long)i);
8546 if (result == NULL)
8547 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008548 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008549 return NULL;
8550 }
8551#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008552 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 }
8556 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008557 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008558 if (i == 0 && src[1] == '<' && *usedlen > 1)
8559 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008560 buf = buflist_findnr(i);
8561 if (buf == NULL)
8562 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008563 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008564 return NULL;
8565 }
8566 if (lnump != NULL)
8567 *lnump = ECMD_LAST;
8568 if (buf->b_fname == NULL)
8569 {
8570 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008571 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008572 }
8573 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008574 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008575 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008576 tilde_file = STRCMP(result, "~") == 0;
8577 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 break;
8580
8581#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008582 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008583 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 if (result == NULL)
8585 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008586 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 return NULL;
8588 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008589 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 break;
8591#endif
8592
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008593 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008595 if (result != NULL && !autocmd_fname_full)
8596 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008597 // Still need to turn the fname into a full path. It is
8598 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008599 autocmd_fname_full = TRUE;
8600 result = FullName_save(autocmd_fname, FALSE);
8601 vim_free(autocmd_fname);
8602 autocmd_fname = result;
8603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 if (result == NULL)
8605 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008606 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 return NULL;
8608 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008609 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 break;
8611
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008612 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 if (autocmd_bufnr <= 0)
8614 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008615 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 return NULL;
8617 }
8618 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8619 result = strbuf;
8620 break;
8621
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008622 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 result = autocmd_match;
8624 if (result == NULL)
8625 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008626 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 return NULL;
8628 }
8629 break;
8630
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008631 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008632 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008633 result = estack_sfile(spec_idx == SPEC_SFILE
8634 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 if (result == NULL)
8636 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008637 *errormsg = spec_idx == SPEC_SFILE
8638 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8639 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 return NULL;
8641 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008642 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008644
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008645 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008646 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008647 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008648 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008649 return NULL;
8650 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008651 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008652 result = strbuf;
8653 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008654
8655#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008656 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008657 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008658 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008659 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008660 return NULL;
8661 }
8662 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008663 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008664 result = strbuf;
8665 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008666
Bram Moolenaar90944302020-08-01 20:45:11 +02008667 case SPEC_SID:
8668 if (current_sctx.sc_sid <= 0)
8669 {
8670 *errormsg = _(e_usingsid);
8671 return NULL;
8672 }
8673 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
8674 result = strbuf;
8675 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02008676#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02008677
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008678#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008679 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008680 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8681 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 result = strbuf;
8683 break;
8684#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008685
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008686 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008687 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008688 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 }
8690
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008691 resultlen = (int)STRLEN(result); // length of new string
8692 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 {
8694 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 resultlen = (int)(s - result);
8697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 else if (!skip_mod)
8699 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008700 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 &resultlen);
8702 if (result == NULL)
8703 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008704 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 return NULL;
8706 }
8707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 }
8709
8710 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8711 {
8712 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008713 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008714 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008716 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 result = NULL;
8718 }
8719 else
8720 result = vim_strnsave(result, resultlen);
8721 vim_free(resultbuf);
8722 return result;
8723}
8724
8725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 * Expand the <sfile> string in "arg".
8727 *
8728 * Returns an allocated string, or NULL for any error.
8729 */
8730 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008731expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008733 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 int len;
8735 char_u *result;
8736 char_u *newres;
8737 char_u *repl;
8738 int srclen;
8739 char_u *p;
8740
8741 result = vim_strsave(arg);
8742 if (result == NULL)
8743 return NULL;
8744
8745 for (p = result; *p; )
8746 {
8747 if (STRNCMP(p, "<sfile>", 7) != 0)
8748 ++p;
8749 else
8750 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008751 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008752 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 if (errormsg != NULL)
8754 {
8755 if (*errormsg)
8756 emsg(errormsg);
8757 vim_free(result);
8758 return NULL;
8759 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008760 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 {
8762 p += srclen;
8763 continue;
8764 }
8765 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8766 newres = alloc(len);
8767 if (newres == NULL)
8768 {
8769 vim_free(repl);
8770 vim_free(result);
8771 return NULL;
8772 }
8773 mch_memmove(newres, result, (size_t)(p - result));
8774 STRCPY(newres + (p - result), repl);
8775 len = (int)STRLEN(newres);
8776 STRCAT(newres, p + srclen);
8777 vim_free(repl);
8778 vim_free(result);
8779 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008780 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 }
8782 }
8783
8784 return result;
8785}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008788/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008789 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008790 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008793dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 if (fname == NULL)
8796 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008797 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798}
8799#endif
8800
8801/*
8802 * ":behave {mswin,xterm}"
8803 */
8804 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008805ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806{
8807 if (STRCMP(eap->arg, "mswin") == 0)
8808 {
8809 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8810 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8811 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8812 set_option_value((char_u *)"keymodel", 0L,
8813 (char_u *)"startsel,stopsel", 0);
8814 }
8815 else if (STRCMP(eap->arg, "xterm") == 0)
8816 {
8817 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8818 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8819 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8820 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8821 }
8822 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008823 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824}
8825
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826static int filetype_detect = FALSE;
8827static int filetype_plugin = FALSE;
8828static int filetype_indent = FALSE;
8829
8830/*
8831 * ":filetype [plugin] [indent] {on,off,detect}"
8832 * on: Load the filetype.vim file to install autocommands for file types.
8833 * off: Load the ftoff.vim file to remove all autocommands for file types.
8834 * plugin on: load filetype.vim and ftplugin.vim
8835 * plugin off: load ftplugof.vim
8836 * indent on: load filetype.vim and indent.vim
8837 * indent off: load indoff.vim
8838 */
8839 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008840ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841{
8842 char_u *arg = eap->arg;
8843 int plugin = FALSE;
8844 int indent = FALSE;
8845
8846 if (*eap->arg == NUL)
8847 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008848 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008849 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 filetype_detect ? "ON" : "OFF",
8851 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8852 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8853 return;
8854 }
8855
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008856 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 for (;;)
8858 {
8859 if (STRNCMP(arg, "plugin", 6) == 0)
8860 {
8861 plugin = TRUE;
8862 arg = skipwhite(arg + 6);
8863 continue;
8864 }
8865 if (STRNCMP(arg, "indent", 6) == 0)
8866 {
8867 indent = TRUE;
8868 arg = skipwhite(arg + 6);
8869 continue;
8870 }
8871 break;
8872 }
8873 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8874 {
8875 if (*arg == 'o' || !filetype_detect)
8876 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008877 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 filetype_detect = TRUE;
8879 if (plugin)
8880 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008881 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 filetype_plugin = TRUE;
8883 }
8884 if (indent)
8885 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008886 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 filetype_indent = TRUE;
8888 }
8889 }
8890 if (*arg == 'd')
8891 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008892 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008893 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 }
8895 }
8896 else if (STRCMP(arg, "off") == 0)
8897 {
8898 if (plugin || indent)
8899 {
8900 if (plugin)
8901 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008902 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 filetype_plugin = FALSE;
8904 }
8905 if (indent)
8906 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008907 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 filetype_indent = FALSE;
8909 }
8910 }
8911 else
8912 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008913 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 filetype_detect = FALSE;
8915 }
8916 }
8917 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008918 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919}
8920
8921/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008922 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 */
8924 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008925ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926{
8927 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008928 {
8929 char_u *arg = eap->arg;
8930
8931 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8932 arg += 9;
8933
8934 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8935 if (arg != eap->arg)
8936 did_filetype = FALSE;
8937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008941ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942{
8943#ifdef FEAT_DIGRAPHS
8944 if (*eap->arg != NUL)
8945 putdigraph(eap->arg);
8946 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008947 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008949 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950#endif
8951}
8952
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008953#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8954 void
8955set_no_hlsearch(int flag)
8956{
8957 no_hlsearch = flag;
8958# ifdef FEAT_EVAL
8959 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8960# endif
8961}
8962
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963/*
8964 * ":nohlsearch"
8965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008967ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008969 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008970 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972#endif
8973
8974#ifdef FEAT_CRYPT
8975/*
8976 * ":X": Get crypt key
8977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008979ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008981 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008982 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983}
8984#endif
8985
8986#ifdef FEAT_FOLDING
8987 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008988ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989{
8990 if (foldManualAllowed(TRUE))
8991 foldCreate(eap->line1, eap->line2);
8992}
8993
8994 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008995ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996{
8997 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8998 eap->forceit, FALSE);
8999}
9000
9001 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009002ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003{
9004 linenr_T lnum;
9005
Bram Moolenaar4facea32019-10-12 20:17:40 +02009006# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009007 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009008# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009009
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009010 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9012 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9013 ml_setmarked(lnum);
9014
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009015 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009017 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009018# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009019 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009020# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021}
9022#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009023
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009024#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009025/*
9026 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9027 * instead of a quickfix command.
9028 */
9029 int
9030is_loclist_cmd(int cmdidx)
9031{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009032 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009033 return FALSE;
9034 return cmdnames[cmdidx].cmd_name[0] == 'l';
9035}
9036#endif
9037
Bram Moolenaar4facea32019-10-12 20:17:40 +02009038#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009039 int
9040get_pressedreturn(void)
9041{
9042 return ex_pressedreturn;
9043}
9044
9045 void
9046set_pressedreturn(int val)
9047{
9048 ex_pressedreturn = val;
9049}
9050#endif