blob: 05378fdcd7a1daa3f1a4e5bb4e74091f72925726 [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
Bram Moolenaar373863e2020-09-26 17:20:53 +02003423 // ":fina" means ":finally" for backwards compatibility.
3424 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3425 eap->cmdidx = CMD_finally;
3426
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 return p;
3428}
3429
3430#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003431static struct cmdmod
3432{
3433 char *name;
3434 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003435 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003436} cmdmods[] = {
3437 {"aboveleft", 3, FALSE},
3438 {"belowright", 3, FALSE},
3439 {"botright", 2, FALSE},
3440 {"browse", 3, FALSE},
3441 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003442 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003443 {"hide", 3, FALSE},
3444 {"keepalt", 5, FALSE},
3445 {"keepjumps", 5, FALSE},
3446 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003447 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003448 {"leftabove", 5, FALSE},
3449 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003450 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003451 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003452 {"rightbelow", 6, FALSE},
3453 {"sandbox", 3, FALSE},
3454 {"silent", 3, FALSE},
3455 {"tab", 3, TRUE},
3456 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003457 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003458 {"verbose", 4, TRUE},
3459 {"vertical", 4, FALSE},
3460};
3461
3462/*
3463 * Return length of a command modifier (including optional count).
3464 * Return zero when it's not a modifier.
3465 */
3466 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003467modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003468{
3469 int i, j;
3470 char_u *p = cmd;
3471
3472 if (VIM_ISDIGIT(*cmd))
3473 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003474 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003475 {
3476 for (j = 0; p[j] != NUL; ++j)
3477 if (p[j] != cmdmods[i].name[j])
3478 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003479 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003480 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003481 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003482 }
3483 return 0;
3484}
3485
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486/*
3487 * Return > 0 if an Ex command "name" exists.
3488 * Return 2 if there is an exact match.
3489 * Return 3 if there is an ambiguous match.
3490 */
3491 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003492cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493{
3494 exarg_T ea;
3495 int full = FALSE;
3496 int i;
3497 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003498 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003500 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003501 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 {
3503 for (j = 0; name[j] != NUL; ++j)
3504 if (name[j] != cmdmods[i].name[j])
3505 break;
3506 if (name[j] == NUL && j >= cmdmods[i].minlen)
3507 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3508 }
3509
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003510 // Check built-in commands and user defined commands.
3511 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003512 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003515 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003517 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3518 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003519 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003520 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3522}
3523#endif
3524
Bram Moolenaard0190392019-08-23 21:17:35 +02003525 cmdidx_T
3526excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527{
Bram Moolenaard0190392019-08-23 21:17:35 +02003528 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529
Bram Moolenaard0190392019-08-23 21:17:35 +02003530 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3531 idx = (cmdidx_T)((int)idx + 1))
3532 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 break;
3534
Bram Moolenaard0190392019-08-23 21:17:35 +02003535 return idx;
3536}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537
Bram Moolenaard0190392019-08-23 21:17:35 +02003538 long
3539excmd_get_argt(cmdidx_T idx)
3540{
3541 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542}
3543
3544/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003545 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 *
3547 * Backslashed delimiters after / or ? will be skipped, and commands will
3548 * not be expanded between /'s and ?'s or after "'".
3549 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003550 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 * Returns the "cmd" pointer advanced to beyond the range.
3552 */
3553 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003554skip_range(
3555 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003556 int skip_star, // skip "*" used for Visual range
3557 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003559 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003561 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003563 if (*cmd == '\\')
3564 {
3565 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3566 ++cmd;
3567 else
3568 break;
3569 }
3570 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
3572 if (*++cmd == NUL && ctx != NULL)
3573 *ctx = EXPAND_NOTHING;
3574 }
3575 else if (*cmd == '/' || *cmd == '?')
3576 {
3577 delim = *cmd++;
3578 while (*cmd != NUL && *cmd != delim)
3579 if (*cmd++ == '\\' && *cmd != NUL)
3580 ++cmd;
3581 if (*cmd == NUL && ctx != NULL)
3582 *ctx = EXPAND_NOTHING;
3583 }
3584 if (*cmd != NUL)
3585 ++cmd;
3586 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003587
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003588 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003589 while (*cmd == ':')
3590 cmd = skipwhite(cmd + 1);
3591
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003592 // Skip "*" used for Visual range.
3593 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3594 cmd = skipwhite(cmd + 1);
3595
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 return cmd;
3597}
3598
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003599 static void
3600addr_error(cmd_addr_T addr_type)
3601{
3602 if (addr_type == ADDR_NONE)
3603 emsg(_(e_norange));
3604 else
3605 emsg(_(e_invrange));
3606}
3607
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003609 * Return the default address for an address type.
3610 */
3611 static linenr_T
3612default_address(exarg_T *eap)
3613{
3614 linenr_T lnum = 0;
3615
3616 switch (eap->addr_type)
3617 {
3618 case ADDR_LINES:
3619 case ADDR_OTHER:
3620 // Default is the cursor line number. Avoid using an invalid
3621 // line number though.
3622 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3623 lnum = curbuf->b_ml.ml_line_count;
3624 else
3625 lnum = curwin->w_cursor.lnum;
3626 break;
3627 case ADDR_WINDOWS:
3628 lnum = CURRENT_WIN_NR;
3629 break;
3630 case ADDR_ARGUMENTS:
3631 lnum = curwin->w_arg_idx + 1;
3632 if (lnum > ARGCOUNT)
3633 lnum = ARGCOUNT;
3634 break;
3635 case ADDR_LOADED_BUFFERS:
3636 case ADDR_BUFFERS:
3637 lnum = curbuf->b_fnum;
3638 break;
3639 case ADDR_TABS:
3640 lnum = CURRENT_TAB_NR;
3641 break;
3642 case ADDR_TABS_RELATIVE:
3643 case ADDR_UNSIGNED:
3644 lnum = 1;
3645 break;
3646 case ADDR_QUICKFIX:
3647#ifdef FEAT_QUICKFIX
3648 lnum = qf_get_cur_idx(eap);
3649#endif
3650 break;
3651 case ADDR_QUICKFIX_VALID:
3652#ifdef FEAT_QUICKFIX
3653 lnum = qf_get_cur_valid_idx(eap);
3654#endif
3655 break;
3656 case ADDR_NONE:
3657 // Will give an error later if a range is found.
3658 break;
3659 }
3660 return lnum;
3661}
3662
3663/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003664 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 *
3666 * Set ptr to the next character after the part that was interpreted.
3667 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003668 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 *
3670 * Return MAXLNUM when no Ex address was found.
3671 */
3672 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003673get_address(
3674 exarg_T *eap UNUSED,
3675 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003676 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003677 int skip, // only skip the address, don't use it
3678 int silent, // no errors or side effects
3679 int to_other_file, // flag: may jump to other file
3680 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681{
3682 int c;
3683 int i;
3684 long n;
3685 char_u *cmd;
3686 pos_T pos;
3687 pos_T *fp;
3688 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003689 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690
3691 cmd = skipwhite(*ptr);
3692 lnum = MAXLNUM;
3693 do
3694 {
3695 switch (*cmd)
3696 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003697 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003698 ++cmd;
3699 switch (addr_type)
3700 {
3701 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003702 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 lnum = curwin->w_cursor.lnum;
3704 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003705 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003706 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003707 break;
3708 case ADDR_ARGUMENTS:
3709 lnum = curwin->w_arg_idx + 1;
3710 break;
3711 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003712 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003713 lnum = curbuf->b_fnum;
3714 break;
3715 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003716 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003717 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003718 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003719 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003720 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003721 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003722 cmd = NULL;
3723 goto error;
3724 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003725 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003726#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003727 lnum = qf_get_cur_idx(eap);
3728#endif
3729 break;
3730 case ADDR_QUICKFIX_VALID:
3731#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003732 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003733#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003734 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003735 }
3736 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003738 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003739 ++cmd;
3740 switch (addr_type)
3741 {
3742 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003743 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 lnum = curbuf->b_ml.ml_line_count;
3745 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003746 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003747 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003748 break;
3749 case ADDR_ARGUMENTS:
3750 lnum = ARGCOUNT;
3751 break;
3752 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003753 buf = lastbuf;
3754 while (buf->b_ml.ml_mfp == NULL)
3755 {
3756 if (buf->b_prev == NULL)
3757 break;
3758 buf = buf->b_prev;
3759 }
3760 lnum = buf->b_fnum;
3761 break;
3762 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003763 lnum = lastbuf->b_fnum;
3764 break;
3765 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003766 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003767 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003768 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003769 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003770 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003771 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003772 cmd = NULL;
3773 goto error;
3774 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003775 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003776#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003777 lnum = qf_get_size(eap);
3778 if (lnum == 0)
3779 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003780#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003781 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003782 case ADDR_QUICKFIX_VALID:
3783#ifdef FEAT_QUICKFIX
3784 lnum = qf_get_valid_size(eap);
3785 if (lnum == 0)
3786 lnum = 1;
3787#endif
3788 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003789 }
3790 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003792 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003793 if (*++cmd == NUL)
3794 {
3795 cmd = NULL;
3796 goto error;
3797 }
3798 if (addr_type != ADDR_LINES)
3799 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003800 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003801 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003802 goto error;
3803 }
3804 if (skip)
3805 ++cmd;
3806 else
3807 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003808 // Only accept a mark in another file when it is
3809 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003810 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3811 ++cmd;
3812 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003813 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003814 lnum = curwin->w_cursor.lnum;
3815 else
3816 {
3817 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 {
3819 cmd = NULL;
3820 goto error;
3821 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003822 lnum = fp->lnum;
3823 }
3824 }
3825 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826
3827 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003828 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003829 c = *cmd++;
3830 if (addr_type != ADDR_LINES)
3831 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003832 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003833 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003834 goto error;
3835 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003836 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003837 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003838 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003839 if (*cmd == c)
3840 ++cmd;
3841 }
3842 else
3843 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003844 int flags;
3845
3846 pos = curwin->w_cursor; // save curwin->w_cursor
3847
3848 // When '/' or '?' follows another address, start from
3849 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003850 if (lnum != MAXLNUM)
3851 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003852
3853 // Start a forward search at the end of the line (unless
3854 // before the first line).
3855 // Start a backward search at the start of the line.
3856 // This makes sure we never match in the current
3857 // line, and can match anywhere in the
3858 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003859 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003860 curwin->w_cursor.col = MAXCOL;
3861 else
3862 curwin->w_cursor.col = 0;
3863 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003864 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003865 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003866 {
3867 curwin->w_cursor = pos;
3868 cmd = NULL;
3869 goto error;
3870 }
3871 lnum = curwin->w_cursor.lnum;
3872 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003873 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003874 cmd += searchcmdlen;
3875 }
3876 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003878 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003879 ++cmd;
3880 if (addr_type != ADDR_LINES)
3881 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003882 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003883 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003884 goto error;
3885 }
3886 if (*cmd == '&')
3887 i = RE_SUBST;
3888 else if (*cmd == '?' || *cmd == '/')
3889 i = RE_SEARCH;
3890 else
3891 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003892 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003893 cmd = NULL;
3894 goto error;
3895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003897 if (!skip)
3898 {
3899 /*
3900 * When search follows another address, start from
3901 * there.
3902 */
3903 if (lnum != MAXLNUM)
3904 pos.lnum = lnum;
3905 else
3906 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003908 /*
3909 * Start the search just like for the above
3910 * do_search().
3911 */
3912 if (*cmd != '?')
3913 pos.col = MAXCOL;
3914 else
3915 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003916 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003917 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003918 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003919 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003920 lnum = pos.lnum;
3921 else
3922 {
3923 cmd = NULL;
3924 goto error;
3925 }
3926 }
3927 ++cmd;
3928 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929
3930 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003931 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003932 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 }
3934
3935 for (;;)
3936 {
3937 cmd = skipwhite(cmd);
3938 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3939 break;
3940
3941 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003942 {
3943 switch (addr_type)
3944 {
3945 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003946 case ADDR_OTHER:
3947 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003948 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003949 break;
3950 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003951 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003952 break;
3953 case ADDR_ARGUMENTS:
3954 lnum = curwin->w_arg_idx + 1;
3955 break;
3956 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003957 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003958 lnum = curbuf->b_fnum;
3959 break;
3960 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003961 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003962 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003963 case ADDR_TABS_RELATIVE:
3964 lnum = 1;
3965 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003966 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003967#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003968 lnum = qf_get_cur_idx(eap);
3969#endif
3970 break;
3971 case ADDR_QUICKFIX_VALID:
3972#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003973 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003974#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003975 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003976 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003977 case ADDR_UNSIGNED:
3978 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003979 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003980 }
3981 }
3982
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003984 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 else
3986 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003987 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 n = 1;
3989 else
3990 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003991
3992 if (addr_type == ADDR_TABS_RELATIVE)
3993 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003994 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003995 cmd = NULL;
3996 goto error;
3997 }
3998 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003999 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004000 lnum = compute_buffer_local_count(
4001 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004003 {
4004#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004005 // Relative line addressing, need to adjust for folded lines
4006 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004007 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4008 && address_count >= 2)
4009 (void)hasFolding(lnum, NULL, &lnum);
4010#endif
4011 if (i == '-')
4012 lnum -= n;
4013 else
4014 lnum += n;
4015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017 } while (*cmd == '/' || *cmd == '?');
4018
4019error:
4020 *ptr = cmd;
4021 return lnum;
4022}
4023
4024/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004025 * Set eap->line1 and eap->line2 to the whole range.
4026 * Used for commands with the EX_DFLALL flag and no range given.
4027 */
4028 static void
4029address_default_all(exarg_T *eap)
4030{
4031 eap->line1 = 1;
4032 switch (eap->addr_type)
4033 {
4034 case ADDR_LINES:
4035 case ADDR_OTHER:
4036 eap->line2 = curbuf->b_ml.ml_line_count;
4037 break;
4038 case ADDR_LOADED_BUFFERS:
4039 {
4040 buf_T *buf = firstbuf;
4041
4042 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4043 buf = buf->b_next;
4044 eap->line1 = buf->b_fnum;
4045 buf = lastbuf;
4046 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4047 buf = buf->b_prev;
4048 eap->line2 = buf->b_fnum;
4049 }
4050 break;
4051 case ADDR_BUFFERS:
4052 eap->line1 = firstbuf->b_fnum;
4053 eap->line2 = lastbuf->b_fnum;
4054 break;
4055 case ADDR_WINDOWS:
4056 eap->line2 = LAST_WIN_NR;
4057 break;
4058 case ADDR_TABS:
4059 eap->line2 = LAST_TAB_NR;
4060 break;
4061 case ADDR_TABS_RELATIVE:
4062 eap->line2 = 1;
4063 break;
4064 case ADDR_ARGUMENTS:
4065 if (ARGCOUNT == 0)
4066 eap->line1 = eap->line2 = 0;
4067 else
4068 eap->line2 = ARGCOUNT;
4069 break;
4070 case ADDR_QUICKFIX_VALID:
4071#ifdef FEAT_QUICKFIX
4072 eap->line2 = qf_get_valid_size(eap);
4073 if (eap->line2 == 0)
4074 eap->line2 = 1;
4075#endif
4076 break;
4077 case ADDR_NONE:
4078 case ADDR_UNSIGNED:
4079 case ADDR_QUICKFIX:
4080 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4081 break;
4082 }
4083}
4084
4085
4086/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004087 * Get flags from an Ex command argument.
4088 */
4089 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004090get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004091{
4092 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4093 {
4094 if (*eap->arg == 'l')
4095 eap->flags |= EXFLAG_LIST;
4096 else if (*eap->arg == 'p')
4097 eap->flags |= EXFLAG_PRINT;
4098 else
4099 eap->flags |= EXFLAG_NR;
4100 eap->arg = skipwhite(eap->arg + 1);
4101 }
4102}
4103
4104/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 * Function called for command which is Not Implemented. NI!
4106 */
4107 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004108ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109{
4110 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004111 eap->errmsg =
4112 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113}
4114
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004115#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116/*
4117 * Function called for script command which is Not Implemented. NI!
4118 * Skips over ":perl <<EOF" constructs.
4119 */
4120 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004121ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122{
4123 if (!eap->skip)
4124 ex_ni(eap);
4125 else
4126 vim_free(script_get(eap, eap->arg));
4127}
4128#endif
4129
4130/*
4131 * Check range in Ex command for validity.
4132 * Return NULL when valid, error message when invalid.
4133 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004134 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004135invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004137 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004138
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 if ( eap->line1 < 0
4140 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004141 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004142 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004143
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004144 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004145 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004146 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004147 {
4148 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004149 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004150#ifdef FEAT_DIFF
4151 + (eap->cmdidx == CMD_diffget)
4152#endif
4153 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004154 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004155 break;
4156 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004157 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004158 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004159 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004160 break;
4161 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004162 // Only a boundary check, not whether the buffers actually
4163 // exist.
4164 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004165 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004166 break;
4167 case ADDR_LOADED_BUFFERS:
4168 buf = firstbuf;
4169 while (buf->b_ml.ml_mfp == NULL)
4170 {
4171 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004172 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004173 buf = buf->b_next;
4174 }
4175 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004176 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004177 buf = lastbuf;
4178 while (buf->b_ml.ml_mfp == NULL)
4179 {
4180 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004181 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004182 buf = buf->b_prev;
4183 }
4184 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004185 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004186 break;
4187 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004188 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004189 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004190 break;
4191 case ADDR_TABS:
4192 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004193 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004194 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004195 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004196 case ADDR_OTHER:
4197 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004198 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004199 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004200#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004201 // No error for value that is too big, will use the last entry.
4202 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004203 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004204#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004205 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004206 case ADDR_QUICKFIX_VALID:
4207#ifdef FEAT_QUICKFIX
4208 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4209 || eap->line2 < 0)
4210 return _(e_invrange);
4211#endif
4212 break;
4213 case ADDR_UNSIGNED:
4214 if (eap->line2 < 0)
4215 return _(e_invrange);
4216 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004217 case ADDR_NONE:
4218 // Will give an error elsewhere.
4219 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004220 }
4221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 return NULL;
4223}
4224
4225/*
4226 * Correct the range for zero line number, if required.
4227 */
4228 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004229correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004231 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 {
4233 if (eap->line1 == 0)
4234 eap->line1 = 1;
4235 if (eap->line2 == 0)
4236 eap->line2 = 1;
4237 }
4238}
4239
Bram Moolenaar748bf032005-02-02 23:04:36 +00004240#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004241/*
4242 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4243 * pattern. Otherwise return eap->arg.
4244 */
4245 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004246skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004247{
4248 char_u *p = eap->arg;
4249
Bram Moolenaara37420f2006-02-04 22:37:47 +00004250 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4251 || eap->cmdidx == CMD_vimgrepadd
4252 || eap->cmdidx == CMD_lvimgrepadd
4253 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004254 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004255 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004256 if (p == NULL)
4257 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004258 }
4259 return p;
4260}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004261
4262/*
4263 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4264 * in the command line, so that things like % get expanded.
4265 */
4266 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004267replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004268{
4269 char_u *new_cmdline;
4270 char_u *program;
4271 char_u *pos;
4272 char_u *ptr;
4273 int len;
4274 int i;
4275
4276 /*
4277 * Don't do it when ":vimgrep" is used for ":grep".
4278 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004279 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4280 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4281 || eap->cmdidx == CMD_grepadd
4282 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004283 && !grep_internal(eap->cmdidx))
4284 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004285 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4286 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004287 {
4288 if (*curbuf->b_p_gp == NUL)
4289 program = p_gp;
4290 else
4291 program = curbuf->b_p_gp;
4292 }
4293 else
4294 {
4295 if (*curbuf->b_p_mp == NUL)
4296 program = p_mp;
4297 else
4298 program = curbuf->b_p_mp;
4299 }
4300
4301 p = skipwhite(p);
4302
4303 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4304 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004305 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004306 i = 1;
4307 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4308 ++i;
4309 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004310 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004311 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004312 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004313 ptr = new_cmdline;
4314 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4315 {
4316 i = (int)(pos - program);
4317 STRNCPY(ptr, program, i);
4318 STRCPY(ptr += i, p);
4319 ptr += len;
4320 program = pos + 2;
4321 }
4322 STRCPY(ptr, program);
4323 }
4324 else
4325 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004326 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004327 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004328 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004329 STRCPY(new_cmdline, program);
4330 STRCAT(new_cmdline, " ");
4331 STRCAT(new_cmdline, p);
4332 }
4333 msg_make(p);
4334
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004335 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004336 vim_free(*cmdlinep);
4337 *cmdlinep = new_cmdline;
4338 p = new_cmdline;
4339 }
4340 return p;
4341}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004342#endif
4343
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344/*
4345 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004346 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 * Return FAIL for failure, OK otherwise.
4348 */
4349 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004350expand_filename(
4351 exarg_T *eap,
4352 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004353 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004355 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 char_u *repl;
4357 int srclen;
4358 char_u *p;
4359 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004360 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361
Bram Moolenaar748bf032005-02-02 23:04:36 +00004362#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004363 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004364 p = skip_grep_pat(eap);
4365#else
4366 p = eap->arg;
4367#endif
4368
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 /*
4370 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4371 * the file name contains a wildcard it should not cause expanding.
4372 * (it will be expanded anyway if there is a wildcard before replacing).
4373 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004374 has_wildcards = mch_has_wildcard(p);
4375 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004377#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004378 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004379 if (p[0] == '`' && p[1] == '=')
4380 {
4381 p += 2;
4382 (void)skip_expr(&p);
4383 if (*p == '`')
4384 ++p;
4385 continue;
4386 }
4387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 /*
4389 * Quick check if this cannot be the start of a special string.
4390 * Also removes backslash before '%', '#' and '<'.
4391 */
4392 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4393 {
4394 ++p;
4395 continue;
4396 }
4397
4398 /*
4399 * Try to find a match at this position.
4400 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004401 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4402 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004403 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004405 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 {
4407 p += srclen;
4408 continue;
4409 }
4410
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004411 // Wildcards won't be expanded below, the replacement is taken
4412 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004413 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4414 {
4415 char_u *l = repl;
4416
4417 repl = expand_env_save(repl);
4418 vim_free(l);
4419 }
4420
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004421 // Need to escape white space et al. with a backslash.
4422 // Don't do this for:
4423 // - replacement that already has been escaped: "##"
4424 // - shell commands (may have to use quotes instead).
4425 // - non-unix systems when there is a single argument (spaces don't
4426 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004428 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 && eap->cmdidx != CMD_grep
4431 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004432 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004433 && eap->cmdidx != CMD_lgrep
4434 && eap->cmdidx != CMD_lgrepadd
4435 && eap->cmdidx != CMD_lmake
4436 && eap->cmdidx != CMD_make
4437 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004439 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440#endif
4441 )
4442 {
4443 char_u *l;
4444#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004445 // Don't escape a backslash here, because rem_backslash() doesn't
4446 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 static char_u *nobslash = (char_u *)" \t\"|";
4448# define ESCAPE_CHARS nobslash
4449#else
4450# define ESCAPE_CHARS escape_chars
4451#endif
4452
4453 for (l = repl; *l; ++l)
4454 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4455 {
4456 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4457 if (l != NULL)
4458 {
4459 vim_free(repl);
4460 repl = l;
4461 }
4462 break;
4463 }
4464 }
4465
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004466 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004467 if ((eap->usefilter || eap->cmdidx == CMD_bang
4468 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004469 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 {
4471 char_u *l;
4472
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004473 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 if (l != NULL)
4475 {
4476 vim_free(repl);
4477 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 }
4479 }
4480
4481 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4482 vim_free(repl);
4483 if (p == NULL)
4484 return FAIL;
4485 }
4486
4487 /*
4488 * One file argument: Expand wildcards.
4489 * Don't do this with ":r !command" or ":w !command".
4490 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004491 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 {
4493 /*
4494 * May do this twice:
4495 * 1. Replace environment variables.
4496 * 2. Replace any other wildcards, remove backslashes.
4497 */
4498 for (n = 1; n <= 2; ++n)
4499 {
4500 if (n == 2)
4501 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 /*
4503 * Halve the number of backslashes (this is Vi compatible).
4504 * For Unix and OS/2, when wildcards are expanded, this is
4505 * done by ExpandOne() below.
4506 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004507#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 if (!has_wildcards)
4509#endif
4510 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 }
4512
4513 if (has_wildcards)
4514 {
4515 if (n == 1)
4516 {
4517 /*
4518 * First loop: May expand environment variables. This
4519 * can be done much faster with expand_env() than with
4520 * something else (e.g., calling a shell).
4521 * After expanding environment variables, check again
4522 * if there are still wildcards present.
4523 */
4524 if (vim_strchr(eap->arg, '$') != NULL
4525 || vim_strchr(eap->arg, '~') != NULL)
4526 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004527 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004528 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 has_wildcards = mch_has_wildcard(NameBuff);
4530 p = NameBuff;
4531 }
4532 else
4533 p = NULL;
4534 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004535 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 {
4537 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004538 int options = WILD_LIST_NOTFOUND
4539 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540
4541 ExpandInit(&xpc);
4542 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004543 if (p_wic)
4544 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004546 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 if (p == NULL)
4548 return FAIL;
4549 }
4550 if (p != NULL)
4551 {
4552 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4553 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004554 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 vim_free(p);
4556 }
4557 }
4558 }
4559 }
4560 return OK;
4561}
4562
4563/*
4564 * Replace part of the command line, keeping eap->cmd, eap->arg and
4565 * eap->nextcmd correct.
4566 * "src" points to the part that is to be replaced, of length "srclen".
4567 * "repl" is the replacement string.
4568 * Returns a pointer to the character after the replaced string.
4569 * Returns NULL for failure.
4570 */
4571 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004572repl_cmdline(
4573 exarg_T *eap,
4574 char_u *src,
4575 int srclen,
4576 char_u *repl,
4577 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578{
4579 int len;
4580 int i;
4581 char_u *new_cmdline;
4582
4583 /*
4584 * The new command line is build in new_cmdline[].
4585 * First allocate it.
4586 * Careful: a "+cmd" argument may have been NUL terminated.
4587 */
4588 len = (int)STRLEN(repl);
4589 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004590 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004591 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004592 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004593 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594
4595 /*
4596 * Copy the stuff before the expanded part.
4597 * Copy the expanded stuff.
4598 * Copy what came after the expanded part.
4599 * Copy the next commands, if there are any.
4600 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004601 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004603
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004605 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004607 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004609 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 {
4611 i = (int)STRLEN(new_cmdline) + 1;
4612 STRCPY(new_cmdline + i, eap->nextcmd);
4613 eap->nextcmd = new_cmdline + i;
4614 }
4615 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4616 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4617 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4618 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4619 vim_free(*cmdlinep);
4620 *cmdlinep = new_cmdline;
4621
4622 return src;
4623}
4624
4625/*
4626 * Check for '|' to separate commands and '"' to start comments.
4627 */
4628 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004629separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630{
4631 char_u *p;
4632
Bram Moolenaar86b68352004-12-27 21:59:20 +00004633#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004634 p = skip_grep_pat(eap);
4635#else
4636 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004637#endif
4638
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004639 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 {
4641 if (*p == Ctrl_V)
4642 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004643 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004644 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004646 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004647 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004648 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 break;
4650 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004651
4652#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004653 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004654 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004655 {
4656 p += 2;
4657 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004658 }
4659#endif
4660
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004661 // Check for '"': start of comment or '|': next command
4662 // :@" and :*" do not start a comment!
4663 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004664 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004665#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004666 && !in_vim9script()
4667#endif
4668 && !(eap->argt & EX_NOTRLCOM)
4669 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4670 || p != eap->arg)
4671 && (eap->cmdidx != CMD_redir
4672 || p != eap->arg + 1 || p[-1] != '@'))
4673#ifdef FEAT_EVAL
4674 || (*p == '#'
4675 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004676 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004677 && p[1] != '{'
4678 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 || *p == '|' || *p == '\n')
4681 {
4682 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004683 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 * AND 'b' is present in 'cpoptions'.
4685 */
4686 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004687 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004689 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 --p;
4691 }
4692 else
4693 {
4694 eap->nextcmd = check_nextcmd(p);
4695 *p = NUL;
4696 break;
4697 }
4698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004700
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004701 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 del_trailing_spaces(eap->arg);
4703}
4704
4705/*
4706 * get + command from ex argument
4707 */
4708 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004709getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710{
4711 char_u *arg = *argp;
4712 char_u *command = NULL;
4713
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004714 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 {
4716 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004717 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 command = dollar_command;
4719 else
4720 {
4721 command = arg;
4722 arg = skip_cmd_arg(command, TRUE);
4723 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004724 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 }
4726
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004727 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 *argp = arg;
4729 }
4730 return command;
4731}
4732
4733/*
4734 * Find end of "+command" argument. Skip over "\ " and "\\".
4735 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004736 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004737skip_cmd_arg(
4738 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004739 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740{
4741 while (*p && !vim_isspace(*p))
4742 {
4743 if (*p == '\\' && p[1] != NUL)
4744 {
4745 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004746 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 else
4748 ++p;
4749 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004750 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 }
4752 return p;
4753}
4754
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004755 int
4756get_bad_opt(char_u *p, exarg_T *eap)
4757{
4758 if (STRICMP(p, "keep") == 0)
4759 eap->bad_char = BAD_KEEP;
4760 else if (STRICMP(p, "drop") == 0)
4761 eap->bad_char = BAD_DROP;
4762 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4763 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004764 else
4765 return FAIL;
4766 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004767}
4768
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769/*
4770 * Get "++opt=arg" argument.
4771 * Return FAIL or OK.
4772 */
4773 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004774getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775{
4776 char_u *arg = eap->arg + 2;
4777 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004778 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004781 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4783 {
4784 if (*arg == 'n')
4785 {
4786 arg += 2;
4787 eap->force_bin = FORCE_NOBIN;
4788 }
4789 else
4790 eap->force_bin = FORCE_BIN;
4791 if (!checkforcmd(&arg, "binary", 3))
4792 return FAIL;
4793 eap->arg = skipwhite(arg);
4794 return OK;
4795 }
4796
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004797 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004798 if (STRNCMP(arg, "edit", 4) == 0)
4799 {
4800 eap->read_edit = TRUE;
4801 eap->arg = skipwhite(arg + 4);
4802 return OK;
4803 }
4804
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 if (STRNCMP(arg, "ff", 2) == 0)
4806 {
4807 arg += 2;
4808 pp = &eap->force_ff;
4809 }
4810 else if (STRNCMP(arg, "fileformat", 10) == 0)
4811 {
4812 arg += 10;
4813 pp = &eap->force_ff;
4814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 else if (STRNCMP(arg, "enc", 3) == 0)
4816 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004817 if (STRNCMP(arg, "encoding", 8) == 0)
4818 arg += 8;
4819 else
4820 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 pp = &eap->force_enc;
4822 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004823 else if (STRNCMP(arg, "bad", 3) == 0)
4824 {
4825 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004826 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828
4829 if (pp == NULL || *arg != '=')
4830 return FAIL;
4831
4832 ++arg;
4833 *pp = (int)(arg - eap->cmd);
4834 arg = skip_cmd_arg(arg, FALSE);
4835 eap->arg = skipwhite(arg);
4836 *arg = NUL;
4837
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 if (pp == &eap->force_ff)
4839 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4841 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004842 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004844 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004846 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4848 *p = TOLOWER_ASC(*p);
4849 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004850 else
4851 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004852 // Check ++bad= argument. Must be a single-byte character, "keep" or
4853 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004854 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004855 return FAIL;
4856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857
4858 return OK;
4859}
4860
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004862ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863{
4864 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004865 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 * directory for security reasons.
4867 */
4868 if (secure)
4869 {
4870 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004871 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 }
4873 else if (eap->cmdidx == CMD_autocmd)
4874 do_autocmd(eap->arg, eap->forceit);
4875 else
4876 do_augroup(eap->arg, eap->forceit);
4877}
4878
4879/*
4880 * ":doautocmd": Apply the automatic commands to the current buffer.
4881 */
4882 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004883ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004885 char_u *arg = eap->arg;
4886 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004887 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004888
Bram Moolenaar1610d052016-06-09 22:53:01 +02004889 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004890 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004891 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004892 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895/*
4896 * :[N]bunload[!] [N] [bufname] unload buffer
4897 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4898 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4899 */
4900 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004901ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004903 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004904 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 eap->errmsg = do_bufdel(
4906 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4907 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4908 : DOBUF_UNLOAD, eap->arg,
4909 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4910}
4911
4912/*
4913 * :[N]buffer [N] to buffer N
4914 * :[N]sbuffer [N] to buffer N
4915 */
4916 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004917ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004919 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004920 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004922 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 else
4924 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004925 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4927 else
4928 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004929 if (eap->do_ecmd_cmd != NULL)
4930 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 }
4932}
4933
4934/*
4935 * :[N]bmodified [N] to next mod. buffer
4936 * :[N]sbmodified [N] to next mod. buffer
4937 */
4938 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004939ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940{
4941 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004942 if (eap->do_ecmd_cmd != NULL)
4943 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944}
4945
4946/*
4947 * :[N]bnext [N] to next buffer
4948 * :[N]sbnext [N] split and to next buffer
4949 */
4950 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004951ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004953 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004954 return;
4955
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004957 if (eap->do_ecmd_cmd != NULL)
4958 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959}
4960
4961/*
4962 * :[N]bNext [N] to previous buffer
4963 * :[N]bprevious [N] to previous buffer
4964 * :[N]sbNext [N] split and to previous buffer
4965 * :[N]sbprevious [N] split and to previous buffer
4966 */
4967 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004968ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004970 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004971 return;
4972
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004974 if (eap->do_ecmd_cmd != NULL)
4975 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976}
4977
4978/*
4979 * :brewind to first buffer
4980 * :bfirst to first buffer
4981 * :sbrewind split and to first buffer
4982 * :sbfirst split and to first buffer
4983 */
4984 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004985ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004987 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004988 return;
4989
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004991 if (eap->do_ecmd_cmd != NULL)
4992 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993}
4994
4995/*
4996 * :blast to last buffer
4997 * :sblast split and to last buffer
4998 */
4999 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005000ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005002 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005003 return;
5004
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005006 if (eap->do_ecmd_cmd != NULL)
5007 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009
Bram Moolenaar7a092242020-04-16 22:10:49 +02005010/*
5011 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02005012 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005015ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005017 int comment_char = '"';
5018
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005019#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005020 if (in_vim9script())
5021 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005022#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005023 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005024}
5025
5026/*
5027 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5028 * to "cmd_start" or has a white space character before it.
5029 */
5030 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005031ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005032{
5033 int c = *cmd;
5034
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005035 if (c == NUL || c == '|' || c == '\n')
5036 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005037#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005038 if (in_vim9script())
5039 return c == '#' && cmd[1] != '{'
5040 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005041#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005042 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043}
5044
5045#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5046 || defined(PROTO)
5047/*
5048 * Return the next command, after the first '|' or '\n'.
5049 * Return NULL if not found.
5050 */
5051 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005052find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053{
5054 while (*p != '|' && *p != '\n')
5055 {
5056 if (*p == NUL)
5057 return NULL;
5058 ++p;
5059 }
5060 return (p + 1);
5061}
5062#endif
5063
5064/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005065 * Check if *p is a separator between Ex commands, skipping over white space.
5066 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 */
5068 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005069check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005071 char_u *s = skipwhite(p);
5072
5073 if (*s == '|' || *s == '\n')
5074 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 else
5076 return NULL;
5077}
5078
5079/*
5080 * - if there are more files to edit
5081 * - and this is the last window
5082 * - and forceit not used
5083 * - and not repeated twice on a row
5084 * return FAIL and give error message if 'message' TRUE
5085 * return OK otherwise
5086 */
5087 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005088check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005089 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005090 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091{
5092 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5093
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005094 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005095 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 {
5097 if (message)
5098 {
5099#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5100 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5101 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005102 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005104 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5105 NGETTEXT("%d more file to edit. Quit anyway?",
5106 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5108 return OK;
5109 return FAIL;
5110 }
5111#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005112 semsg(NGETTEXT("E173: %d more file to edit",
5113 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005114 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 }
5116 return FAIL;
5117 }
5118 return OK;
5119}
5120
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121/*
5122 * Function given to ExpandGeneric() to obtain the list of command names.
5123 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005125get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126{
5127 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 return cmdnames[idx].cmd_name;
5130}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005133ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134{
Bram Moolenaare6850792010-05-14 15:28:44 +02005135 if (*eap->arg == NUL)
5136 {
5137#ifdef FEAT_EVAL
5138 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5139 char_u *p = NULL;
5140
5141 if (expr != NULL)
5142 {
5143 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005144 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005145 --emsg_off;
5146 vim_free(expr);
5147 }
5148 if (p != NULL)
5149 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005150 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005151 vim_free(p);
5152 }
5153 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005154 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005155#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005156 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005157#endif
5158 }
5159 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005160 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005161
5162#ifdef FEAT_VTP
5163 else if (has_vtp_working())
5164 {
5165 // background color change requires clear + redraw
5166 update_screen(CLEAR);
5167 redrawcmd();
5168 }
5169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170}
5171
5172 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005173ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174{
5175 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005176 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 do_highlight(eap->arg, eap->forceit, FALSE);
5178}
5179
5180
5181/*
5182 * Call this function if we thought we were going to exit, but we won't
5183 * (because of an error). May need to restore the terminal mode.
5184 */
5185 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005186not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187{
5188 exiting = FALSE;
5189 settmode(TMODE_RAW);
5190}
5191
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005192 static int
5193before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5194{
5195 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5196
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005197 // Bail out when autocommands closed the window.
5198 // Refuse to quit when the buffer in the last window is being closed (can
5199 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005200 if (!win_valid(wp)
5201 || curbuf_locked()
5202 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5203 return TRUE;
5204
5205 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5206 {
5207 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005208 // Refuse to quit when locked or when the window was closed or the
5209 // buffer in the last window is being closed (can only happen in
5210 // autocommands).
5211 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005212 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5213 return TRUE;
5214 }
5215
5216 return FALSE;
5217}
5218
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005220 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005221 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005222 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005224 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005225ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005227 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005228
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229#ifdef FEAT_CMDWIN
5230 if (cmdwin_type != 0)
5231 {
5232 cmdwin_result = Ctrl_C;
5233 return;
5234 }
5235#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005236 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005237 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005238 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005239 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005240 return;
5241 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005242 if (eap->addr_count > 0)
5243 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005244 int wnr = eap->line2;
5245
5246 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5247 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005248 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005249 }
5250 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005251 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005252
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005253 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005254 if (curbuf_locked())
5255 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005256
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005257 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005258 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005259 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260
5261#ifdef FEAT_NETBEANS_INTG
5262 netbeansForcedQuit = eap->forceit;
5263#endif
5264
5265 /*
5266 * If there are more files or windows we won't exit.
5267 */
5268 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5269 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005270 if ((!buf_hide(wp->w_buffer)
5271 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005272 | (eap->forceit ? CCGD_FORCEIT : 0)
5273 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005275 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 {
5277 not_exiting();
5278 }
5279 else
5280 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005281 // quit last window
5282 // Note: only_one_window() returns true, even so a help window is
5283 // still open. In that case only quit, if no address has been
5284 // specified. Example:
5285 // :h|wincmd w|1q - don't quit
5286 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005287 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005289 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005290#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005292#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005293 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005294 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 }
5296}
5297
5298/*
5299 * ":cquit".
5300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005302ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005304 // this does not always pass on the exit code to the Manx compiler. why?
5305 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306}
5307
5308/*
5309 * ":qall": try to quit all windows
5310 */
5311 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005312ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313{
5314# ifdef FEAT_CMDWIN
5315 if (cmdwin_type != 0)
5316 {
5317 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005318 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 else
5320 cmdwin_result = K_XF2;
5321 return;
5322 }
5323# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005324
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005325 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005326 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005327 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005328 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005329 return;
5330 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005331
5332 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005333 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005334
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005336 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 getout(0);
5338 not_exiting();
5339}
5340
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341/*
5342 * ":close": close current window, unless it is the last one
5343 */
5344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005345ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005347 win_T *win;
5348 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005349#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005351 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005353#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005354 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005355 {
5356 if (eap->addr_count == 0)
5357 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005358 else
5359 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005360 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005361 {
5362 winnr++;
5363 if (winnr == eap->line2)
5364 break;
5365 }
5366 if (win == NULL)
5367 win = lastwin;
5368 ex_win_close(eap->forceit, win, NULL);
5369 }
5370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371}
5372
Bram Moolenaar4033c552017-09-16 20:54:51 +02005373#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005374/*
5375 * ":pclose": Close any preview window.
5376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005378ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005379{
5380 win_T *win;
5381
Bram Moolenaar79648732019-07-18 21:43:07 +02005382 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005383 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005384 if (win->w_p_pvw)
5385 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005386 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005387 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005388 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005389# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005390 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005391 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005392# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005393}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005394#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005395
Bram Moolenaarf740b292006-02-16 22:11:02 +00005396/*
5397 * Close window "win" and take care of handling closing the last window for a
5398 * modified buffer.
5399 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005400 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005401ex_win_close(
5402 int forceit,
5403 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005404 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405{
5406 int need_hide;
5407 buf_T *buf = win->w_buffer;
5408
Bram Moolenaarcf844172020-06-26 19:44:06 +02005409 // Never close the autocommand window.
5410 if (win == aucmd_win)
5411 {
5412 emsg(_(e_autocmd_close));
5413 return;
5414 }
5415
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005417 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005419#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 if ((p_confirm || cmdmod.confirm) && p_write)
5421 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005422 bufref_T bufref;
5423
5424 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005426 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 return;
5428 need_hide = FALSE;
5429 }
5430 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005433 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 return;
5435 }
5436 }
5437
Bram Moolenaar4033c552017-09-16 20:54:51 +02005438#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005440#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005441
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005442 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005443 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005444 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005445 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005446 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447}
5448
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005450 * Handle the argument for a tabpage related ex command.
5451 * Returns a tabpage number.
5452 * When an error is encountered then eap->errmsg is set.
5453 */
5454 static int
5455get_tabpage_arg(exarg_T *eap)
5456{
5457 int tab_number;
5458 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5459
5460 if (eap->arg && *eap->arg != NUL)
5461 {
5462 char_u *p = eap->arg;
5463 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005464 int relative = 0; // argument +N/-N means: go to N places to the
5465 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005466
5467 if (*p == '-')
5468 {
5469 relative = -1;
5470 p++;
5471 }
5472 else if (*p == '+')
5473 {
5474 relative = 1;
5475 p++;
5476 }
5477
5478 p_save = p;
5479 tab_number = getdigits(&p);
5480
5481 if (relative == 0)
5482 {
5483 if (STRCMP(p, "$") == 0)
5484 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005485 else if (STRCMP(p, "#") == 0)
5486 if (valid_tabpage(lastused_tabpage))
5487 tab_number = tabpage_index(lastused_tabpage);
5488 else
5489 {
5490 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5491 tab_number = 0;
5492 goto theend;
5493 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005494 else if (p == p_save || *p_save == '-' || *p != NUL
5495 || tab_number > LAST_TAB_NR)
5496 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005497 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005498 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005499 goto theend;
5500 }
5501 }
5502 else
5503 {
5504 if (*p_save == NUL)
5505 tab_number = 1;
5506 else if (p == p_save || *p_save == '-' || *p != NUL
5507 || tab_number == 0)
5508 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005509 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005510 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005511 goto theend;
5512 }
5513 tab_number = tab_number * relative + tabpage_index(curtab);
5514 if (!unaccept_arg0 && relative == -1)
5515 --tab_number;
5516 }
5517 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005518 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005519 }
5520 else if (eap->addr_count > 0)
5521 {
5522 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005523 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005524 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005525 tab_number = 0;
5526 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005527 else
5528 {
5529 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005530 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005531 {
5532 --tab_number;
5533 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005534 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005535 }
5536 }
5537 }
5538 else
5539 {
5540 switch (eap->cmdidx)
5541 {
5542 case CMD_tabnext:
5543 tab_number = tabpage_index(curtab) + 1;
5544 if (tab_number > LAST_TAB_NR)
5545 tab_number = 1;
5546 break;
5547 case CMD_tabmove:
5548 tab_number = LAST_TAB_NR;
5549 break;
5550 default:
5551 tab_number = tabpage_index(curtab);
5552 }
5553 }
5554
5555theend:
5556 return tab_number;
5557}
5558
5559/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005560 * ":tabclose": close current tab page, unless it is the last one.
5561 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 */
5563 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005564ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005566 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005567 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005568
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005569# ifdef FEAT_CMDWIN
5570 if (cmdwin_type != 0)
5571 cmdwin_result = K_IGNORE;
5572 else
5573# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005574 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005575 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005576 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005578 tab_number = get_tabpage_arg(eap);
5579 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005580 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005581 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005582 if (tp == NULL)
5583 {
5584 beep_flush();
5585 return;
5586 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005587 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005588 {
5589 tabpage_close_other(tp, eap->forceit);
5590 return;
5591 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005592 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005593 tabpage_close(eap->forceit);
5594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005596}
5597
5598/*
5599 * ":tabonly": close all tab pages except the current one
5600 */
5601 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005602ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005603{
5604 tabpage_T *tp;
5605 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005606 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005607
5608# ifdef FEAT_CMDWIN
5609 if (cmdwin_type != 0)
5610 cmdwin_result = K_IGNORE;
5611 else
5612# endif
5613 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005614 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005615 else
5616 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005617 tab_number = get_tabpage_arg(eap);
5618 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005619 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005620 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005621 // Repeat this up to a 1000 times, because autocommands may
5622 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005623 for (done = 0; done < 1000; ++done)
5624 {
5625 FOR_ALL_TABPAGES(tp)
5626 if (tp->tp_topframe != topframe)
5627 {
5628 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005629 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005630 if (valid_tabpage(tp))
5631 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005632 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005633 break;
5634 }
5635 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005636 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005637 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005638 }
5639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641
5642/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005643 * Close the current tab page.
5644 */
5645 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005646tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005647{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005648 // First close all the windows but the current one. If that worked then
5649 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005650 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005651 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005652 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005653 ex_win_close(forceit, curwin, NULL);
5654# ifdef FEAT_GUI
5655 need_mouse_correct = TRUE;
5656# endif
5657}
5658
5659/*
5660 * Close tab page "tp", which is not the current tab page.
5661 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005662 * Also takes care of the tab pages line disappearing when closing the
5663 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005664 */
5665 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005666tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005667{
5668 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005669 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005670 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005671
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005672 // Limit to 1000 windows, autocommands may add a window while we close
5673 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005674 while (++done < 1000)
5675 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005676 wp = tp->tp_firstwin;
5677 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005678
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005679 // Autocommands may delete the tab page under our fingers and we may
5680 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005681 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005682 break;
5683 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005684
Bram Moolenaar29323592016-07-24 22:04:11 +02005685 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005686
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005687 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005688 if (h != tabline_height())
5689 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005690}
5691
5692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 * ":only".
5694 */
5695 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005696ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005698 win_T *wp;
5699 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700# ifdef FEAT_GUI
5701 need_mouse_correct = TRUE;
5702# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005703 if (eap->addr_count > 0)
5704 {
5705 wnr = eap->line2;
5706 for (wp = firstwin; --wnr > 0; )
5707 {
5708 if (wp->w_next == NULL)
5709 break;
5710 else
5711 wp = wp->w_next;
5712 }
5713 win_goto(wp);
5714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 close_others(TRUE, eap->forceit);
5716}
5717
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005719ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005721 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005722 if (!eap->skip)
5723 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005724#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005725 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005726#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005727 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005728 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005729 else
5730 {
5731 int winnr = 0;
5732 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005733
Bram Moolenaar2256c992016-11-15 21:17:07 +01005734 FOR_ALL_WINDOWS(win)
5735 {
5736 winnr++;
5737 if (winnr == eap->line2)
5738 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005739 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005740 if (win == NULL)
5741 win = lastwin;
5742 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 }
5745}
5746
5747/*
5748 * ":stop" and ":suspend": Suspend Vim.
5749 */
5750 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005751ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752{
5753 /*
5754 * Disallow suspending for "rvim".
5755 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005756 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 {
5758 if (!eap->forceit)
5759 autowrite_all();
5760 windgoto((int)Rows - 1, 0);
5761 out_char('\n');
5762 out_flush();
5763 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005764 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005766 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005768 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769#ifdef FEAT_TITLE
5770 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005771 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772#endif
5773 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005774 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005776 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 }
5778}
5779
5780/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005781 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 */
5783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005784ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785{
Bram Moolenaar461f2122020-07-28 20:25:47 +02005786#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02005787 if (not_in_vim9(eap) == FAIL)
5788 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02005789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790#ifdef FEAT_CMDWIN
5791 if (cmdwin_type != 0)
5792 {
5793 cmdwin_result = Ctrl_C;
5794 return;
5795 }
5796#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005797 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005798 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005799 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005800 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005801 return;
5802 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005803
5804 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005805 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806
5807 /*
5808 * if more files or windows we won't exit
5809 */
5810 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5811 exiting = TRUE;
5812 if ( ((eap->cmdidx == CMD_wq
5813 || curbufIsChanged())
5814 && do_write(eap) == FAIL)
5815 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005816 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 {
5818 not_exiting();
5819 }
5820 else
5821 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005822 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005824 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825# ifdef FEAT_GUI
5826 need_mouse_correct = TRUE;
5827# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005828 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005829 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 }
5831}
5832
5833/*
5834 * ":print", ":list", ":number".
5835 */
5836 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005837ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005839 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005840 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005841 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005843 for ( ;!got_int; ui_breakcheck())
5844 {
5845 print_line(eap->line1,
5846 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5847 || (eap->flags & EXFLAG_NR)),
5848 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5849 if (++eap->line1 > eap->line2)
5850 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005851 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005852 }
5853 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005854 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005855 curwin->w_cursor.lnum = eap->line2;
5856 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 }
5858
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860}
5861
5862#ifdef FEAT_BYTEOFF
5863 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005864ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865{
5866 goto_byte(eap->line2);
5867}
5868#endif
5869
5870/*
5871 * ":shell".
5872 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005874ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875{
5876 do_shell(NULL, 0);
5877}
5878
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005879#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005881static int drop_busy = FALSE;
5882static int drop_filec;
5883static char_u **drop_filev = NULL;
5884static int drop_split;
5885static void (*drop_callback)(void *);
5886static void *drop_cookie;
5887
5888 static void
5889handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890{
5891 exarg_T ea;
5892 int save_msg_scroll = msg_scroll;
5893
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005894 // Setting the argument list may cause screen updates and being called
5895 // recursively. Avoid that by setting drop_busy.
5896 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005898 // Check whether the current buffer is changed. If so, we will need
5899 // to split the current window or data could be lost.
5900 // We don't need to check if the 'hidden' option is set, as in this
5901 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005902 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 {
5904 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005905 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 --emsg_off;
5907 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005908 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 if (win_split(0, 0) == FAIL)
5911 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005912 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005914 // When splitting the window, create a new alist. Otherwise the
5915 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 alist_unlink(curwin->w_alist);
5917 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 }
5919
5920 /*
5921 * Set up the new argument list.
5922 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005923 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924
5925 /*
5926 * Move to the first file.
5927 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005928 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005929 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 ea.cmd = (char_u *)"next";
5931 do_argfile(&ea, 0);
5932
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005933 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5934 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 need_start_insertmode = FALSE;
5936
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005937 // Restore msg_scroll, otherwise a following command may cause scrolling
5938 // unexpectedly. The screen will be redrawn by the caller, thus
5939 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005941
5942 if (drop_callback != NULL)
5943 drop_callback(drop_cookie);
5944
5945 drop_filev = NULL;
5946 drop_busy = FALSE;
5947}
5948
5949/*
5950 * Handle a file drop. The code is here because a drop is *nearly* like an
5951 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005952 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005953 * code is very similar to :args and hence needs access to a lot of the static
5954 * functions in this file.
5955 *
5956 * The "filev" list must have been allocated using alloc(), as should each item
5957 * in the list. This function takes over responsibility for freeing the "filev"
5958 * list.
5959 */
5960 void
5961handle_drop(
5962 int filec, // the number of files dropped
5963 char_u **filev, // the list of files dropped
5964 int split, // force splitting the window
5965 void (*callback)(void *), // to be called after setting the argument
5966 // list
5967 void *cookie) // argument for "callback" (allocated)
5968{
5969 // Cannot handle recursive drops, finish the pending one.
5970 if (drop_busy)
5971 {
5972 FreeWild(filec, filev);
5973 vim_free(cookie);
5974 return;
5975 }
5976
5977 // When calling handle_drop() more than once in a row we only use the last
5978 // one.
5979 if (drop_filev != NULL)
5980 {
5981 FreeWild(drop_filec, drop_filev);
5982 vim_free(drop_cookie);
5983 }
5984
5985 drop_filec = filec;
5986 drop_filev = filev;
5987 drop_split = split;
5988 drop_callback = callback;
5989 drop_cookie = cookie;
5990
5991 // Postpone this when:
5992 // - editing the command line
5993 // - not possible to change the current buffer
5994 // - updating the screen
5995 // As it may change buffers and window structures that are in use and cause
5996 // freed memory to be used.
5997 if (text_locked() || curbuf_locked() || updating_screen)
5998 return;
5999
6000 handle_drop_internal();
6001}
6002
6003/*
6004 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6005 * reset: Handle a postponed drop.
6006 */
6007 void
6008handle_any_postponed_drop(void)
6009{
6010 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006011 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006012 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013}
6014#endif
6015
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 * ":preserve".
6018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006020ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006022 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 ml_preserve(curbuf, TRUE);
6024}
6025
6026/*
6027 * ":recover".
6028 */
6029 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006030ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006032 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006034 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6035 | CCGD_MULTWIN
6036 | (eap->forceit ? CCGD_FORCEIT : 0)
6037 | CCGD_EXCMD)
6038
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 && (*eap->arg == NUL
6040 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006041 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 recoverymode = FALSE;
6043}
6044
6045/*
6046 * Command modifier used in a wrong way.
6047 */
6048 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006049ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006051 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052}
6053
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054/*
6055 * :sview [+command] file split window with new file, read-only
6056 * :split [[+command] file] split window with current or new file
6057 * :vsplit [[+command] file] split window vertically with current or new file
6058 * :new [[+command] file] split window with no or new file
6059 * :vnew [[+command] file] split vertically window with no or new file
6060 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006061 *
6062 * :tabedit open new Tab page with empty window
6063 * :tabedit [+command] file open new Tab page and edit "file"
6064 * :tabnew [[+command] file] just like :tabedit
6065 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 */
6067 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006068ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006070 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006071#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006073#endif
6074#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006076#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006077 int use_tab = eap->cmdidx == CMD_tabedit
6078 || eap->cmdidx == CMD_tabfind
6079 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006081 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006082 return;
6083
Bram Moolenaar4033c552017-09-16 20:54:51 +02006084#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087
Bram Moolenaar4033c552017-09-16 20:54:51 +02006088#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006089 // A ":split" in the quickfix window works like ":new". Don't want two
6090 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006091 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 {
6093 if (eap->cmdidx == CMD_split)
6094 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 if (eap->cmdidx == CMD_vsplit)
6096 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099
Bram Moolenaar4033c552017-09-16 20:54:51 +02006100#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006101 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 {
6103 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6104 FNAME_MESS, TRUE, curbuf->b_ffname);
6105 if (fname == NULL)
6106 goto theend;
6107 eap->arg = fname;
6108 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006109# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006110 else
6111# endif
6112#endif
6113#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 && eap->cmdidx != CMD_new)
6117 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006118 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006119# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006120 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006121# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006122 au_has_group((char_u *)"FileExplorer"))
6123 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006124 // No browsing supported but we do have the file explorer:
6125 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006126 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6127 eap->arg = (char_u *)".";
6128 }
6129 else
6130 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006131 fname = do_browse(0, (char_u *)(use_tab
6132 ? _("Edit File in new tab page")
6133 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006135 if (fname == NULL)
6136 goto theend;
6137 eap->arg = fname;
6138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006140 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006143 /*
6144 * Either open new tab page or split the window.
6145 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006146 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006147 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006148 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6149 : eap->addr_count == 0 ? 0
6150 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006151 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006152 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006153
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006154 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006155 if (curwin != old_curwin
6156 && win_valid(old_curwin)
6157 && old_curwin->w_buffer != curbuf
6158 && !cmdmod.keepalt)
6159 old_curwin->w_alt_fnum = curbuf->b_fnum;
6160 }
6161 }
6162 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6164 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006165 // Reset 'scrollbind' when editing another file, but keep it when
6166 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006168# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006170# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006172 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 else
6174 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 do_exedit(eap, old_curwin);
6176 }
6177
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006178# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006182# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183theend:
6184 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006185# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006187
6188/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006189 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006190 */
6191 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006192tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006193{
6194 exarg_T ea;
6195
Bram Moolenaara80faa82020-04-12 19:37:17 +02006196 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006197 ea.cmdidx = CMD_tabnew;
6198 ea.cmd = (char_u *)"tabn";
6199 ea.arg = (char_u *)"";
6200 ex_splitview(&ea);
6201}
6202
6203/*
6204 * :tabnext command
6205 */
6206 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006207ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006208{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006209 int tab_number;
6210
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006211 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006212 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006213 switch (eap->cmdidx)
6214 {
6215 case CMD_tabfirst:
6216 case CMD_tabrewind:
6217 goto_tabpage(1);
6218 break;
6219 case CMD_tablast:
6220 goto_tabpage(9999);
6221 break;
6222 case CMD_tabprevious:
6223 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006224 if (eap->arg && *eap->arg != NUL)
6225 {
6226 char_u *p = eap->arg;
6227 char_u *p_save = p;
6228
6229 tab_number = getdigits(&p);
6230 if (p == p_save || *p_save == '-' || *p != NUL
6231 || tab_number == 0)
6232 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006233 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006234 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006235 return;
6236 }
6237 }
6238 else
6239 {
6240 if (eap->addr_count == 0)
6241 tab_number = 1;
6242 else
6243 {
6244 tab_number = eap->line2;
6245 if (tab_number < 1)
6246 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006247 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006248 return;
6249 }
6250 }
6251 }
6252 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006253 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006254 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006255 tab_number = get_tabpage_arg(eap);
6256 if (eap->errmsg == NULL)
6257 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006258 break;
6259 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006260}
6261
6262/*
6263 * :tabmove command
6264 */
6265 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006266ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006267{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006268 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006269
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006270 tab_number = get_tabpage_arg(eap);
6271 if (eap->errmsg == NULL)
6272 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006273}
6274
6275/*
6276 * :tabs command: List tabs and their contents.
6277 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006278 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006279ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006280{
6281 tabpage_T *tp;
6282 win_T *wp;
6283 int tabcount = 1;
6284
6285 msg_start();
6286 msg_scroll = TRUE;
6287 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6288 {
6289 msg_putchar('\n');
6290 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006291 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006292 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006293 ui_breakcheck();
6294
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006295 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006296 wp = firstwin;
6297 else
6298 wp = tp->tp_firstwin;
6299 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6300 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006301 msg_putchar('\n');
6302 msg_putchar(wp == curwin ? '>' : ' ');
6303 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006304 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6305 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006306 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006307 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006308 else
6309 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6310 IObuff, IOSIZE, TRUE);
6311 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006312 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006313 ui_breakcheck();
6314 }
6315 }
6316}
6317
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318/*
6319 * ":mode": Set screen mode.
6320 * If no argument given, just get the screen size and redraw.
6321 */
6322 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006323ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324{
6325 if (*eap->arg == NUL)
6326 shell_resized();
6327 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006328 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329}
6330
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331/*
6332 * ":resize".
6333 * set, increment or decrement current window height
6334 */
6335 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006336ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337{
6338 int n;
6339 win_T *wp = curwin;
6340
6341 if (eap->addr_count > 0)
6342 {
6343 n = eap->line2;
6344 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6345 ;
6346 }
6347
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006348# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006350# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 if (cmdmod.split & WSP_VERT)
6353 {
6354 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006355 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006356 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 n = 9999;
6358 win_setwidth_win((int)n, wp);
6359 }
6360 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 {
6362 if (*eap->arg == '-' || *eap->arg == '+')
6363 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006364 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 n = 9999;
6366 win_setheight_win((int)n, wp);
6367 }
6368}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369
6370/*
6371 * ":find [+command] <file>" command.
6372 */
6373 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006374ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375{
6376#ifdef FEAT_SEARCHPATH
6377 char_u *fname;
6378 int count;
6379
6380 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6381 TRUE, curbuf->b_ffname);
6382 if (eap->addr_count > 0)
6383 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006384 // Repeat finding the file "count" times. This matters when it
6385 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 count = eap->line2;
6387 while (fname != NULL && --count > 0)
6388 {
6389 vim_free(fname);
6390 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6391 FALSE, curbuf->b_ffname);
6392 }
6393 }
6394
6395 if (fname != NULL)
6396 {
6397 eap->arg = fname;
6398#endif
6399 do_exedit(eap, NULL);
6400#ifdef FEAT_SEARCHPATH
6401 vim_free(fname);
6402 }
6403#endif
6404}
6405
6406/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006407 * ":open" simulation: for now just work like ":visual".
6408 */
6409 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006410ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006411{
6412 regmatch_T regmatch;
6413 char_u *p;
6414
6415 curwin->w_cursor.lnum = eap->line2;
6416 beginline(BL_SOL | BL_FIX);
6417 if (*eap->arg == '/')
6418 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006419 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006420 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006421 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006422 *p = NUL;
6423 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6424 if (regmatch.regprog != NULL)
6425 {
6426 regmatch.rm_ic = p_ic;
6427 p = ml_get_curline();
6428 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006429 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006430 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006431 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006432 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006433 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006434 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006435 eap->arg += STRLEN(eap->arg);
6436 }
6437 check_cursor();
6438
6439 eap->cmdidx = CMD_visual;
6440 do_exedit(eap, NULL);
6441}
6442
6443/*
6444 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 */
6446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006447ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448{
6449 do_exedit(eap, NULL);
6450}
6451
6452/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006453 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006456do_exedit(
6457 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006458 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459{
6460 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006462 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006464 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6465 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006466 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 /*
6468 * ":vi" command ends Ex mode.
6469 */
6470 if (exmode_active && (eap->cmdidx == CMD_visual
6471 || eap->cmdidx == CMD_view))
6472 {
6473 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006474 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006476 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006477 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006478 if (global_busy)
6479 {
6480 int rd = RedrawingDisabled;
6481 int nwr = no_wait_return;
6482 int ms = msg_scroll;
6483#ifdef FEAT_GUI
6484 int he = hold_gui_events;
6485#endif
6486
6487 if (eap->nextcmd != NULL)
6488 {
6489 stuffReadbuff(eap->nextcmd);
6490 eap->nextcmd = NULL;
6491 }
6492
6493 if (exmode_was != EXMODE_VIM)
6494 settmode(TMODE_RAW);
6495 RedrawingDisabled = 0;
6496 no_wait_return = 0;
6497 need_wait_return = FALSE;
6498 msg_scroll = 0;
6499#ifdef FEAT_GUI
6500 hold_gui_events = 0;
6501#endif
6502 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006503 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006504
6505 main_loop(FALSE, TRUE);
6506
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006507 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006508 RedrawingDisabled = rd;
6509 no_wait_return = nwr;
6510 msg_scroll = ms;
6511#ifdef FEAT_GUI
6512 hold_gui_events = he;
6513#endif
6514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 }
6518
6519 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006520 || eap->cmdidx == CMD_tabnew
6521 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006522 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006524 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 setpcmark();
6526 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006527 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6528 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006530 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 || *eap->arg != NUL
6532#ifdef FEAT_BROWSE
6533 || cmdmod.browse
6534#endif
6535 )
6536 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006537 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6538 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006539 if (*eap->arg != NUL && curbuf_locked())
6540 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006541
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 n = readonlymode;
6543 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6544 readonlymode = TRUE;
6545 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006546 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6547 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 setpcmark();
6549 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6550 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006551 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6553 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6554 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006555 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006557 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006558 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006560 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006562 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 if (old_curwin != NULL)
6564 {
6565 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006566 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006568#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006569 cleanup_T cs;
6570
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006571 // Reset the error/interrupt/exception state here so that
6572 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006573 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006574#endif
6575#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006577#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006578 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006579
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006580#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006581 // Restore the error/interrupt/exception state if not
6582 // discarded by a new aborting error, interrupt, or
6583 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006584 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 }
6587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 }
6589 else if (readonlymode && curbuf->b_nwindows == 1)
6590 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006591 // When editing an already visited buffer, 'readonly' won't be set
6592 // but the previous value is kept. With ":view" and ":sview" we
6593 // want the file to be readonly, except when another window is
6594 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 curbuf->b_p_ro = TRUE;
6596 }
6597 readonlymode = n;
6598 }
6599 else
6600 {
6601 if (eap->do_ecmd_cmd != NULL)
6602 do_cmdline_cmd(eap->do_ecmd_cmd);
6603#ifdef FEAT_TITLE
6604 n = curwin->w_arg_idx_invalid;
6605#endif
6606 check_arg_idx(curwin);
6607#ifdef FEAT_TITLE
6608 if (n != curwin->w_arg_idx_invalid)
6609 maketitle();
6610#endif
6611 }
6612
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 /*
6614 * if ":split file" worked, set alternate file name in old window to new
6615 * file
6616 */
6617 if (old_curwin != NULL
6618 && *eap->arg != NUL
6619 && curwin != old_curwin
6620 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006621 && old_curwin->w_buffer != curbuf
6622 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624
6625 ex_no_reprint = TRUE;
6626}
6627
6628#ifndef FEAT_GUI
6629/*
6630 * ":gui" and ":gvim" when there is no GUI.
6631 */
6632 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006633ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006635 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636}
6637#endif
6638
Bram Moolenaar4f974752019-02-17 17:44:42 +01006639#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006641ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642{
6643 gui_make_tearoff(eap->arg);
6644}
6645#endif
6646
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006647#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6648 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006650ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006652# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6653 if (gui.in_use)
6654 gui_make_popup(eap->arg, eap->forceit);
6655# ifdef FEAT_TERM_POPUP_MENU
6656 else
6657# endif
6658# endif
6659# ifdef FEAT_TERM_POPUP_MENU
6660 pum_make_popup(eap->arg, eap->forceit);
6661# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662}
6663#endif
6664
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006666ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667{
6668 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006669 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006671 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672}
6673
6674/*
6675 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6676 * offset.
6677 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006680ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006683 win_T *save_curwin = curwin;
6684 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 long topline;
6686 long y;
6687 linenr_T old_linenr = curwin->w_cursor.lnum;
6688
6689 setpcmark();
6690
6691 /*
6692 * determine max topline
6693 */
6694 if (curwin->w_p_scb)
6695 {
6696 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006697 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 {
6699 if (wp->w_p_scb && wp->w_buffer)
6700 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006701 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 if (topline > y)
6703 topline = y;
6704 }
6705 }
6706 if (topline < 1)
6707 topline = 1;
6708 }
6709 else
6710 {
6711 topline = 1;
6712 }
6713
6714
6715 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006716 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006718 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 {
6720 if (curwin->w_p_scb)
6721 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006722 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 y = topline - curwin->w_topline;
6724 if (y > 0)
6725 scrollup(y, TRUE);
6726 else
6727 scrolldown(-y, TRUE);
6728 curwin->w_scbind_pos = topline;
6729 redraw_later(VALID);
6730 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 }
6733 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006734 curwin = save_curwin;
6735 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 if (curwin->w_p_scb)
6737 {
6738 did_syncbind = TRUE;
6739 checkpcmark();
6740 if (old_linenr != curwin->w_cursor.lnum)
6741 {
6742 char_u ctrl_o[2];
6743
6744 ctrl_o[0] = Ctrl_O;
6745 ctrl_o[1] = 0;
6746 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6747 }
6748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749}
6750
6751
6752 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006753ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006755 int i;
6756 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6757 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006759 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 do_bang(1, eap, FALSE, FALSE, TRUE);
6761 else
6762 {
6763 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6764 return;
6765
6766#ifdef FEAT_BROWSE
6767 if (cmdmod.browse)
6768 {
6769 char_u *browseFile;
6770
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006771 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 NULL, NULL, NULL, curbuf);
6773 if (browseFile != NULL)
6774 {
6775 i = readfile(browseFile, NULL,
6776 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6777 vim_free(browseFile);
6778 }
6779 else
6780 i = OK;
6781 }
6782 else
6783#endif
6784 if (*eap->arg == NUL)
6785 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006786 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 return;
6788 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6789 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6790 }
6791 else
6792 {
6793 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6794 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6795 i = readfile(eap->arg, NULL,
6796 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6797
6798 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006799 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006801#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 if (!aborting())
6803#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006804 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 }
6806 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006807 {
6808 if (empty && exmode_active)
6809 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006810 // Delete the empty line that remains. Historically ex does
6811 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006812 if (eap->line2 == 0)
6813 lnum = curbuf->b_ml.ml_line_count;
6814 else
6815 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006816 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006817 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006818 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006819 if (curwin->w_cursor.lnum > 1
6820 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006821 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006822 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006823 }
6824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 }
6828}
6829
Bram Moolenaarea408852005-06-25 22:49:46 +00006830static char_u *prev_dir = NULL;
6831
6832#if defined(EXITFREE) || defined(PROTO)
6833 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006834free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006835{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006836 VIM_CLEAR(prev_dir);
6837 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006838}
6839#endif
6840
Bram Moolenaarf4258302013-06-02 18:20:17 +02006841/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006842 * Get the previous directory for the given chdir scope.
6843 */
6844 static char_u *
6845get_prevdir(cdscope_T scope)
6846{
6847 if (scope == CDSCOPE_WINDOW)
6848 return curwin->w_prevdir;
6849 else if (scope == CDSCOPE_TABPAGE)
6850 return curtab->tp_prevdir;
6851 return prev_dir;
6852}
6853
6854/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006855 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006856 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6857 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006858 */
6859 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006860post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006861{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006862 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006863 // Clear tab local directory for both :cd and :tcd
6864 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006865 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006866 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006867 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006868 char_u *pdir = get_prevdir(scope);
6869
6870 // If still in the global directory, need to remember current
6871 // directory as the global directory.
6872 if (globaldir == NULL && pdir != NULL)
6873 globaldir = vim_strsave(pdir);
6874
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006875 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006876 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006877 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006878 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006879 curtab->tp_localdir = vim_strsave(NameBuff);
6880 else
6881 curwin->w_localdir = vim_strsave(NameBuff);
6882 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006883 }
6884 else
6885 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006886 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006887 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006888 }
6889
6890 shorten_fnames(TRUE);
6891}
6892
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006893/*
6894 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006895 * chdir() function.
6896 * scope == CDSCOPE_WINDOW: changes the window-local directory
6897 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6898 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006899 * Returns TRUE if the directory is successfully changed.
6900 */
6901 int
6902changedir_func(
6903 char_u *new_dir,
6904 int forceit,
6905 cdscope_T scope)
6906{
6907 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006908 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006909 int dir_differs;
6910 int retval = FALSE;
6911
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006912 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006913 return FALSE;
6914
6915 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6916 {
6917 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6918 return FALSE;
6919 }
6920
6921 // ":cd -": Change to previous directory
6922 if (STRCMP(new_dir, "-") == 0)
6923 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006924 pdir = get_prevdir(scope);
6925 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006926 {
6927 emsg(_("E186: No previous directory"));
6928 return FALSE;
6929 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006930 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006931 }
6932
Bram Moolenaar002bc792020-06-05 22:33:42 +02006933 // Free the previous directory
6934 tofree = get_prevdir(scope);
6935
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006936 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006937 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006938 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006939 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006940 pdir = NULL;
6941 if (scope == CDSCOPE_WINDOW)
6942 curwin->w_prevdir = pdir;
6943 else if (scope == CDSCOPE_TABPAGE)
6944 curtab->tp_prevdir = pdir;
6945 else
6946 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006947
6948#if defined(UNIX) || defined(VMS)
6949 // for UNIX ":cd" means: go to home directory
6950 if (*new_dir == NUL)
6951 {
6952 // use NameBuff for home directory name
6953# ifdef VMS
6954 char_u *p;
6955
6956 p = mch_getenv((char_u *)"SYS$LOGIN");
6957 if (p == NULL || *p == NUL) // empty is the same as not set
6958 NameBuff[0] = NUL;
6959 else
6960 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6961# else
6962 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6963# endif
6964 new_dir = NameBuff;
6965 }
6966#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006967 dir_differs = new_dir == NULL || pdir == NULL
6968 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006969 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6970 emsg(_(e_failed));
6971 else
6972 {
6973 char_u *acmd_fname;
6974
6975 post_chdir(scope);
6976
6977 if (dir_differs)
6978 {
6979 if (scope == CDSCOPE_WINDOW)
6980 acmd_fname = (char_u *)"window";
6981 else if (scope == CDSCOPE_TABPAGE)
6982 acmd_fname = (char_u *)"tabpage";
6983 else
6984 acmd_fname = (char_u *)"global";
6985 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6986 curbuf);
6987 }
6988 retval = TRUE;
6989 }
6990 vim_free(tofree);
6991
6992 return retval;
6993}
Bram Moolenaarea408852005-06-25 22:49:46 +00006994
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006996 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006998 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006999ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007001 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002
7003 new_dir = eap->arg;
7004#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007005 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 if (*new_dir == NUL)
7007 ex_pwd(NULL);
7008 else
7009#endif
7010 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007011 cdscope_T scope = CDSCOPE_GLOBAL;
7012
7013 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7014 scope = CDSCOPE_WINDOW;
7015 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7016 scope = CDSCOPE_TABPAGE;
7017
7018 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007019 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007020 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007021 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 ex_pwd(eap);
7023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 }
7025}
7026
7027/*
7028 * ":pwd".
7029 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007031ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032{
7033 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7034 {
7035#ifdef BACKSLASH_IN_FILENAME
7036 slash_adjust(NameBuff);
7037#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007038 if (p_verbose > 0)
7039 {
7040 char *context = "global";
7041
7042 if (curwin->w_localdir != NULL)
7043 context = "window";
7044 else if (curtab->tp_localdir != NULL)
7045 context = "tabpage";
7046 smsg("[%s] %s", context, (char *)NameBuff);
7047 }
7048 else
7049 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 }
7051 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007052 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053}
7054
7055/*
7056 * ":=".
7057 */
7058 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007059ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007061 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007062 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063}
7064
7065 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007066ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007068 int n;
7069 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070
7071 if (cursor_valid())
7072 {
7073 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7074 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007075 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007077
7078 len = eap->line2;
7079 switch (*eap->arg)
7080 {
7081 case 'm': break;
7082 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007083 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007084 }
7085 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086}
7087
7088/*
7089 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7090 */
7091 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007092do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093{
Bram Moolenaar52953192019-08-16 10:27:13 +02007094 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007095 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007096# ifdef ELAPSED_FUNC
7097 elapsed_T start_tv;
7098
7099 // Remember at what time we started, so that we know how much longer we
7100 // should wait after waiting for a bit.
7101 ELAPSED_INIT(start_tv);
7102# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103
7104 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007105 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007106 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007108 wait_now = msec - done > 1000L ? 1000L : msec - done;
7109#ifdef FEAT_TIMERS
7110 {
7111 long due_time = check_due_timer();
7112
7113 if (due_time > 0 && due_time < wait_now)
7114 wait_now = due_time;
7115 }
7116#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007117#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007118 if (has_any_channel() && wait_now > 20L)
7119 wait_now = 20L;
7120#endif
7121#ifdef FEAT_SOUND
7122 if (has_any_sound_callback() && wait_now > 20L)
7123 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007124#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007125 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007126
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007127#ifdef FEAT_JOB_CHANNEL
7128 if (has_any_channel())
7129 ui_breakcheck_force(TRUE);
7130 else
7131#endif
7132 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007133#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007134 // Process the netbeans and clientserver messages that may have been
7135 // received in the call to ui_breakcheck() when the GUI is in use. This
7136 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007137 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007138#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007139
7140# ifdef ELAPSED_FUNC
7141 // actual time passed
7142 done = ELAPSED_FUNC(start_tv);
7143# else
7144 // guestimate time passed (will actually be more)
7145 done += wait_now;
7146# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007148
7149 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7150 // input buffer, otherwise a following call to input() fails.
7151 if (got_int)
7152 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153}
7154
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155/*
7156 * ":winsize" command (obsolete).
7157 */
7158 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007159ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160{
7161 int w, h;
7162 char_u *arg = eap->arg;
7163 char_u *p;
7164
7165 w = getdigits(&arg);
7166 arg = skipwhite(arg);
7167 p = arg;
7168 h = getdigits(&arg);
7169 if (*p != NUL && *arg == NUL)
7170 set_shellsize(w, h, TRUE);
7171 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007172 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173}
7174
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007176ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177{
7178 int xchar = NUL;
7179 char_u *p;
7180
7181 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7182 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007183 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 if (eap->arg[1] == NUL)
7185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007186 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 return;
7188 }
7189 xchar = eap->arg[1];
7190 p = eap->arg + 2;
7191 }
7192 else
7193 p = eap->arg + 1;
7194
7195 eap->nextcmd = check_nextcmd(p);
7196 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007197 if (*p != NUL && *p != (
7198#ifdef FEAT_EVAL
7199 in_vim9script() ? '#' :
7200#endif
7201 '"')
7202 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007203 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007204 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007206 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007208 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7210 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007211 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 }
7213}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214
Bram Moolenaar843ee412004-06-30 16:16:41 +00007215#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216/*
7217 * ":winpos".
7218 */
7219 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007220ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221{
7222 int x, y;
7223 char_u *arg = eap->arg;
7224 char_u *p;
7225
7226 if (*arg == NUL)
7227 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007228# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007229# ifdef VIMDLL
7230 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7231 mch_get_winpos(&x, &y) != FAIL)
7232# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007234# else
7235 if (mch_get_winpos(&x, &y) != FAIL)
7236# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 {
7238 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007239 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 }
7241 else
7242# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007243 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 }
7245 else
7246 {
7247 x = getdigits(&arg);
7248 arg = skipwhite(arg);
7249 p = arg;
7250 y = getdigits(&arg);
7251 if (*p == NUL || *arg != NUL)
7252 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007253 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 return;
7255 }
7256# ifdef FEAT_GUI
7257 if (gui.in_use)
7258 gui_mch_set_winpos(x, y);
7259 else if (gui.starting)
7260 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007261 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 gui_win_x = x;
7263 gui_win_y = y;
7264 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007265# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 else
7267# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007268# endif
7269# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007270 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271# endif
7272# ifdef HAVE_TGETENT
7273 if (*T_CWP)
7274 term_set_winpos(x, y);
7275# endif
7276 }
7277}
7278#endif
7279
7280/*
7281 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7282 */
7283 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007284ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285{
7286 oparg_T oa;
7287
7288 clear_oparg(&oa);
7289 oa.regname = eap->regname;
7290 oa.start.lnum = eap->line1;
7291 oa.end.lnum = eap->line2;
7292 oa.line_count = eap->line2 - eap->line1 + 1;
7293 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007295 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 {
7297 setpcmark();
7298 curwin->w_cursor.lnum = eap->line1;
7299 beginline(BL_SOL | BL_FIX);
7300 }
7301
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007302 if (VIsual_active)
7303 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007304
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 switch (eap->cmdidx)
7306 {
7307 case CMD_delete:
7308 oa.op_type = OP_DELETE;
7309 op_delete(&oa);
7310 break;
7311
7312 case CMD_yank:
7313 oa.op_type = OP_YANK;
7314 (void)op_yank(&oa, FALSE, TRUE);
7315 break;
7316
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007317 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007318 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007320 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7321#else
7322 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007324 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 oa.op_type = OP_RSHIFT;
7326 else
7327 oa.op_type = OP_LSHIFT;
7328 op_shift(&oa, FALSE, eap->amount);
7329 break;
7330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007332 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333}
7334
7335/*
7336 * ":put".
7337 */
7338 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007339ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007341 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 if (eap->line2 == 0)
7343 {
7344 eap->line2 = 1;
7345 eap->forceit = TRUE;
7346 }
7347 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007348 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007349 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350}
7351
7352/*
7353 * Handle ":copy" and ":move".
7354 */
7355 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007356ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357{
7358 long n;
7359
Bram Moolenaar491799b2020-08-01 19:23:43 +02007360#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007361 if (not_in_vim9(eap) == FAIL)
7362 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007363#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007364 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007365 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 {
7367 eap->nextcmd = NULL;
7368 return;
7369 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007370 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371
7372 /*
7373 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7374 */
7375 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7376 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007377 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 return;
7379 }
7380
7381 if (eap->cmdidx == CMD_move)
7382 {
7383 if (do_move(eap->line1, eap->line2, n) == FAIL)
7384 return;
7385 }
7386 else
7387 ex_copy(eap->line1, eap->line2, n);
7388 u_clearline();
7389 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007390 ex_may_print(eap);
7391}
7392
7393/*
7394 * Print the current line if flags were given to the Ex command.
7395 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007396 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007397ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007398{
7399 if (eap->flags != 0)
7400 {
7401 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7402 (eap->flags & EXFLAG_LIST));
7403 ex_no_reprint = TRUE;
7404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405}
7406
7407/*
7408 * ":smagic" and ":snomagic".
7409 */
7410 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007411ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412{
7413 int magic_save = p_magic;
7414
7415 p_magic = (eap->cmdidx == CMD_smagic);
Bram Moolenaar66e00142020-08-12 21:58:12 +02007416 ex_substitute(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 p_magic = magic_save;
7418}
7419
7420/*
7421 * ":join".
7422 */
7423 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007424ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425{
7426 curwin->w_cursor.lnum = eap->line1;
7427 if (eap->line1 == eap->line2)
7428 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007429 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 return;
7431 if (eap->line2 == curbuf->b_ml.ml_line_count)
7432 {
7433 beep_flush();
7434 return;
7435 }
7436 ++eap->line2;
7437 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007438 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007440 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441}
7442
7443/*
7444 * ":[addr]@r" or ":[addr]*r": execute register
7445 */
7446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007447ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448{
7449 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007450 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451
7452 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007453 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454
7455#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007456 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457#endif
7458
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007459 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 c = *eap->arg;
7461 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7462 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007463 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007464 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7465 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007466 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 else
7470 {
7471 int save_efr = exec_from_reg;
7472
7473 exec_from_reg = TRUE;
7474
7475 /*
7476 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007477 * Continue until the stuff buffer is empty and all added characters
7478 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007480 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7482
7483 exec_from_reg = save_efr;
7484 }
7485}
7486
7487/*
7488 * ":!".
7489 */
7490 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007491ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492{
7493 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7494}
7495
7496/*
7497 * ":undo".
7498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007500ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007502 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007503 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007504 else
7505 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506}
7507
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007508#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007510ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007511{
7512 char_u hash[UNDO_HASH_SIZE];
7513
7514 u_compute_hash(hash);
7515 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7516}
7517
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007519ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007520{
7521 char_u hash[UNDO_HASH_SIZE];
7522
7523 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007524 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007525}
7526#endif
7527
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528/*
7529 * ":redo".
7530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007532ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533{
7534 u_redo(1);
7535}
7536
7537/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007538 * ":earlier" and ":later".
7539 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007540 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007541ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007542{
7543 long count = 0;
7544 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007545 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007546 char_u *p = eap->arg;
7547
7548 if (*p == NUL)
7549 count = 1;
7550 else if (isdigit(*p))
7551 {
7552 count = getdigits(&p);
7553 switch (*p)
7554 {
7555 case 's': ++p; sec = TRUE; break;
7556 case 'm': ++p; sec = TRUE; count *= 60; break;
7557 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007558 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7559 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007560 }
7561 }
7562
7563 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007564 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007565 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007566 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7567 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007568}
7569
7570/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 * ":redir": start/stop redirection.
7572 */
7573 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007574ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575{
7576 char *mode;
7577 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007578 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579
Bram Moolenaarba768492016-07-08 15:32:54 +02007580#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007581 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007582 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007583 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007584 return;
7585 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007586#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007587
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 if (STRICMP(eap->arg, "END") == 0)
7589 close_redir();
7590 else
7591 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007592 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007594 ++arg;
7595 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007597 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 mode = "a";
7599 }
7600 else
7601 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007602 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603
7604 close_redir();
7605
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007606 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007607 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 if (fname == NULL)
7609 return;
7610#ifdef FEAT_BROWSE
7611 if (cmdmod.browse)
7612 {
7613 char_u *browseFile;
7614
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007615 browseFile = do_browse(BROWSE_SAVE,
7616 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007617 fname, NULL, NULL,
7618 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007620 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 vim_free(fname);
7622 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007623 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 }
7625#endif
7626
7627 redir_fd = open_exfile(fname, eap->forceit, mode);
7628 vim_free(fname);
7629 }
7630#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007631 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007633 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007635 ++arg;
7636 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007638 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007639 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007641 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007643 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007644 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007645 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007646 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007648 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007649 if (*arg == '>')
7650 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007651 // Make register empty when not using @A-@Z and the
7652 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007653 if (*arg == NUL && !isupper(redir_reg))
7654 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007656 }
7657 if (*arg != NUL)
7658 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007659 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007660 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007661 }
7662 }
7663 else if (*arg == '=' && arg[1] == '>')
7664 {
7665 int append;
7666
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007667 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007668 close_redir();
7669 arg += 2;
7670
7671 if (*arg == '>')
7672 {
7673 ++arg;
7674 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 }
7676 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007677 append = FALSE;
7678
7679 if (var_redir_start(skipwhite(arg), append) == OK)
7680 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 }
7682#endif
7683
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007684 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007687 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007689
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007690 // Make sure redirection is not off. Can happen for cmdline completion
7691 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007692 if (redir_fd != NULL
7693#ifdef FEAT_EVAL
7694 || redir_reg || redir_vname
7695#endif
7696 )
7697 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698}
7699
7700/*
7701 * ":redraw": force redraw
7702 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007703 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007704ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705{
7706 int r = RedrawingDisabled;
7707 int p = p_lz;
7708
7709 RedrawingDisabled = 0;
7710 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007711 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007713 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714#ifdef FEAT_TITLE
7715 if (need_maketitle)
7716 maketitle();
7717#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007718#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7719# ifdef VIMDLL
7720 if (!gui.in_use)
7721# endif
7722 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 RedrawingDisabled = r;
7725 p_lz = p;
7726
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007727 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 msg_didout = FALSE;
7729 msg_col = 0;
7730
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007731 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007732 need_wait_return = FALSE;
7733
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 out_flush();
7735}
7736
7737/*
7738 * ":redrawstatus": force redraw of status line(s)
7739 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007741ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 int r = RedrawingDisabled;
7744 int p = p_lz;
7745
7746 RedrawingDisabled = 0;
7747 p_lz = FALSE;
7748 if (eap->forceit)
7749 status_redraw_all();
7750 else
7751 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007752 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 RedrawingDisabled = r;
7754 p_lz = p;
7755 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756}
7757
Bram Moolenaare12bab32019-01-08 22:02:56 +01007758/*
7759 * ":redrawtabline": force redraw of the tabline
7760 */
7761 static void
7762ex_redrawtabline(exarg_T *eap UNUSED)
7763{
7764 int r = RedrawingDisabled;
7765 int p = p_lz;
7766
7767 RedrawingDisabled = 0;
7768 p_lz = FALSE;
7769
7770 draw_tabline();
7771
7772 RedrawingDisabled = r;
7773 p_lz = p;
7774 out_flush();
7775}
7776
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007778close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779{
7780 if (redir_fd != NULL)
7781 {
7782 fclose(redir_fd);
7783 redir_fd = NULL;
7784 }
7785#ifdef FEAT_EVAL
7786 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007787 if (redir_vname)
7788 {
7789 var_redir_stop();
7790 redir_vname = 0;
7791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792#endif
7793}
7794
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007795#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007796 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007797vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007798{
7799 if (vim_mkdir(name, prot) != 0)
7800 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007801 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007802 return FAIL;
7803 }
7804 return OK;
7805}
7806#endif
7807
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808/*
7809 * Open a file for writing for an Ex command, with some checks.
7810 * Return file descriptor, or NULL on failure.
7811 */
7812 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007813open_exfile(
7814 char_u *fname,
7815 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007816 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817{
7818 FILE *fd;
7819
7820#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007821 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 if (mch_isdir(fname))
7823 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007824 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 return NULL;
7826 }
7827#endif
7828 if (!forceit && *mode != 'a' && vim_fexists(fname))
7829 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007830 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 return NULL;
7832 }
7833
7834 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007835 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836
7837 return fd;
7838}
7839
7840/*
7841 * ":mark" and ":k".
7842 */
7843 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007844ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845{
7846 pos_T pos;
7847
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007848 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007849 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007850 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02007851 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 else
7853 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007854 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 curwin->w_cursor.lnum = eap->line2;
7856 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007857 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007858 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007859 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 }
7861}
7862
7863/*
7864 * Update w_topline, w_leftcol and the cursor position.
7865 */
7866 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007867update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007869 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 update_topline();
7871 if (!curwin->w_p_wrap)
7872 validate_cursor();
7873 update_curswant();
7874}
7875
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007877 * Save the current State and go to Normal mode.
7878 * Return TRUE if the typeahead could be saved.
7879 */
7880 int
7881save_current_state(save_state_T *sst)
7882{
7883 sst->save_msg_scroll = msg_scroll;
7884 sst->save_restart_edit = restart_edit;
7885 sst->save_msg_didout = msg_didout;
7886 sst->save_State = State;
7887 sst->save_insertmode = p_im;
7888 sst->save_finish_op = finish_op;
7889 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007890 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007891
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007892 msg_scroll = FALSE; // no msg scrolling in Normal mode
7893 restart_edit = 0; // don't go to Insert mode
7894 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007895
7896 /*
7897 * Save the current typeahead. This is required to allow using ":normal"
7898 * from an event handler and makes sure we don't hang when the argument
7899 * ends with half a command.
7900 */
7901 save_typeahead(&sst->tabuf);
7902 return sst->tabuf.typebuf_valid;
7903}
7904
7905 void
7906restore_current_state(save_state_T *sst)
7907{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007908 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007909 restore_typeahead(&sst->tabuf);
7910
7911 msg_scroll = sst->save_msg_scroll;
7912 restart_edit = sst->save_restart_edit;
7913 p_im = sst->save_insertmode;
7914 finish_op = sst->save_finish_op;
7915 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007916 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007917 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007918
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007919 // Restore the state (needed when called from a function executed for
7920 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007921 State = sst->save_State;
7922#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007923 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007924#endif
7925}
7926
7927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 * ":normal[!] {commands}": Execute normal mode commands.
7929 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007930 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007931ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007933 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 char_u *arg = NULL;
7935 int l;
7936 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007938 if (ex_normal_lock > 0)
7939 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007940 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007941 return;
7942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 if (ex_normal_busy >= p_mmd)
7944 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007945 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 return;
7947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 /*
7950 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7951 * this for the K_SPECIAL leading byte, otherwise special keys will not
7952 * work.
7953 */
7954 if (has_mbyte)
7955 {
7956 int len = 0;
7957
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007958 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 for (p = eap->arg; *p != NUL; ++p)
7960 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007961#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007962 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007964#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007965 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007966 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007967#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 )
7971 len += 2;
7972 }
7973 if (len > 0)
7974 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007975 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 if (arg != NULL)
7977 {
7978 len = 0;
7979 for (p = eap->arg; *p != NUL; ++p)
7980 {
7981 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007982#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 if (*p == CSI)
7984 {
7985 arg[len++] = KS_EXTRA;
7986 arg[len++] = (int)KE_CSI;
7987 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007988#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007989 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {
7991 arg[len++] = *++p;
7992 if (*p == K_SPECIAL)
7993 {
7994 arg[len++] = KS_SPECIAL;
7995 arg[len++] = KE_FILLER;
7996 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007997#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 else if (*p == CSI)
7999 {
8000 arg[len++] = KS_EXTRA;
8001 arg[len++] = (int)KE_CSI;
8002 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 }
8005 arg[len] = NUL;
8006 }
8007 }
8008 }
8009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008011 ++ex_normal_busy;
8012 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 {
8014 /*
8015 * Repeat the :normal command for each line in the range. When no
8016 * range given, execute it just once, without positioning the cursor
8017 * first.
8018 */
8019 do
8020 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 if (eap->addr_count != 0)
8022 {
8023 curwin->w_cursor.lnum = eap->line1++;
8024 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008025 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 }
8027
Bram Moolenaar13505972019-01-24 15:04:48 +01008028 exec_normal_cmd(arg != NULL
8029 ? arg
8030 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 }
8032 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8033 }
8034
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008035 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 update_topline_cursor();
8037
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008038 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 --ex_normal_busy;
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