blob: a7542f35b4eba320ecad9a758fddbc1b8cbbe31f [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
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200294# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200295# define ex_lockvar ex_ni
296# define ex_oldfiles ex_ni
297# define ex_options ex_ni
298# define ex_packadd ex_ni
299# define ex_packloadall ex_ni
300# define ex_return ex_ni
301# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302# define ex_throw ex_ni
303# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000305# define ex_unlockvar ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100306# define ex_vim9script ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200307# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100308# define ex_import ex_ni
309# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200311#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_loadview ex_ni
313#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200314#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315# define ex_viminfo ex_ni
316#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_filetype(exarg_T *eap);
319static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000321# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322# define ex_diffpatch ex_ni
323# define ex_diffgetput ex_ni
324# define ex_diffsplit ex_ni
325# define ex_diffthis ex_ni
326# define ex_diffupdate ex_ni
327#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100328static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100330static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#else
332# define ex_nohlsearch ex_ni
333# define ex_match ex_ni
334#endif
335#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100336static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337#else
338# define ex_X ex_ni
339#endif
340#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100341static void ex_fold(exarg_T *eap);
342static void ex_foldopen(exarg_T *eap);
343static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344#else
345# define ex_fold ex_ni
346# define ex_foldopen ex_ni
347# define ex_folddo ex_ni
348#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100349#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350# define ex_language ex_ni
351#endif
352#ifndef FEAT_SIGNS
353# define ex_sign ex_ni
354#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000355#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200356# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000357# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200358# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361#ifndef FEAT_JUMPLIST
362# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200363# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364# define ex_changes ex_ni
365#endif
366
Bram Moolenaar05159a02005-02-26 23:04:13 +0000367#ifndef FEAT_PROFILE
368# define ex_profile ex_ni
369#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200370#ifndef FEAT_TERMINAL
371# define ex_terminal ex_ni
372#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200373#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
374# define ex_xrestore ex_ni
375#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100376#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200377# define ex_popupclear ex_ni
378#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000379
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380/*
381 * Declare cmdnames[].
382 */
383#define DO_DECLARE_EXCMD
384#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200385#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100386
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387static char_u dollar_command[2] = {'$', 0};
388
389
390#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100391// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392typedef struct
393{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100394 char_u *line; // command line
395 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396} wcmd_T;
397
398/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000399 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000401 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000403struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100405 garray_T *lines_gap; // growarray with line info
406 int current_line; // last read line from growarray
407 int repeating; // TRUE when looping a second time
408 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200409 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 void *cookie;
411};
412
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200413static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100414static int store_loop_line(garray_T *gap, char_u *line);
415static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000416
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100417// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000418struct dbg_stuff
419{
420 int trylevel;
421 int force_abort;
422 except_T *caught_stack;
423 char_u *vv_exception;
424 char_u *vv_throwpoint;
425 int did_emsg;
426 int got_int;
427 int did_throw;
428 int need_rethrow;
429 int check_cstack;
430 except_T *current_exception;
431};
432
Bram Moolenaared203462004-06-16 11:19:22 +0000433 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100434save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000435{
436 dsp->trylevel = trylevel; trylevel = 0;
437 dsp->force_abort = force_abort; force_abort = FALSE;
438 dsp->caught_stack = caught_stack; caught_stack = NULL;
439 dsp->vv_exception = v_exception(NULL);
440 dsp->vv_throwpoint = v_throwpoint(NULL);
441
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100442 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000443 dsp->did_emsg = did_emsg; did_emsg = FALSE;
444 dsp->got_int = got_int; got_int = FALSE;
445 dsp->did_throw = did_throw; did_throw = FALSE;
446 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
447 dsp->check_cstack = check_cstack; check_cstack = FALSE;
448 dsp->current_exception = current_exception; current_exception = NULL;
449}
450
451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100452restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000453{
454 suppress_errthrow = FALSE;
455 trylevel = dsp->trylevel;
456 force_abort = dsp->force_abort;
457 caught_stack = dsp->caught_stack;
458 (void)v_exception(dsp->vv_exception);
459 (void)v_throwpoint(dsp->vv_throwpoint);
460 did_emsg = dsp->did_emsg;
461 got_int = dsp->got_int;
462 did_throw = dsp->did_throw;
463 need_rethrow = dsp->need_rethrow;
464 check_cstack = dsp->check_cstack;
465 current_exception = dsp->current_exception;
466}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467#endif
468
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469/*
470 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
471 * command is given.
472 */
473 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100474do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100475 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476{
477 int save_msg_scroll;
478 int prev_msg_row;
479 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100480 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000481
482 if (improved)
483 exmode_active = EXMODE_VIM;
484 else
485 exmode_active = EXMODE_NORMAL;
486 State = NORMAL;
487
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100488 // When using ":global /pat/ visual" and then "Q" we return to continue
489 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000490 if (global_busy)
491 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492
493 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100494 ++RedrawingDisabled; // don't redisplay the window
495 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100497 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 ++hold_gui_events;
499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500
Bram Moolenaar32526b32019-01-19 17:43:09 +0100501 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 while (exmode_active)
503 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100504 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000505 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
506 {
507 exmode_active = FALSE;
508 break;
509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 msg_scroll = TRUE;
511 need_wait_return = FALSE;
512 ex_pressedreturn = FALSE;
513 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100514 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 prev_msg_row = msg_row;
516 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 if (improved)
518 {
519 cmdline_row = msg_row;
520 do_cmdline(NULL, getexline, NULL, 0);
521 }
522 else
523 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
524 lines_left = Rows - 1;
525
Bram Moolenaardf177f62005-02-22 08:39:57 +0000526 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100527 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000529 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100530 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000531 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000533 if (ex_pressedreturn)
534 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100535 // go up one line, to overwrite the ":<CR>" line, so the
536 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000537 msg_row = prev_msg_row;
538 if (prev_msg_row == Rows - 1)
539 msg_row--;
540 }
541 msg_col = 0;
542 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
543 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100546 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 {
548 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100549 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000550 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100551 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 }
554
555#ifdef FEAT_GUI
556 --hold_gui_events;
557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 --RedrawingDisabled;
559 --no_wait_return;
560 update_screen(CLEAR);
561 need_wait_return = FALSE;
562 msg_scroll = save_msg_scroll;
563}
564
565/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200566 * Print the executed command for when 'verbose' is set.
567 * When "lnum" is 0 only print the command.
568 */
569 static void
570msg_verbose_cmd(linenr_T lnum, char_u *cmd)
571{
572 ++no_wait_return;
573 verbose_enter_scroll();
574
575 if (lnum == 0)
576 smsg(_("Executing: %s"), cmd);
577 else
578 smsg(_("line %ld: %s"), (long)lnum, cmd);
579 if (msg_silent == 0)
580 msg_puts("\n"); // don't overwrite this
581
582 verbose_leave_scroll();
583 --no_wait_return;
584}
585
586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 * Execute a simple command line. Used for translated commands like "*".
588 */
589 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100590do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591{
592 return do_cmdline(cmd, NULL, NULL,
593 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
594}
595
596/*
597 * do_cmdline(): execute one Ex command line
598 *
599 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100600 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 * 2. Split up in parts separated with '|'.
602 *
603 * This function can be called recursively!
604 *
605 * flags:
606 * DOCMD_VERBOSE - The command will be included in the error message.
607 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100608 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 * DOCMD_KEYTYPED - Don't reset KeyTyped.
610 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
611 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
612 *
613 * return FAIL if cmdline could not be executed, OK otherwise
614 */
615 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100616do_cmdline(
617 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200618 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100619 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100620 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100622 char_u *next_cmdline; // next cmd to execute
623 char_u *cmdline_copy = NULL; // copy of cmd line
624 int used_getline = FALSE; // used "fgetline" to obtain command
625 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100627 int count = 0; // line number count
628 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 int retval = OK;
630#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100631 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100632 garray_T lines_ga; // keep lines for ":while"/":for"
633 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200634 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100635 char_u *fname = NULL; // function or script name
636 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
637 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
638 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200640 msglist_T **saved_msg_list = NULL;
641 msglist_T *private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100643 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200644 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000646 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000648 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100650# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651# define cmd_cookie cookie
652#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100653 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200654#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100655 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100657 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
658 // location for storing error messages to be converted to an exception.
659 // This ensures that the do_errthrow() call in do_one_cmd() does not
660 // combine the messages stored by an earlier invocation of do_one_cmd()
661 // with the command name of the later one. This would happen when
662 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 saved_msg_list = msg_list;
664 msg_list = &private_msg_list;
665 private_msg_list = NULL;
666#endif
667
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100668 // It's possible to create an endless loop with ":execute", catch that
669 // here. The value of 200 allows nested function calls, ":source", etc.
670 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100671 if (call_depth >= 200
672#ifdef FEAT_EVAL
673 && call_depth >= p_mfd
674#endif
675 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100677 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100679 // When converting to an exception, we do not include the command name
680 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100681 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 msg_list = saved_msg_list;
683#endif
684 return FAIL;
685 }
686 ++call_depth;
687
688#ifdef FEAT_EVAL
689 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000690 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 cstack.cs_trylevel = 0;
692 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000693 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
695
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100696 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100698 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100699 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000700 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 ++ex_nesting_level;
702
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100703 // Get the function or script name and the address where the next breakpoint
704 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000705 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 {
707 fname = func_name(real_cookie);
708 breakpoint = func_breakpoint(real_cookie);
709 dbg_tick = func_dbg_tick(real_cookie);
710 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100711 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100713 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 breakpoint = source_breakpoint(real_cookie);
715 dbg_tick = source_dbg_tick(real_cookie);
716 }
717
718 /*
719 * Initialize "force_abort" and "suppress_errthrow" at the top level.
720 */
721 if (!recursive)
722 {
723 force_abort = FALSE;
724 suppress_errthrow = FALSE;
725 }
726
727 /*
728 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000729 * exception handling (used when debugging). Otherwise clear it to avoid
730 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000732 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000733 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000734 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200735 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736
737 initial_trylevel = trylevel;
738
739 /*
740 * "did_throw" will be set to TRUE when an exception is being thrown.
741 */
742 did_throw = FALSE;
743#endif
744 /*
745 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000746 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 * If force_abort is set, we cancel everything.
748 */
749 did_emsg = FALSE;
750
751 /*
752 * KeyTyped is only set when calling vgetc(). Reset it here when not
753 * calling vgetc() (sourced command lines).
754 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100755 if (!(flags & DOCMD_KEYTYPED)
756 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 KeyTyped = FALSE;
758
759 /*
760 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000761 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 * - for multiple commands on one line, separated with '|'
763 * - when repeating until there are no more lines (for ":source")
764 */
765 next_cmdline = cmdline;
766 do
767 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000768#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100769 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000770#endif
771
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100772 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 if (next_cmdline == NULL
774#ifdef FEAT_EVAL
775 && !force_abort
776 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000777 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778#endif
779 )
780 did_emsg = FALSE;
781
782 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000783 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100784 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 * 3. If a line is given: Make a copy, so we can mess with it.
786 */
787
788#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100789 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000790 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100792 // Each '|' separated command is stored separately in lines_ga, to
793 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100794 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100796 // Check if a function has returned or, unless it has an unclosed
797 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000798 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000800# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000801 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000802 func_line_end(real_cookie);
803# endif
804 if (func_has_ended(real_cookie))
805 {
806 retval = FAIL;
807 break;
808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000811 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100812 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000813 script_line_end();
814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100816 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100817 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 {
819 retval = FAIL;
820 break;
821 }
822
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100823 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 if (breakpoint != NULL && dbg_tick != NULL
825 && *dbg_tick != debug_tick)
826 {
827 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100828 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100829 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 *dbg_tick = debug_tick;
831 }
832
833 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100834 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100836 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100838 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100840 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100841 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100843 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100844 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 *dbg_tick = debug_tick;
846 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000847# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000848 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000849 {
850 if (getline_is_func)
851 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100852 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000853 script_line_start();
854 }
855# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857#endif
858
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100859 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 if (next_cmdline == NULL)
861 {
862 /*
863 * Need to set msg_didout for the first line after an ":if",
864 * otherwise the ":if" will be overwritten.
865 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100866 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100868 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869#ifdef FEAT_EVAL
870 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
871#else
872 0
873#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200874 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100876 // Don't call wait_return for aborted command line. The NULL
877 // returned for the end of a sourced file or executed function
878 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (KeyTyped && !(flags & DOCMD_REPEAT))
880 need_wait_return = FALSE;
881 retval = FAIL;
882 break;
883 }
884 used_getline = TRUE;
885
886 /*
887 * Keep the first typed line. Clear it when more lines are typed.
888 */
889 if (flags & DOCMD_KEEPLINE)
890 {
891 vim_free(repeat_cmdline);
892 if (count == 0)
893 repeat_cmdline = vim_strsave(next_cmdline);
894 else
895 repeat_cmdline = NULL;
896 }
897 }
898
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100899 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 else if (cmdline_copy == NULL)
901 {
902 next_cmdline = vim_strsave(next_cmdline);
903 if (next_cmdline == NULL)
904 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100905 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 retval = FAIL;
907 break;
908 }
909 }
910 cmdline_copy = next_cmdline;
911
912#ifdef FEAT_EVAL
913 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200914 * Inside a while/for loop, and when the command looks like a ":while"
915 * or ":for", the line is stored, because we may need it later when
916 * looping.
917 *
918 * When there is a '|' and another command, it is stored separately,
919 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000920 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200921 *
922 * Pass a different "fgetline" function to do_one_cmd() below,
923 * that it stores lines in or reads them from "lines_ga". Makes it
924 * possible to define a function inside a while/for loop and handles
925 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200927 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200929 cmd_getline = get_loop_line;
930 cmd_cookie = (void *)&cmd_loop_cookie;
931 cmd_loop_cookie.lines_gap = &lines_ga;
932 cmd_loop_cookie.current_line = current_line;
933 cmd_loop_cookie.getline = fgetline;
934 cmd_loop_cookie.cookie = cookie;
935 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
936
937 // Save the current line when encountering it the first time.
938 if (current_line == lines_ga.ga_len
939 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 {
941 retval = FAIL;
942 break;
943 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200944 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200946 else
947 {
948 cmd_getline = fgetline;
949 cmd_cookie = cookie;
950 }
951
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 did_endif = FALSE;
953#endif
954
955 if (count++ == 0)
956 {
957 /*
958 * All output from the commands is put below each other, without
959 * waiting for a return. Don't do this when executing commands
960 * from a script or when being called recursive (e.g. for ":e
961 * +command file").
962 */
963 if (!(flags & DOCMD_NOWAIT) && !recursive)
964 {
965 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100966 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100968 msg_scroll = TRUE; // put messages below each other
969 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 ++RedrawingDisabled;
971 did_inc = TRUE;
972 }
973 }
974
Bram Moolenaar823654b2020-05-29 23:03:09 +0200975 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100976 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
978 /*
979 * 2. Execute one '|' separated command.
980 * do_one_cmd() will return NULL if there is no trailing '|'.
981 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
982 */
983 ++recursive;
984 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
985#ifdef FEAT_EVAL
986 &cstack,
987#endif
988 cmd_getline, cmd_cookie);
989 --recursive;
990
991#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000992 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100993 // Use "current_line" from "cmd_loop_cookie", it may have been
994 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000995 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996#endif
997
998 if (next_cmdline == NULL)
999 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001000 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 /*
1003 * If the command was typed, remember it for the ':' register.
1004 * Do this AFTER executing the command to make :@: work.
1005 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001006 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 && new_last_cmdline != NULL)
1008 {
1009 vim_free(last_cmdline);
1010 last_cmdline = new_last_cmdline;
1011 new_last_cmdline = NULL;
1012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 }
1014 else
1015 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001016 // need to copy the command after the '|' to cmdline_copy, for the
1017 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001018 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 next_cmdline = cmdline_copy;
1020 }
1021
1022
1023#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001024 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001026 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 && !func_has_abort(real_cookie))
1028 did_emsg = FALSE;
1029
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001030 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 {
1032 ++current_line;
1033
1034 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001035 * An ":endwhile", ":endfor" and ":continue" is handled here.
1036 * If we were executing commands, jump back to the ":while" or
1037 * ":for".
1038 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001040 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001042 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001044 // Jump back to the matching ":while" or ":for". Be careful
1045 // not to use a cs_line[] from an entry that isn't a ":while"
1046 // or ":for": It would make "current_line" invalid and can
1047 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 if (!did_emsg && !got_int && !did_throw
1049 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001050 && (cstack.cs_flags[cstack.cs_idx]
1051 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 && cstack.cs_line[cstack.cs_idx] >= 0
1053 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1054 {
1055 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001056 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001057 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001058 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001060 // Check for the next breakpoint at or after the ":while"
1061 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 if (breakpoint != NULL)
1063 {
1064 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001065 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 fname,
1067 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1068 *dbg_tick = debug_tick;
1069 }
1070 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001071 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001073 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001075 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1076 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 }
1078 }
1079
1080 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001081 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001083 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001085 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001086 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 }
1088 }
1089
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001090 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001091 if (breakpoint != NULL && has_watchexpr())
1092 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001093 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001094 *dbg_tick = debug_tick;
1095 }
1096
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 /*
1098 * When not inside any ":while" loop, clear remembered lines.
1099 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001100 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 {
1102 if (lines_ga.ga_len > 0)
1103 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001104 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1106 free_cmdlines(&lines_ga);
1107 }
1108 current_line = 0;
1109 }
1110
1111 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001112 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1113 * being restored at the ":endtry". Reset them here and set the
1114 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1115 * This includes the case where a missing ":endif", ":endwhile" or
1116 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001118 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001120 cstack.cs_lflags &= ~CSL_HAD_FINA;
1121 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1122 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 did_throw ? (void *)current_exception : NULL);
1124 did_emsg = got_int = did_throw = FALSE;
1125 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1126 }
1127
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001128 // Update global "trylevel" for recursive calls to do_cmdline() from
1129 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 trylevel = initial_trylevel + cstack.cs_trylevel;
1131
1132 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001133 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 * files) is aborted because of an error, an interrupt, or an uncaught
1135 * exception, cancel everything. If it is left normally, reset
1136 * force_abort to get the non-EH compatible abortion behavior for
1137 * the rest of the script.
1138 */
1139 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1140 force_abort = FALSE;
1141
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001142 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001144#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145
1146 }
1147 /*
1148 * Continue executing command lines when:
1149 * - no CTRL-C typed, no aborting error, no exception thrown or try
1150 * conditionals need to be checked for executing finally clauses or
1151 * catching an interrupt exception
1152 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001153 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 * looping for ":source" command or function call.
1155 */
1156 while (!((got_int
1157#ifdef FEAT_EVAL
1158 || (did_emsg && force_abort) || did_throw
1159#endif
1160 )
1161#ifdef FEAT_EVAL
1162 && cstack.cs_trylevel == 0
1163#endif
1164 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001165 && !(did_emsg
1166#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001167 // Keep going when inside try/catch, so that the error can be
1168 // deal with, except when it is a syntax error, it may cause
1169 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001170 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1171#endif
1172 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001173 && (getline_equal(fgetline, cookie, getexmodeline)
1174 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 && (next_cmdline != NULL
1176#ifdef FEAT_EVAL
1177 || cstack.cs_idx >= 0
1178#endif
1179 || (flags & DOCMD_REPEAT)));
1180
1181 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001182 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183#ifdef FEAT_EVAL
1184 free_cmdlines(&lines_ga);
1185 ga_clear(&lines_ga);
1186
1187 if (cstack.cs_idx >= 0)
1188 {
1189 /*
1190 * If a sourced file or executed function ran to its end, report the
1191 * unclosed conditional.
1192 */
1193 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001194 && ((getline_equal(fgetline, cookie, getsourceline)
1195 && !source_finished(fgetline, cookie))
1196 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 && !func_has_ended(real_cookie))))
1198 {
1199 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001200 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001202 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001203 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001204 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001206 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 }
1208
1209 /*
1210 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1211 * ":endtry" in a sourced file or executed function. If the try
1212 * conditional is in its finally clause, ignore anything pending.
1213 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001214 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 */
1216 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001217 {
1218 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1219
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001220 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001221 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001222 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1223 &cstack.cs_looplevel);
1224 }
1225 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 trylevel = initial_trylevel;
1227 }
1228
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001229 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1230 // lack was reported above and the error message is to be converted to an
1231 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001232 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 ? (char_u *)"endfunction" : (char_u *)NULL);
1234
1235 if (trylevel == 0)
1236 {
1237 /*
1238 * When an exception is being thrown out of the outermost try
1239 * conditional, discard the uncaught exception, disable the conversion
1240 * of interrupts or errors to exceptions, and ensure that no more
1241 * commands are executed.
1242 */
1243 if (did_throw)
1244 {
1245 void *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001246 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247
1248 /*
1249 * If the uncaught exception is a user exception, report it as an
1250 * error. If it is an error exception, display the saved error
1251 * message now. For an interrupt exception, do nothing; the
1252 * interrupt message is given elsewhere.
1253 */
1254 switch (current_exception->type)
1255 {
1256 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001257 vim_snprintf((char *)IObuff, IOSIZE,
1258 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 current_exception->value);
1260 p = vim_strsave(IObuff);
1261 break;
1262 case ET_ERROR:
1263 messages = current_exception->messages;
1264 current_exception->messages = NULL;
1265 break;
1266 case ET_INTERRUPT:
1267 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 }
1269
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001270 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1271 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001272 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 current_exception->throw_name = NULL;
1274
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001275 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 suppress_errthrow = TRUE;
1277 force_abort = TRUE;
1278
1279 if (messages != NULL)
1280 {
1281 do
1282 {
1283 next = messages->next;
1284 emsg(messages->msg);
1285 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001286 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 vim_free(messages);
1288 messages = next;
1289 }
1290 while (messages != NULL);
1291 }
1292 else if (p != NULL)
1293 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001294 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 vim_free(p);
1296 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001297 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001298 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001299 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 }
1301
1302 /*
1303 * On an interrupt or an aborting error not converted to an exception,
1304 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001305 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 * when force_abort is set and did_emsg unset in case of an interrupt
1307 * from a finally clause after an error.
1308 */
1309 else if (got_int || (did_emsg && force_abort))
1310 suppress_errthrow = TRUE;
1311 }
1312
1313 /*
1314 * The current cstack will be freed when do_cmdline() returns. An uncaught
1315 * exception will have to be rethrown in the previous cstack. If a function
1316 * has just returned or a script file was just finished and the previous
1317 * cstack belongs to the same function or, respectively, script file, it
1318 * will have to be checked for finally clauses to be executed due to the
1319 * ":return" or ":finish". This is done in do_one_cmd().
1320 */
1321 if (did_throw)
1322 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001323 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001325 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 && ex_nesting_level > func_level(real_cookie) + 1))
1327 {
1328 if (!did_throw)
1329 check_cstack = TRUE;
1330 }
1331 else
1332 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001333 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001334 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 --ex_nesting_level;
1336 /*
1337 * Go to debug mode when returning from a function in which we are
1338 * single-stepping.
1339 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001340 if ((getline_equal(fgetline, cookie, getsourceline)
1341 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001343 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 ? (char_u *)_("End of sourced file")
1345 : (char_u *)_("End of function"));
1346 }
1347
1348 /*
1349 * Restore the exception environment (done after returning from the
1350 * debugger).
1351 */
1352 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001353 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354
1355 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001356
1357 // Cleanup if "cs_emsg_silent_list" remains.
1358 if (cstack.cs_emsg_silent_list != NULL)
1359 {
1360 eslist_T *elem, *temp;
1361
1362 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1363 {
1364 temp = elem->next;
1365 vim_free(elem);
1366 }
1367 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001368#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369
1370 /*
1371 * If there was too much output to fit on the command line, ask the user to
1372 * hit return before redrawing the screen. With the ":global" command we do
1373 * this only once after the command is finished.
1374 */
1375 if (did_inc)
1376 {
1377 --RedrawingDisabled;
1378 --no_wait_return;
1379 msg_scroll = FALSE;
1380
1381 /*
1382 * When just finished an ":if"-":else" which was typed, no need to
1383 * wait for hit-return. Also for an error situation.
1384 */
1385 if (retval == FAIL
1386#ifdef FEAT_EVAL
1387 || (did_endif && KeyTyped && !did_emsg)
1388#endif
1389 )
1390 {
1391 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001392 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 }
1394 else if (need_wait_return)
1395 {
1396 /*
1397 * The msg_start() above clears msg_didout. The wait_return we do
1398 * here should not overwrite the command that may be shown before
1399 * doing that.
1400 */
1401 msg_didout |= msg_didout_before_start;
1402 wait_return(FALSE);
1403 }
1404 }
1405
Bram Moolenaar2a942252012-11-28 23:03:07 +01001406#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001407 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001408#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 /*
1410 * Reset if_level, in case a sourced script file contains more ":if" than
1411 * ":endif" (could be ":if x | foo | endif").
1412 */
1413 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001414#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001415
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 --call_depth;
1417 return retval;
1418}
1419
1420#ifdef FEAT_EVAL
1421/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001422 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 */
1424 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001425get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001427 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 wcmd_T *wp;
1429 char_u *line;
1430
1431 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1432 {
1433 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001434 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001436 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001438 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001440 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001441 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 ++cp->current_line;
1443
1444 return line;
1445 }
1446
1447 KeyTyped = FALSE;
1448 ++cp->current_line;
1449 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001450 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 return vim_strsave(wp->line);
1452}
1453
1454/*
1455 * Store a line in "gap" so that a ":while" loop can execute it again.
1456 */
1457 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001458store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459{
1460 if (ga_grow(gap, 1) == FAIL)
1461 return FAIL;
1462 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001463 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 return OK;
1466}
1467
1468/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001469 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 */
1471 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001472free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473{
1474 while (gap->ga_len > 0)
1475 {
1476 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1477 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 }
1479}
1480#endif
1481
1482/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001483 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1484 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001487getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001488 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001489 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001490 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491{
1492#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001493 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001494 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001496 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1497 // function that's originally used to obtain the lines. This may be
1498 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001499 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001500 cp = (struct loop_cookie *)cookie;
1501 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 {
1503 gp = cp->getline;
1504 cp = cp->cookie;
1505 }
1506 return gp == func;
1507#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001508 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509#endif
1510}
1511
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001513 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 * getline function. Otherwise return "cookie".
1515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001517getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001518 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001519 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520{
Bram Moolenaar13505972019-01-24 15:04:48 +01001521#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001522 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001523 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001525 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1526 // cookie that's originally used to obtain the lines. This may be nested
1527 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001528 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001529 cp = (struct loop_cookie *)cookie;
1530 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 {
1532 gp = cp->getline;
1533 cp = cp->cookie;
1534 }
1535 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001536#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001539}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540
Bram Moolenaard5053d02020-06-28 15:51:16 +02001541#if defined(FEAT_EVAL) || defined(PROT)
1542/*
1543 * Get the next line source line without advancing.
1544 */
1545 char_u *
1546getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001547 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001548 void *cookie) // argument for fgetline()
1549{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001550 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001551 struct loop_cookie *cp;
1552 wcmd_T *wp;
1553
1554 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1555 // cookie that's originally used to obtain the lines. This may be nested
1556 // several levels.
1557 gp = fgetline;
1558 cp = (struct loop_cookie *)cookie;
1559 while (gp == get_loop_line)
1560 {
1561 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1562 {
1563 // executing lines a second time, use the stored copy
1564 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1565 return wp->line;
1566 }
1567 gp = cp->getline;
1568 cp = cp->cookie;
1569 }
1570 if (gp == getsourceline)
1571 return source_nextline(cp);
1572 return NULL;
1573}
1574#endif
1575
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001576
1577/*
1578 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1579 * ":bwipeout", etc.
1580 * Returns the buffer number.
1581 */
1582 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001583compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001584{
1585 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001586 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001587 int count = offset;
1588
1589 buf = firstbuf;
1590 while (buf->b_next != NULL && buf->b_fnum < lnum)
1591 buf = buf->b_next;
1592 while (count != 0)
1593 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001594 count += (offset < 0) ? 1 : -1;
1595 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1596 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001597 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001598 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001599 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001600 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001601 while (buf->b_ml.ml_mfp == NULL)
1602 {
1603 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1604 if (nextbuf == NULL)
1605 break;
1606 buf = nextbuf;
1607 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001608 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001609 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001610 if (addr_type == ADDR_LOADED_BUFFERS)
1611 while (buf->b_ml.ml_mfp == NULL)
1612 {
1613 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1614 if (nextbuf == NULL)
1615 break;
1616 buf = nextbuf;
1617 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001618 return buf->b_fnum;
1619}
1620
Bram Moolenaarb7316892019-05-01 18:08:42 +02001621/*
1622 * Return the window number of "win".
1623 * When "win" is NULL return the number of windows.
1624 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001625 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001626current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001627{
1628 win_T *wp;
1629 int nr = 0;
1630
Bram Moolenaar29323592016-07-24 22:04:11 +02001631 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001632 {
1633 ++nr;
1634 if (wp == win)
1635 break;
1636 }
1637 return nr;
1638}
1639
1640 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001641current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001642{
1643 tabpage_T *tp;
1644 int nr = 0;
1645
Bram Moolenaar29323592016-07-24 22:04:11 +02001646 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001647 {
1648 ++nr;
1649 if (tp == tab)
1650 break;
1651 }
1652 return nr;
1653}
1654
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001655 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001656comment_start(char_u *p, int starts_with_colon UNUSED)
1657{
1658#ifdef FEAT_EVAL
1659 if (in_vim9script())
1660 return p[0] == '#' && p[1] != '{' && !starts_with_colon;
1661#endif
1662 return *p == '"';
1663}
1664
Bram Moolenaarf240e182014-11-27 18:33:02 +01001665# define CURRENT_WIN_NR current_win_nr(curwin)
1666# define LAST_WIN_NR current_win_nr(NULL)
1667# define CURRENT_TAB_NR current_tab_nr(curtab)
1668# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001669
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670/*
1671 * Execute one Ex command.
1672 *
1673 * If 'sourcing' is TRUE, the command will be included in the error message.
1674 *
1675 * 1. skip comment lines and leading space
1676 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001677 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001678 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001679 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001680 * 6. parse arguments
1681 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001683 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 *
1685 * This function may be called recursively!
1686 */
1687#if (_MSC_VER == 1200)
1688/*
Bram Moolenaared203462004-06-16 11:19:22 +00001689 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001691 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692#endif
1693 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001694do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001695 char_u **cmdlinep,
1696 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001698 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001700 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001701 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001703 char_u *p;
1704 linenr_T lnum;
1705 long n;
1706 char *errormsg = NULL; // error message
1707 char_u *after_modifier = NULL;
1708 exarg_T ea; // Ex command arguments
1709 int save_msg_scroll = msg_scroll;
1710 cmdmod_T save_cmdmod;
1711 int save_reg_executing = reg_executing;
1712 int ni; // set when Not Implemented
1713 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001714 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001715#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001716 int may_have_range;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001717 int vim9script = in_vim9script();
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719
Bram Moolenaara80faa82020-04-12 19:37:17 +02001720 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 ea.line1 = 1;
1722 ea.line2 = 1;
1723#ifdef FEAT_EVAL
1724 ++ex_nesting_level;
1725#endif
1726
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001727 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 if (quitmore
1729#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001730 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001731 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001732#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001733 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001734 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 --quitmore;
1736
1737 /*
1738 * Reset browse, confirm, etc.. They are restored when returning, for
1739 * recursive calls.
1740 */
1741 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001743 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001744 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1745 goto doend;
1746
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001747/*
1748 * 1. Skip comment lines and leading white space and colons.
1749 * 2. Handle command modifiers.
1750 */
1751 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001753 ea.cmdlinep = cmdlinep;
1754 ea.getline = fgetline;
1755 ea.cookie = cookie;
1756#ifdef FEAT_EVAL
1757 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001758 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001760 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001761 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001763 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764
1765#ifdef FEAT_EVAL
1766 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1767 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1768#else
1769 ea.skip = (if_level > 0);
1770#endif
1771
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001773 * 3. Skip over the range to find the command. Let "p" point to after it.
1774 *
1775 * We need the command to know what kind of range it uses.
1776 */
1777 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001778#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001779 // In Vim9 script a colon is required before the range.
1780 may_have_range = !vim9script || starts_with_colon;
1781 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001782#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001783 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784
1785#ifdef FEAT_EVAL
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001786 if (vim9script && !starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001787 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001788 if (ea.cmd == cmd + 1 && *cmd == '$')
1789 // should be "$VAR = val"
1790 --ea.cmd;
1791 else if (ea.cmd > cmd)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001792 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001793 emsg(_(e_colon_required_before_a_range));
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001794 goto doend;
1795 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001797 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001798 else
1799#endif
1800 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001801
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001802#ifdef FEAT_EVAL
1803# ifdef FEAT_PROFILE
1804 // Count this line for profiling if skip is TRUE.
1805 if (do_profiling == PROF_YES
1806 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1807 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1808 {
1809 int skip = did_emsg || got_int || did_throw;
1810
1811 if (ea.cmdidx == CMD_catch)
1812 skip = !skip && !(cstack->cs_idx >= 0
1813 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1814 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1815 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1816 skip = skip || !(cstack->cs_idx >= 0
1817 && !(cstack->cs_flags[cstack->cs_idx]
1818 & (CSF_ACTIVE | CSF_TRUE)));
1819 else if (ea.cmdidx == CMD_finally)
1820 skip = FALSE;
1821 else if (ea.cmdidx != CMD_endif
1822 && ea.cmdidx != CMD_endfor
1823 && ea.cmdidx != CMD_endtry
1824 && ea.cmdidx != CMD_endwhile)
1825 skip = ea.skip;
1826
1827 if (!skip)
1828 {
1829 if (getline_equal(fgetline, cookie, get_func_line))
1830 func_line_exec(getline_cookie(fgetline, cookie));
1831 else if (getline_equal(fgetline, cookie, getsourceline))
1832 script_line_exec();
1833 }
1834 }
1835# endif
1836
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001837 // May go to debug mode. If this happens and the ">quit" debug command is
1838 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001839 dbg_check_breakpoint(&ea);
1840 if (!ea.skip && got_int)
1841 {
1842 ea.skip = TRUE;
1843 (void)do_intthrow(cstack);
1844 }
1845#endif
1846
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001847/*
1848 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 *
1850 * where 'addr' is:
1851 *
1852 * % (entire file)
1853 * $ [+-NUM]
1854 * 'x [+-NUM] (where x denotes a currently defined mark)
1855 * . [+-NUM]
1856 * [+-NUM]..
1857 * NUM
1858 *
1859 * The ea.cmd pointer is updated to point to the first character following the
1860 * range spec. If an initial address is found, but no second, the upper bound
1861 * is equal to the lower.
1862 */
1863
Bram Moolenaar25190db2019-05-04 15:05:28 +02001864 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001865 if (!IS_USER_CMDIDX(ea.cmdidx))
1866 {
1867 if (ea.cmdidx != CMD_SIZE)
1868 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1869 else
1870 ea.addr_type = ADDR_LINES;
1871
Bram Moolenaar25190db2019-05-04 15:05:28 +02001872 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001873 if (ea.cmdidx == CMD_wincmd && p != NULL)
1874 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001875#ifdef FEAT_QUICKFIX
1876 // :.cc in quickfix window uses line number
1877 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1878 ea.addr_type = ADDR_OTHER;
1879#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001880 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001881
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001882 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001883#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001884 if (!may_have_range)
1885 ea.line1 = ea.line2 = default_address(&ea);
1886 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001887#endif
1888 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1889 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001892 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 */
1894
1895 /*
1896 * Skip ':' and any white space
1897 */
1898 ea.cmd = skipwhite(ea.cmd);
1899 while (*ea.cmd == ':')
1900 ea.cmd = skipwhite(ea.cmd + 1);
1901
1902 /*
1903 * If we got a line, but no command, then go to the line.
1904 * If we find a '|' or '\n' we set ea.nextcmd.
1905 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001906 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1907 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 {
1909 /*
1910 * strange vi behaviour:
1911 * ":3" jumps to line 3
1912 * ":3|..." prints line 3
1913 * ":|" prints current line
1914 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001915 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001917 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 {
1919 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001920 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 if ((errormsg = invalid_range(&ea)) == NULL)
1922 {
1923 correct_range(&ea);
1924 ex_print(&ea);
1925 }
1926 }
1927 else if (ea.addr_count != 0)
1928 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001929 if (ea.line2 > curbuf->b_ml.ml_line_count)
1930 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001931 // With '-' in 'cpoptions' a line number past the file is an
1932 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001933 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1934 ea.line2 = -1;
1935 else
1936 ea.line2 = curbuf->b_ml.ml_line_count;
1937 }
1938
1939 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001940 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 else
1942 {
1943 if (ea.line2 == 0)
1944 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 else
1946 curwin->w_cursor.lnum = ea.line2;
1947 beginline(BL_SOL | BL_FIX);
1948 }
1949 }
1950 goto doend;
1951 }
1952
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001953 // If this looks like an undefined user command and there are CmdUndefined
1954 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001955 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1956 && ASCII_ISUPPER(*ea.cmd)
1957 && has_cmdundefined())
1958 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001959 int ret;
1960
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001961 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001962 while (ASCII_ISALNUM(*p))
1963 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001964 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001965 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1966 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001967 // If the autocommands did something and didn't cause an error, try
1968 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001969 p = (ret
1970#ifdef FEAT_EVAL
1971 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001972#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001973 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001974 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001975
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 if (p == NULL)
1977 {
1978 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001979 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 goto doend;
1981 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001982 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001983 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1984 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 {
1986 errormsg = uc_fun_cmd();
1987 goto doend;
1988 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001989
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 if (ea.cmdidx == CMD_SIZE)
1991 {
1992 if (!ea.skip)
1993 {
1994 STRCPY(IObuff, _("E492: Not an editor command"));
1995 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001996 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001997 // If the modifier was parsed OK the error must be in the
1998 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001999 if (after_modifier != NULL)
2000 append_command(after_modifier);
2001 else
2002 append_command(*cmdlinep);
2003 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002004 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002005 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 }
2007 goto doend;
2008 }
2009
Bram Moolenaar958636c2014-10-21 20:01:58 +02002010 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2011 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002012#ifdef HAVE_EX_SCRIPT_NI
2013 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2014#endif
2015 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016
2017#ifndef FEAT_EVAL
2018 /*
2019 * When the expression evaluation is disabled, recognize the ":if" and
2020 * ":endif" commands and ignore everything in between it.
2021 */
2022 if (ea.cmdidx == CMD_if)
2023 ++if_level;
2024 if (if_level)
2025 {
2026 if (ea.cmdidx == CMD_endif)
2027 --if_level;
2028 goto doend;
2029 }
2030
2031#endif
2032
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002033 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002034 if (*p == '!' && ea.cmdidx != CMD_substitute
2035 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 {
2037 ++p;
2038 ea.forceit = TRUE;
2039 }
2040 else
2041 ea.forceit = FALSE;
2042
2043/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002044 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002046 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002047 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048
2049 if (!ea.skip)
2050 {
2051#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002052 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002054 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002055 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 goto doend;
2057 }
2058#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002059 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002060 {
2061 errormsg = _("E981: Command not allowed in rvim");
2062 goto doend;
2063 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002064 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002066 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002067 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 goto doend;
2069 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002070
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002071 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002073#ifdef FEAT_CMDWIN
2074 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2075 {
2076 // Command not allowed in the command line window
2077 errormsg = _(e_cmdwin);
2078 goto doend;
2079 }
2080#endif
2081 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2082 {
2083 // Command not allowed when text is locked
2084 errormsg = _(get_text_locked_msg());
2085 goto doend;
2086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002088
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002089 // Disallow editing another buffer when "curbuf_lock" is set.
2090 // Do allow ":checktime" (it is postponed).
2091 // Do allow ":edit" (check for an argument later).
2092 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002093 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002094 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002095 && ea.cmdidx != CMD_edit
2096 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002097 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002098 && curbuf_locked())
2099 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002101 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002103 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002104 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 goto doend;
2106 }
2107 }
2108
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002109 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002111 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 goto doend;
2113 }
2114
2115 /*
2116 * Don't complain about the range if it is not used
2117 * (could happen if line_count is accidentally set to 0).
2118 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002119 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 {
2121 /*
2122 * If the range is backwards, ask for confirmation and, if given, swap
2123 * ea.line1 & ea.line2 so it's forwards again.
2124 * When global command is busy, don't ask, will fail below.
2125 */
2126 if (!global_busy && ea.line1 > ea.line2)
2127 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002128 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002130 if (sourcing || exmode_active)
2131 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002132 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002133 goto doend;
2134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 if (ask_yesno((char_u *)
2136 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002137 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 }
2139 lnum = ea.line1;
2140 ea.line1 = ea.line2;
2141 ea.line2 = lnum;
2142 }
2143 if ((errormsg = invalid_range(&ea)) != NULL)
2144 goto doend;
2145 }
2146
Bram Moolenaarb7316892019-05-01 18:08:42 +02002147 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2148 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 ea.line2 = 1;
2150
2151 correct_range(&ea);
2152
2153#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002154 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002155 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002157 // Put the first line at the start of a closed fold, put the last line
2158 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 (void)hasFolding(ea.line1, &ea.line1, NULL);
2160 (void)hasFolding(ea.line2, NULL, &ea.line2);
2161 }
2162#endif
2163
2164#ifdef FEAT_QUICKFIX
2165 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002166 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 * option here, so things like % get expanded.
2168 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002169 p = replace_makeprg(&ea, p, cmdlinep);
2170 if (p == NULL)
2171 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172#endif
2173
2174 /*
2175 * Skip to start of argument.
2176 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2177 */
2178 if (ea.cmdidx == CMD_bang)
2179 ea.arg = p;
2180 else
2181 ea.arg = skipwhite(p);
2182
Bram Moolenaar379fb762018-08-30 15:58:28 +02002183 // ":file" cannot be run with an argument when "curbuf_lock" is set
2184 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2185 goto doend;
2186
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 /*
2188 * Check for "++opt=val" argument.
2189 * Must be first, allow ":w ++enc=utf8 !cmd"
2190 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002191 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2193 if (getargopt(&ea) == FAIL && !ni)
2194 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002195 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 goto doend;
2197 }
2198
2199 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2200 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002201 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002203 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002205 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 goto doend;
2207 }
2208 ea.arg = skipwhite(ea.arg + 1);
2209 ea.append = TRUE;
2210 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002211 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 {
2213 ++ea.arg;
2214 ea.usefilter = TRUE;
2215 }
2216 }
2217
2218 if (ea.cmdidx == CMD_read)
2219 {
2220 if (ea.forceit)
2221 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002222 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 ea.forceit = FALSE;
2224 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002225 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 {
2227 ++ea.arg;
2228 ea.usefilter = TRUE;
2229 }
2230 }
2231
2232 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2233 {
2234 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002235 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 {
2237 ++ea.arg;
2238 ++ea.amount;
2239 }
2240 ea.arg = skipwhite(ea.arg);
2241 }
2242
2243 /*
2244 * Check for "+command" argument, before checking for next command.
2245 * Don't do this for ":read !cmd" and ":write !cmd".
2246 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002247 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2249
2250 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002251 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 * Don't do this for ":read !cmd" and ":write !cmd".
2253 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002254 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 separate_nextcmd(&ea);
2256
2257 /*
2258 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002259 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2260 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002262 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002263 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002264 || ea.cmdidx == CMD_global
2265 || ea.cmdidx == CMD_vglobal
2266 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 {
2268 for (p = ea.arg; *p; ++p)
2269 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002270 // Remove one backslash before a newline, so that it's possible to
2271 // pass a newline to the shell and also a newline that is preceded
2272 // with a backslash. This makes it impossible to end a shell
2273 // command in a backslash, but that doesn't appear useful.
2274 // Halving the number of backslashes is incompatible with previous
2275 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002277 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 else if (*p == '\n')
2279 {
2280 ea.nextcmd = p + 1;
2281 *p = NUL;
2282 break;
2283 }
2284 }
2285 }
2286
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002287 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002288 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002290 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002291 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002293 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002294 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002295 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002297#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002298 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002299 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002301 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002302 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 }
2304#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002305 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2306 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002307 {
2308 ea.regname = *ea.arg++;
2309#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002310 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002311 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2312 {
2313 set_expr_line(vim_strsave(ea.arg));
2314 ea.arg += STRLEN(ea.arg);
2315 }
2316#endif
2317 ea.arg = skipwhite(ea.arg);
2318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 }
2320
2321 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002322 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 * count, it's a buffer name.
2324 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002325 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2326 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002327 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 {
2329 n = getdigits(&ea.arg);
2330 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002331 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002333 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 goto doend;
2335 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002336 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 {
2338 ea.line2 = n;
2339 if (ea.addr_count == 0)
2340 ea.addr_count = 1;
2341 }
2342 else
2343 {
2344 ea.line1 = ea.line2;
2345 ea.line2 += n - 1;
2346 ++ea.addr_count;
2347 /*
2348 * Be vi compatible: no error message for out of range.
2349 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002350 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 ea.line2 = curbuf->b_ml.ml_line_count;
2352 }
2353 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002354
2355 /*
2356 * Check for flags: 'l', 'p' and '#'.
2357 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002358 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002359 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002360 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2361 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002363 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002364 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 goto doend;
2366 }
2367
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002368 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002370 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 goto doend;
2372 }
2373
2374#ifdef FEAT_EVAL
2375 /*
2376 * Skip the command when it's not going to be executed.
2377 * The commands like :if, :endif, etc. always need to be executed.
2378 * Also make an exception for commands that handle a trailing command
2379 * themselves.
2380 */
2381 if (ea.skip)
2382 {
2383 switch (ea.cmdidx)
2384 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002385 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 case CMD_while:
2387 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002388 case CMD_for:
2389 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 case CMD_if:
2391 case CMD_elseif:
2392 case CMD_else:
2393 case CMD_endif:
2394 case CMD_try:
2395 case CMD_catch:
2396 case CMD_finally:
2397 case CMD_endtry:
2398 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002399 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 break;
2401
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002402 // Commands that handle '|' themselves. Check: A command should
2403 // either have the EX_TRLBAR flag, appear in this list or appear in
2404 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 case CMD_aboveleft:
2406 case CMD_and:
2407 case CMD_belowright:
2408 case CMD_botright:
2409 case CMD_browse:
2410 case CMD_call:
2411 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002412 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 case CMD_delfunction:
2414 case CMD_djump:
2415 case CMD_dlist:
2416 case CMD_dsearch:
2417 case CMD_dsplit:
2418 case CMD_echo:
2419 case CMD_echoerr:
2420 case CMD_echomsg:
2421 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002422 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002424 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002425 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 case CMD_help:
2427 case CMD_hide:
2428 case CMD_ijump:
2429 case CMD_ilist:
2430 case CMD_isearch:
2431 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002432 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 case CMD_keepjumps:
2434 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002435 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 case CMD_leftabove:
2437 case CMD_let:
2438 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002439 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002440 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002442 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002443 case CMD_noautocmd:
2444 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 case CMD_perl:
2446 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002447 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002448 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002449 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 case CMD_return:
2451 case CMD_rightbelow:
2452 case CMD_ruby:
2453 case CMD_silent:
2454 case CMD_smagic:
2455 case CMD_snomagic:
2456 case CMD_substitute:
2457 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002458 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 case CMD_tcl:
2460 case CMD_throw:
2461 case CMD_tilde:
2462 case CMD_topleft:
2463 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002464 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002465 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 case CMD_verbose:
2467 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002468 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 break;
2470
2471 default: goto doend;
2472 }
2473 }
2474#endif
2475
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002476 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 {
2478 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2479 goto doend;
2480 }
2481
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 /*
2483 * Accept buffer name. Cannot be used at the same time with a buffer
2484 * number. Don't do this for a user command.
2485 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002486 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002487 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 {
2489 /*
2490 * :bdelete, :bwipeout and :bunload take several arguments, separated
2491 * by spaces: find next space (skipping over escaped characters).
2492 * The others take one argument: ignore trailing spaces.
2493 */
2494 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2495 || ea.cmdidx == CMD_bunload)
2496 p = skiptowhite_esc(ea.arg);
2497 else
2498 {
2499 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002500 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 --p;
2502 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002503 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002504 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002505 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 goto doend;
2507 ea.addr_count = 1;
2508 ea.arg = skipwhite(p);
2509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002511 // The :try command saves the emsg_silent flag, reset it here when
2512 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002513 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002514 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002515 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002516 if (emsg_silent < 0)
2517 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002518 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002519 }
2520
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002522 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524
Bram Moolenaar958636c2014-10-21 20:01:58 +02002525 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
2527 /*
2528 * Execute a user-defined command.
2529 */
2530 do_ucmd(&ea);
2531 }
2532 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 {
2534 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002535 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 */
2537 ea.errmsg = NULL;
2538 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2539 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002540 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 }
2542
2543#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002545 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2546 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002547 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 /*
2550 * If the command just executed called do_cmdline(), any throw or ":return"
2551 * or ":finish" encountered there must also check the cstack of the still
2552 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2553 * exception, or reanimate a returned function or finished script file and
2554 * return or finish it again.
2555 */
2556 if (need_rethrow)
2557 do_throw(cstack);
2558 else if (check_cstack)
2559 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002560 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002562 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 && current_func_returned())
2564 do_return(&ea, TRUE, FALSE, NULL);
2565 }
2566 need_rethrow = check_cstack = FALSE;
2567#endif
2568
2569doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002570 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002571 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002573 curwin->w_cursor.col = 0;
2574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575
2576 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2577 {
2578 if (sourcing)
2579 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002580 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 {
2582 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002583 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002585 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 }
2587 emsg(errormsg);
2588 }
2589#ifdef FEAT_EVAL
2590 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002591 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2592 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593#endif
2594
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002595 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002597 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002599#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002600 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002601 --sandbox;
2602#endif
2603
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002604 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 ea.nextcmd = NULL;
2606
2607#ifdef FEAT_EVAL
2608 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002609 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610#endif
2611
2612 return ea.nextcmd;
2613}
2614#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002615 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616#endif
2617
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002618static char ex_error_buf[MSG_BUF_LEN];
2619
2620/*
2621 * Return an error message with argument included.
2622 * Uses a static buffer, only the last error will be kept.
2623 * "msg" will be translated, caller should use N_().
2624 */
2625 char *
2626ex_errmsg(char *msg, char_u *arg)
2627{
2628 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2629 return ex_error_buf;
2630}
2631
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002633 * Parse and skip over command modifiers:
2634 * - update eap->cmd
2635 * - store flags in "cmdmod".
2636 * - Set ex_pressedreturn for an empty command line.
2637 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002638 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002639 * - set p_verbose for ":verbose"
2640 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002641 * When "skip_only" is TRUE the global variables are not changed, except for
2642 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002643 * Return FAIL when the command is not to be executed.
2644 * May set "errormsg" to an error message.
2645 */
2646 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002647parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002648{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002649 char_u *p;
2650 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002651
Bram Moolenaara80faa82020-04-12 19:37:17 +02002652 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002653 eap->verbose_save = -1;
2654 eap->save_msg_silent = -1;
2655
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002656 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002657 for (;;)
2658 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002659 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002660 {
2661 if (*eap->cmd == ':')
2662 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002663 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002664 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002665
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002666 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002667 if (*eap->cmd == NUL && exmode_active
2668 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2669 || getline_equal(eap->getline, eap->cookie, getexline))
2670 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2671 {
2672 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002673 if (!skip_only)
2674 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002675 }
2676
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002677 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002678 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002679 return FAIL;
2680 if (*eap->cmd == NUL)
2681 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002682 if (!skip_only)
2683 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002684 return FAIL;
2685 }
2686
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002687 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002688 switch (*p)
2689 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002690 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002691 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2692 break;
2693 cmdmod.split |= WSP_ABOVE;
2694 continue;
2695
2696 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2697 {
2698 cmdmod.split |= WSP_BELOW;
2699 continue;
2700 }
2701 if (checkforcmd(&eap->cmd, "browse", 3))
2702 {
2703#ifdef FEAT_BROWSE_CMD
2704 cmdmod.browse = TRUE;
2705#endif
2706 continue;
2707 }
2708 if (!checkforcmd(&eap->cmd, "botright", 2))
2709 break;
2710 cmdmod.split |= WSP_BOT;
2711 continue;
2712
2713 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2714 break;
2715#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2716 cmdmod.confirm = TRUE;
2717#endif
2718 continue;
2719
2720 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2721 {
2722 cmdmod.keepmarks = TRUE;
2723 continue;
2724 }
2725 if (checkforcmd(&eap->cmd, "keepalt", 5))
2726 {
2727 cmdmod.keepalt = TRUE;
2728 continue;
2729 }
2730 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2731 {
2732 cmdmod.keeppatterns = TRUE;
2733 continue;
2734 }
2735 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2736 break;
2737 cmdmod.keepjumps = TRUE;
2738 continue;
2739
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002740 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002741 {
2742 char_u *reg_pat;
2743
2744 if (!checkforcmd(&p, "filter", 4)
2745 || *p == NUL || ends_excmd(*p))
2746 break;
2747 if (*p == '!')
2748 {
2749 cmdmod.filter_force = TRUE;
2750 p = skipwhite(p + 1);
2751 if (*p == NUL || ends_excmd(*p))
2752 break;
2753 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002754#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002755 // Avoid that "filter(arg)" is recognized.
2756 if (in_vim9script() && !VIM_ISWHITE(*p))
2757 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002758#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002759 if (skip_only)
2760 p = skip_vimgrep_pat(p, NULL, NULL);
2761 else
2762 // NOTE: This puts a NUL after the pattern.
2763 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002764 if (p == NULL || *p == NUL)
2765 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002766 if (!skip_only)
2767 {
2768 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002769 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002770 if (cmdmod.filter_regmatch.regprog == NULL)
2771 break;
2772 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002773 eap->cmd = p;
2774 continue;
2775 }
2776
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002777 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002778 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2779 || *p == NUL || ends_excmd(*p))
2780 break;
2781 eap->cmd = p;
2782 cmdmod.hide = TRUE;
2783 continue;
2784
2785 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2786 {
2787 cmdmod.lockmarks = TRUE;
2788 continue;
2789 }
2790
2791 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2792 break;
2793 cmdmod.split |= WSP_ABOVE;
2794 continue;
2795
2796 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2797 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002798 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002799 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002800 // Set 'eventignore' to "all". Restore the
2801 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002802 cmdmod.save_ei = vim_strsave(p_ei);
2803 set_string_option_direct((char_u *)"ei", -1,
2804 (char_u *)"all", OPT_FREE, SID_NONE);
2805 }
2806 continue;
2807 }
2808 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2809 break;
2810 cmdmod.noswapfile = TRUE;
2811 continue;
2812
2813 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2814 break;
2815 cmdmod.split |= WSP_BELOW;
2816 continue;
2817
2818 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2819 {
2820#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002821 if (!skip_only)
2822 {
2823 if (!eap->did_sandbox)
2824 ++sandbox;
2825 eap->did_sandbox = TRUE;
2826 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002827#endif
2828 continue;
2829 }
2830 if (!checkforcmd(&eap->cmd, "silent", 3))
2831 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002832 if (!skip_only)
2833 {
2834 if (eap->save_msg_silent == -1)
2835 eap->save_msg_silent = msg_silent;
2836 ++msg_silent;
2837 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002838 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2839 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002840 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002841 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002842 if (!skip_only)
2843 {
2844 ++emsg_silent;
2845 ++eap->did_esilent;
2846 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002847 }
2848 continue;
2849
2850 case 't': if (checkforcmd(&p, "tab", 3))
2851 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002852 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002853 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002854 long tabnr = get_address(eap, &eap->cmd,
2855 ADDR_TABS, eap->skip,
2856 skip_only, FALSE, 1);
2857 if (tabnr == MAXLNUM)
2858 cmdmod.tab = tabpage_index(curtab) + 1;
2859 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002860 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002861 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2862 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002863 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002864 return FAIL;
2865 }
2866 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002867 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002868 }
2869 eap->cmd = p;
2870 continue;
2871 }
2872 if (!checkforcmd(&eap->cmd, "topleft", 2))
2873 break;
2874 cmdmod.split |= WSP_TOP;
2875 continue;
2876
2877 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2878 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002879 if (!skip_only)
2880 {
2881 if (eap->save_msg_silent == -1)
2882 eap->save_msg_silent = msg_silent;
2883 msg_silent = 0;
2884 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002885 continue;
2886
2887 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2888 {
2889 cmdmod.split |= WSP_VERT;
2890 continue;
2891 }
2892 if (!checkforcmd(&p, "verbose", 4))
2893 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002894 if (!skip_only)
2895 {
2896 if (eap->verbose_save < 0)
2897 eap->verbose_save = p_verbose;
2898 if (vim_isdigit(*eap->cmd))
2899 p_verbose = atoi((char *)eap->cmd);
2900 else
2901 p_verbose = 1;
2902 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002903 eap->cmd = p;
2904 continue;
2905 }
2906 break;
2907 }
2908
2909 return OK;
2910}
2911
2912/*
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002913 * Undo and free contents of "cmdmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002914 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002915 void
2916undo_cmdmod(exarg_T *eap, int save_msg_scroll)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002917{
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002918 if (eap->verbose_save >= 0)
2919 p_verbose = eap->verbose_save;
2920
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002921 if (cmdmod.save_ei != NULL)
2922 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002923 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002924 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2925 OPT_FREE, SID_NONE);
2926 free_string_option(cmdmod.save_ei);
2927 }
2928
2929 if (cmdmod.filter_regmatch.regprog != NULL)
2930 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002931
2932 if (eap->save_msg_silent != -1)
2933 {
2934 // messages could be enabled for a serious error, need to check if the
2935 // counters don't become negative
2936 if (!did_emsg || msg_silent > eap->save_msg_silent)
2937 msg_silent = eap->save_msg_silent;
2938 emsg_silent -= eap->did_esilent;
2939 if (emsg_silent < 0)
2940 emsg_silent = 0;
2941 // Restore msg_scroll, it's set by file I/O commands, even when no
2942 // message is actually displayed.
2943 msg_scroll = save_msg_scroll;
2944
2945 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2946 // somewhere in the line. Put it back in the first column.
2947 if (redirecting())
2948 msg_col = 0;
2949 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002950}
2951
2952/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002953 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002954 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002955 * Return FAIL and set "errormsg" or return OK.
2956 */
2957 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002958parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002959{
2960 int address_count = 1;
2961 linenr_T lnum;
2962
2963 // Repeat for all ',' or ';' separated addresses.
2964 for (;;)
2965 {
2966 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002967 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002968 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002969 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002970 eap->addr_count == 0, address_count++);
2971 if (eap->cmd == NULL) // error detected
2972 return FAIL;
2973 if (lnum == MAXLNUM)
2974 {
2975 if (*eap->cmd == '%') // '%' - all lines
2976 {
2977 ++eap->cmd;
2978 switch (eap->addr_type)
2979 {
2980 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002981 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002982 eap->line1 = 1;
2983 eap->line2 = curbuf->b_ml.ml_line_count;
2984 break;
2985 case ADDR_LOADED_BUFFERS:
2986 {
2987 buf_T *buf = firstbuf;
2988
2989 while (buf->b_next != NULL
2990 && buf->b_ml.ml_mfp == NULL)
2991 buf = buf->b_next;
2992 eap->line1 = buf->b_fnum;
2993 buf = lastbuf;
2994 while (buf->b_prev != NULL
2995 && buf->b_ml.ml_mfp == NULL)
2996 buf = buf->b_prev;
2997 eap->line2 = buf->b_fnum;
2998 break;
2999 }
3000 case ADDR_BUFFERS:
3001 eap->line1 = firstbuf->b_fnum;
3002 eap->line2 = lastbuf->b_fnum;
3003 break;
3004 case ADDR_WINDOWS:
3005 case ADDR_TABS:
3006 if (IS_USER_CMDIDX(eap->cmdidx))
3007 {
3008 eap->line1 = 1;
3009 eap->line2 = eap->addr_type == ADDR_WINDOWS
3010 ? LAST_WIN_NR : LAST_TAB_NR;
3011 }
3012 else
3013 {
3014 // there is no Vim command which uses '%' and
3015 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003016 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003017 return FAIL;
3018 }
3019 break;
3020 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003021 case ADDR_UNSIGNED:
3022 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003023 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003024 return FAIL;
3025 case ADDR_ARGUMENTS:
3026 if (ARGCOUNT == 0)
3027 eap->line1 = eap->line2 = 0;
3028 else
3029 {
3030 eap->line1 = 1;
3031 eap->line2 = ARGCOUNT;
3032 }
3033 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003034 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003035#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003036 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003037 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003038 if (eap->line2 == 0)
3039 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003040#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003041 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003042 case ADDR_NONE:
3043 // Will give an error later if a range is found.
3044 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003045 }
3046 ++eap->addr_count;
3047 }
3048 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3049 {
3050 pos_T *fp;
3051
3052 // '*' - visual area
3053 if (eap->addr_type != ADDR_LINES)
3054 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003055 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003056 return FAIL;
3057 }
3058
3059 ++eap->cmd;
3060 if (!eap->skip)
3061 {
3062 fp = getmark('<', FALSE);
3063 if (check_mark(fp) == FAIL)
3064 return FAIL;
3065 eap->line1 = fp->lnum;
3066 fp = getmark('>', FALSE);
3067 if (check_mark(fp) == FAIL)
3068 return FAIL;
3069 eap->line2 = fp->lnum;
3070 ++eap->addr_count;
3071 }
3072 }
3073 }
3074 else
3075 eap->line2 = lnum;
3076 eap->addr_count++;
3077
3078 if (*eap->cmd == ';')
3079 {
3080 if (!eap->skip)
3081 {
3082 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003083 // Don't leave the cursor on an illegal line or column, but do
3084 // accept zero as address, so 0;/PATTERN/ works correctly.
3085 if (eap->line2 > 0)
3086 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003087 }
3088 }
3089 else if (*eap->cmd != ',')
3090 break;
3091 ++eap->cmd;
3092 }
3093
3094 // One address given: set start and end lines.
3095 if (eap->addr_count == 1)
3096 {
3097 eap->line1 = eap->line2;
3098 // ... but only implicit: really no address given
3099 if (lnum == MAXLNUM)
3100 eap->addr_count = 0;
3101 }
3102 return OK;
3103}
3104
3105/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003106 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 * If there is a match advance "pp" to the argument and return TRUE.
3108 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003109 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003110checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003111 char_u **pp, // start of command
3112 char *cmd, // name of command
3113 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114{
3115 int i;
3116
3117 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003118 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 break;
3120 if (i >= len && !isalpha((*pp)[i]))
3121 {
3122 *pp = skipwhite(*pp + i);
3123 return TRUE;
3124 }
3125 return FALSE;
3126}
3127
3128/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003129 * Append "cmd" to the error message in IObuff.
3130 * Takes care of limiting the length and handling 0xa0, which would be
3131 * invisible otherwise.
3132 */
3133 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003134append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003135{
3136 char_u *s = cmd;
3137 char_u *d;
3138
3139 STRCAT(IObuff, ": ");
3140 d = IObuff + STRLEN(IObuff);
3141 while (*s != NUL && d - IObuff < IOSIZE - 7)
3142 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003143 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003144 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003145 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003146 STRCPY(d, "<a0>");
3147 d += 4;
3148 }
3149 else
3150 MB_COPY_CHAR(s, d);
3151 }
3152 *d = NUL;
3153}
3154
3155/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003156 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3157 * the name. Otherwise just return "start".
3158 */
3159 char_u *
3160skip_option_env_lead(char_u *start)
3161{
3162 char_u *name = start;
3163
3164 if (*start == '&')
3165 {
3166 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3167 name += 3;
3168 else
3169 name += 1;
3170 }
3171 else if (*start == '$')
3172 name += 1;
3173 return name;
3174}
3175
3176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003178 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003179 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003180 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 *
3182 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3183 * assignment without "let". Sets eap->cmdidx to the command while returning
3184 * "eap->cmd".
3185 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 * Returns NULL for an ambiguous user command.
3187 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 char_u *
3189find_ex_command(
3190 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003191 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003192 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003193 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194{
3195 int len;
3196 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003197 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199#ifdef FEAT_EVAL
3200 /*
3201 * Recognize a Vim9 script function/method call and assignment:
3202 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3203 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003204 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003205 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003207 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208
Bram Moolenaar83144542020-08-02 20:40:43 +02003209 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003210 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003212 int oplen;
3213 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003215 if (
3216 // "(..." is an expression.
3217 // "funcname(" is always a function call.
3218 *p == '('
3219 || (p == eap->cmd
3220 ? (
3221 // "{..." is an dict expression.
3222 *eap->cmd == '{'
3223 // "'string'->func()" is an expression.
3224 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003225 // '"string"->func()' is an expression.
3226 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003227 // "g:varname" is an expression.
3228 || eap->cmd[1] == ':'
3229 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003230 // "varname->func()" is an expression.
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003231 : (*p == '-' && p[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003232 {
3233 eap->cmdidx = CMD_eval;
3234 return eap->cmd;
3235 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003236
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003237 if (p != eap->cmd && (
3238 // "varname[]" is an expression.
3239 *p == '['
3240 // "varname.key" is an expression.
3241 || (*p == '.' && ASCII_ISALPHA(p[1]))))
3242 {
3243 char_u *after = p;
3244
3245 // When followed by "=" or "+=" then it is an assignment.
3246 ++emsg_silent;
3247 if (skip_expr(&after) == OK
3248 && (*after == '='
3249 || (*after != NUL && after[1] == '=')))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003250 eap->cmdidx = CMD_var;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003251 else
3252 eap->cmdidx = CMD_eval;
3253 --emsg_silent;
3254 return eap->cmd;
3255 }
3256
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003257 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3258 // an assignment.
3259 // If there is no line break inside the "[...]" then "p" is
3260 // advanced to after the "]" by to_name_const_end(): check if a "="
3261 // follows.
3262 // If "[...]" has a line break "p" still points at the "[" and it
3263 // can't be an assignment.
3264 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003265 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003266 p = to_name_const_end(eap->cmd);
3267 if (p == eap->cmd || *skipwhite(p) != '=')
3268 {
3269 eap->cmdidx = CMD_eval;
3270 return eap->cmd;
3271 }
3272 if (p > eap->cmd && *skipwhite(p) == '=')
3273 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003274 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003275 return eap->cmd;
3276 }
3277 }
3278
3279 // Recognize an assignment if we recognize the variable name:
3280 // "g:var = expr"
3281 // "var = expr" where "var" is a local var name.
Bram Moolenaar83144542020-08-02 20:40:43 +02003282 if (*eap->cmd == '@')
3283 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003284 oplen = assignment_len(skipwhite(p), &heredoc);
3285 if (oplen > 0)
3286 {
3287 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3288 || *eap->cmd == '&'
3289 || *eap->cmd == '$'
3290 || *eap->cmd == '@'
3291 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
3292 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003293 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003294 return eap->cmd;
3295 }
3296 }
3297
3298 // Recognize using a type for a w:, b:, t: or g: variable:
3299 // "w:varname: number = 123".
3300 if (eap->cmd[1] == ':' && *p == ':')
3301 {
3302 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003303 return eap->cmd;
3304 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003305 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 }
3307#endif
3308
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 /*
3310 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003311 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 * - the 'k' command can directly be followed by any character.
3313 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003314 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003316 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 */
3318 p = eap->cmd;
3319 if (*p == 'k')
3320 {
3321 eap->cmdidx = CMD_k;
3322 ++p;
3323 }
3324 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003325 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3326 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 || p[1] == 'g'
3328 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3329 || p[1] == 'I'
3330 || (p[1] == 'r' && p[2] != 'e')))
3331 {
3332 eap->cmdidx = CMD_substitute;
3333 ++p;
3334 }
3335 else
3336 {
3337 while (ASCII_ISALPHA(*p))
3338 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003339 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003340 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003342 while (ASCII_ISALNUM(*p))
3343 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344 }
3345 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3346 {
3347 // include "9" for "vim9script"
3348 ++p;
3349 while (ASCII_ISALPHA(*p))
3350 ++p;
3351 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003352
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003353 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3355 ++p;
3356 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003357 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3358 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003359 // Check for ":dl", ":dell", etc. to ":deletel": that's
3360 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003361 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003362 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003363 break;
3364 if (i == len - 1)
3365 {
3366 --len;
3367 if (p[-1] == 'l')
3368 eap->flags |= EXFLAG_LIST;
3369 else
3370 eap->flags |= EXFLAG_PRINT;
3371 }
3372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003374 if (ASCII_ISLOWER(eap->cmd[0]))
3375 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003376 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003377 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003378
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003379 if (command_count != (int)CMD_SIZE)
3380 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003381 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003382 getout(1);
3383 }
3384
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003385 // Use a precomputed index for fast look-up in cmdnames[]
3386 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003387 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3388 if (ASCII_ISLOWER(c2))
3389 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3390 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003391 else if (ASCII_ISUPPER(eap->cmd[0]))
3392 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003394 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395
3396 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3397 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3398 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3399 (size_t)len) == 0)
3400 {
3401#ifdef FEAT_EVAL
3402 if (full != NULL
3403 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3404 *full = TRUE;
3405#endif
3406 break;
3407 }
3408
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003409 // Look for a user defined command as a last resort. Let ":Print" be
3410 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003411 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3412 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003414 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 while (ASCII_ISALNUM(*p))
3416 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003417 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003419 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 eap->cmdidx = CMD_SIZE;
3421 }
3422
3423 return p;
3424}
3425
3426#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003427static struct cmdmod
3428{
3429 char *name;
3430 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003431 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003432} cmdmods[] = {
3433 {"aboveleft", 3, FALSE},
3434 {"belowright", 3, FALSE},
3435 {"botright", 2, FALSE},
3436 {"browse", 3, FALSE},
3437 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003438 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003439 {"hide", 3, FALSE},
3440 {"keepalt", 5, FALSE},
3441 {"keepjumps", 5, FALSE},
3442 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003443 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003444 {"leftabove", 5, FALSE},
3445 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003446 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003447 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003448 {"rightbelow", 6, FALSE},
3449 {"sandbox", 3, FALSE},
3450 {"silent", 3, FALSE},
3451 {"tab", 3, TRUE},
3452 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003453 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003454 {"verbose", 4, TRUE},
3455 {"vertical", 4, FALSE},
3456};
3457
3458/*
3459 * Return length of a command modifier (including optional count).
3460 * Return zero when it's not a modifier.
3461 */
3462 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003463modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003464{
3465 int i, j;
3466 char_u *p = cmd;
3467
3468 if (VIM_ISDIGIT(*cmd))
3469 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003470 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003471 {
3472 for (j = 0; p[j] != NUL; ++j)
3473 if (p[j] != cmdmods[i].name[j])
3474 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003475 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003476 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003477 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003478 }
3479 return 0;
3480}
3481
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482/*
3483 * Return > 0 if an Ex command "name" exists.
3484 * Return 2 if there is an exact match.
3485 * Return 3 if there is an ambiguous match.
3486 */
3487 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003488cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489{
3490 exarg_T ea;
3491 int full = FALSE;
3492 int i;
3493 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003494 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003496 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003497 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 {
3499 for (j = 0; name[j] != NUL; ++j)
3500 if (name[j] != cmdmods[i].name[j])
3501 break;
3502 if (name[j] == NUL && j >= cmdmods[i].minlen)
3503 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3504 }
3505
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003506 // Check built-in commands and user defined commands.
3507 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003508 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003511 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003513 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3514 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003515 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003516 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3518}
3519#endif
3520
Bram Moolenaard0190392019-08-23 21:17:35 +02003521 cmdidx_T
3522excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523{
Bram Moolenaard0190392019-08-23 21:17:35 +02003524 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
Bram Moolenaard0190392019-08-23 21:17:35 +02003526 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3527 idx = (cmdidx_T)((int)idx + 1))
3528 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 break;
3530
Bram Moolenaard0190392019-08-23 21:17:35 +02003531 return idx;
3532}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533
Bram Moolenaard0190392019-08-23 21:17:35 +02003534 long
3535excmd_get_argt(cmdidx_T idx)
3536{
3537 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538}
3539
3540/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003541 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 *
3543 * Backslashed delimiters after / or ? will be skipped, and commands will
3544 * not be expanded between /'s and ?'s or after "'".
3545 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003546 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 * Returns the "cmd" pointer advanced to beyond the range.
3548 */
3549 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003550skip_range(
3551 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003552 int skip_star, // skip "*" used for Visual range
3553 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003555 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003557 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003559 if (*cmd == '\\')
3560 {
3561 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3562 ++cmd;
3563 else
3564 break;
3565 }
3566 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 {
3568 if (*++cmd == NUL && ctx != NULL)
3569 *ctx = EXPAND_NOTHING;
3570 }
3571 else if (*cmd == '/' || *cmd == '?')
3572 {
3573 delim = *cmd++;
3574 while (*cmd != NUL && *cmd != delim)
3575 if (*cmd++ == '\\' && *cmd != NUL)
3576 ++cmd;
3577 if (*cmd == NUL && ctx != NULL)
3578 *ctx = EXPAND_NOTHING;
3579 }
3580 if (*cmd != NUL)
3581 ++cmd;
3582 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003583
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003584 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003585 while (*cmd == ':')
3586 cmd = skipwhite(cmd + 1);
3587
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003588 // Skip "*" used for Visual range.
3589 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3590 cmd = skipwhite(cmd + 1);
3591
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 return cmd;
3593}
3594
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003595 static void
3596addr_error(cmd_addr_T addr_type)
3597{
3598 if (addr_type == ADDR_NONE)
3599 emsg(_(e_norange));
3600 else
3601 emsg(_(e_invrange));
3602}
3603
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003605 * Return the default address for an address type.
3606 */
3607 static linenr_T
3608default_address(exarg_T *eap)
3609{
3610 linenr_T lnum = 0;
3611
3612 switch (eap->addr_type)
3613 {
3614 case ADDR_LINES:
3615 case ADDR_OTHER:
3616 // Default is the cursor line number. Avoid using an invalid
3617 // line number though.
3618 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3619 lnum = curbuf->b_ml.ml_line_count;
3620 else
3621 lnum = curwin->w_cursor.lnum;
3622 break;
3623 case ADDR_WINDOWS:
3624 lnum = CURRENT_WIN_NR;
3625 break;
3626 case ADDR_ARGUMENTS:
3627 lnum = curwin->w_arg_idx + 1;
3628 if (lnum > ARGCOUNT)
3629 lnum = ARGCOUNT;
3630 break;
3631 case ADDR_LOADED_BUFFERS:
3632 case ADDR_BUFFERS:
3633 lnum = curbuf->b_fnum;
3634 break;
3635 case ADDR_TABS:
3636 lnum = CURRENT_TAB_NR;
3637 break;
3638 case ADDR_TABS_RELATIVE:
3639 case ADDR_UNSIGNED:
3640 lnum = 1;
3641 break;
3642 case ADDR_QUICKFIX:
3643#ifdef FEAT_QUICKFIX
3644 lnum = qf_get_cur_idx(eap);
3645#endif
3646 break;
3647 case ADDR_QUICKFIX_VALID:
3648#ifdef FEAT_QUICKFIX
3649 lnum = qf_get_cur_valid_idx(eap);
3650#endif
3651 break;
3652 case ADDR_NONE:
3653 // Will give an error later if a range is found.
3654 break;
3655 }
3656 return lnum;
3657}
3658
3659/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003660 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 *
3662 * Set ptr to the next character after the part that was interpreted.
3663 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003664 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 *
3666 * Return MAXLNUM when no Ex address was found.
3667 */
3668 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003669get_address(
3670 exarg_T *eap UNUSED,
3671 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003672 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003673 int skip, // only skip the address, don't use it
3674 int silent, // no errors or side effects
3675 int to_other_file, // flag: may jump to other file
3676 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677{
3678 int c;
3679 int i;
3680 long n;
3681 char_u *cmd;
3682 pos_T pos;
3683 pos_T *fp;
3684 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003685 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686
3687 cmd = skipwhite(*ptr);
3688 lnum = MAXLNUM;
3689 do
3690 {
3691 switch (*cmd)
3692 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003693 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003694 ++cmd;
3695 switch (addr_type)
3696 {
3697 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003698 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 lnum = curwin->w_cursor.lnum;
3700 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003701 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003702 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003703 break;
3704 case ADDR_ARGUMENTS:
3705 lnum = curwin->w_arg_idx + 1;
3706 break;
3707 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003708 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003709 lnum = curbuf->b_fnum;
3710 break;
3711 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003712 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003713 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003714 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003715 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003716 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003717 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003718 cmd = NULL;
3719 goto error;
3720 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003721 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003722#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003723 lnum = qf_get_cur_idx(eap);
3724#endif
3725 break;
3726 case ADDR_QUICKFIX_VALID:
3727#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003728 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003729#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003730 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003731 }
3732 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003734 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003735 ++cmd;
3736 switch (addr_type)
3737 {
3738 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003739 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 lnum = curbuf->b_ml.ml_line_count;
3741 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003742 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003743 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003744 break;
3745 case ADDR_ARGUMENTS:
3746 lnum = ARGCOUNT;
3747 break;
3748 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003749 buf = lastbuf;
3750 while (buf->b_ml.ml_mfp == NULL)
3751 {
3752 if (buf->b_prev == NULL)
3753 break;
3754 buf = buf->b_prev;
3755 }
3756 lnum = buf->b_fnum;
3757 break;
3758 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003759 lnum = lastbuf->b_fnum;
3760 break;
3761 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003762 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003763 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003764 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003765 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003766 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003767 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003768 cmd = NULL;
3769 goto error;
3770 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003771 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003772#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003773 lnum = qf_get_size(eap);
3774 if (lnum == 0)
3775 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003776#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003777 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003778 case ADDR_QUICKFIX_VALID:
3779#ifdef FEAT_QUICKFIX
3780 lnum = qf_get_valid_size(eap);
3781 if (lnum == 0)
3782 lnum = 1;
3783#endif
3784 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003785 }
3786 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003788 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003789 if (*++cmd == NUL)
3790 {
3791 cmd = NULL;
3792 goto error;
3793 }
3794 if (addr_type != ADDR_LINES)
3795 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003796 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003797 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003798 goto error;
3799 }
3800 if (skip)
3801 ++cmd;
3802 else
3803 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003804 // Only accept a mark in another file when it is
3805 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003806 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3807 ++cmd;
3808 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003809 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003810 lnum = curwin->w_cursor.lnum;
3811 else
3812 {
3813 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 {
3815 cmd = NULL;
3816 goto error;
3817 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003818 lnum = fp->lnum;
3819 }
3820 }
3821 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822
3823 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003824 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003825 c = *cmd++;
3826 if (addr_type != ADDR_LINES)
3827 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003828 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003829 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003830 goto error;
3831 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003832 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003833 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003834 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003835 if (*cmd == c)
3836 ++cmd;
3837 }
3838 else
3839 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003840 int flags;
3841
3842 pos = curwin->w_cursor; // save curwin->w_cursor
3843
3844 // When '/' or '?' follows another address, start from
3845 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003846 if (lnum != MAXLNUM)
3847 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003848
3849 // Start a forward search at the end of the line (unless
3850 // before the first line).
3851 // Start a backward search at the start of the line.
3852 // This makes sure we never match in the current
3853 // line, and can match anywhere in the
3854 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003855 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003856 curwin->w_cursor.col = MAXCOL;
3857 else
3858 curwin->w_cursor.col = 0;
3859 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003860 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003861 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003862 {
3863 curwin->w_cursor = pos;
3864 cmd = NULL;
3865 goto error;
3866 }
3867 lnum = curwin->w_cursor.lnum;
3868 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003869 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003870 cmd += searchcmdlen;
3871 }
3872 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003874 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003875 ++cmd;
3876 if (addr_type != ADDR_LINES)
3877 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003878 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003879 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003880 goto error;
3881 }
3882 if (*cmd == '&')
3883 i = RE_SUBST;
3884 else if (*cmd == '?' || *cmd == '/')
3885 i = RE_SEARCH;
3886 else
3887 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003888 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003889 cmd = NULL;
3890 goto error;
3891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003893 if (!skip)
3894 {
3895 /*
3896 * When search follows another address, start from
3897 * there.
3898 */
3899 if (lnum != MAXLNUM)
3900 pos.lnum = lnum;
3901 else
3902 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003904 /*
3905 * Start the search just like for the above
3906 * do_search().
3907 */
3908 if (*cmd != '?')
3909 pos.col = MAXCOL;
3910 else
3911 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003912 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003913 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003914 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003915 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003916 lnum = pos.lnum;
3917 else
3918 {
3919 cmd = NULL;
3920 goto error;
3921 }
3922 }
3923 ++cmd;
3924 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925
3926 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003927 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003928 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 }
3930
3931 for (;;)
3932 {
3933 cmd = skipwhite(cmd);
3934 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3935 break;
3936
3937 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003938 {
3939 switch (addr_type)
3940 {
3941 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003942 case ADDR_OTHER:
3943 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003944 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003945 break;
3946 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003947 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003948 break;
3949 case ADDR_ARGUMENTS:
3950 lnum = curwin->w_arg_idx + 1;
3951 break;
3952 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003953 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003954 lnum = curbuf->b_fnum;
3955 break;
3956 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003957 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003958 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003959 case ADDR_TABS_RELATIVE:
3960 lnum = 1;
3961 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003962 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003963#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003964 lnum = qf_get_cur_idx(eap);
3965#endif
3966 break;
3967 case ADDR_QUICKFIX_VALID:
3968#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003969 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003970#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003971 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003972 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003973 case ADDR_UNSIGNED:
3974 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003975 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003976 }
3977 }
3978
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003980 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 else
3982 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003983 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 n = 1;
3985 else
3986 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003987
3988 if (addr_type == ADDR_TABS_RELATIVE)
3989 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003990 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003991 cmd = NULL;
3992 goto error;
3993 }
3994 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003995 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003996 lnum = compute_buffer_local_count(
3997 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003999 {
4000#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004001 // Relative line addressing, need to adjust for folded lines
4002 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004003 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4004 && address_count >= 2)
4005 (void)hasFolding(lnum, NULL, &lnum);
4006#endif
4007 if (i == '-')
4008 lnum -= n;
4009 else
4010 lnum += n;
4011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 }
4013 } while (*cmd == '/' || *cmd == '?');
4014
4015error:
4016 *ptr = cmd;
4017 return lnum;
4018}
4019
4020/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004021 * Set eap->line1 and eap->line2 to the whole range.
4022 * Used for commands with the EX_DFLALL flag and no range given.
4023 */
4024 static void
4025address_default_all(exarg_T *eap)
4026{
4027 eap->line1 = 1;
4028 switch (eap->addr_type)
4029 {
4030 case ADDR_LINES:
4031 case ADDR_OTHER:
4032 eap->line2 = curbuf->b_ml.ml_line_count;
4033 break;
4034 case ADDR_LOADED_BUFFERS:
4035 {
4036 buf_T *buf = firstbuf;
4037
4038 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4039 buf = buf->b_next;
4040 eap->line1 = buf->b_fnum;
4041 buf = lastbuf;
4042 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4043 buf = buf->b_prev;
4044 eap->line2 = buf->b_fnum;
4045 }
4046 break;
4047 case ADDR_BUFFERS:
4048 eap->line1 = firstbuf->b_fnum;
4049 eap->line2 = lastbuf->b_fnum;
4050 break;
4051 case ADDR_WINDOWS:
4052 eap->line2 = LAST_WIN_NR;
4053 break;
4054 case ADDR_TABS:
4055 eap->line2 = LAST_TAB_NR;
4056 break;
4057 case ADDR_TABS_RELATIVE:
4058 eap->line2 = 1;
4059 break;
4060 case ADDR_ARGUMENTS:
4061 if (ARGCOUNT == 0)
4062 eap->line1 = eap->line2 = 0;
4063 else
4064 eap->line2 = ARGCOUNT;
4065 break;
4066 case ADDR_QUICKFIX_VALID:
4067#ifdef FEAT_QUICKFIX
4068 eap->line2 = qf_get_valid_size(eap);
4069 if (eap->line2 == 0)
4070 eap->line2 = 1;
4071#endif
4072 break;
4073 case ADDR_NONE:
4074 case ADDR_UNSIGNED:
4075 case ADDR_QUICKFIX:
4076 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4077 break;
4078 }
4079}
4080
4081
4082/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004083 * Get flags from an Ex command argument.
4084 */
4085 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004086get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004087{
4088 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4089 {
4090 if (*eap->arg == 'l')
4091 eap->flags |= EXFLAG_LIST;
4092 else if (*eap->arg == 'p')
4093 eap->flags |= EXFLAG_PRINT;
4094 else
4095 eap->flags |= EXFLAG_NR;
4096 eap->arg = skipwhite(eap->arg + 1);
4097 }
4098}
4099
4100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 * Function called for command which is Not Implemented. NI!
4102 */
4103 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004104ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105{
4106 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004107 eap->errmsg =
4108 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109}
4110
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004111#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112/*
4113 * Function called for script command which is Not Implemented. NI!
4114 * Skips over ":perl <<EOF" constructs.
4115 */
4116 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004117ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118{
4119 if (!eap->skip)
4120 ex_ni(eap);
4121 else
4122 vim_free(script_get(eap, eap->arg));
4123}
4124#endif
4125
4126/*
4127 * Check range in Ex command for validity.
4128 * Return NULL when valid, error message when invalid.
4129 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004130 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004131invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004133 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004134
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 if ( eap->line1 < 0
4136 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004137 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004138 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004139
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004140 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004141 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004142 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004143 {
4144 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004145 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004146#ifdef FEAT_DIFF
4147 + (eap->cmdidx == CMD_diffget)
4148#endif
4149 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004150 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004151 break;
4152 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004153 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004154 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004155 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004156 break;
4157 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004158 // Only a boundary check, not whether the buffers actually
4159 // exist.
4160 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004161 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004162 break;
4163 case ADDR_LOADED_BUFFERS:
4164 buf = firstbuf;
4165 while (buf->b_ml.ml_mfp == NULL)
4166 {
4167 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004168 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004169 buf = buf->b_next;
4170 }
4171 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004172 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004173 buf = lastbuf;
4174 while (buf->b_ml.ml_mfp == NULL)
4175 {
4176 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004177 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004178 buf = buf->b_prev;
4179 }
4180 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004181 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004182 break;
4183 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004184 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004185 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004186 break;
4187 case ADDR_TABS:
4188 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004189 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004190 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004191 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004192 case ADDR_OTHER:
4193 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004194 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004195 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004196#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004197 // No error for value that is too big, will use the last entry.
4198 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004199 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004200#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004201 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004202 case ADDR_QUICKFIX_VALID:
4203#ifdef FEAT_QUICKFIX
4204 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4205 || eap->line2 < 0)
4206 return _(e_invrange);
4207#endif
4208 break;
4209 case ADDR_UNSIGNED:
4210 if (eap->line2 < 0)
4211 return _(e_invrange);
4212 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004213 case ADDR_NONE:
4214 // Will give an error elsewhere.
4215 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004216 }
4217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 return NULL;
4219}
4220
4221/*
4222 * Correct the range for zero line number, if required.
4223 */
4224 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004225correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004227 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
4229 if (eap->line1 == 0)
4230 eap->line1 = 1;
4231 if (eap->line2 == 0)
4232 eap->line2 = 1;
4233 }
4234}
4235
Bram Moolenaar748bf032005-02-02 23:04:36 +00004236#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004237/*
4238 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4239 * pattern. Otherwise return eap->arg.
4240 */
4241 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004242skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004243{
4244 char_u *p = eap->arg;
4245
Bram Moolenaara37420f2006-02-04 22:37:47 +00004246 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4247 || eap->cmdidx == CMD_vimgrepadd
4248 || eap->cmdidx == CMD_lvimgrepadd
4249 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004250 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004251 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004252 if (p == NULL)
4253 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004254 }
4255 return p;
4256}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004257
4258/*
4259 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4260 * in the command line, so that things like % get expanded.
4261 */
4262 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004263replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004264{
4265 char_u *new_cmdline;
4266 char_u *program;
4267 char_u *pos;
4268 char_u *ptr;
4269 int len;
4270 int i;
4271
4272 /*
4273 * Don't do it when ":vimgrep" is used for ":grep".
4274 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004275 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4276 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4277 || eap->cmdidx == CMD_grepadd
4278 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004279 && !grep_internal(eap->cmdidx))
4280 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004281 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4282 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004283 {
4284 if (*curbuf->b_p_gp == NUL)
4285 program = p_gp;
4286 else
4287 program = curbuf->b_p_gp;
4288 }
4289 else
4290 {
4291 if (*curbuf->b_p_mp == NUL)
4292 program = p_mp;
4293 else
4294 program = curbuf->b_p_mp;
4295 }
4296
4297 p = skipwhite(p);
4298
4299 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4300 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004301 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004302 i = 1;
4303 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4304 ++i;
4305 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004306 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004307 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004308 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004309 ptr = new_cmdline;
4310 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4311 {
4312 i = (int)(pos - program);
4313 STRNCPY(ptr, program, i);
4314 STRCPY(ptr += i, p);
4315 ptr += len;
4316 program = pos + 2;
4317 }
4318 STRCPY(ptr, program);
4319 }
4320 else
4321 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004322 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004323 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004324 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004325 STRCPY(new_cmdline, program);
4326 STRCAT(new_cmdline, " ");
4327 STRCAT(new_cmdline, p);
4328 }
4329 msg_make(p);
4330
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004331 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004332 vim_free(*cmdlinep);
4333 *cmdlinep = new_cmdline;
4334 p = new_cmdline;
4335 }
4336 return p;
4337}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004338#endif
4339
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340/*
4341 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004342 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 * Return FAIL for failure, OK otherwise.
4344 */
4345 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004346expand_filename(
4347 exarg_T *eap,
4348 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004349 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004351 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 char_u *repl;
4353 int srclen;
4354 char_u *p;
4355 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004356 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357
Bram Moolenaar748bf032005-02-02 23:04:36 +00004358#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004359 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004360 p = skip_grep_pat(eap);
4361#else
4362 p = eap->arg;
4363#endif
4364
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 /*
4366 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4367 * the file name contains a wildcard it should not cause expanding.
4368 * (it will be expanded anyway if there is a wildcard before replacing).
4369 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004370 has_wildcards = mch_has_wildcard(p);
4371 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004373#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004374 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004375 if (p[0] == '`' && p[1] == '=')
4376 {
4377 p += 2;
4378 (void)skip_expr(&p);
4379 if (*p == '`')
4380 ++p;
4381 continue;
4382 }
4383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 /*
4385 * Quick check if this cannot be the start of a special string.
4386 * Also removes backslash before '%', '#' and '<'.
4387 */
4388 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4389 {
4390 ++p;
4391 continue;
4392 }
4393
4394 /*
4395 * Try to find a match at this position.
4396 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004397 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4398 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004399 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004401 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 {
4403 p += srclen;
4404 continue;
4405 }
4406
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004407 // Wildcards won't be expanded below, the replacement is taken
4408 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004409 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4410 {
4411 char_u *l = repl;
4412
4413 repl = expand_env_save(repl);
4414 vim_free(l);
4415 }
4416
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004417 // Need to escape white space et al. with a backslash.
4418 // Don't do this for:
4419 // - replacement that already has been escaped: "##"
4420 // - shell commands (may have to use quotes instead).
4421 // - non-unix systems when there is a single argument (spaces don't
4422 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004424 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 && eap->cmdidx != CMD_grep
4427 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004428 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004429 && eap->cmdidx != CMD_lgrep
4430 && eap->cmdidx != CMD_lgrepadd
4431 && eap->cmdidx != CMD_lmake
4432 && eap->cmdidx != CMD_make
4433 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004435 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436#endif
4437 )
4438 {
4439 char_u *l;
4440#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004441 // Don't escape a backslash here, because rem_backslash() doesn't
4442 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 static char_u *nobslash = (char_u *)" \t\"|";
4444# define ESCAPE_CHARS nobslash
4445#else
4446# define ESCAPE_CHARS escape_chars
4447#endif
4448
4449 for (l = repl; *l; ++l)
4450 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4451 {
4452 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4453 if (l != NULL)
4454 {
4455 vim_free(repl);
4456 repl = l;
4457 }
4458 break;
4459 }
4460 }
4461
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004462 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004463 if ((eap->usefilter || eap->cmdidx == CMD_bang
4464 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004465 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 {
4467 char_u *l;
4468
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004469 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 if (l != NULL)
4471 {
4472 vim_free(repl);
4473 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 }
4475 }
4476
4477 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4478 vim_free(repl);
4479 if (p == NULL)
4480 return FAIL;
4481 }
4482
4483 /*
4484 * One file argument: Expand wildcards.
4485 * Don't do this with ":r !command" or ":w !command".
4486 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004487 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 {
4489 /*
4490 * May do this twice:
4491 * 1. Replace environment variables.
4492 * 2. Replace any other wildcards, remove backslashes.
4493 */
4494 for (n = 1; n <= 2; ++n)
4495 {
4496 if (n == 2)
4497 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 /*
4499 * Halve the number of backslashes (this is Vi compatible).
4500 * For Unix and OS/2, when wildcards are expanded, this is
4501 * done by ExpandOne() below.
4502 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004503#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 if (!has_wildcards)
4505#endif
4506 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 }
4508
4509 if (has_wildcards)
4510 {
4511 if (n == 1)
4512 {
4513 /*
4514 * First loop: May expand environment variables. This
4515 * can be done much faster with expand_env() than with
4516 * something else (e.g., calling a shell).
4517 * After expanding environment variables, check again
4518 * if there are still wildcards present.
4519 */
4520 if (vim_strchr(eap->arg, '$') != NULL
4521 || vim_strchr(eap->arg, '~') != NULL)
4522 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004523 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004524 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 has_wildcards = mch_has_wildcard(NameBuff);
4526 p = NameBuff;
4527 }
4528 else
4529 p = NULL;
4530 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004531 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 {
4533 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004534 int options = WILD_LIST_NOTFOUND
4535 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536
4537 ExpandInit(&xpc);
4538 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004539 if (p_wic)
4540 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004542 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 if (p == NULL)
4544 return FAIL;
4545 }
4546 if (p != NULL)
4547 {
4548 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4549 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004550 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 vim_free(p);
4552 }
4553 }
4554 }
4555 }
4556 return OK;
4557}
4558
4559/*
4560 * Replace part of the command line, keeping eap->cmd, eap->arg and
4561 * eap->nextcmd correct.
4562 * "src" points to the part that is to be replaced, of length "srclen".
4563 * "repl" is the replacement string.
4564 * Returns a pointer to the character after the replaced string.
4565 * Returns NULL for failure.
4566 */
4567 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004568repl_cmdline(
4569 exarg_T *eap,
4570 char_u *src,
4571 int srclen,
4572 char_u *repl,
4573 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574{
4575 int len;
4576 int i;
4577 char_u *new_cmdline;
4578
4579 /*
4580 * The new command line is build in new_cmdline[].
4581 * First allocate it.
4582 * Careful: a "+cmd" argument may have been NUL terminated.
4583 */
4584 len = (int)STRLEN(repl);
4585 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004586 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004587 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004588 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004589 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590
4591 /*
4592 * Copy the stuff before the expanded part.
4593 * Copy the expanded stuff.
4594 * Copy what came after the expanded part.
4595 * Copy the next commands, if there are any.
4596 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004597 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004599
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004601 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004603 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004605 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 {
4607 i = (int)STRLEN(new_cmdline) + 1;
4608 STRCPY(new_cmdline + i, eap->nextcmd);
4609 eap->nextcmd = new_cmdline + i;
4610 }
4611 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4612 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4613 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4614 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4615 vim_free(*cmdlinep);
4616 *cmdlinep = new_cmdline;
4617
4618 return src;
4619}
4620
4621/*
4622 * Check for '|' to separate commands and '"' to start comments.
4623 */
4624 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004625separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626{
4627 char_u *p;
4628
Bram Moolenaar86b68352004-12-27 21:59:20 +00004629#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004630 p = skip_grep_pat(eap);
4631#else
4632 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004633#endif
4634
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004635 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 {
4637 if (*p == Ctrl_V)
4638 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004639 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004640 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004642 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004643 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004644 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 break;
4646 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004647
4648#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004649 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004650 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004651 {
4652 p += 2;
4653 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004654 }
4655#endif
4656
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004657 // Check for '"': start of comment or '|': next command
4658 // :@" and :*" do not start a comment!
4659 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004660 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004661#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004662 && !in_vim9script()
4663#endif
4664 && !(eap->argt & EX_NOTRLCOM)
4665 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4666 || p != eap->arg)
4667 && (eap->cmdidx != CMD_redir
4668 || p != eap->arg + 1 || p[-1] != '@'))
4669#ifdef FEAT_EVAL
4670 || (*p == '#'
4671 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004672 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004673 && p[1] != '{'
4674 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 || *p == '|' || *p == '\n')
4677 {
4678 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004679 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 * AND 'b' is present in 'cpoptions'.
4681 */
4682 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004683 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004685 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 --p;
4687 }
4688 else
4689 {
4690 eap->nextcmd = check_nextcmd(p);
4691 *p = NUL;
4692 break;
4693 }
4694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004696
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004697 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 del_trailing_spaces(eap->arg);
4699}
4700
4701/*
4702 * get + command from ex argument
4703 */
4704 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004705getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706{
4707 char_u *arg = *argp;
4708 char_u *command = NULL;
4709
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004710 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 {
4712 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004713 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 command = dollar_command;
4715 else
4716 {
4717 command = arg;
4718 arg = skip_cmd_arg(command, TRUE);
4719 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004720 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 }
4722
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004723 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 *argp = arg;
4725 }
4726 return command;
4727}
4728
4729/*
4730 * Find end of "+command" argument. Skip over "\ " and "\\".
4731 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004732 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004733skip_cmd_arg(
4734 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004735 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736{
4737 while (*p && !vim_isspace(*p))
4738 {
4739 if (*p == '\\' && p[1] != NUL)
4740 {
4741 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004742 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 else
4744 ++p;
4745 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004746 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 }
4748 return p;
4749}
4750
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004751 int
4752get_bad_opt(char_u *p, exarg_T *eap)
4753{
4754 if (STRICMP(p, "keep") == 0)
4755 eap->bad_char = BAD_KEEP;
4756 else if (STRICMP(p, "drop") == 0)
4757 eap->bad_char = BAD_DROP;
4758 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4759 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004760 else
4761 return FAIL;
4762 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004763}
4764
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765/*
4766 * Get "++opt=arg" argument.
4767 * Return FAIL or OK.
4768 */
4769 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004770getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771{
4772 char_u *arg = eap->arg + 2;
4773 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004774 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004777 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4779 {
4780 if (*arg == 'n')
4781 {
4782 arg += 2;
4783 eap->force_bin = FORCE_NOBIN;
4784 }
4785 else
4786 eap->force_bin = FORCE_BIN;
4787 if (!checkforcmd(&arg, "binary", 3))
4788 return FAIL;
4789 eap->arg = skipwhite(arg);
4790 return OK;
4791 }
4792
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004793 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004794 if (STRNCMP(arg, "edit", 4) == 0)
4795 {
4796 eap->read_edit = TRUE;
4797 eap->arg = skipwhite(arg + 4);
4798 return OK;
4799 }
4800
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 if (STRNCMP(arg, "ff", 2) == 0)
4802 {
4803 arg += 2;
4804 pp = &eap->force_ff;
4805 }
4806 else if (STRNCMP(arg, "fileformat", 10) == 0)
4807 {
4808 arg += 10;
4809 pp = &eap->force_ff;
4810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 else if (STRNCMP(arg, "enc", 3) == 0)
4812 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004813 if (STRNCMP(arg, "encoding", 8) == 0)
4814 arg += 8;
4815 else
4816 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 pp = &eap->force_enc;
4818 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004819 else if (STRNCMP(arg, "bad", 3) == 0)
4820 {
4821 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004822 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824
4825 if (pp == NULL || *arg != '=')
4826 return FAIL;
4827
4828 ++arg;
4829 *pp = (int)(arg - eap->cmd);
4830 arg = skip_cmd_arg(arg, FALSE);
4831 eap->arg = skipwhite(arg);
4832 *arg = NUL;
4833
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 if (pp == &eap->force_ff)
4835 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4837 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004838 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004840 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004842 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4844 *p = TOLOWER_ASC(*p);
4845 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004846 else
4847 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004848 // Check ++bad= argument. Must be a single-byte character, "keep" or
4849 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004850 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004851 return FAIL;
4852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853
4854 return OK;
4855}
4856
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004858ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859{
4860 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004861 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 * directory for security reasons.
4863 */
4864 if (secure)
4865 {
4866 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004867 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 }
4869 else if (eap->cmdidx == CMD_autocmd)
4870 do_autocmd(eap->arg, eap->forceit);
4871 else
4872 do_augroup(eap->arg, eap->forceit);
4873}
4874
4875/*
4876 * ":doautocmd": Apply the automatic commands to the current buffer.
4877 */
4878 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004879ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004881 char_u *arg = eap->arg;
4882 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004883 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004884
Bram Moolenaar1610d052016-06-09 22:53:01 +02004885 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004886 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004887 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004888 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891/*
4892 * :[N]bunload[!] [N] [bufname] unload buffer
4893 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4894 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4895 */
4896 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004897ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004899 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004900 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 eap->errmsg = do_bufdel(
4902 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4903 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4904 : DOBUF_UNLOAD, eap->arg,
4905 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4906}
4907
4908/*
4909 * :[N]buffer [N] to buffer N
4910 * :[N]sbuffer [N] to buffer N
4911 */
4912 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004913ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004915 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004916 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004918 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 else
4920 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004921 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4923 else
4924 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004925 if (eap->do_ecmd_cmd != NULL)
4926 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
4928}
4929
4930/*
4931 * :[N]bmodified [N] to next mod. buffer
4932 * :[N]sbmodified [N] to next mod. buffer
4933 */
4934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004935ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936{
4937 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004938 if (eap->do_ecmd_cmd != NULL)
4939 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940}
4941
4942/*
4943 * :[N]bnext [N] to next buffer
4944 * :[N]sbnext [N] split and to next buffer
4945 */
4946 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004947ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004949 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004950 return;
4951
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004953 if (eap->do_ecmd_cmd != NULL)
4954 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955}
4956
4957/*
4958 * :[N]bNext [N] to previous buffer
4959 * :[N]bprevious [N] to previous buffer
4960 * :[N]sbNext [N] split and to previous buffer
4961 * :[N]sbprevious [N] split and to previous buffer
4962 */
4963 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004964ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004966 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004967 return;
4968
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004970 if (eap->do_ecmd_cmd != NULL)
4971 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972}
4973
4974/*
4975 * :brewind to first buffer
4976 * :bfirst to first buffer
4977 * :sbrewind split and to first buffer
4978 * :sbfirst split and to first buffer
4979 */
4980 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004981ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004983 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004984 return;
4985
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004987 if (eap->do_ecmd_cmd != NULL)
4988 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989}
4990
4991/*
4992 * :blast to last buffer
4993 * :sblast split and to last buffer
4994 */
4995 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004996ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004998 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004999 return;
5000
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005002 if (eap->do_ecmd_cmd != NULL)
5003 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005
Bram Moolenaar7a092242020-04-16 22:10:49 +02005006/*
5007 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02005008 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005009 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005011ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005013 int comment_char = '"';
5014
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005015#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005016 if (in_vim9script())
5017 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005018#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005019 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005020}
5021
5022/*
5023 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5024 * to "cmd_start" or has a white space character before it.
5025 */
5026 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005027ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005028{
5029 int c = *cmd;
5030
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005031 if (c == NUL || c == '|' || c == '\n')
5032 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005033#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005034 if (in_vim9script())
5035 return c == '#' && cmd[1] != '{'
5036 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005037#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005038 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039}
5040
5041#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5042 || defined(PROTO)
5043/*
5044 * Return the next command, after the first '|' or '\n'.
5045 * Return NULL if not found.
5046 */
5047 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005048find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049{
5050 while (*p != '|' && *p != '\n')
5051 {
5052 if (*p == NUL)
5053 return NULL;
5054 ++p;
5055 }
5056 return (p + 1);
5057}
5058#endif
5059
5060/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005061 * Check if *p is a separator between Ex commands, skipping over white space.
5062 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 */
5064 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005065check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005067 char_u *s = skipwhite(p);
5068
5069 if (*s == '|' || *s == '\n')
5070 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 else
5072 return NULL;
5073}
5074
5075/*
5076 * - if there are more files to edit
5077 * - and this is the last window
5078 * - and forceit not used
5079 * - and not repeated twice on a row
5080 * return FAIL and give error message if 'message' TRUE
5081 * return OK otherwise
5082 */
5083 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005084check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005085 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005086 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087{
5088 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5089
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005090 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005091 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 {
5093 if (message)
5094 {
5095#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5096 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5097 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005098 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005100 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5101 NGETTEXT("%d more file to edit. Quit anyway?",
5102 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5104 return OK;
5105 return FAIL;
5106 }
5107#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005108 semsg(NGETTEXT("E173: %d more file to edit",
5109 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005110 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 }
5112 return FAIL;
5113 }
5114 return OK;
5115}
5116
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117/*
5118 * Function given to ExpandGeneric() to obtain the list of command names.
5119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005121get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122{
5123 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 return cmdnames[idx].cmd_name;
5126}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005129ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130{
Bram Moolenaare6850792010-05-14 15:28:44 +02005131 if (*eap->arg == NUL)
5132 {
5133#ifdef FEAT_EVAL
5134 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5135 char_u *p = NULL;
5136
5137 if (expr != NULL)
5138 {
5139 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005140 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005141 --emsg_off;
5142 vim_free(expr);
5143 }
5144 if (p != NULL)
5145 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005146 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005147 vim_free(p);
5148 }
5149 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005150 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005151#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005152 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005153#endif
5154 }
5155 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005156 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005157
5158#ifdef FEAT_VTP
5159 else if (has_vtp_working())
5160 {
5161 // background color change requires clear + redraw
5162 update_screen(CLEAR);
5163 redrawcmd();
5164 }
5165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166}
5167
5168 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005169ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170{
5171 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005172 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 do_highlight(eap->arg, eap->forceit, FALSE);
5174}
5175
5176
5177/*
5178 * Call this function if we thought we were going to exit, but we won't
5179 * (because of an error). May need to restore the terminal mode.
5180 */
5181 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005182not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183{
5184 exiting = FALSE;
5185 settmode(TMODE_RAW);
5186}
5187
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005188 static int
5189before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5190{
5191 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5192
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005193 // Bail out when autocommands closed the window.
5194 // Refuse to quit when the buffer in the last window is being closed (can
5195 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005196 if (!win_valid(wp)
5197 || curbuf_locked()
5198 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5199 return TRUE;
5200
5201 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5202 {
5203 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005204 // Refuse to quit when locked or when the window was closed or the
5205 // buffer in the last window is being closed (can only happen in
5206 // autocommands).
5207 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005208 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5209 return TRUE;
5210 }
5211
5212 return FALSE;
5213}
5214
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005216 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005217 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005218 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005220 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005221ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005223 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005224
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225#ifdef FEAT_CMDWIN
5226 if (cmdwin_type != 0)
5227 {
5228 cmdwin_result = Ctrl_C;
5229 return;
5230 }
5231#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005232 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005233 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005234 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005235 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005236 return;
5237 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005238 if (eap->addr_count > 0)
5239 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005240 int wnr = eap->line2;
5241
5242 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5243 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005244 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005245 }
5246 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005247 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005248
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005249 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005250 if (curbuf_locked())
5251 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005252
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005253 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005254 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005255 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256
5257#ifdef FEAT_NETBEANS_INTG
5258 netbeansForcedQuit = eap->forceit;
5259#endif
5260
5261 /*
5262 * If there are more files or windows we won't exit.
5263 */
5264 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5265 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005266 if ((!buf_hide(wp->w_buffer)
5267 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005268 | (eap->forceit ? CCGD_FORCEIT : 0)
5269 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005271 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 {
5273 not_exiting();
5274 }
5275 else
5276 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005277 // quit last window
5278 // Note: only_one_window() returns true, even so a help window is
5279 // still open. In that case only quit, if no address has been
5280 // specified. Example:
5281 // :h|wincmd w|1q - don't quit
5282 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005283 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005285 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005286#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005288#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005289 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005290 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 }
5292}
5293
5294/*
5295 * ":cquit".
5296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005298ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005300 // this does not always pass on the exit code to the Manx compiler. why?
5301 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302}
5303
5304/*
5305 * ":qall": try to quit all windows
5306 */
5307 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005308ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309{
5310# ifdef FEAT_CMDWIN
5311 if (cmdwin_type != 0)
5312 {
5313 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005314 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 else
5316 cmdwin_result = K_XF2;
5317 return;
5318 }
5319# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005320
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005321 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005322 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005323 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005324 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005325 return;
5326 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005327
5328 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005329 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005330
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005332 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 getout(0);
5334 not_exiting();
5335}
5336
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337/*
5338 * ":close": close current window, unless it is the last one
5339 */
5340 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005341ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005343 win_T *win;
5344 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005345#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005347 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005349#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005350 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005351 {
5352 if (eap->addr_count == 0)
5353 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005354 else
5355 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005356 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005357 {
5358 winnr++;
5359 if (winnr == eap->line2)
5360 break;
5361 }
5362 if (win == NULL)
5363 win = lastwin;
5364 ex_win_close(eap->forceit, win, NULL);
5365 }
5366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367}
5368
Bram Moolenaar4033c552017-09-16 20:54:51 +02005369#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005370/*
5371 * ":pclose": Close any preview window.
5372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005374ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005375{
5376 win_T *win;
5377
Bram Moolenaar79648732019-07-18 21:43:07 +02005378 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005379 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005380 if (win->w_p_pvw)
5381 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005382 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005383 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005384 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005385# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005386 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005387 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005388# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005389}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005390#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005391
Bram Moolenaarf740b292006-02-16 22:11:02 +00005392/*
5393 * Close window "win" and take care of handling closing the last window for a
5394 * modified buffer.
5395 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005396 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005397ex_win_close(
5398 int forceit,
5399 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005400 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401{
5402 int need_hide;
5403 buf_T *buf = win->w_buffer;
5404
Bram Moolenaarcf844172020-06-26 19:44:06 +02005405 // Never close the autocommand window.
5406 if (win == aucmd_win)
5407 {
5408 emsg(_(e_autocmd_close));
5409 return;
5410 }
5411
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005413 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005415#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 if ((p_confirm || cmdmod.confirm) && p_write)
5417 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005418 bufref_T bufref;
5419
5420 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005422 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 return;
5424 need_hide = FALSE;
5425 }
5426 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005429 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 return;
5431 }
5432 }
5433
Bram Moolenaar4033c552017-09-16 20:54:51 +02005434#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005436#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005437
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005438 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005439 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005440 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005441 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005442 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443}
5444
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005446 * Handle the argument for a tabpage related ex command.
5447 * Returns a tabpage number.
5448 * When an error is encountered then eap->errmsg is set.
5449 */
5450 static int
5451get_tabpage_arg(exarg_T *eap)
5452{
5453 int tab_number;
5454 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5455
5456 if (eap->arg && *eap->arg != NUL)
5457 {
5458 char_u *p = eap->arg;
5459 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005460 int relative = 0; // argument +N/-N means: go to N places to the
5461 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005462
5463 if (*p == '-')
5464 {
5465 relative = -1;
5466 p++;
5467 }
5468 else if (*p == '+')
5469 {
5470 relative = 1;
5471 p++;
5472 }
5473
5474 p_save = p;
5475 tab_number = getdigits(&p);
5476
5477 if (relative == 0)
5478 {
5479 if (STRCMP(p, "$") == 0)
5480 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005481 else if (STRCMP(p, "#") == 0)
5482 if (valid_tabpage(lastused_tabpage))
5483 tab_number = tabpage_index(lastused_tabpage);
5484 else
5485 {
5486 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5487 tab_number = 0;
5488 goto theend;
5489 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005490 else if (p == p_save || *p_save == '-' || *p != NUL
5491 || tab_number > LAST_TAB_NR)
5492 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005493 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005494 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005495 goto theend;
5496 }
5497 }
5498 else
5499 {
5500 if (*p_save == NUL)
5501 tab_number = 1;
5502 else if (p == p_save || *p_save == '-' || *p != NUL
5503 || tab_number == 0)
5504 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005505 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005506 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005507 goto theend;
5508 }
5509 tab_number = tab_number * relative + tabpage_index(curtab);
5510 if (!unaccept_arg0 && relative == -1)
5511 --tab_number;
5512 }
5513 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005514 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005515 }
5516 else if (eap->addr_count > 0)
5517 {
5518 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005519 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005520 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005521 tab_number = 0;
5522 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005523 else
5524 {
5525 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005526 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005527 {
5528 --tab_number;
5529 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005530 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005531 }
5532 }
5533 }
5534 else
5535 {
5536 switch (eap->cmdidx)
5537 {
5538 case CMD_tabnext:
5539 tab_number = tabpage_index(curtab) + 1;
5540 if (tab_number > LAST_TAB_NR)
5541 tab_number = 1;
5542 break;
5543 case CMD_tabmove:
5544 tab_number = LAST_TAB_NR;
5545 break;
5546 default:
5547 tab_number = tabpage_index(curtab);
5548 }
5549 }
5550
5551theend:
5552 return tab_number;
5553}
5554
5555/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005556 * ":tabclose": close current tab page, unless it is the last one.
5557 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 */
5559 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005560ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005562 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005563 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005564
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005565# ifdef FEAT_CMDWIN
5566 if (cmdwin_type != 0)
5567 cmdwin_result = K_IGNORE;
5568 else
5569# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005570 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005571 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005572 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005574 tab_number = get_tabpage_arg(eap);
5575 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005576 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005577 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005578 if (tp == NULL)
5579 {
5580 beep_flush();
5581 return;
5582 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005583 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005584 {
5585 tabpage_close_other(tp, eap->forceit);
5586 return;
5587 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005588 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005589 tabpage_close(eap->forceit);
5590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005592}
5593
5594/*
5595 * ":tabonly": close all tab pages except the current one
5596 */
5597 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005598ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005599{
5600 tabpage_T *tp;
5601 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005602 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005603
5604# ifdef FEAT_CMDWIN
5605 if (cmdwin_type != 0)
5606 cmdwin_result = K_IGNORE;
5607 else
5608# endif
5609 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005610 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005611 else
5612 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005613 tab_number = get_tabpage_arg(eap);
5614 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005615 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005616 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005617 // Repeat this up to a 1000 times, because autocommands may
5618 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005619 for (done = 0; done < 1000; ++done)
5620 {
5621 FOR_ALL_TABPAGES(tp)
5622 if (tp->tp_topframe != topframe)
5623 {
5624 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005625 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005626 if (valid_tabpage(tp))
5627 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005628 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005629 break;
5630 }
5631 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005632 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005633 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005634 }
5635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637
5638/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005639 * Close the current tab page.
5640 */
5641 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005642tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005643{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005644 // First close all the windows but the current one. If that worked then
5645 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005646 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005647 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005648 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005649 ex_win_close(forceit, curwin, NULL);
5650# ifdef FEAT_GUI
5651 need_mouse_correct = TRUE;
5652# endif
5653}
5654
5655/*
5656 * Close tab page "tp", which is not the current tab page.
5657 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005658 * Also takes care of the tab pages line disappearing when closing the
5659 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005660 */
5661 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005662tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005663{
5664 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005665 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005666 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005667
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005668 // Limit to 1000 windows, autocommands may add a window while we close
5669 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005670 while (++done < 1000)
5671 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005672 wp = tp->tp_firstwin;
5673 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005674
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005675 // Autocommands may delete the tab page under our fingers and we may
5676 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005677 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005678 break;
5679 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005680
Bram Moolenaar29323592016-07-24 22:04:11 +02005681 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005682
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005683 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005684 if (h != tabline_height())
5685 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005686}
5687
5688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 * ":only".
5690 */
5691 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005692ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005694 win_T *wp;
5695 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696# ifdef FEAT_GUI
5697 need_mouse_correct = TRUE;
5698# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005699 if (eap->addr_count > 0)
5700 {
5701 wnr = eap->line2;
5702 for (wp = firstwin; --wnr > 0; )
5703 {
5704 if (wp->w_next == NULL)
5705 break;
5706 else
5707 wp = wp->w_next;
5708 }
5709 win_goto(wp);
5710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 close_others(TRUE, eap->forceit);
5712}
5713
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005715ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005717 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005718 if (!eap->skip)
5719 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005720#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005721 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005722#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005723 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005724 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005725 else
5726 {
5727 int winnr = 0;
5728 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005729
Bram Moolenaar2256c992016-11-15 21:17:07 +01005730 FOR_ALL_WINDOWS(win)
5731 {
5732 winnr++;
5733 if (winnr == eap->line2)
5734 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005735 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005736 if (win == NULL)
5737 win = lastwin;
5738 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 }
5741}
5742
5743/*
5744 * ":stop" and ":suspend": Suspend Vim.
5745 */
5746 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005747ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748{
5749 /*
5750 * Disallow suspending for "rvim".
5751 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005752 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 {
5754 if (!eap->forceit)
5755 autowrite_all();
5756 windgoto((int)Rows - 1, 0);
5757 out_char('\n');
5758 out_flush();
5759 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005760 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005762 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005764 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765#ifdef FEAT_TITLE
5766 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005767 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768#endif
5769 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005770 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005772 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 }
5774}
5775
5776/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005777 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 */
5779 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005780ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781{
Bram Moolenaar461f2122020-07-28 20:25:47 +02005782#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02005783 if (not_in_vim9(eap) == FAIL)
5784 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02005785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786#ifdef FEAT_CMDWIN
5787 if (cmdwin_type != 0)
5788 {
5789 cmdwin_result = Ctrl_C;
5790 return;
5791 }
5792#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005793 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005794 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005795 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005796 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005797 return;
5798 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005799
5800 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005801 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802
5803 /*
5804 * if more files or windows we won't exit
5805 */
5806 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5807 exiting = TRUE;
5808 if ( ((eap->cmdidx == CMD_wq
5809 || curbufIsChanged())
5810 && do_write(eap) == FAIL)
5811 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005812 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 {
5814 not_exiting();
5815 }
5816 else
5817 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005818 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005820 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821# ifdef FEAT_GUI
5822 need_mouse_correct = TRUE;
5823# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005824 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005825 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 }
5827}
5828
5829/*
5830 * ":print", ":list", ":number".
5831 */
5832 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005833ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005835 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005836 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005837 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005839 for ( ;!got_int; ui_breakcheck())
5840 {
5841 print_line(eap->line1,
5842 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5843 || (eap->flags & EXFLAG_NR)),
5844 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5845 if (++eap->line1 > eap->line2)
5846 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005847 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005848 }
5849 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005850 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005851 curwin->w_cursor.lnum = eap->line2;
5852 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 }
5854
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856}
5857
5858#ifdef FEAT_BYTEOFF
5859 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005860ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861{
5862 goto_byte(eap->line2);
5863}
5864#endif
5865
5866/*
5867 * ":shell".
5868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005870ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871{
5872 do_shell(NULL, 0);
5873}
5874
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005875#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005877static int drop_busy = FALSE;
5878static int drop_filec;
5879static char_u **drop_filev = NULL;
5880static int drop_split;
5881static void (*drop_callback)(void *);
5882static void *drop_cookie;
5883
5884 static void
5885handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886{
5887 exarg_T ea;
5888 int save_msg_scroll = msg_scroll;
5889
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005890 // Setting the argument list may cause screen updates and being called
5891 // recursively. Avoid that by setting drop_busy.
5892 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005894 // Check whether the current buffer is changed. If so, we will need
5895 // to split the current window or data could be lost.
5896 // We don't need to check if the 'hidden' option is set, as in this
5897 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005898 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 {
5900 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005901 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 --emsg_off;
5903 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005904 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 if (win_split(0, 0) == FAIL)
5907 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005908 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005910 // When splitting the window, create a new alist. Otherwise the
5911 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 alist_unlink(curwin->w_alist);
5913 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 }
5915
5916 /*
5917 * Set up the new argument list.
5918 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005919 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920
5921 /*
5922 * Move to the first file.
5923 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005924 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005925 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 ea.cmd = (char_u *)"next";
5927 do_argfile(&ea, 0);
5928
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005929 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5930 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 need_start_insertmode = FALSE;
5932
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005933 // Restore msg_scroll, otherwise a following command may cause scrolling
5934 // unexpectedly. The screen will be redrawn by the caller, thus
5935 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005937
5938 if (drop_callback != NULL)
5939 drop_callback(drop_cookie);
5940
5941 drop_filev = NULL;
5942 drop_busy = FALSE;
5943}
5944
5945/*
5946 * Handle a file drop. The code is here because a drop is *nearly* like an
5947 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005948 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005949 * code is very similar to :args and hence needs access to a lot of the static
5950 * functions in this file.
5951 *
5952 * The "filev" list must have been allocated using alloc(), as should each item
5953 * in the list. This function takes over responsibility for freeing the "filev"
5954 * list.
5955 */
5956 void
5957handle_drop(
5958 int filec, // the number of files dropped
5959 char_u **filev, // the list of files dropped
5960 int split, // force splitting the window
5961 void (*callback)(void *), // to be called after setting the argument
5962 // list
5963 void *cookie) // argument for "callback" (allocated)
5964{
5965 // Cannot handle recursive drops, finish the pending one.
5966 if (drop_busy)
5967 {
5968 FreeWild(filec, filev);
5969 vim_free(cookie);
5970 return;
5971 }
5972
5973 // When calling handle_drop() more than once in a row we only use the last
5974 // one.
5975 if (drop_filev != NULL)
5976 {
5977 FreeWild(drop_filec, drop_filev);
5978 vim_free(drop_cookie);
5979 }
5980
5981 drop_filec = filec;
5982 drop_filev = filev;
5983 drop_split = split;
5984 drop_callback = callback;
5985 drop_cookie = cookie;
5986
5987 // Postpone this when:
5988 // - editing the command line
5989 // - not possible to change the current buffer
5990 // - updating the screen
5991 // As it may change buffers and window structures that are in use and cause
5992 // freed memory to be used.
5993 if (text_locked() || curbuf_locked() || updating_screen)
5994 return;
5995
5996 handle_drop_internal();
5997}
5998
5999/*
6000 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6001 * reset: Handle a postponed drop.
6002 */
6003 void
6004handle_any_postponed_drop(void)
6005{
6006 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006007 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006008 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009}
6010#endif
6011
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 * ":preserve".
6014 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006016ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006018 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 ml_preserve(curbuf, TRUE);
6020}
6021
6022/*
6023 * ":recover".
6024 */
6025 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006026ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006028 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006030 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6031 | CCGD_MULTWIN
6032 | (eap->forceit ? CCGD_FORCEIT : 0)
6033 | CCGD_EXCMD)
6034
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 && (*eap->arg == NUL
6036 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006037 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 recoverymode = FALSE;
6039}
6040
6041/*
6042 * Command modifier used in a wrong way.
6043 */
6044 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006045ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006047 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048}
6049
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050/*
6051 * :sview [+command] file split window with new file, read-only
6052 * :split [[+command] file] split window with current or new file
6053 * :vsplit [[+command] file] split window vertically with current or new file
6054 * :new [[+command] file] split window with no or new file
6055 * :vnew [[+command] file] split vertically window with no or new file
6056 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006057 *
6058 * :tabedit open new Tab page with empty window
6059 * :tabedit [+command] file open new Tab page and edit "file"
6060 * :tabnew [[+command] file] just like :tabedit
6061 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 */
6063 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006064ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006066 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006067#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006069#endif
6070#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006072#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006073 int use_tab = eap->cmdidx == CMD_tabedit
6074 || eap->cmdidx == CMD_tabfind
6075 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006077 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006078 return;
6079
Bram Moolenaar4033c552017-09-16 20:54:51 +02006080#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083
Bram Moolenaar4033c552017-09-16 20:54:51 +02006084#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006085 // A ":split" in the quickfix window works like ":new". Don't want two
6086 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006087 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 {
6089 if (eap->cmdidx == CMD_split)
6090 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 if (eap->cmdidx == CMD_vsplit)
6092 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095
Bram Moolenaar4033c552017-09-16 20:54:51 +02006096#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006097 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 {
6099 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6100 FNAME_MESS, TRUE, curbuf->b_ffname);
6101 if (fname == NULL)
6102 goto theend;
6103 eap->arg = fname;
6104 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006105# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006106 else
6107# endif
6108#endif
6109#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 && eap->cmdidx != CMD_new)
6113 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006114 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006115# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006116 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006117# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006118 au_has_group((char_u *)"FileExplorer"))
6119 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006120 // No browsing supported but we do have the file explorer:
6121 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006122 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6123 eap->arg = (char_u *)".";
6124 }
6125 else
6126 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006127 fname = do_browse(0, (char_u *)(use_tab
6128 ? _("Edit File in new tab page")
6129 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006131 if (fname == NULL)
6132 goto theend;
6133 eap->arg = fname;
6134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006136 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006139 /*
6140 * Either open new tab page or split the window.
6141 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006142 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006143 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006144 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6145 : eap->addr_count == 0 ? 0
6146 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006147 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006148 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006149
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006150 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006151 if (curwin != old_curwin
6152 && win_valid(old_curwin)
6153 && old_curwin->w_buffer != curbuf
6154 && !cmdmod.keepalt)
6155 old_curwin->w_alt_fnum = curbuf->b_fnum;
6156 }
6157 }
6158 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6160 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006161 // Reset 'scrollbind' when editing another file, but keep it when
6162 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006164# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006166# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006168 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 else
6170 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 do_exedit(eap, old_curwin);
6172 }
6173
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006174# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006176# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006178# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179theend:
6180 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006181# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006183
6184/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006185 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006186 */
6187 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006188tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006189{
6190 exarg_T ea;
6191
Bram Moolenaara80faa82020-04-12 19:37:17 +02006192 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006193 ea.cmdidx = CMD_tabnew;
6194 ea.cmd = (char_u *)"tabn";
6195 ea.arg = (char_u *)"";
6196 ex_splitview(&ea);
6197}
6198
6199/*
6200 * :tabnext command
6201 */
6202 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006203ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006204{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006205 int tab_number;
6206
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006207 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006208 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006209 switch (eap->cmdidx)
6210 {
6211 case CMD_tabfirst:
6212 case CMD_tabrewind:
6213 goto_tabpage(1);
6214 break;
6215 case CMD_tablast:
6216 goto_tabpage(9999);
6217 break;
6218 case CMD_tabprevious:
6219 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006220 if (eap->arg && *eap->arg != NUL)
6221 {
6222 char_u *p = eap->arg;
6223 char_u *p_save = p;
6224
6225 tab_number = getdigits(&p);
6226 if (p == p_save || *p_save == '-' || *p != NUL
6227 || tab_number == 0)
6228 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006229 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006230 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006231 return;
6232 }
6233 }
6234 else
6235 {
6236 if (eap->addr_count == 0)
6237 tab_number = 1;
6238 else
6239 {
6240 tab_number = eap->line2;
6241 if (tab_number < 1)
6242 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006243 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006244 return;
6245 }
6246 }
6247 }
6248 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006249 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006250 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006251 tab_number = get_tabpage_arg(eap);
6252 if (eap->errmsg == NULL)
6253 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006254 break;
6255 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006256}
6257
6258/*
6259 * :tabmove command
6260 */
6261 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006262ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006263{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006264 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006265
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006266 tab_number = get_tabpage_arg(eap);
6267 if (eap->errmsg == NULL)
6268 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006269}
6270
6271/*
6272 * :tabs command: List tabs and their contents.
6273 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006275ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006276{
6277 tabpage_T *tp;
6278 win_T *wp;
6279 int tabcount = 1;
6280
6281 msg_start();
6282 msg_scroll = TRUE;
6283 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6284 {
6285 msg_putchar('\n');
6286 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006287 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006288 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006289 ui_breakcheck();
6290
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006291 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006292 wp = firstwin;
6293 else
6294 wp = tp->tp_firstwin;
6295 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6296 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006297 msg_putchar('\n');
6298 msg_putchar(wp == curwin ? '>' : ' ');
6299 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006300 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6301 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006302 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006303 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006304 else
6305 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6306 IObuff, IOSIZE, TRUE);
6307 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006308 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006309 ui_breakcheck();
6310 }
6311 }
6312}
6313
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314/*
6315 * ":mode": Set screen mode.
6316 * If no argument given, just get the screen size and redraw.
6317 */
6318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006319ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320{
6321 if (*eap->arg == NUL)
6322 shell_resized();
6323 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006324 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325}
6326
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327/*
6328 * ":resize".
6329 * set, increment or decrement current window height
6330 */
6331 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006332ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333{
6334 int n;
6335 win_T *wp = curwin;
6336
6337 if (eap->addr_count > 0)
6338 {
6339 n = eap->line2;
6340 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6341 ;
6342 }
6343
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006344# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006346# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 if (cmdmod.split & WSP_VERT)
6349 {
6350 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006351 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006352 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 n = 9999;
6354 win_setwidth_win((int)n, wp);
6355 }
6356 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 {
6358 if (*eap->arg == '-' || *eap->arg == '+')
6359 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006360 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 n = 9999;
6362 win_setheight_win((int)n, wp);
6363 }
6364}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365
6366/*
6367 * ":find [+command] <file>" command.
6368 */
6369 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006370ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371{
6372#ifdef FEAT_SEARCHPATH
6373 char_u *fname;
6374 int count;
6375
6376 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6377 TRUE, curbuf->b_ffname);
6378 if (eap->addr_count > 0)
6379 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006380 // Repeat finding the file "count" times. This matters when it
6381 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 count = eap->line2;
6383 while (fname != NULL && --count > 0)
6384 {
6385 vim_free(fname);
6386 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6387 FALSE, curbuf->b_ffname);
6388 }
6389 }
6390
6391 if (fname != NULL)
6392 {
6393 eap->arg = fname;
6394#endif
6395 do_exedit(eap, NULL);
6396#ifdef FEAT_SEARCHPATH
6397 vim_free(fname);
6398 }
6399#endif
6400}
6401
6402/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006403 * ":open" simulation: for now just work like ":visual".
6404 */
6405 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006406ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006407{
6408 regmatch_T regmatch;
6409 char_u *p;
6410
6411 curwin->w_cursor.lnum = eap->line2;
6412 beginline(BL_SOL | BL_FIX);
6413 if (*eap->arg == '/')
6414 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006415 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006416 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006417 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006418 *p = NUL;
6419 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6420 if (regmatch.regprog != NULL)
6421 {
6422 regmatch.rm_ic = p_ic;
6423 p = ml_get_curline();
6424 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006425 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006426 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006427 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006428 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006429 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006430 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006431 eap->arg += STRLEN(eap->arg);
6432 }
6433 check_cursor();
6434
6435 eap->cmdidx = CMD_visual;
6436 do_exedit(eap, NULL);
6437}
6438
6439/*
6440 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 */
6442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006443ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444{
6445 do_exedit(eap, NULL);
6446}
6447
6448/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006449 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006452do_exedit(
6453 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006454 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455{
6456 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006458 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006460 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6461 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006462 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 /*
6464 * ":vi" command ends Ex mode.
6465 */
6466 if (exmode_active && (eap->cmdidx == CMD_visual
6467 || eap->cmdidx == CMD_view))
6468 {
6469 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006470 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006472 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006473 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006474 if (global_busy)
6475 {
6476 int rd = RedrawingDisabled;
6477 int nwr = no_wait_return;
6478 int ms = msg_scroll;
6479#ifdef FEAT_GUI
6480 int he = hold_gui_events;
6481#endif
6482
6483 if (eap->nextcmd != NULL)
6484 {
6485 stuffReadbuff(eap->nextcmd);
6486 eap->nextcmd = NULL;
6487 }
6488
6489 if (exmode_was != EXMODE_VIM)
6490 settmode(TMODE_RAW);
6491 RedrawingDisabled = 0;
6492 no_wait_return = 0;
6493 need_wait_return = FALSE;
6494 msg_scroll = 0;
6495#ifdef FEAT_GUI
6496 hold_gui_events = 0;
6497#endif
6498 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006499 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006500
6501 main_loop(FALSE, TRUE);
6502
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006503 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006504 RedrawingDisabled = rd;
6505 no_wait_return = nwr;
6506 msg_scroll = ms;
6507#ifdef FEAT_GUI
6508 hold_gui_events = he;
6509#endif
6510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 }
6514
6515 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006516 || eap->cmdidx == CMD_tabnew
6517 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006518 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006520 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 setpcmark();
6522 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006523 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6524 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006526 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 || *eap->arg != NUL
6528#ifdef FEAT_BROWSE
6529 || cmdmod.browse
6530#endif
6531 )
6532 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006533 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6534 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006535 if (*eap->arg != NUL && curbuf_locked())
6536 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006537
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 n = readonlymode;
6539 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6540 readonlymode = TRUE;
6541 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006542 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6543 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 setpcmark();
6545 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6546 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006547 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6549 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6550 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006551 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006553 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006554 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006556 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006558 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 if (old_curwin != NULL)
6560 {
6561 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006562 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006564#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006565 cleanup_T cs;
6566
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006567 // Reset the error/interrupt/exception state here so that
6568 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006569 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006570#endif
6571#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006573#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006574 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006575
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006576#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006577 // Restore the error/interrupt/exception state if not
6578 // discarded by a new aborting error, interrupt, or
6579 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006580 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 }
6583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 }
6585 else if (readonlymode && curbuf->b_nwindows == 1)
6586 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006587 // When editing an already visited buffer, 'readonly' won't be set
6588 // but the previous value is kept. With ":view" and ":sview" we
6589 // want the file to be readonly, except when another window is
6590 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 curbuf->b_p_ro = TRUE;
6592 }
6593 readonlymode = n;
6594 }
6595 else
6596 {
6597 if (eap->do_ecmd_cmd != NULL)
6598 do_cmdline_cmd(eap->do_ecmd_cmd);
6599#ifdef FEAT_TITLE
6600 n = curwin->w_arg_idx_invalid;
6601#endif
6602 check_arg_idx(curwin);
6603#ifdef FEAT_TITLE
6604 if (n != curwin->w_arg_idx_invalid)
6605 maketitle();
6606#endif
6607 }
6608
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 /*
6610 * if ":split file" worked, set alternate file name in old window to new
6611 * file
6612 */
6613 if (old_curwin != NULL
6614 && *eap->arg != NUL
6615 && curwin != old_curwin
6616 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006617 && old_curwin->w_buffer != curbuf
6618 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620
6621 ex_no_reprint = TRUE;
6622}
6623
6624#ifndef FEAT_GUI
6625/*
6626 * ":gui" and ":gvim" when there is no GUI.
6627 */
6628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006629ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006631 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632}
6633#endif
6634
Bram Moolenaar4f974752019-02-17 17:44:42 +01006635#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006637ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638{
6639 gui_make_tearoff(eap->arg);
6640}
6641#endif
6642
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006643#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6644 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006646ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006648# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6649 if (gui.in_use)
6650 gui_make_popup(eap->arg, eap->forceit);
6651# ifdef FEAT_TERM_POPUP_MENU
6652 else
6653# endif
6654# endif
6655# ifdef FEAT_TERM_POPUP_MENU
6656 pum_make_popup(eap->arg, eap->forceit);
6657# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658}
6659#endif
6660
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006662ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663{
6664 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006665 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006667 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668}
6669
6670/*
6671 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6672 * offset.
6673 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006676ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006679 win_T *save_curwin = curwin;
6680 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 long topline;
6682 long y;
6683 linenr_T old_linenr = curwin->w_cursor.lnum;
6684
6685 setpcmark();
6686
6687 /*
6688 * determine max topline
6689 */
6690 if (curwin->w_p_scb)
6691 {
6692 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006693 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 {
6695 if (wp->w_p_scb && wp->w_buffer)
6696 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006697 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 if (topline > y)
6699 topline = y;
6700 }
6701 }
6702 if (topline < 1)
6703 topline = 1;
6704 }
6705 else
6706 {
6707 topline = 1;
6708 }
6709
6710
6711 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006712 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006714 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 {
6716 if (curwin->w_p_scb)
6717 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006718 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 y = topline - curwin->w_topline;
6720 if (y > 0)
6721 scrollup(y, TRUE);
6722 else
6723 scrolldown(-y, TRUE);
6724 curwin->w_scbind_pos = topline;
6725 redraw_later(VALID);
6726 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 }
6729 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006730 curwin = save_curwin;
6731 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 if (curwin->w_p_scb)
6733 {
6734 did_syncbind = TRUE;
6735 checkpcmark();
6736 if (old_linenr != curwin->w_cursor.lnum)
6737 {
6738 char_u ctrl_o[2];
6739
6740 ctrl_o[0] = Ctrl_O;
6741 ctrl_o[1] = 0;
6742 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6743 }
6744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745}
6746
6747
6748 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006749ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006751 int i;
6752 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6753 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006755 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 do_bang(1, eap, FALSE, FALSE, TRUE);
6757 else
6758 {
6759 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6760 return;
6761
6762#ifdef FEAT_BROWSE
6763 if (cmdmod.browse)
6764 {
6765 char_u *browseFile;
6766
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006767 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 NULL, NULL, NULL, curbuf);
6769 if (browseFile != NULL)
6770 {
6771 i = readfile(browseFile, NULL,
6772 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6773 vim_free(browseFile);
6774 }
6775 else
6776 i = OK;
6777 }
6778 else
6779#endif
6780 if (*eap->arg == NUL)
6781 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006782 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 return;
6784 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6785 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6786 }
6787 else
6788 {
6789 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6790 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6791 i = readfile(eap->arg, NULL,
6792 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6793
6794 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006795 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006797#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 if (!aborting())
6799#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006800 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 }
6802 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006803 {
6804 if (empty && exmode_active)
6805 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006806 // Delete the empty line that remains. Historically ex does
6807 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006808 if (eap->line2 == 0)
6809 lnum = curbuf->b_ml.ml_line_count;
6810 else
6811 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006812 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006813 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006814 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006815 if (curwin->w_cursor.lnum > 1
6816 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006817 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006818 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006819 }
6820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 }
6824}
6825
Bram Moolenaarea408852005-06-25 22:49:46 +00006826static char_u *prev_dir = NULL;
6827
6828#if defined(EXITFREE) || defined(PROTO)
6829 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006830free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006831{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006832 VIM_CLEAR(prev_dir);
6833 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006834}
6835#endif
6836
Bram Moolenaarf4258302013-06-02 18:20:17 +02006837/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006838 * Get the previous directory for the given chdir scope.
6839 */
6840 static char_u *
6841get_prevdir(cdscope_T scope)
6842{
6843 if (scope == CDSCOPE_WINDOW)
6844 return curwin->w_prevdir;
6845 else if (scope == CDSCOPE_TABPAGE)
6846 return curtab->tp_prevdir;
6847 return prev_dir;
6848}
6849
6850/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006851 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006852 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6853 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006854 */
6855 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006856post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006857{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006858 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006859 // Clear tab local directory for both :cd and :tcd
6860 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006861 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006862 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006863 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006864 char_u *pdir = get_prevdir(scope);
6865
6866 // If still in the global directory, need to remember current
6867 // directory as the global directory.
6868 if (globaldir == NULL && pdir != NULL)
6869 globaldir = vim_strsave(pdir);
6870
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006871 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006872 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006873 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006874 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006875 curtab->tp_localdir = vim_strsave(NameBuff);
6876 else
6877 curwin->w_localdir = vim_strsave(NameBuff);
6878 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006879 }
6880 else
6881 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006882 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006883 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006884 }
6885
6886 shorten_fnames(TRUE);
6887}
6888
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006889/*
6890 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006891 * chdir() function.
6892 * scope == CDSCOPE_WINDOW: changes the window-local directory
6893 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6894 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006895 * Returns TRUE if the directory is successfully changed.
6896 */
6897 int
6898changedir_func(
6899 char_u *new_dir,
6900 int forceit,
6901 cdscope_T scope)
6902{
6903 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006904 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006905 int dir_differs;
6906 int retval = FALSE;
6907
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006908 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006909 return FALSE;
6910
6911 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6912 {
6913 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6914 return FALSE;
6915 }
6916
6917 // ":cd -": Change to previous directory
6918 if (STRCMP(new_dir, "-") == 0)
6919 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006920 pdir = get_prevdir(scope);
6921 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006922 {
6923 emsg(_("E186: No previous directory"));
6924 return FALSE;
6925 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006926 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006927 }
6928
Bram Moolenaar002bc792020-06-05 22:33:42 +02006929 // Free the previous directory
6930 tofree = get_prevdir(scope);
6931
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006932 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006933 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006934 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006935 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006936 pdir = NULL;
6937 if (scope == CDSCOPE_WINDOW)
6938 curwin->w_prevdir = pdir;
6939 else if (scope == CDSCOPE_TABPAGE)
6940 curtab->tp_prevdir = pdir;
6941 else
6942 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006943
6944#if defined(UNIX) || defined(VMS)
6945 // for UNIX ":cd" means: go to home directory
6946 if (*new_dir == NUL)
6947 {
6948 // use NameBuff for home directory name
6949# ifdef VMS
6950 char_u *p;
6951
6952 p = mch_getenv((char_u *)"SYS$LOGIN");
6953 if (p == NULL || *p == NUL) // empty is the same as not set
6954 NameBuff[0] = NUL;
6955 else
6956 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6957# else
6958 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6959# endif
6960 new_dir = NameBuff;
6961 }
6962#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006963 dir_differs = new_dir == NULL || pdir == NULL
6964 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006965 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6966 emsg(_(e_failed));
6967 else
6968 {
6969 char_u *acmd_fname;
6970
6971 post_chdir(scope);
6972
6973 if (dir_differs)
6974 {
6975 if (scope == CDSCOPE_WINDOW)
6976 acmd_fname = (char_u *)"window";
6977 else if (scope == CDSCOPE_TABPAGE)
6978 acmd_fname = (char_u *)"tabpage";
6979 else
6980 acmd_fname = (char_u *)"global";
6981 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6982 curbuf);
6983 }
6984 retval = TRUE;
6985 }
6986 vim_free(tofree);
6987
6988 return retval;
6989}
Bram Moolenaarea408852005-06-25 22:49:46 +00006990
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006992 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006994 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006995ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006997 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998
6999 new_dir = eap->arg;
7000#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007001 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 if (*new_dir == NUL)
7003 ex_pwd(NULL);
7004 else
7005#endif
7006 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007007 cdscope_T scope = CDSCOPE_GLOBAL;
7008
7009 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7010 scope = CDSCOPE_WINDOW;
7011 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7012 scope = CDSCOPE_TABPAGE;
7013
7014 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007015 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007016 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007017 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 ex_pwd(eap);
7019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 }
7021}
7022
7023/*
7024 * ":pwd".
7025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007027ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028{
7029 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7030 {
7031#ifdef BACKSLASH_IN_FILENAME
7032 slash_adjust(NameBuff);
7033#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007034 if (p_verbose > 0)
7035 {
7036 char *context = "global";
7037
7038 if (curwin->w_localdir != NULL)
7039 context = "window";
7040 else if (curtab->tp_localdir != NULL)
7041 context = "tabpage";
7042 smsg("[%s] %s", context, (char *)NameBuff);
7043 }
7044 else
7045 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 }
7047 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007048 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049}
7050
7051/*
7052 * ":=".
7053 */
7054 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007055ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007057 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007058 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059}
7060
7061 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007062ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007064 int n;
7065 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066
7067 if (cursor_valid())
7068 {
7069 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7070 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007071 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007073
7074 len = eap->line2;
7075 switch (*eap->arg)
7076 {
7077 case 'm': break;
7078 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007079 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007080 }
7081 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082}
7083
7084/*
7085 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7086 */
7087 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007088do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089{
Bram Moolenaar52953192019-08-16 10:27:13 +02007090 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007091 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007092# ifdef ELAPSED_FUNC
7093 elapsed_T start_tv;
7094
7095 // Remember at what time we started, so that we know how much longer we
7096 // should wait after waiting for a bit.
7097 ELAPSED_INIT(start_tv);
7098# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099
7100 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007101 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007102 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007104 wait_now = msec - done > 1000L ? 1000L : msec - done;
7105#ifdef FEAT_TIMERS
7106 {
7107 long due_time = check_due_timer();
7108
7109 if (due_time > 0 && due_time < wait_now)
7110 wait_now = due_time;
7111 }
7112#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007113#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007114 if (has_any_channel() && wait_now > 20L)
7115 wait_now = 20L;
7116#endif
7117#ifdef FEAT_SOUND
7118 if (has_any_sound_callback() && wait_now > 20L)
7119 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007120#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007121 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007122
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007123#ifdef FEAT_JOB_CHANNEL
7124 if (has_any_channel())
7125 ui_breakcheck_force(TRUE);
7126 else
7127#endif
7128 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007129#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007130 // Process the netbeans and clientserver messages that may have been
7131 // received in the call to ui_breakcheck() when the GUI is in use. This
7132 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007133 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007134#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007135
7136# ifdef ELAPSED_FUNC
7137 // actual time passed
7138 done = ELAPSED_FUNC(start_tv);
7139# else
7140 // guestimate time passed (will actually be more)
7141 done += wait_now;
7142# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007144
7145 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7146 // input buffer, otherwise a following call to input() fails.
7147 if (got_int)
7148 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149}
7150
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151/*
7152 * ":winsize" command (obsolete).
7153 */
7154 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007155ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156{
7157 int w, h;
7158 char_u *arg = eap->arg;
7159 char_u *p;
7160
7161 w = getdigits(&arg);
7162 arg = skipwhite(arg);
7163 p = arg;
7164 h = getdigits(&arg);
7165 if (*p != NUL && *arg == NUL)
7166 set_shellsize(w, h, TRUE);
7167 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007168 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169}
7170
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007172ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173{
7174 int xchar = NUL;
7175 char_u *p;
7176
7177 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7178 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007179 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 if (eap->arg[1] == NUL)
7181 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007182 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 return;
7184 }
7185 xchar = eap->arg[1];
7186 p = eap->arg + 2;
7187 }
7188 else
7189 p = eap->arg + 1;
7190
7191 eap->nextcmd = check_nextcmd(p);
7192 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007193 if (*p != NUL && *p != (
7194#ifdef FEAT_EVAL
7195 in_vim9script() ? '#' :
7196#endif
7197 '"')
7198 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007199 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007200 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007202 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007204 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7206 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007207 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 }
7209}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210
Bram Moolenaar843ee412004-06-30 16:16:41 +00007211#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212/*
7213 * ":winpos".
7214 */
7215 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007216ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217{
7218 int x, y;
7219 char_u *arg = eap->arg;
7220 char_u *p;
7221
7222 if (*arg == NUL)
7223 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007224# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007225# ifdef VIMDLL
7226 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7227 mch_get_winpos(&x, &y) != FAIL)
7228# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007230# else
7231 if (mch_get_winpos(&x, &y) != FAIL)
7232# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 {
7234 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007235 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 }
7237 else
7238# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007239 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 }
7241 else
7242 {
7243 x = getdigits(&arg);
7244 arg = skipwhite(arg);
7245 p = arg;
7246 y = getdigits(&arg);
7247 if (*p == NUL || *arg != NUL)
7248 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007249 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 return;
7251 }
7252# ifdef FEAT_GUI
7253 if (gui.in_use)
7254 gui_mch_set_winpos(x, y);
7255 else if (gui.starting)
7256 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007257 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 gui_win_x = x;
7259 gui_win_y = y;
7260 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007261# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 else
7263# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007264# endif
7265# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007266 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267# endif
7268# ifdef HAVE_TGETENT
7269 if (*T_CWP)
7270 term_set_winpos(x, y);
7271# endif
7272 }
7273}
7274#endif
7275
7276/*
7277 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7278 */
7279 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007280ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281{
7282 oparg_T oa;
7283
7284 clear_oparg(&oa);
7285 oa.regname = eap->regname;
7286 oa.start.lnum = eap->line1;
7287 oa.end.lnum = eap->line2;
7288 oa.line_count = eap->line2 - eap->line1 + 1;
7289 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007291 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 {
7293 setpcmark();
7294 curwin->w_cursor.lnum = eap->line1;
7295 beginline(BL_SOL | BL_FIX);
7296 }
7297
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007298 if (VIsual_active)
7299 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007300
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 switch (eap->cmdidx)
7302 {
7303 case CMD_delete:
7304 oa.op_type = OP_DELETE;
7305 op_delete(&oa);
7306 break;
7307
7308 case CMD_yank:
7309 oa.op_type = OP_YANK;
7310 (void)op_yank(&oa, FALSE, TRUE);
7311 break;
7312
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007313 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007314 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007316 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7317#else
7318 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007320 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 oa.op_type = OP_RSHIFT;
7322 else
7323 oa.op_type = OP_LSHIFT;
7324 op_shift(&oa, FALSE, eap->amount);
7325 break;
7326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007328 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329}
7330
7331/*
7332 * ":put".
7333 */
7334 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007335ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007337 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 if (eap->line2 == 0)
7339 {
7340 eap->line2 = 1;
7341 eap->forceit = TRUE;
7342 }
7343 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007344 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007345 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346}
7347
7348/*
7349 * Handle ":copy" and ":move".
7350 */
7351 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007352ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353{
7354 long n;
7355
Bram Moolenaar491799b2020-08-01 19:23:43 +02007356#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007357 if (not_in_vim9(eap) == FAIL)
7358 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007359#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007360 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007361 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 {
7363 eap->nextcmd = NULL;
7364 return;
7365 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007366 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367
7368 /*
7369 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7370 */
7371 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7372 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007373 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 return;
7375 }
7376
7377 if (eap->cmdidx == CMD_move)
7378 {
7379 if (do_move(eap->line1, eap->line2, n) == FAIL)
7380 return;
7381 }
7382 else
7383 ex_copy(eap->line1, eap->line2, n);
7384 u_clearline();
7385 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007386 ex_may_print(eap);
7387}
7388
7389/*
7390 * Print the current line if flags were given to the Ex command.
7391 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007392 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007393ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007394{
7395 if (eap->flags != 0)
7396 {
7397 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7398 (eap->flags & EXFLAG_LIST));
7399 ex_no_reprint = TRUE;
7400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401}
7402
7403/*
7404 * ":smagic" and ":snomagic".
7405 */
7406 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007407ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408{
7409 int magic_save = p_magic;
7410
7411 p_magic = (eap->cmdidx == CMD_smagic);
Bram Moolenaar66e00142020-08-12 21:58:12 +02007412 ex_substitute(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 p_magic = magic_save;
7414}
7415
7416/*
7417 * ":join".
7418 */
7419 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007420ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421{
7422 curwin->w_cursor.lnum = eap->line1;
7423 if (eap->line1 == eap->line2)
7424 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007425 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 return;
7427 if (eap->line2 == curbuf->b_ml.ml_line_count)
7428 {
7429 beep_flush();
7430 return;
7431 }
7432 ++eap->line2;
7433 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007434 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007436 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437}
7438
7439/*
7440 * ":[addr]@r" or ":[addr]*r": execute register
7441 */
7442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007443ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444{
7445 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007446 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447
7448 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007449 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450
7451#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007452 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453#endif
7454
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007455 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 c = *eap->arg;
7457 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7458 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007459 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007460 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7461 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007462 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 else
7466 {
7467 int save_efr = exec_from_reg;
7468
7469 exec_from_reg = TRUE;
7470
7471 /*
7472 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007473 * Continue until the stuff buffer is empty and all added characters
7474 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007476 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7478
7479 exec_from_reg = save_efr;
7480 }
7481}
7482
7483/*
7484 * ":!".
7485 */
7486 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007487ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488{
7489 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7490}
7491
7492/*
7493 * ":undo".
7494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007496ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007498 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007499 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007500 else
7501 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502}
7503
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007504#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007505 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007506ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007507{
7508 char_u hash[UNDO_HASH_SIZE];
7509
7510 u_compute_hash(hash);
7511 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7512}
7513
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007514 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007515ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007516{
7517 char_u hash[UNDO_HASH_SIZE];
7518
7519 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007520 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007521}
7522#endif
7523
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524/*
7525 * ":redo".
7526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007528ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529{
7530 u_redo(1);
7531}
7532
7533/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007534 * ":earlier" and ":later".
7535 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007536 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007537ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007538{
7539 long count = 0;
7540 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007541 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007542 char_u *p = eap->arg;
7543
7544 if (*p == NUL)
7545 count = 1;
7546 else if (isdigit(*p))
7547 {
7548 count = getdigits(&p);
7549 switch (*p)
7550 {
7551 case 's': ++p; sec = TRUE; break;
7552 case 'm': ++p; sec = TRUE; count *= 60; break;
7553 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007554 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7555 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007556 }
7557 }
7558
7559 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007560 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007561 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007562 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7563 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007564}
7565
7566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 * ":redir": start/stop redirection.
7568 */
7569 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007570ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571{
7572 char *mode;
7573 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007574 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575
Bram Moolenaarba768492016-07-08 15:32:54 +02007576#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007577 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007578 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007579 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007580 return;
7581 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007582#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007583
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 if (STRICMP(eap->arg, "END") == 0)
7585 close_redir();
7586 else
7587 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007588 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007590 ++arg;
7591 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007593 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 mode = "a";
7595 }
7596 else
7597 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007598 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599
7600 close_redir();
7601
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007602 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007603 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 if (fname == NULL)
7605 return;
7606#ifdef FEAT_BROWSE
7607 if (cmdmod.browse)
7608 {
7609 char_u *browseFile;
7610
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007611 browseFile = do_browse(BROWSE_SAVE,
7612 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007613 fname, NULL, NULL,
7614 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007616 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 vim_free(fname);
7618 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007619 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 }
7621#endif
7622
7623 redir_fd = open_exfile(fname, eap->forceit, mode);
7624 vim_free(fname);
7625 }
7626#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007627 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007629 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007631 ++arg;
7632 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007634 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007635 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007637 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007639 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007640 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007641 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007642 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007644 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007645 if (*arg == '>')
7646 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007647 // Make register empty when not using @A-@Z and the
7648 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007649 if (*arg == NUL && !isupper(redir_reg))
7650 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007652 }
7653 if (*arg != NUL)
7654 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007655 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007656 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007657 }
7658 }
7659 else if (*arg == '=' && arg[1] == '>')
7660 {
7661 int append;
7662
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007663 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007664 close_redir();
7665 arg += 2;
7666
7667 if (*arg == '>')
7668 {
7669 ++arg;
7670 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 }
7672 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007673 append = FALSE;
7674
7675 if (var_redir_start(skipwhite(arg), append) == OK)
7676 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 }
7678#endif
7679
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007680 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007683 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007685
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007686 // Make sure redirection is not off. Can happen for cmdline completion
7687 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007688 if (redir_fd != NULL
7689#ifdef FEAT_EVAL
7690 || redir_reg || redir_vname
7691#endif
7692 )
7693 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694}
7695
7696/*
7697 * ":redraw": force redraw
7698 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007699 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007700ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701{
7702 int r = RedrawingDisabled;
7703 int p = p_lz;
7704
7705 RedrawingDisabled = 0;
7706 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007707 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007709 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710#ifdef FEAT_TITLE
7711 if (need_maketitle)
7712 maketitle();
7713#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007714#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7715# ifdef VIMDLL
7716 if (!gui.in_use)
7717# endif
7718 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 RedrawingDisabled = r;
7721 p_lz = p;
7722
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007723 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 msg_didout = FALSE;
7725 msg_col = 0;
7726
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007727 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007728 need_wait_return = FALSE;
7729
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 out_flush();
7731}
7732
7733/*
7734 * ":redrawstatus": force redraw of status line(s)
7735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007737ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 int r = RedrawingDisabled;
7740 int p = p_lz;
7741
7742 RedrawingDisabled = 0;
7743 p_lz = FALSE;
7744 if (eap->forceit)
7745 status_redraw_all();
7746 else
7747 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007748 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 RedrawingDisabled = r;
7750 p_lz = p;
7751 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752}
7753
Bram Moolenaare12bab32019-01-08 22:02:56 +01007754/*
7755 * ":redrawtabline": force redraw of the tabline
7756 */
7757 static void
7758ex_redrawtabline(exarg_T *eap UNUSED)
7759{
7760 int r = RedrawingDisabled;
7761 int p = p_lz;
7762
7763 RedrawingDisabled = 0;
7764 p_lz = FALSE;
7765
7766 draw_tabline();
7767
7768 RedrawingDisabled = r;
7769 p_lz = p;
7770 out_flush();
7771}
7772
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007774close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775{
7776 if (redir_fd != NULL)
7777 {
7778 fclose(redir_fd);
7779 redir_fd = NULL;
7780 }
7781#ifdef FEAT_EVAL
7782 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007783 if (redir_vname)
7784 {
7785 var_redir_stop();
7786 redir_vname = 0;
7787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788#endif
7789}
7790
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007791#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007792 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007793vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007794{
7795 if (vim_mkdir(name, prot) != 0)
7796 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007797 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007798 return FAIL;
7799 }
7800 return OK;
7801}
7802#endif
7803
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804/*
7805 * Open a file for writing for an Ex command, with some checks.
7806 * Return file descriptor, or NULL on failure.
7807 */
7808 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007809open_exfile(
7810 char_u *fname,
7811 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007812 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813{
7814 FILE *fd;
7815
7816#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007817 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 if (mch_isdir(fname))
7819 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007820 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 return NULL;
7822 }
7823#endif
7824 if (!forceit && *mode != 'a' && vim_fexists(fname))
7825 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007826 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 return NULL;
7828 }
7829
7830 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007831 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832
7833 return fd;
7834}
7835
7836/*
7837 * ":mark" and ":k".
7838 */
7839 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007840ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841{
7842 pos_T pos;
7843
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007844 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007845 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007846 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02007847 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 else
7849 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007850 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 curwin->w_cursor.lnum = eap->line2;
7852 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007853 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007854 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007855 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 }
7857}
7858
7859/*
7860 * Update w_topline, w_leftcol and the cursor position.
7861 */
7862 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007863update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007865 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 update_topline();
7867 if (!curwin->w_p_wrap)
7868 validate_cursor();
7869 update_curswant();
7870}
7871
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007873 * Save the current State and go to Normal mode.
7874 * Return TRUE if the typeahead could be saved.
7875 */
7876 int
7877save_current_state(save_state_T *sst)
7878{
7879 sst->save_msg_scroll = msg_scroll;
7880 sst->save_restart_edit = restart_edit;
7881 sst->save_msg_didout = msg_didout;
7882 sst->save_State = State;
7883 sst->save_insertmode = p_im;
7884 sst->save_finish_op = finish_op;
7885 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007886 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007887
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007888 msg_scroll = FALSE; // no msg scrolling in Normal mode
7889 restart_edit = 0; // don't go to Insert mode
7890 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007891
7892 /*
7893 * Save the current typeahead. This is required to allow using ":normal"
7894 * from an event handler and makes sure we don't hang when the argument
7895 * ends with half a command.
7896 */
7897 save_typeahead(&sst->tabuf);
7898 return sst->tabuf.typebuf_valid;
7899}
7900
7901 void
7902restore_current_state(save_state_T *sst)
7903{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007904 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007905 restore_typeahead(&sst->tabuf);
7906
7907 msg_scroll = sst->save_msg_scroll;
7908 restart_edit = sst->save_restart_edit;
7909 p_im = sst->save_insertmode;
7910 finish_op = sst->save_finish_op;
7911 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007912 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007913 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007914
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007915 // Restore the state (needed when called from a function executed for
7916 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007917 State = sst->save_State;
7918#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007919 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007920#endif
7921}
7922
7923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 * ":normal[!] {commands}": Execute normal mode commands.
7925 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007926 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007927ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007929 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 char_u *arg = NULL;
7931 int l;
7932 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007934 if (ex_normal_lock > 0)
7935 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007936 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007937 return;
7938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 if (ex_normal_busy >= p_mmd)
7940 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007941 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 return;
7943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 /*
7946 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7947 * this for the K_SPECIAL leading byte, otherwise special keys will not
7948 * work.
7949 */
7950 if (has_mbyte)
7951 {
7952 int len = 0;
7953
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007954 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 for (p = eap->arg; *p != NUL; ++p)
7956 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007957#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007958 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007960#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007961 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007962 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007963#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 )
7967 len += 2;
7968 }
7969 if (len > 0)
7970 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007971 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 if (arg != NULL)
7973 {
7974 len = 0;
7975 for (p = eap->arg; *p != NUL; ++p)
7976 {
7977 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007978#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 if (*p == CSI)
7980 {
7981 arg[len++] = KS_EXTRA;
7982 arg[len++] = (int)KE_CSI;
7983 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007984#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007985 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 {
7987 arg[len++] = *++p;
7988 if (*p == K_SPECIAL)
7989 {
7990 arg[len++] = KS_SPECIAL;
7991 arg[len++] = KE_FILLER;
7992 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007993#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 else if (*p == CSI)
7995 {
7996 arg[len++] = KS_EXTRA;
7997 arg[len++] = (int)KE_CSI;
7998 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 }
8001 arg[len] = NUL;
8002 }
8003 }
8004 }
8005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008007 ++ex_normal_busy;
8008 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 {
8010 /*
8011 * Repeat the :normal command for each line in the range. When no
8012 * range given, execute it just once, without positioning the cursor
8013 * first.
8014 */
8015 do
8016 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 if (eap->addr_count != 0)
8018 {
8019 curwin->w_cursor.lnum = eap->line1++;
8020 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008021 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 }
8023
Bram Moolenaar13505972019-01-24 15:04:48 +01008024 exec_normal_cmd(arg != NULL
8025 ? arg
8026 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 }
8028 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8029 }
8030
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008031 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 update_topline_cursor();
8033
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008034 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 --ex_normal_busy;
Bram Moolenaar189832b2020-09-23 12:29:11 +02008036#ifdef FEAT_PROP_POPUP
8037 if (ex_normal_busy == 0)
8038 ex_normal_busy_done = FALSE;
8039#endif
Bram Moolenaareda73602014-11-05 09:53:23 +01008040 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008041#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008042 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008043#endif
8044
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046}
8047
8048/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008049 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 */
8051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008052ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008054 if (eap->forceit)
8055 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008056 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008057 if (!curwin->w_cursor.lnum)
8058 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008059 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008060 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008061#ifdef FEAT_TERMINAL
8062 // Ignore this when running in an active terminal.
8063 if (term_job_running(curbuf->b_term))
8064 return;
8065#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008066
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008067 // Ignore the command when already in Insert mode. Inserting an
8068 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008069 if (State & INSERT)
8070 return;
8071
Bram Moolenaar64969662005-12-14 21:59:55 +00008072 if (eap->cmdidx == CMD_startinsert)
8073 restart_edit = 'a';
8074 else if (eap->cmdidx == CMD_startreplace)
8075 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008077 restart_edit = 'V';
8078
8079 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008081 if (eap->cmdidx == CMD_startinsert)
8082 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008083 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 }
8085}
8086
8087/*
8088 * ":stopinsert"
8089 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008091ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092{
8093 restart_edit = 0;
8094 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008095 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008098/*
8099 * Execute normal mode command "cmd".
8100 * "remap" can be REMAP_NONE or REMAP_YES.
8101 */
8102 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008103exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008104{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008105 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008106 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008107 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008108}
Bram Moolenaar25281632016-01-21 23:32:32 +01008109
Bram Moolenaar25281632016-01-21 23:32:32 +01008110/*
8111 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008112 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008113 */
8114 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008115exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008116{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008117 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008118 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008119
Bram Moolenaar905dd902019-04-07 14:21:47 +02008120 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8121 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008122 clear_oparg(&oa);
8123 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008124 while ((!stuff_empty()
8125 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008126 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008127 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008128 {
8129 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008130#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008131 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008132 && oa.op_type == OP_NOP && oa.regname == NUL
8133 && !VIsual_active)
8134 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008135 // If terminal_loop() returns OK we got a key that is handled
8136 // in Normal model. With FAIL we first need to position the
8137 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008138 if (terminal_loop(TRUE) == OK)
8139 normal_cmd(&oa, TRUE);
8140 }
8141 else
8142#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008143 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008144 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008145 }
8146}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008147
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148#ifdef FEAT_FIND_ID
8149 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008150ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151{
8152 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8153 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8154 (linenr_T)1, (linenr_T)MAXLNUM);
8155}
8156
Bram Moolenaar4033c552017-09-16 20:54:51 +02008157#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158/*
8159 * ":psearch"
8160 */
8161 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008162ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163{
8164 g_do_tagpreview = p_pvh;
8165 ex_findpat(eap);
8166 g_do_tagpreview = 0;
8167}
8168#endif
8169
8170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008171ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172{
8173 int whole = TRUE;
8174 long n;
8175 char_u *p;
8176 int action;
8177
8178 switch (cmdnames[eap->cmdidx].cmd_name[2])
8179 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008180 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8182 action = ACTION_GOTO;
8183 else
8184 action = ACTION_SHOW;
8185 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008186 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 action = ACTION_SHOW_ALL;
8188 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008189 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 action = ACTION_GOTO;
8191 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008192 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 action = ACTION_SPLIT;
8194 break;
8195 }
8196
8197 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008198 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {
8200 n = getdigits(&eap->arg);
8201 eap->arg = skipwhite(eap->arg);
8202 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008203 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {
8205 whole = FALSE;
8206 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008207 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 if (*p)
8209 {
8210 *p++ = NUL;
8211 p = skipwhite(p);
8212
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008213 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008214 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008215 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 else
8217 eap->nextcmd = check_nextcmd(p);
8218 }
8219 }
8220 if (!eap->skip)
8221 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8222 whole, !eap->forceit,
8223 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8224 n, action, eap->line1, eap->line2);
8225}
8226#endif
8227
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228
Bram Moolenaar4033c552017-09-16 20:54:51 +02008229#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230/*
8231 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8232 */
8233 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008234ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008236 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8238}
8239
8240/*
8241 * ":pedit"
8242 */
8243 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008244ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245{
8246 win_T *curwin_save = curwin;
8247
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008248 if (ERROR_IF_ANY_POPUP_WINDOW)
8249 return;
8250
Bram Moolenaar026587b2019-08-17 15:08:00 +02008251 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008253 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008254
Bram Moolenaar026587b2019-08-17 15:08:00 +02008255 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008257
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 if (curwin != curwin_save && win_valid(curwin_save))
8259 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008260 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 validate_cursor();
8262 redraw_later(VALID);
8263 win_enter(curwin_save, TRUE);
8264 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008265# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008266 else if (WIN_IS_POPUP(curwin))
8267 {
8268 // can't keep focus in popup window
8269 win_enter(firstwin, TRUE);
8270 }
8271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 g_do_tagpreview = 0;
8273}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275
8276/*
8277 * ":stag", ":stselect" and ":stjump".
8278 */
8279 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008280ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281{
8282 postponed_split = -1;
8283 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008284 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8286 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008287 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289
8290/*
8291 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8292 */
8293 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008294ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295{
8296 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8297}
8298
8299 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008300ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301{
8302 int cmd;
8303
8304 switch (name[1])
8305 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008306 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008308 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008310 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008312 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008314 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008316 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008318 case 'f': // ":tfirst"
8319 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008321 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008323 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008325 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008327 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 return;
8329 }
8330#endif
8331 cmd = DT_TAG;
8332 break;
8333 }
8334
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008335 if (name[0] == 'l')
8336 {
8337#ifndef FEAT_QUICKFIX
8338 ex_ni(eap);
8339 return;
8340#else
8341 cmd = DT_LTAG;
8342#endif
8343 }
8344
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8346 eap->forceit, TRUE);
8347}
8348
8349/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008350 * Check "str" for starting with a special cmdline variable.
8351 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8352 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8353 */
8354 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008355find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008356{
8357 int len;
8358 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008359 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008360 "%",
8361#define SPEC_PERC 0
8362 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008363#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008364 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008365#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008366 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008367#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008368 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008369#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008370 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008371#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008372 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008373#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008374 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008375#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008376 "<stack>", // call stack
8377#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008378 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008379#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008380 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008381#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008382 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008383#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008384 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008385#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008386 "<SID>", // script ID: <SNR>123_
8387#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008388#ifdef FEAT_CLIENTSERVER
8389 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008390# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008391#endif
8392 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008393
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008394 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008395 {
8396 len = (int)STRLEN(spec_str[i]);
8397 if (STRNCMP(src, spec_str[i], len) == 0)
8398 {
8399 *usedlen = len;
8400 return i;
8401 }
8402 }
8403 return -1;
8404}
8405
8406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 * Evaluate cmdline variables.
8408 *
8409 * change '%' to curbuf->b_ffname
8410 * '#' to curwin->w_altfile
8411 * '<cword>' to word under the cursor
8412 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008413 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 * '<cfile>' to path name under the cursor
8415 * '<sfile>' to sourced file name
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008416 * '<stack>' to call stack
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008417 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 * '<afile>' to file name for autocommand
8419 * '<abuf>' to buffer number for autocommand
8420 * '<amatch>' to matching name for autocommand
8421 *
8422 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8423 * "" for error without a message) and NULL is returned.
8424 * Returns an allocated string if a valid match was found.
8425 * Returns NULL if no match was found. "usedlen" then still contains the
8426 * number of characters to skip.
8427 */
8428 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008429eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008430 char_u *src, // pointer into commandline
8431 char_u *srcstart, // beginning of valid memory for src
8432 int *usedlen, // characters after src that are used
8433 linenr_T *lnump, // line number for :e command, or NULL
8434 char **errormsg, // pointer to error message
8435 int *escaped) // return value has escaped white space (can
8436 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437{
8438 int i;
8439 char_u *s;
8440 char_u *result;
8441 char_u *resultbuf = NULL;
8442 int resultlen;
8443 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008444 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008446 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449
8450 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008451 if (escaped != NULL)
8452 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453
8454 /*
8455 * Check if there is something to do.
8456 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008457 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008458 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 {
8460 *usedlen = 1;
8461 return NULL;
8462 }
8463
8464 /*
8465 * Skip when preceded with a backslash "\%" and "\#".
8466 * Note: In "\\%" the % is also not recognized!
8467 */
8468 if (src > srcstart && src[-1] == '\\')
8469 {
8470 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008471 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 return NULL;
8473 }
8474
8475 /*
8476 * word or WORD under cursor
8477 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008478 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8479 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008481 resultlen = find_ident_under_cursor(&result,
8482 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8483 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8484 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 if (resultlen == 0)
8486 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008487 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 return NULL;
8489 }
8490 }
8491
8492 /*
8493 * '#': Alternate file name
8494 * '%': Current file name
8495 * File name under the cursor
8496 * File name for autocommand
8497 * and following modifiers
8498 */
8499 else
8500 {
8501 switch (spec_idx)
8502 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008503 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 if (curbuf->b_fname == NULL)
8505 {
8506 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008507 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 }
8509 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008510 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008512 tilde_file = STRCMP(result, "~") == 0;
8513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 break;
8515
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008516 case SPEC_HASH: // '#' or "#99": alternate file
8517 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 {
8519 result = arg_all();
8520 resultbuf = result;
8521 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008522 if (escaped != NULL)
8523 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 break;
8526 }
8527 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008528 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008529 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008531 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008532 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008533 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008534 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008536 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008538 if (*usedlen < 2)
8539 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008540 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008541 *usedlen = 1;
8542 return NULL;
8543 }
8544#ifdef FEAT_EVAL
8545 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8546 (long)i);
8547 if (result == NULL)
8548 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008549 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008550 return NULL;
8551 }
8552#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008553 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 }
8557 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008558 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008559 if (i == 0 && src[1] == '<' && *usedlen > 1)
8560 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008561 buf = buflist_findnr(i);
8562 if (buf == NULL)
8563 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008564 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008565 return NULL;
8566 }
8567 if (lnump != NULL)
8568 *lnump = ECMD_LAST;
8569 if (buf->b_fname == NULL)
8570 {
8571 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008572 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008573 }
8574 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008575 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008576 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008577 tilde_file = STRCMP(result, "~") == 0;
8578 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 break;
8581
8582#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008583 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008584 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 if (result == NULL)
8586 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008587 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 return NULL;
8589 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008590 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 break;
8592#endif
8593
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008594 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008596 if (result != NULL && !autocmd_fname_full)
8597 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008598 // Still need to turn the fname into a full path. It is
8599 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008600 autocmd_fname_full = TRUE;
8601 result = FullName_save(autocmd_fname, FALSE);
8602 vim_free(autocmd_fname);
8603 autocmd_fname = result;
8604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 if (result == NULL)
8606 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008607 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 return NULL;
8609 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008610 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 break;
8612
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008613 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 if (autocmd_bufnr <= 0)
8615 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008616 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 return NULL;
8618 }
8619 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8620 result = strbuf;
8621 break;
8622
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008623 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 result = autocmd_match;
8625 if (result == NULL)
8626 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008627 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 return NULL;
8629 }
8630 break;
8631
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008632 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008633 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008634 result = estack_sfile(spec_idx == SPEC_SFILE
8635 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 if (result == NULL)
8637 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008638 *errormsg = spec_idx == SPEC_SFILE
8639 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8640 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 return NULL;
8642 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008643 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008645
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008646 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008647 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008648 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008649 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008650 return NULL;
8651 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008652 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008653 result = strbuf;
8654 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008655
8656#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008657 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008658 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008659 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008660 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008661 return NULL;
8662 }
8663 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008664 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008665 result = strbuf;
8666 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008667
Bram Moolenaar90944302020-08-01 20:45:11 +02008668 case SPEC_SID:
8669 if (current_sctx.sc_sid <= 0)
8670 {
8671 *errormsg = _(e_usingsid);
8672 return NULL;
8673 }
8674 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
8675 result = strbuf;
8676 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02008677#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02008678
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008679#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008680 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008681 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8682 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 result = strbuf;
8684 break;
8685#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008686
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008687 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008688 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008689 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 }
8691
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008692 resultlen = (int)STRLEN(result); // length of new string
8693 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 {
8695 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 resultlen = (int)(s - result);
8698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 else if (!skip_mod)
8700 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008701 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 &resultlen);
8703 if (result == NULL)
8704 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008705 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 return NULL;
8707 }
8708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 }
8710
8711 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8712 {
8713 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008714 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008715 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008717 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 result = NULL;
8719 }
8720 else
8721 result = vim_strnsave(result, resultlen);
8722 vim_free(resultbuf);
8723 return result;
8724}
8725
8726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 * Expand the <sfile> string in "arg".
8728 *
8729 * Returns an allocated string, or NULL for any error.
8730 */
8731 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008732expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008734 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 int len;
8736 char_u *result;
8737 char_u *newres;
8738 char_u *repl;
8739 int srclen;
8740 char_u *p;
8741
8742 result = vim_strsave(arg);
8743 if (result == NULL)
8744 return NULL;
8745
8746 for (p = result; *p; )
8747 {
8748 if (STRNCMP(p, "<sfile>", 7) != 0)
8749 ++p;
8750 else
8751 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008752 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008753 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 if (errormsg != NULL)
8755 {
8756 if (*errormsg)
8757 emsg(errormsg);
8758 vim_free(result);
8759 return NULL;
8760 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008761 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 {
8763 p += srclen;
8764 continue;
8765 }
8766 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8767 newres = alloc(len);
8768 if (newres == NULL)
8769 {
8770 vim_free(repl);
8771 vim_free(result);
8772 return NULL;
8773 }
8774 mch_memmove(newres, result, (size_t)(p - result));
8775 STRCPY(newres + (p - result), repl);
8776 len = (int)STRLEN(newres);
8777 STRCAT(newres, p + srclen);
8778 vim_free(repl);
8779 vim_free(result);
8780 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008781 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 }
8783 }
8784
8785 return result;
8786}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008789/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008790 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008791 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008792 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008794dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 if (fname == NULL)
8797 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008798 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799}
8800#endif
8801
8802/*
8803 * ":behave {mswin,xterm}"
8804 */
8805 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008806ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807{
8808 if (STRCMP(eap->arg, "mswin") == 0)
8809 {
8810 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8811 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8812 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8813 set_option_value((char_u *)"keymodel", 0L,
8814 (char_u *)"startsel,stopsel", 0);
8815 }
8816 else if (STRCMP(eap->arg, "xterm") == 0)
8817 {
8818 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8819 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8820 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8821 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8822 }
8823 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008824 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825}
8826
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827static int filetype_detect = FALSE;
8828static int filetype_plugin = FALSE;
8829static int filetype_indent = FALSE;
8830
8831/*
8832 * ":filetype [plugin] [indent] {on,off,detect}"
8833 * on: Load the filetype.vim file to install autocommands for file types.
8834 * off: Load the ftoff.vim file to remove all autocommands for file types.
8835 * plugin on: load filetype.vim and ftplugin.vim
8836 * plugin off: load ftplugof.vim
8837 * indent on: load filetype.vim and indent.vim
8838 * indent off: load indoff.vim
8839 */
8840 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008841ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842{
8843 char_u *arg = eap->arg;
8844 int plugin = FALSE;
8845 int indent = FALSE;
8846
8847 if (*eap->arg == NUL)
8848 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008849 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008850 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 filetype_detect ? "ON" : "OFF",
8852 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8853 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8854 return;
8855 }
8856
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008857 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 for (;;)
8859 {
8860 if (STRNCMP(arg, "plugin", 6) == 0)
8861 {
8862 plugin = TRUE;
8863 arg = skipwhite(arg + 6);
8864 continue;
8865 }
8866 if (STRNCMP(arg, "indent", 6) == 0)
8867 {
8868 indent = TRUE;
8869 arg = skipwhite(arg + 6);
8870 continue;
8871 }
8872 break;
8873 }
8874 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8875 {
8876 if (*arg == 'o' || !filetype_detect)
8877 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008878 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 filetype_detect = TRUE;
8880 if (plugin)
8881 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008882 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 filetype_plugin = TRUE;
8884 }
8885 if (indent)
8886 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008887 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 filetype_indent = TRUE;
8889 }
8890 }
8891 if (*arg == 'd')
8892 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008893 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008894 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 }
8896 }
8897 else if (STRCMP(arg, "off") == 0)
8898 {
8899 if (plugin || indent)
8900 {
8901 if (plugin)
8902 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008903 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 filetype_plugin = FALSE;
8905 }
8906 if (indent)
8907 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008908 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 filetype_indent = FALSE;
8910 }
8911 }
8912 else
8913 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008914 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 filetype_detect = FALSE;
8916 }
8917 }
8918 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008919 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920}
8921
8922/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008923 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 */
8925 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008926ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927{
8928 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008929 {
8930 char_u *arg = eap->arg;
8931
8932 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8933 arg += 9;
8934
8935 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8936 if (arg != eap->arg)
8937 did_filetype = FALSE;
8938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008942ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943{
8944#ifdef FEAT_DIGRAPHS
8945 if (*eap->arg != NUL)
8946 putdigraph(eap->arg);
8947 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008948 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008950 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951#endif
8952}
8953
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008954#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8955 void
8956set_no_hlsearch(int flag)
8957{
8958 no_hlsearch = flag;
8959# ifdef FEAT_EVAL
8960 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8961# endif
8962}
8963
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964/*
8965 * ":nohlsearch"
8966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008968ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008970 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008971 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973#endif
8974
8975#ifdef FEAT_CRYPT
8976/*
8977 * ":X": Get crypt key
8978 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008980ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008982 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008983 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984}
8985#endif
8986
8987#ifdef FEAT_FOLDING
8988 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008989ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990{
8991 if (foldManualAllowed(TRUE))
8992 foldCreate(eap->line1, eap->line2);
8993}
8994
8995 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008996ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997{
8998 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8999 eap->forceit, FALSE);
9000}
9001
9002 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009003ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004{
9005 linenr_T lnum;
9006
Bram Moolenaar4facea32019-10-12 20:17:40 +02009007# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009008 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009009# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009010
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009011 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9013 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9014 ml_setmarked(lnum);
9015
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009016 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009018 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009019# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009020 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009021# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022}
9023#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009024
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009025#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009026/*
9027 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9028 * instead of a quickfix command.
9029 */
9030 int
9031is_loclist_cmd(int cmdidx)
9032{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009033 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009034 return FALSE;
9035 return cmdnames[cmdidx].cmd_name[0] == 'l';
9036}
9037#endif
9038
Bram Moolenaar4facea32019-10-12 20:17:40 +02009039#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009040 int
9041get_pressedreturn(void)
9042{
9043 return ex_pressedreturn;
9044}
9045
9046 void
9047set_pressedreturn(int val)
9048{
9049 ex_pressedreturn = val;
9050}
9051#endif