blob: 7a035972137905c4956dae7283a824e7fbc319f8 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +020023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaar20b23c62020-08-20 15:25:00 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
39static void ex_bmodified(exarg_T *eap);
40static void ex_bnext(exarg_T *eap);
41static void ex_bprevious(exarg_T *eap);
42static void ex_brewind(exarg_T *eap);
43static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifndef FEAT_QUICKFIX
47# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000048# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# define ex_cc ex_ni
50# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020051# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cfile ex_ni
53# define qf_list ex_ni
54# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020055# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000057# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020059#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define ex_cclose ex_ni
61# define ex_copen ex_ni
62# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020063# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000065#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
66# define ex_cexpr ex_ni
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020069static linenr_T default_address(exarg_T *eap);
Bram Moolenaarb7316892019-05-01 18:08:42 +020070static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020071static void address_default_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010072static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020073#if !defined(FEAT_PERL) \
74 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
75 || !defined(FEAT_TCL) \
76 || !defined(FEAT_RUBY) \
77 || !defined(FEAT_LUA) \
78 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000079# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010080static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010082static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000086#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010087static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
88static void ex_highlight(exarg_T *eap);
89static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_cquit(exarg_T *eap);
91static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010092static void ex_close(exarg_T *eap);
93static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
94static void ex_only(exarg_T *eap);
95static void ex_resize(exarg_T *eap);
96static void ex_stag(exarg_T *eap);
97static void ex_tabclose(exarg_T *eap);
98static void ex_tabonly(exarg_T *eap);
99static void ex_tabnext(exarg_T *eap);
100static void ex_tabmove(exarg_T *eap);
101static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200102#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100103static void ex_pclose(exarg_T *eap);
104static void ex_ptag(exarg_T *eap);
105static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106#else
107# define ex_pclose ex_ni
108# define ex_ptag ex_ni
109# define ex_pedit ex_ni
110#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100111static void ex_hide(exarg_T *eap);
112static void ex_stop(exarg_T *eap);
113static void ex_exit(exarg_T *eap);
114static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100116static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#else
118# define ex_goto ex_ni
119#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100120static void ex_shell(exarg_T *eap);
121static void ex_preserve(exarg_T *eap);
122static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100123static void ex_mode(exarg_T *eap);
124static void ex_wrongmodifier(exarg_T *eap);
125static void ex_find(exarg_T *eap);
126static void ex_open(exarg_T *eap);
127static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#ifndef FEAT_GUI
129# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100132#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100133static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#else
135# define ex_tearoff ex_ni
136#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100137#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
138 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#else
141# define ex_popup ex_ni
142#endif
143#ifndef FEAT_GUI_MSWIN
144# define ex_simalt ex_ni
145#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000146#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147# define gui_mch_find_dialog ex_ni
148# define gui_mch_replace_dialog ex_ni
149#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000150#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151# define ex_helpfind ex_ni
152#endif
153#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100154# define ex_cscope ex_ni
155# define ex_scscope ex_ni
156# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#endif
158#ifndef FEAT_SYN_HL
159# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200160# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000161#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200162#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200163# define ex_syntime ex_ni
164#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000165#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000166# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000167# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000168# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000169# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000170# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000171#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200172#ifndef FEAT_PERSISTENT_UNDO
173# define ex_rundo ex_ni
174# define ex_wundo ex_ni
175#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200176#ifndef FEAT_LUA
177# define ex_lua ex_script_ni
178# define ex_luado ex_ni
179# define ex_luafile ex_ni
180#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000181#ifndef FEAT_MZSCHEME
182# define ex_mzscheme ex_script_ni
183# define ex_mzfile ex_ni
184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#ifndef FEAT_PERL
186# define ex_perl ex_script_ni
187# define ex_perldo ex_ni
188#endif
189#ifndef FEAT_PYTHON
190# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200191# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192# define ex_pyfile ex_ni
193#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200194#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200195# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200196# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200197# define ex_py3file ex_ni
198#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100199#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
200# define ex_pyx ex_script_ni
201# define ex_pyxdo ex_ni
202# define ex_pyxfile ex_ni
203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#ifndef FEAT_TCL
205# define ex_tcl ex_script_ni
206# define ex_tcldo ex_ni
207# define ex_tclfile ex_ni
208#endif
209#ifndef FEAT_RUBY
210# define ex_ruby ex_script_ni
211# define ex_rubydo ex_ni
212# define ex_rubyfile ex_ni
213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#ifndef FEAT_KEYMAP
215# define ex_loadkeymap ex_ni
216#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100217static void ex_swapname(exarg_T *eap);
218static void ex_syncbind(exarg_T *eap);
219static void ex_read(exarg_T *eap);
220static void ex_pwd(exarg_T *eap);
221static void ex_equal(exarg_T *eap);
222static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000225#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#else
228# define ex_winpos ex_ni
229#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100230static void ex_operators(exarg_T *eap);
231static void ex_put(exarg_T *eap);
232static void ex_copymove(exarg_T *eap);
233static void ex_submagic(exarg_T *eap);
234static void ex_join(exarg_T *eap);
235static void ex_at(exarg_T *eap);
236static void ex_bang(exarg_T *eap);
237static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200238#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100239static void ex_wundo(exarg_T *eap);
240static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200241#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ex_redo(exarg_T *eap);
243static void ex_later(exarg_T *eap);
244static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100246static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100249static void ex_startinsert(exarg_T *eap);
250static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100252static void ex_checkpath(exarg_T *eap);
253static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254#else
255# define ex_findpat ex_ni
256# define ex_checkpath ex_ni
257#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200258#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100259static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260#else
261# define ex_psearch ex_ni
262#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static void ex_tag(exarg_T *eap);
264static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#ifndef FEAT_EVAL
Bram Moolenaar1b1e9df2020-10-10 22:26:52 +0200266# define ex_block ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200267# define ex_break ex_ni
268# define ex_breakadd ex_ni
269# define ex_breakdel ex_ni
270# define ex_breaklist ex_ni
271# define ex_call ex_ni
272# define ex_catch ex_ni
273# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200274# define ex_continue ex_ni
275# define ex_debug ex_ni
276# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200277# define ex_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 Moolenaar1b1e9df2020-10-10 22:26:52 +0200283# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200284# define ex_endfunction ex_ni
285# define ex_endif ex_ni
286# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200288# define ex_eval ex_ni
289# define ex_execute ex_ni
290# define ex_finally ex_ni
291# define ex_finish ex_ni
292# define ex_function ex_ni
293# define ex_if ex_ni
294# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200295# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200296# define ex_lockvar ex_ni
297# define ex_oldfiles ex_ni
298# define ex_options ex_ni
299# define ex_packadd ex_ni
300# define ex_packloadall ex_ni
301# define ex_return ex_ni
302# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# define ex_throw ex_ni
304# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000306# define ex_unlockvar 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/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100597 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
598 * This allows for using a range without ":" in Vim9 script.
599 */
600 int
601do_cmd_argument(char_u *cmd)
602{
603 return do_cmdline(cmd, NULL, NULL,
604 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
605}
606
607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 * do_cmdline(): execute one Ex command line
609 *
610 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100611 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 * 2. Split up in parts separated with '|'.
613 *
614 * This function can be called recursively!
615 *
616 * flags:
617 * DOCMD_VERBOSE - The command will be included in the error message.
618 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100619 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 * DOCMD_KEYTYPED - Don't reset KeyTyped.
621 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
622 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
623 *
624 * return FAIL if cmdline could not be executed, OK otherwise
625 */
626 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100627do_cmdline(
628 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200629 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100630 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100631 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100633 char_u *next_cmdline; // next cmd to execute
634 char_u *cmdline_copy = NULL; // copy of cmd line
635 int used_getline = FALSE; // used "fgetline" to obtain command
636 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100638 int count = 0; // line number count
639 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 int retval = OK;
641#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100642 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100643 garray_T lines_ga; // keep lines for ":while"/":for"
644 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200645 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100646 char_u *fname = NULL; // function or script name
647 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
648 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
649 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200651 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200652 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100654 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200655 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000657 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000659 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100661# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662# define cmd_cookie cookie
663#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100664 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200665#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100666 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100668 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
669 // location for storing error messages to be converted to an exception.
670 // This ensures that the do_errthrow() call in do_one_cmd() does not
671 // combine the messages stored by an earlier invocation of do_one_cmd()
672 // with the command name of the later one. This would happen when
673 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 saved_msg_list = msg_list;
675 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676#endif
677
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100678 // It's possible to create an endless loop with ":execute", catch that
679 // here. The value of 200 allows nested function calls, ":source", etc.
680 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100681 if (call_depth >= 200
682#ifdef FEAT_EVAL
683 && call_depth >= p_mfd
684#endif
685 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100687 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100689 // When converting to an exception, we do not include the command name
690 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100691 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 msg_list = saved_msg_list;
693#endif
694 return FAIL;
695 }
696 ++call_depth;
697
698#ifdef FEAT_EVAL
Bram Moolenaarce0370d2021-01-26 19:32:53 +0100699 CLEAR_FIELD(cstack);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 cstack.cs_idx = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
702
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100703 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100705 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100706 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000707 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 ++ex_nesting_level;
709
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100710 // Get the function or script name and the address where the next breakpoint
711 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000712 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {
714 fname = func_name(real_cookie);
715 breakpoint = func_breakpoint(real_cookie);
716 dbg_tick = func_dbg_tick(real_cookie);
717 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100718 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100720 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 breakpoint = source_breakpoint(real_cookie);
722 dbg_tick = source_dbg_tick(real_cookie);
723 }
724
725 /*
726 * Initialize "force_abort" and "suppress_errthrow" at the top level.
727 */
728 if (!recursive)
729 {
730 force_abort = FALSE;
731 suppress_errthrow = FALSE;
732 }
733
734 /*
735 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000736 * exception handling (used when debugging). Otherwise clear it to avoid
737 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000739 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000740 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000741 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200742 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
744 initial_trylevel = trylevel;
745
746 /*
747 * "did_throw" will be set to TRUE when an exception is being thrown.
748 */
749 did_throw = FALSE;
750#endif
751 /*
752 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000753 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 * If force_abort is set, we cancel everything.
755 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100756#ifdef FEAT_EVAL
757 did_emsg_cumul += did_emsg;
758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 did_emsg = FALSE;
760
761 /*
762 * KeyTyped is only set when calling vgetc(). Reset it here when not
763 * calling vgetc() (sourced command lines).
764 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100765 if (!(flags & DOCMD_KEYTYPED)
766 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 KeyTyped = FALSE;
768
769 /*
770 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000771 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 * - for multiple commands on one line, separated with '|'
773 * - when repeating until there are no more lines (for ":source")
774 */
775 next_cmdline = cmdline;
776 do
777 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000778#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100779 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000780#endif
781
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100782 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 if (next_cmdline == NULL
784#ifdef FEAT_EVAL
785 && !force_abort
786 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000787 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788#endif
789 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100790 {
791#ifdef FEAT_EVAL
792 did_emsg_cumul += did_emsg;
793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
797 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000798 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100799 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 * 3. If a line is given: Make a copy, so we can mess with it.
801 */
802
803#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100804 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000805 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100807 // Each '|' separated command is stored separately in lines_ga, to
808 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100809 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100811 // Check if a function has returned or, unless it has an unclosed
812 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000813 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000815# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000816 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000817 func_line_end(real_cookie);
818# endif
819 if (func_has_ended(real_cookie))
820 {
821 retval = FAIL;
822 break;
823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000825#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000826 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000828 script_line_end();
829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100831 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100832 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 {
834 retval = FAIL;
835 break;
836 }
837
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100838 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (breakpoint != NULL && dbg_tick != NULL
840 && *dbg_tick != debug_tick)
841 {
842 *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 }
847
848 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100849 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100851 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100853 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100855 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100856 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100858 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100859 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 *dbg_tick = debug_tick;
861 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000862# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000863 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000864 {
865 if (getline_is_func)
Bram Moolenaarb2049902021-01-24 12:53:53 +0100866 func_line_start(real_cookie, SOURCING_LNUM);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100867 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000868 script_line_start();
869 }
870# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872#endif
873
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100874 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (next_cmdline == NULL)
876 {
877 /*
878 * Need to set msg_didout for the first line after an ":if",
879 * otherwise the ":if" will be overwritten.
880 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100881 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100883 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#ifdef FEAT_EVAL
885 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
886#else
887 0
888#endif
Bram Moolenaar8242ebb2020-12-29 11:15:01 +0100889 , in_vim9script() ? GETLINE_CONCAT_CONTBAR
890 : GETLINE_CONCAT_CONT)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100892 // Don't call wait_return for aborted command line. The NULL
893 // returned for the end of a sourced file or executed function
894 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 if (KeyTyped && !(flags & DOCMD_REPEAT))
896 need_wait_return = FALSE;
897 retval = FAIL;
898 break;
899 }
900 used_getline = TRUE;
901
902 /*
903 * Keep the first typed line. Clear it when more lines are typed.
904 */
905 if (flags & DOCMD_KEEPLINE)
906 {
907 vim_free(repeat_cmdline);
908 if (count == 0)
909 repeat_cmdline = vim_strsave(next_cmdline);
910 else
911 repeat_cmdline = NULL;
912 }
913 }
914
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100915 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 else if (cmdline_copy == NULL)
917 {
918 next_cmdline = vim_strsave(next_cmdline);
919 if (next_cmdline == NULL)
920 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100921 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 retval = FAIL;
923 break;
924 }
925 }
926 cmdline_copy = next_cmdline;
927
928#ifdef FEAT_EVAL
929 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200930 * Inside a while/for loop, and when the command looks like a ":while"
931 * or ":for", the line is stored, because we may need it later when
932 * looping.
933 *
934 * When there is a '|' and another command, it is stored separately,
935 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000936 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200937 *
938 * Pass a different "fgetline" function to do_one_cmd() below,
939 * that it stores lines in or reads them from "lines_ga". Makes it
940 * possible to define a function inside a while/for loop and handles
941 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200943 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200945 cmd_getline = get_loop_line;
946 cmd_cookie = (void *)&cmd_loop_cookie;
947 cmd_loop_cookie.lines_gap = &lines_ga;
948 cmd_loop_cookie.current_line = current_line;
949 cmd_loop_cookie.getline = fgetline;
950 cmd_loop_cookie.cookie = cookie;
951 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
952
953 // Save the current line when encountering it the first time.
954 if (current_line == lines_ga.ga_len
955 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 {
957 retval = FAIL;
958 break;
959 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200960 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200962 else
963 {
964 cmd_getline = fgetline;
965 cmd_cookie = cookie;
966 }
967
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 did_endif = FALSE;
969#endif
970
971 if (count++ == 0)
972 {
973 /*
974 * All output from the commands is put below each other, without
975 * waiting for a return. Don't do this when executing commands
976 * from a script or when being called recursive (e.g. for ":e
977 * +command file").
978 */
979 if (!(flags & DOCMD_NOWAIT) && !recursive)
980 {
981 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100982 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100984 msg_scroll = TRUE; // put messages below each other
985 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 ++RedrawingDisabled;
987 did_inc = TRUE;
988 }
989 }
990
Bram Moolenaar823654b2020-05-29 23:03:09 +0200991 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100992 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
994 /*
995 * 2. Execute one '|' separated command.
996 * do_one_cmd() will return NULL if there is no trailing '|'.
997 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
998 */
999 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001000 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001#ifdef FEAT_EVAL
1002 &cstack,
1003#endif
1004 cmd_getline, cmd_cookie);
1005 --recursive;
1006
1007#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001008 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001009 // Use "current_line" from "cmd_loop_cookie", it may have been
1010 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001011 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012#endif
1013
1014 if (next_cmdline == NULL)
1015 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001016 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001017
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 /*
1019 * If the command was typed, remember it for the ':' register.
1020 * Do this AFTER executing the command to make :@: work.
1021 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001022 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 && new_last_cmdline != NULL)
1024 {
1025 vim_free(last_cmdline);
1026 last_cmdline = new_last_cmdline;
1027 new_last_cmdline = NULL;
1028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 }
1030 else
1031 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001032 // need to copy the command after the '|' to cmdline_copy, for the
1033 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001034 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 next_cmdline = cmdline_copy;
1036 }
1037
1038
1039#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001040 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001042 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001044 {
1045 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001049 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 {
1051 ++current_line;
1052
1053 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001054 * An ":endwhile", ":endfor" and ":continue" is handled here.
1055 * If we were executing commands, jump back to the ":while" or
1056 * ":for".
1057 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001059 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001061 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001063 // Jump back to the matching ":while" or ":for". Be careful
1064 // not to use a cs_line[] from an entry that isn't a ":while"
1065 // or ":for": It would make "current_line" invalid and can
1066 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 if (!did_emsg && !got_int && !did_throw
1068 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001069 && (cstack.cs_flags[cstack.cs_idx]
1070 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 && cstack.cs_line[cstack.cs_idx] >= 0
1072 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1073 {
1074 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001075 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001076 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001077 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001079 // Check for the next breakpoint at or after the ":while"
1080 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 if (breakpoint != NULL)
1082 {
1083 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001084 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 fname,
1086 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1087 *dbg_tick = debug_tick;
1088 }
1089 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001090 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001092 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001094 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1095 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 }
1097 }
1098
1099 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001100 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001102 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001104 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001105 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 }
1107 }
1108
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001109 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001110 if (breakpoint != NULL && has_watchexpr())
1111 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001112 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001113 *dbg_tick = debug_tick;
1114 }
1115
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 /*
1117 * When not inside any ":while" loop, clear remembered lines.
1118 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001119 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 {
1121 if (lines_ga.ga_len > 0)
1122 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001123 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1125 free_cmdlines(&lines_ga);
1126 }
1127 current_line = 0;
1128 }
1129
1130 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1132 * being restored at the ":endtry". Reset them here and set the
1133 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1134 * This includes the case where a missing ":endif", ":endwhile" or
1135 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001137 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001139 cstack.cs_lflags &= ~CSL_HAD_FINA;
1140 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1141 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 did_throw ? (void *)current_exception : NULL);
1143 did_emsg = got_int = did_throw = FALSE;
1144 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1145 }
1146
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001147 // Update global "trylevel" for recursive calls to do_cmdline() from
1148 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 trylevel = initial_trylevel + cstack.cs_trylevel;
1150
1151 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001152 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 * files) is aborted because of an error, an interrupt, or an uncaught
1154 * exception, cancel everything. If it is left normally, reset
1155 * force_abort to get the non-EH compatible abortion behavior for
1156 * the rest of the script.
1157 */
1158 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1159 force_abort = FALSE;
1160
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001161 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001163#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164
1165 }
1166 /*
1167 * Continue executing command lines when:
1168 * - no CTRL-C typed, no aborting error, no exception thrown or try
1169 * conditionals need to be checked for executing finally clauses or
1170 * catching an interrupt exception
1171 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001172 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 * looping for ":source" command or function call.
1174 */
1175 while (!((got_int
1176#ifdef FEAT_EVAL
1177 || (did_emsg && force_abort) || did_throw
1178#endif
1179 )
1180#ifdef FEAT_EVAL
1181 && cstack.cs_trylevel == 0
1182#endif
1183 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001184 && !(did_emsg
1185#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001186 // Keep going when inside try/catch, so that the error can be
1187 // deal with, except when it is a syntax error, it may cause
1188 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001189 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1190#endif
1191 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001192 && (getline_equal(fgetline, cookie, getexmodeline)
1193 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 && (next_cmdline != NULL
1195#ifdef FEAT_EVAL
1196 || cstack.cs_idx >= 0
1197#endif
1198 || (flags & DOCMD_REPEAT)));
1199
1200 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001201 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202#ifdef FEAT_EVAL
1203 free_cmdlines(&lines_ga);
1204 ga_clear(&lines_ga);
1205
1206 if (cstack.cs_idx >= 0)
1207 {
1208 /*
1209 * If a sourced file or executed function ran to its end, report the
1210 * unclosed conditional.
1211 */
1212 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001213 && ((getline_equal(fgetline, cookie, getsourceline)
1214 && !source_finished(fgetline, cookie))
1215 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 && !func_has_ended(real_cookie))))
1217 {
1218 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001219 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001221 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001222 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001223 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001225 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 }
1227
1228 /*
1229 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1230 * ":endtry" in a sourced file or executed function. If the try
1231 * conditional is in its finally clause, ignore anything pending.
1232 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001233 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 */
1235 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001236 {
1237 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1238
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001239 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001240 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001241 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1242 &cstack.cs_looplevel);
1243 }
1244 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 trylevel = initial_trylevel;
1246 }
1247
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001248 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1249 // lack was reported above and the error message is to be converted to an
1250 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001251 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 ? (char_u *)"endfunction" : (char_u *)NULL);
1253
1254 if (trylevel == 0)
1255 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001256 // Just in case did_throw got set but current_exception wasn't.
1257 if (current_exception == NULL)
1258 did_throw = FALSE;
1259
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 /*
1261 * When an exception is being thrown out of the outermost try
1262 * conditional, discard the uncaught exception, disable the conversion
1263 * of interrupts or errors to exceptions, and ensure that no more
1264 * commands are executed.
1265 */
1266 if (did_throw)
1267 {
Bram Moolenaar033135e2021-01-14 21:40:22 +01001268 char *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001269 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
1271 /*
1272 * If the uncaught exception is a user exception, report it as an
1273 * error. If it is an error exception, display the saved error
1274 * message now. For an interrupt exception, do nothing; the
1275 * interrupt message is given elsewhere.
1276 */
1277 switch (current_exception->type)
1278 {
1279 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001280 vim_snprintf((char *)IObuff, IOSIZE,
1281 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 current_exception->value);
Bram Moolenaar033135e2021-01-14 21:40:22 +01001283 p = (char *)vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 break;
1285 case ET_ERROR:
1286 messages = current_exception->messages;
1287 current_exception->messages = NULL;
1288 break;
1289 case ET_INTERRUPT:
1290 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001293 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1294 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001295 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 current_exception->throw_name = NULL;
1297
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001298 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 suppress_errthrow = TRUE;
1300 force_abort = TRUE;
1301
1302 if (messages != NULL)
1303 {
1304 do
1305 {
1306 next = messages->next;
1307 emsg(messages->msg);
1308 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001309 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 vim_free(messages);
1311 messages = next;
1312 }
1313 while (messages != NULL);
1314 }
1315 else if (p != NULL)
1316 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001317 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 vim_free(p);
1319 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001320 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001321 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001322 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 }
1324
1325 /*
1326 * On an interrupt or an aborting error not converted to an exception,
1327 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001328 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 * when force_abort is set and did_emsg unset in case of an interrupt
1330 * from a finally clause after an error.
1331 */
1332 else if (got_int || (did_emsg && force_abort))
1333 suppress_errthrow = TRUE;
1334 }
1335
1336 /*
1337 * The current cstack will be freed when do_cmdline() returns. An uncaught
1338 * exception will have to be rethrown in the previous cstack. If a function
1339 * has just returned or a script file was just finished and the previous
1340 * cstack belongs to the same function or, respectively, script file, it
1341 * will have to be checked for finally clauses to be executed due to the
1342 * ":return" or ":finish". This is done in do_one_cmd().
1343 */
1344 if (did_throw)
1345 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001346 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001348 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 && ex_nesting_level > func_level(real_cookie) + 1))
1350 {
1351 if (!did_throw)
1352 check_cstack = TRUE;
1353 }
1354 else
1355 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001356 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001357 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 --ex_nesting_level;
1359 /*
1360 * Go to debug mode when returning from a function in which we are
1361 * single-stepping.
1362 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001363 if ((getline_equal(fgetline, cookie, getsourceline)
1364 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001366 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 ? (char_u *)_("End of sourced file")
1368 : (char_u *)_("End of function"));
1369 }
1370
1371 /*
1372 * Restore the exception environment (done after returning from the
1373 * debugger).
1374 */
1375 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001376 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377
1378 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001379
1380 // Cleanup if "cs_emsg_silent_list" remains.
1381 if (cstack.cs_emsg_silent_list != NULL)
1382 {
1383 eslist_T *elem, *temp;
1384
1385 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1386 {
1387 temp = elem->next;
1388 vim_free(elem);
1389 }
1390 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001391#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392
1393 /*
1394 * If there was too much output to fit on the command line, ask the user to
1395 * hit return before redrawing the screen. With the ":global" command we do
1396 * this only once after the command is finished.
1397 */
1398 if (did_inc)
1399 {
1400 --RedrawingDisabled;
1401 --no_wait_return;
1402 msg_scroll = FALSE;
1403
1404 /*
1405 * When just finished an ":if"-":else" which was typed, no need to
1406 * wait for hit-return. Also for an error situation.
1407 */
1408 if (retval == FAIL
1409#ifdef FEAT_EVAL
1410 || (did_endif && KeyTyped && !did_emsg)
1411#endif
1412 )
1413 {
1414 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001415 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 }
1417 else if (need_wait_return)
1418 {
1419 /*
1420 * The msg_start() above clears msg_didout. The wait_return we do
1421 * here should not overwrite the command that may be shown before
1422 * doing that.
1423 */
1424 msg_didout |= msg_didout_before_start;
1425 wait_return(FALSE);
1426 }
1427 }
1428
Bram Moolenaar2a942252012-11-28 23:03:07 +01001429#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001430 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001431#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 /*
1433 * Reset if_level, in case a sourced script file contains more ":if" than
1434 * ":endif" (could be ":if x | foo | endif").
1435 */
1436 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001437#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001438
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 --call_depth;
1440 return retval;
1441}
1442
1443#ifdef FEAT_EVAL
1444/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001445 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 */
1447 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001448get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001450 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 wcmd_T *wp;
1452 char_u *line;
1453
1454 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1455 {
1456 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001457 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001459 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001461 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001463 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001464 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 ++cp->current_line;
1466
1467 return line;
1468 }
1469
1470 KeyTyped = FALSE;
1471 ++cp->current_line;
1472 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001473 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 return vim_strsave(wp->line);
1475}
1476
1477/*
1478 * Store a line in "gap" so that a ":while" loop can execute it again.
1479 */
1480 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001481store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482{
1483 if (ga_grow(gap, 1) == FAIL)
1484 return FAIL;
1485 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001486 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 return OK;
1489}
1490
1491/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001492 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 */
1494 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001495free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496{
1497 while (gap->ga_len > 0)
1498 {
1499 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1500 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 }
1502}
1503#endif
1504
1505/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001506 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1507 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001510getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001511 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001512 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001513 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514{
1515#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001516 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001517 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001519 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1520 // function that's originally used to obtain the lines. This may be
1521 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001522 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001523 cp = (struct loop_cookie *)cookie;
1524 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 {
1526 gp = cp->getline;
1527 cp = cp->cookie;
1528 }
1529 return gp == func;
1530#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001531 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532#endif
1533}
1534
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001536 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 * getline function. Otherwise return "cookie".
1538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001540getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001541 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001542 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543{
Bram Moolenaar13505972019-01-24 15:04:48 +01001544#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001545 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001546 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001548 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1549 // cookie that's originally used to obtain the lines. This may be nested
1550 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001551 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001552 cp = (struct loop_cookie *)cookie;
1553 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 {
1555 gp = cp->getline;
1556 cp = cp->cookie;
1557 }
1558 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001559#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001562}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563
Bram Moolenaard5053d02020-06-28 15:51:16 +02001564#if defined(FEAT_EVAL) || defined(PROT)
1565/*
1566 * Get the next line source line without advancing.
1567 */
1568 char_u *
1569getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001570 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001571 void *cookie) // argument for fgetline()
1572{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001573 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001574 struct loop_cookie *cp;
1575 wcmd_T *wp;
1576
1577 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1578 // cookie that's originally used to obtain the lines. This may be nested
1579 // several levels.
1580 gp = fgetline;
1581 cp = (struct loop_cookie *)cookie;
1582 while (gp == get_loop_line)
1583 {
1584 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1585 {
1586 // executing lines a second time, use the stored copy
1587 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1588 return wp->line;
1589 }
1590 gp = cp->getline;
1591 cp = cp->cookie;
1592 }
1593 if (gp == getsourceline)
1594 return source_nextline(cp);
1595 return NULL;
1596}
1597#endif
1598
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001599
1600/*
1601 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1602 * ":bwipeout", etc.
1603 * Returns the buffer number.
1604 */
1605 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001606compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001607{
1608 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001609 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001610 int count = offset;
1611
1612 buf = firstbuf;
1613 while (buf->b_next != NULL && buf->b_fnum < lnum)
1614 buf = buf->b_next;
1615 while (count != 0)
1616 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001617 count += (offset < 0) ? 1 : -1;
1618 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1619 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001620 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001621 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001622 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001623 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001624 while (buf->b_ml.ml_mfp == NULL)
1625 {
1626 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1627 if (nextbuf == NULL)
1628 break;
1629 buf = nextbuf;
1630 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001631 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001632 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001633 if (addr_type == ADDR_LOADED_BUFFERS)
1634 while (buf->b_ml.ml_mfp == NULL)
1635 {
1636 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1637 if (nextbuf == NULL)
1638 break;
1639 buf = nextbuf;
1640 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001641 return buf->b_fnum;
1642}
1643
Bram Moolenaarb7316892019-05-01 18:08:42 +02001644/*
1645 * Return the window number of "win".
1646 * When "win" is NULL return the number of windows.
1647 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001648 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001649current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001650{
1651 win_T *wp;
1652 int nr = 0;
1653
Bram Moolenaar29323592016-07-24 22:04:11 +02001654 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001655 {
1656 ++nr;
1657 if (wp == win)
1658 break;
1659 }
1660 return nr;
1661}
1662
1663 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001664current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001665{
1666 tabpage_T *tp;
1667 int nr = 0;
1668
Bram Moolenaar29323592016-07-24 22:04:11 +02001669 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001670 {
1671 ++nr;
1672 if (tp == tab)
1673 break;
1674 }
1675 return nr;
1676}
1677
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001678 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001679comment_start(char_u *p, int starts_with_colon UNUSED)
1680{
1681#ifdef FEAT_EVAL
1682 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001683 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001684#endif
1685 return *p == '"';
1686}
1687
Bram Moolenaarf240e182014-11-27 18:33:02 +01001688# define CURRENT_WIN_NR current_win_nr(curwin)
1689# define LAST_WIN_NR current_win_nr(NULL)
1690# define CURRENT_TAB_NR current_tab_nr(curtab)
1691# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001692
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693/*
1694 * Execute one Ex command.
1695 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001696 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1697 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 *
1699 * 1. skip comment lines and leading space
1700 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001701 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001702 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001703 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001704 * 6. parse arguments
1705 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001707 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 *
1709 * This function may be called recursively!
1710 */
1711#if (_MSC_VER == 1200)
1712/*
Bram Moolenaared203462004-06-16 11:19:22 +00001713 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001715 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716#endif
1717 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001718do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001719 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001720 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001722 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001724 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001725 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001727 char_u *p;
1728 linenr_T lnum;
1729 long n;
1730 char *errormsg = NULL; // error message
1731 char_u *after_modifier = NULL;
1732 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001733 cmdmod_T save_cmdmod;
1734 int save_reg_executing = reg_executing;
1735 int ni; // set when Not Implemented
1736 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001737 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001738#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001739 int may_have_range;
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001740 int vim9script;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001741 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001742#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001743 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744
Bram Moolenaara80faa82020-04-12 19:37:17 +02001745 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 ea.line1 = 1;
1747 ea.line2 = 1;
1748#ifdef FEAT_EVAL
1749 ++ex_nesting_level;
1750#endif
1751
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001752 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 if (quitmore
1754#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001755 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001756 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001757#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001758 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001759 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 --quitmore;
1761
1762 /*
1763 * Reset browse, confirm, etc.. They are restored when returning, for
1764 * recursive calls.
1765 */
1766 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001768 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001769 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1770 goto doend;
1771
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001772/*
1773 * 1. Skip comment lines and leading white space and colons.
1774 * 2. Handle command modifiers.
1775 */
1776 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001778 ea.cmdlinep = cmdlinep;
1779 ea.getline = fgetline;
1780 ea.cookie = cookie;
1781#ifdef FEAT_EVAL
1782 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001783 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001785 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001786 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001787 apply_cmdmod(&cmdmod);
Bram Moolenaar2379f872021-02-14 14:07:34 +01001788#ifdef FEAT_EVAL
Bram Moolenaar39f3b142021-02-14 12:57:36 +01001789 vim9script = in_vim9script();
Bram Moolenaar2379f872021-02-14 14:07:34 +01001790#endif
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001791 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792
1793#ifdef FEAT_EVAL
1794 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1795 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1796#else
1797 ea.skip = (if_level > 0);
1798#endif
1799
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001801 * 3. Skip over the range to find the command. Let "p" point to after it.
1802 *
1803 * We need the command to know what kind of range it uses.
1804 */
1805 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001806#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001807 // In Vim9 script a colon is required before the range. This may also be
1808 // after command modifiers.
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001809 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001810 {
1811 may_have_range = FALSE;
1812 for (p = ea.cmd; p >= *cmdlinep; --p)
1813 {
1814 if (*p == ':')
1815 may_have_range = TRUE;
1816 if (p < ea.cmd && !VIM_ISWHITE(*p))
1817 break;
1818 }
1819 }
1820 else
1821 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001822 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001823#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001824 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825
1826#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001827 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001828 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001829 if (ea.cmd == cmd + 1 && *cmd == '$')
1830 // should be "$VAR = val"
1831 --ea.cmd;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01001832 p = find_ex_command(&ea, NULL, lookup_scriptitem, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001833 if (ea.cmdidx == CMD_SIZE)
1834 {
1835 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1836
1837 // If a ':' before the range is missing, give a clearer error
1838 // message.
1839 if (ar > ea.cmd)
1840 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001841 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001842 goto doend;
1843 }
1844 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001845 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846 else
1847#endif
1848 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001849
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001850#ifdef FEAT_EVAL
1851# ifdef FEAT_PROFILE
1852 // Count this line for profiling if skip is TRUE.
1853 if (do_profiling == PROF_YES
1854 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1855 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1856 {
1857 int skip = did_emsg || got_int || did_throw;
1858
1859 if (ea.cmdidx == CMD_catch)
1860 skip = !skip && !(cstack->cs_idx >= 0
1861 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1862 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1863 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1864 skip = skip || !(cstack->cs_idx >= 0
1865 && !(cstack->cs_flags[cstack->cs_idx]
1866 & (CSF_ACTIVE | CSF_TRUE)));
1867 else if (ea.cmdidx == CMD_finally)
1868 skip = FALSE;
1869 else if (ea.cmdidx != CMD_endif
1870 && ea.cmdidx != CMD_endfor
1871 && ea.cmdidx != CMD_endtry
1872 && ea.cmdidx != CMD_endwhile)
1873 skip = ea.skip;
1874
1875 if (!skip)
1876 {
1877 if (getline_equal(fgetline, cookie, get_func_line))
1878 func_line_exec(getline_cookie(fgetline, cookie));
1879 else if (getline_equal(fgetline, cookie, getsourceline))
1880 script_line_exec();
1881 }
1882 }
1883# endif
1884
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001885 // May go to debug mode. If this happens and the ">quit" debug command is
1886 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001887 dbg_check_breakpoint(&ea);
1888 if (!ea.skip && got_int)
1889 {
1890 ea.skip = TRUE;
1891 (void)do_intthrow(cstack);
1892 }
1893#endif
1894
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001895/*
1896 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 *
1898 * where 'addr' is:
1899 *
1900 * % (entire file)
1901 * $ [+-NUM]
1902 * 'x [+-NUM] (where x denotes a currently defined mark)
1903 * . [+-NUM]
1904 * [+-NUM]..
1905 * NUM
1906 *
1907 * The ea.cmd pointer is updated to point to the first character following the
1908 * range spec. If an initial address is found, but no second, the upper bound
1909 * is equal to the lower.
1910 */
1911
Bram Moolenaar25190db2019-05-04 15:05:28 +02001912 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001913 if (!IS_USER_CMDIDX(ea.cmdidx))
1914 {
1915 if (ea.cmdidx != CMD_SIZE)
1916 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1917 else
1918 ea.addr_type = ADDR_LINES;
1919
Bram Moolenaar25190db2019-05-04 15:05:28 +02001920 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001921 if (ea.cmdidx == CMD_wincmd && p != NULL)
1922 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001923#ifdef FEAT_QUICKFIX
1924 // :.cc in quickfix window uses line number
1925 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1926 ea.addr_type = ADDR_OTHER;
1927#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001928 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001929
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001930 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001931#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001932 if (!may_have_range)
1933 ea.line1 = ea.line2 = default_address(&ea);
1934 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001935#endif
1936 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1937 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001940 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 */
1942
1943 /*
1944 * Skip ':' and any white space
1945 */
1946 ea.cmd = skipwhite(ea.cmd);
1947 while (*ea.cmd == ':')
1948 ea.cmd = skipwhite(ea.cmd + 1);
1949
1950 /*
1951 * If we got a line, but no command, then go to the line.
1952 * If we find a '|' or '\n' we set ea.nextcmd.
1953 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001954 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1955 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 {
1957 /*
1958 * strange vi behaviour:
1959 * ":3" jumps to line 3
Bram Moolenaarb8554302021-02-15 21:30:30 +01001960 * ":3|..." prints line 3 (not in Vim9 script)
1961 * ":|" prints current line (not in Vim9 script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001963 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 goto doend;
Bram Moolenaarb8554302021-02-15 21:30:30 +01001965 if ((*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
1966#ifdef FEAT_EVAL
1967 && !vim9script
1968#endif
1969 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 {
1971 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001972 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 if ((errormsg = invalid_range(&ea)) == NULL)
1974 {
1975 correct_range(&ea);
1976 ex_print(&ea);
1977 }
1978 }
1979 else if (ea.addr_count != 0)
1980 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001981 if (ea.line2 > curbuf->b_ml.ml_line_count)
1982 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001983 // With '-' in 'cpoptions' a line number past the file is an
1984 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001985 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1986 ea.line2 = -1;
1987 else
1988 ea.line2 = curbuf->b_ml.ml_line_count;
1989 }
1990
1991 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001992 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 else
1994 {
1995 if (ea.line2 == 0)
1996 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 else
1998 curwin->w_cursor.lnum = ea.line2;
1999 beginline(BL_SOL | BL_FIX);
2000 }
2001 }
2002 goto doend;
2003 }
2004
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002005 // If this looks like an undefined user command and there are CmdUndefined
2006 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002007 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2008 && ASCII_ISUPPER(*ea.cmd)
2009 && has_cmdundefined())
2010 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002011 int ret;
2012
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002013 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002014 while (ASCII_ISALNUM(*p))
2015 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002016 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002017 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2018 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002019 // If the autocommands did something and didn't cause an error, try
2020 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002021 p = (ret
2022#ifdef FEAT_EVAL
2023 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002024#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002026 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002027
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 if (p == NULL)
2029 {
2030 if (!ea.skip)
Bram Moolenaard1510ee2021-01-04 16:15:58 +01002031 errormsg = _(e_ambiguous_use_of_user_defined_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 goto doend;
2033 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002034 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002035 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2036 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 {
2038 errormsg = uc_fun_cmd();
2039 goto doend;
2040 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002041
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 if (ea.cmdidx == CMD_SIZE)
2043 {
2044 if (!ea.skip)
2045 {
2046 STRCPY(IObuff, _("E492: Not an editor command"));
2047 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002048 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002049 // If the modifier was parsed OK the error must be in the
2050 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002051 if (after_modifier != NULL)
2052 append_command(after_modifier);
2053 else
2054 append_command(*cmdlinep);
2055 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002056 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002057 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 }
2059 goto doend;
2060 }
2061
Bram Moolenaar958636c2014-10-21 20:01:58 +02002062 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2063 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002064#ifdef HAVE_EX_SCRIPT_NI
2065 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2066#endif
2067 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068
2069#ifndef FEAT_EVAL
2070 /*
2071 * When the expression evaluation is disabled, recognize the ":if" and
2072 * ":endif" commands and ignore everything in between it.
2073 */
2074 if (ea.cmdidx == CMD_if)
2075 ++if_level;
2076 if (if_level)
2077 {
2078 if (ea.cmdidx == CMD_endif)
2079 --if_level;
2080 goto doend;
2081 }
2082
2083#endif
2084
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002085 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002086 if (*p == '!' && ea.cmdidx != CMD_substitute
2087 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 {
2089 ++p;
2090 ea.forceit = TRUE;
2091 }
2092 else
2093 ea.forceit = FALSE;
2094
2095/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002096 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002098 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002099 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100
2101 if (!ea.skip)
2102 {
2103#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002104 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002106 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002107 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 goto doend;
2109 }
2110#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002111 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002112 {
2113 errormsg = _("E981: Command not allowed in rvim");
2114 goto doend;
2115 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002116 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002118 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002119 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 goto doend;
2121 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002122
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002123 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002125#ifdef FEAT_CMDWIN
2126 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2127 {
2128 // Command not allowed in the command line window
2129 errormsg = _(e_cmdwin);
2130 goto doend;
2131 }
2132#endif
2133 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2134 {
2135 // Command not allowed when text is locked
2136 errormsg = _(get_text_locked_msg());
2137 goto doend;
2138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002140
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002141 // Disallow editing another buffer when "curbuf_lock" is set.
2142 // Do allow ":checktime" (it is postponed).
2143 // Do allow ":edit" (check for an argument later).
2144 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002145 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002146 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002147 && ea.cmdidx != CMD_edit
2148 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002149 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002150 && curbuf_locked())
2151 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002153 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002155 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002156 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 goto doend;
2158 }
2159 }
2160
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002161 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002163 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 goto doend;
2165 }
2166
2167 /*
2168 * Don't complain about the range if it is not used
2169 * (could happen if line_count is accidentally set to 0).
2170 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002171 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 {
2173 /*
2174 * If the range is backwards, ask for confirmation and, if given, swap
2175 * ea.line1 & ea.line2 so it's forwards again.
2176 * When global command is busy, don't ask, will fail below.
2177 */
2178 if (!global_busy && ea.line1 > ea.line2)
2179 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002180 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002182 if (sourcing || exmode_active)
2183 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002184 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002185 goto doend;
2186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 if (ask_yesno((char_u *)
2188 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002189 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 }
2191 lnum = ea.line1;
2192 ea.line1 = ea.line2;
2193 ea.line2 = lnum;
2194 }
2195 if ((errormsg = invalid_range(&ea)) != NULL)
2196 goto doend;
2197 }
2198
Bram Moolenaarb7316892019-05-01 18:08:42 +02002199 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2200 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 ea.line2 = 1;
2202
2203 correct_range(&ea);
2204
2205#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002206 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002207 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002209 // Put the first line at the start of a closed fold, put the last line
2210 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 (void)hasFolding(ea.line1, &ea.line1, NULL);
2212 (void)hasFolding(ea.line2, NULL, &ea.line2);
2213 }
2214#endif
2215
2216#ifdef FEAT_QUICKFIX
2217 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002218 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 * option here, so things like % get expanded.
2220 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002221 p = replace_makeprg(&ea, p, cmdlinep);
2222 if (p == NULL)
2223 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224#endif
2225
2226 /*
2227 * Skip to start of argument.
2228 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2229 */
2230 if (ea.cmdidx == CMD_bang)
2231 ea.arg = p;
2232 else
2233 ea.arg = skipwhite(p);
2234
Bram Moolenaar379fb762018-08-30 15:58:28 +02002235 // ":file" cannot be run with an argument when "curbuf_lock" is set
2236 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2237 goto doend;
2238
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 /*
2240 * Check for "++opt=val" argument.
2241 * Must be first, allow ":w ++enc=utf8 !cmd"
2242 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002243 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2245 if (getargopt(&ea) == FAIL && !ni)
2246 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002247 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 goto doend;
2249 }
2250
2251 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2252 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002253 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002255 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002257 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 goto doend;
2259 }
2260 ea.arg = skipwhite(ea.arg + 1);
2261 ea.append = TRUE;
2262 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002263 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 {
2265 ++ea.arg;
2266 ea.usefilter = TRUE;
2267 }
2268 }
2269
2270 if (ea.cmdidx == CMD_read)
2271 {
2272 if (ea.forceit)
2273 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002274 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 ea.forceit = FALSE;
2276 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002277 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 {
2279 ++ea.arg;
2280 ea.usefilter = TRUE;
2281 }
2282 }
2283
2284 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2285 {
2286 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002287 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
2289 ++ea.arg;
2290 ++ea.amount;
2291 }
2292 ea.arg = skipwhite(ea.arg);
2293 }
2294
2295 /*
2296 * Check for "+command" argument, before checking for next command.
2297 * Don't do this for ":read !cmd" and ":write !cmd".
2298 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002299 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2301
2302 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002303 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 * Don't do this for ":read !cmd" and ":write !cmd".
2305 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002306 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 separate_nextcmd(&ea);
2308
2309 /*
2310 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002311 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2312 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002314 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002315 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002316 || ea.cmdidx == CMD_global
2317 || ea.cmdidx == CMD_vglobal
2318 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
2320 for (p = ea.arg; *p; ++p)
2321 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002322 // Remove one backslash before a newline, so that it's possible to
2323 // pass a newline to the shell and also a newline that is preceded
2324 // with a backslash. This makes it impossible to end a shell
2325 // command in a backslash, but that doesn't appear useful.
2326 // Halving the number of backslashes is incompatible with previous
2327 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002329 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 else if (*p == '\n')
2331 {
2332 ea.nextcmd = p + 1;
2333 *p = NUL;
2334 break;
2335 }
2336 }
2337 }
2338
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002339 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002340 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002342 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002343 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002345 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002346 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002347 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002349#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002350 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002351 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002353 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002354 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 }
2356#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002357 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2358 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002359 {
2360 ea.regname = *ea.arg++;
2361#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002362 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002363 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2364 {
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002365 set_expr_line(vim_strsave(ea.arg), &ea);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002366 ea.arg += STRLEN(ea.arg);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002367 did_set_expr_line = TRUE;
Bram Moolenaar85de2062011-05-05 14:26:41 +02002368 }
2369#endif
2370 ea.arg = skipwhite(ea.arg);
2371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 }
2373
2374 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002375 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 * count, it's a buffer name.
2377 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002378 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2379 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002380 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 {
2382 n = getdigits(&ea.arg);
2383 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002384 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002386 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 goto doend;
2388 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002389 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 {
2391 ea.line2 = n;
2392 if (ea.addr_count == 0)
2393 ea.addr_count = 1;
2394 }
2395 else
2396 {
2397 ea.line1 = ea.line2;
2398 ea.line2 += n - 1;
2399 ++ea.addr_count;
2400 /*
2401 * Be vi compatible: no error message for out of range.
2402 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002403 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 ea.line2 = curbuf->b_ml.ml_line_count;
2405 }
2406 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002407
2408 /*
2409 * Check for flags: 'l', 'p' and '#'.
2410 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002411 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002412 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002413 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2414 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002416 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002417 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 goto doend;
2419 }
2420
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002421 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002423 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 goto doend;
2425 }
2426
2427#ifdef FEAT_EVAL
2428 /*
2429 * Skip the command when it's not going to be executed.
2430 * The commands like :if, :endif, etc. always need to be executed.
2431 * Also make an exception for commands that handle a trailing command
2432 * themselves.
2433 */
2434 if (ea.skip)
2435 {
2436 switch (ea.cmdidx)
2437 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002438 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 case CMD_while:
2440 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002441 case CMD_for:
2442 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 case CMD_if:
2444 case CMD_elseif:
2445 case CMD_else:
2446 case CMD_endif:
2447 case CMD_try:
2448 case CMD_catch:
2449 case CMD_finally:
2450 case CMD_endtry:
2451 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002452 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 break;
2454
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002455 // Commands that handle '|' themselves. Check: A command should
2456 // either have the EX_TRLBAR flag, appear in this list or appear in
2457 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 case CMD_aboveleft:
2459 case CMD_and:
2460 case CMD_belowright:
2461 case CMD_botright:
2462 case CMD_browse:
2463 case CMD_call:
2464 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002465 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 case CMD_delfunction:
2467 case CMD_djump:
2468 case CMD_dlist:
2469 case CMD_dsearch:
2470 case CMD_dsplit:
2471 case CMD_echo:
2472 case CMD_echoerr:
2473 case CMD_echomsg:
2474 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002475 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002477 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002478 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 case CMD_help:
2480 case CMD_hide:
2481 case CMD_ijump:
2482 case CMD_ilist:
2483 case CMD_isearch:
2484 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002485 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 case CMD_keepjumps:
2487 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002488 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 case CMD_leftabove:
2490 case CMD_let:
2491 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002492 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002493 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002495 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002496 case CMD_noautocmd:
2497 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 case CMD_perl:
2499 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002500 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002501 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002502 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 case CMD_return:
2504 case CMD_rightbelow:
2505 case CMD_ruby:
2506 case CMD_silent:
2507 case CMD_smagic:
2508 case CMD_snomagic:
2509 case CMD_substitute:
2510 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002511 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 case CMD_tcl:
2513 case CMD_throw:
2514 case CMD_tilde:
2515 case CMD_topleft:
2516 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002517 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002518 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 case CMD_verbose:
2520 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002521 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 break;
2523
2524 default: goto doend;
2525 }
2526 }
2527#endif
2528
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002529 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 {
2531 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2532 goto doend;
2533 }
2534
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 /*
2536 * Accept buffer name. Cannot be used at the same time with a buffer
2537 * number. Don't do this for a user command.
2538 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002539 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002540 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 {
2542 /*
2543 * :bdelete, :bwipeout and :bunload take several arguments, separated
2544 * by spaces: find next space (skipping over escaped characters).
2545 * The others take one argument: ignore trailing spaces.
2546 */
2547 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2548 || ea.cmdidx == CMD_bunload)
2549 p = skiptowhite_esc(ea.arg);
2550 else
2551 {
2552 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002553 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 --p;
2555 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002556 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002557 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002558 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 goto doend;
2560 ea.addr_count = 1;
2561 ea.arg = skipwhite(p);
2562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002564 // The :try command saves the emsg_silent flag, reset it here when
2565 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002566 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002567 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002568 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002569 if (emsg_silent < 0)
2570 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002571 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002572 }
2573
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002575 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577
Bram Moolenaar958636c2014-10-21 20:01:58 +02002578 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 {
2580 /*
2581 * Execute a user-defined command.
2582 */
2583 do_ucmd(&ea);
2584 }
2585 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 {
2587 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002588 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 */
2590 ea.errmsg = NULL;
2591 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2592 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002593 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 }
2595
2596#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002598 // Not if this was a command that wasn't executed or :endif.
Bram Moolenaar16531552020-02-08 16:00:46 +01002599 if (getline_equal(ea.getline, ea.cookie, getsourceline)
Bram Moolenaard3f8a9e2021-02-17 21:57:03 +01002600 && current_sctx.sc_sid > 0
2601 && ea.cmdidx != CMD_endif
2602 && (cstack->cs_idx < 0
2603 || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
Bram Moolenaar2b327002020-12-26 15:39:31 +01002604 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 /*
2607 * If the command just executed called do_cmdline(), any throw or ":return"
2608 * or ":finish" encountered there must also check the cstack of the still
2609 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2610 * exception, or reanimate a returned function or finished script file and
2611 * return or finish it again.
2612 */
2613 if (need_rethrow)
2614 do_throw(cstack);
2615 else if (check_cstack)
2616 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002617 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002619 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 && current_func_returned())
2621 do_return(&ea, TRUE, FALSE, NULL);
2622 }
2623 need_rethrow = check_cstack = FALSE;
2624#endif
2625
2626doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002627 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002628 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002630 curwin->w_cursor.col = 0;
2631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632
2633 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2634 {
2635 if (sourcing)
2636 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002637 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 {
2639 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002640 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002642 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 }
2644 emsg(errormsg);
2645 }
2646#ifdef FEAT_EVAL
2647 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002648 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2649 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002650
2651 if (did_set_expr_line)
2652 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653#endif
2654
Bram Moolenaare1004402020-10-24 20:49:43 +02002655 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002657 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002659 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 ea.nextcmd = NULL;
2661
2662#ifdef FEAT_EVAL
2663 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002664 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665#endif
2666
2667 return ea.nextcmd;
2668}
2669#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002670 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671#endif
2672
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002673static char ex_error_buf[MSG_BUF_LEN];
2674
2675/*
2676 * Return an error message with argument included.
2677 * Uses a static buffer, only the last error will be kept.
2678 * "msg" will be translated, caller should use N_().
2679 */
2680 char *
2681ex_errmsg(char *msg, char_u *arg)
2682{
2683 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2684 return ex_error_buf;
2685}
2686
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687/*
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002688 * Check for an Ex command with optional tail.
2689 * If there is a match advance "pp" to the argument and return TRUE.
2690 * If "noparen" is TRUE do not recognize the command followed by "(".
2691 */
2692 static int
2693checkforcmd_opt(
2694 char_u **pp, // start of command
2695 char *cmd, // name of command
2696 int len, // required length
2697 int noparen)
2698{
2699 int i;
2700
2701 for (i = 0; cmd[i] != NUL; ++i)
2702 if (((char_u *)cmd)[i] != (*pp)[i])
2703 break;
2704 if (i >= len && !isalpha((*pp)[i])
2705 && (*pp)[i] != '_' && (!noparen || (*pp)[i] != '('))
2706 {
2707 *pp = skipwhite(*pp + i);
2708 return TRUE;
2709 }
2710 return FALSE;
2711}
2712
2713/*
2714 * Check for an Ex command with optional tail.
2715 * If there is a match advance "pp" to the argument and return TRUE.
2716 */
2717 int
2718checkforcmd(
2719 char_u **pp, // start of command
2720 char *cmd, // name of command
2721 int len) // required length
2722{
2723 return checkforcmd_opt(pp, cmd, len, FALSE);
2724}
2725
2726/*
2727 * Check for an Ex command with optional tail, not followed by "(".
2728 * If there is a match advance "pp" to the argument and return TRUE.
2729 */
2730 static int
2731checkforcmd_noparen(
2732 char_u **pp, // start of command
2733 char *cmd, // name of command
2734 int len) // required length
2735{
2736 return checkforcmd_opt(pp, cmd, len, TRUE);
2737}
2738
2739/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002740 * Parse and skip over command modifiers:
2741 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002742 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002743 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002744 * When "skip_only" is TRUE the global variables are not changed, except for
2745 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002746 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2747 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002748 * Call apply_cmdmod() to get the side effects of the modifiers:
2749 * - Increment "sandbox" for ":sandbox"
2750 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002751 * - set msg_silent for ":silent"
2752 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002753 * Return FAIL when the command is not to be executed.
2754 * May set "errormsg" to an error message.
2755 */
2756 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002757parse_command_modifiers(
2758 exarg_T *eap,
2759 char **errormsg,
2760 cmdmod_T *cmod,
2761 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002762{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002763 char_u *p;
2764 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002765
Bram Moolenaare1004402020-10-24 20:49:43 +02002766 CLEAR_POINTER(cmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002767
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002768 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002769 for (;;)
2770 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002771 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002772 {
2773 if (*eap->cmd == ':')
2774 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002775 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002776 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002777
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002778 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002779 if (*eap->cmd == NUL && exmode_active
2780 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2781 || getline_equal(eap->getline, eap->cookie, getexline))
2782 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2783 {
2784 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002785 if (!skip_only)
2786 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002787 }
2788
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002789 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002790 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002791 return FAIL;
2792 if (*eap->cmd == NUL)
2793 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002794 if (!skip_only)
2795 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002796 return FAIL;
2797 }
2798
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002799 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002800
2801 // In Vim9 script a variable can shadow a command modifier:
2802 // verbose = 123
2803 // verbose += 123
2804 // silent! verbose = func()
2805 // verbose.member = 2
2806 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002807 // But not:
2808 // verbose [a, b] = list
Bram Moolenaar17126b12021-01-07 22:03:02 +01002809 if (in_vim9script())
2810 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002811 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002812
2813 for (s = p; ASCII_ISALPHA(*s); ++s)
2814 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002815 n = skipwhite(s);
2816 if (vim_strchr((char_u *)".=", *n) != NULL
2817 || *s == '['
2818 || (*n != NUL && n[1] == '='))
Bram Moolenaar17126b12021-01-07 22:03:02 +01002819 break;
2820 }
2821
Bram Moolenaareffed932018-08-14 13:38:17 +02002822 switch (*p)
2823 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002824 // When adding an entry, also modify cmd_exists().
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002825 case 'a': if (!checkforcmd_noparen(&eap->cmd, "aboveleft", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002826 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002827 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002828 continue;
2829
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002830 case 'b': if (checkforcmd_noparen(&eap->cmd, "belowright", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002831 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002832 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002833 continue;
2834 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002835 if (checkforcmd_opt(&eap->cmd, "browse", 3, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002836 {
2837#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002838 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002839#endif
2840 continue;
2841 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002842 if (!checkforcmd_noparen(&eap->cmd, "botright", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002843 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002844 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002845 continue;
2846
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002847 case 'c': if (!checkforcmd_opt(&eap->cmd, "confirm", 4, TRUE))
Bram Moolenaareffed932018-08-14 13:38:17 +02002848 break;
2849#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002850 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002851#endif
2852 continue;
2853
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002854 case 'k': if (checkforcmd_noparen(&eap->cmd, "keepmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002855 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002856 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002857 continue;
2858 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002859 if (checkforcmd_noparen(&eap->cmd, "keepalt", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002860 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002861 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002862 continue;
2863 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002864 if (checkforcmd_noparen(&eap->cmd, "keeppatterns", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002865 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002866 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002867 continue;
2868 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002869 if (!checkforcmd_noparen(&eap->cmd, "keepjumps", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002870 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002871 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002872 continue;
2873
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002874 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002875 {
2876 char_u *reg_pat;
2877
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002878 if (!checkforcmd_noparen(&p, "filter", 4)
Bram Moolenaareffed932018-08-14 13:38:17 +02002879 || *p == NUL || ends_excmd(*p))
2880 break;
2881 if (*p == '!')
2882 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002883 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002884 p = skipwhite(p + 1);
2885 if (*p == NUL || ends_excmd(*p))
2886 break;
2887 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002888#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002889 // Avoid that "filter(arg)" is recognized.
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +01002890 if (in_vim9script() && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002891 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002892#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002893 if (skip_only)
2894 p = skip_vimgrep_pat(p, NULL, NULL);
2895 else
2896 // NOTE: This puts a NUL after the pattern.
2897 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002898 if (p == NULL || *p == NUL)
2899 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002900 if (!skip_only)
2901 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002902 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002903 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002904 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002905 break;
2906 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002907 eap->cmd = p;
2908 continue;
2909 }
2910
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002911 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002912 case 'h': if (p != eap->cmd || !checkforcmd_noparen(&p, "hide", 3)
Bram Moolenaareffed932018-08-14 13:38:17 +02002913 || *p == NUL || ends_excmd(*p))
2914 break;
2915 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002916 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002917 continue;
2918
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002919 case 'l': if (checkforcmd_noparen(&eap->cmd, "lockmarks", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002920 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002921 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002922 continue;
2923 }
2924
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002925 if (!checkforcmd_noparen(&eap->cmd, "leftabove", 5))
Bram Moolenaareffed932018-08-14 13:38:17 +02002926 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002927 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002928 continue;
2929
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002930 case 'n': if (checkforcmd_noparen(&eap->cmd, "noautocmd", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002931 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002932 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002933 continue;
2934 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002935 if (!checkforcmd_noparen(&eap->cmd, "noswapfile", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002936 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002937 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002938 continue;
2939
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002940 case 'r': if (!checkforcmd_noparen(&eap->cmd, "rightbelow", 6))
Bram Moolenaareffed932018-08-14 13:38:17 +02002941 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002942 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002943 continue;
2944
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002945 case 's': if (checkforcmd_noparen(&eap->cmd, "sandbox", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002946 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002947 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02002948 continue;
2949 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002950 if (!checkforcmd_noparen(&eap->cmd, "silent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002951 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002952 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002953 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2954 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002955 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002956 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02002957 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002958 }
2959 continue;
2960
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002961 case 't': if (checkforcmd_noparen(&p, "tab", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002962 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002963 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002964 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002965 long tabnr = get_address(eap, &eap->cmd,
2966 ADDR_TABS, eap->skip,
2967 skip_only, FALSE, 1);
2968 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02002969 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01002970 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002971 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002972 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2973 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002974 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002975 return FAIL;
2976 }
Bram Moolenaare1004402020-10-24 20:49:43 +02002977 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002978 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002979 }
2980 eap->cmd = p;
2981 continue;
2982 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002983 if (!checkforcmd_noparen(&eap->cmd, "topleft", 2))
Bram Moolenaareffed932018-08-14 13:38:17 +02002984 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002985 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02002986 continue;
2987
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002988 case 'u': if (!checkforcmd_noparen(&eap->cmd, "unsilent", 3))
Bram Moolenaareffed932018-08-14 13:38:17 +02002989 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002990 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002991 continue;
2992
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002993 case 'v': if (checkforcmd_noparen(&eap->cmd, "vertical", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02002994 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002995 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002996 continue;
2997 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01002998 if (checkforcmd_noparen(&eap->cmd, "vim9cmd", 4))
Bram Moolenaar39f3b142021-02-14 12:57:36 +01002999 {
3000 if (ends_excmd2(p, eap->cmd))
3001 {
3002 *errormsg =
3003 _(e_vim9cmd_must_be_followed_by_command);
3004 return FAIL;
3005 }
3006 cmod->cmod_flags |= CMOD_VIM9CMD;
3007 continue;
3008 }
Bram Moolenaarf49a1fc2021-03-27 22:20:21 +01003009 if (!checkforcmd_noparen(&p, "verbose", 4))
Bram Moolenaareffed932018-08-14 13:38:17 +02003010 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003011 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02003012 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003013 else
Bram Moolenaare1004402020-10-24 20:49:43 +02003014 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02003015 eap->cmd = p;
3016 continue;
3017 }
3018 break;
3019 }
3020
3021 return OK;
3022}
3023
3024/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01003025 * Return TRUE if "cmod" has anything set.
3026 */
3027 int
3028has_cmdmod(cmdmod_T *cmod)
3029{
3030 return cmod->cmod_flags != 0
3031 || cmod->cmod_split != 0
3032 || cmod->cmod_verbose != 0
3033 || cmod->cmod_tab != 0
3034 || cmod->cmod_filter_regmatch.regprog != NULL;
3035}
3036
3037/*
3038 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
3039 */
3040 int
3041cmdmod_error(void)
3042{
3043 if (in_vim9script() && has_cmdmod(&cmdmod))
3044 {
3045 emsg(_(e_misplaced_command_modifier));
3046 return TRUE;
3047 }
3048 return FALSE;
3049}
3050
3051/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003052 * Apply the command modifiers. Saves current state in "cmdmod", call
3053 * undo_cmdmod() later.
3054 */
3055 void
3056apply_cmdmod(cmdmod_T *cmod)
3057{
3058#ifdef HAVE_SANDBOX
3059 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3060 {
3061 ++sandbox;
3062 cmod->cmod_did_sandbox = TRUE;
3063 }
3064#endif
3065 if (cmod->cmod_verbose > 0)
3066 {
3067 if (cmod->cmod_verbose_save == 0)
3068 cmod->cmod_verbose_save = p_verbose + 1;
3069 p_verbose = cmod->cmod_verbose;
3070 }
3071
3072 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3073 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003074 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003075 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003076 cmod->cmod_save_msg_scroll = msg_scroll;
3077 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003078 if (cmod->cmod_flags & CMOD_SILENT)
3079 ++msg_silent;
3080 if (cmod->cmod_flags & CMOD_UNSILENT)
3081 msg_silent = 0;
3082
3083 if (cmod->cmod_flags & CMOD_ERRSILENT)
3084 {
3085 ++emsg_silent;
3086 ++cmod->cmod_did_esilent;
3087 }
3088
Bram Moolenaare1004402020-10-24 20:49:43 +02003089 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003090 {
3091 // Set 'eventignore' to "all".
3092 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003093 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003094 set_string_option_direct((char_u *)"ei", -1,
3095 (char_u *)"all", OPT_FREE, SID_NONE);
3096 }
3097}
3098
3099/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003100 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003101 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003102 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003103undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003104{
Bram Moolenaare1004402020-10-24 20:49:43 +02003105 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003106 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003107 p_verbose = cmod->cmod_verbose_save - 1;
3108 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003109 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003110
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003111#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003112 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003113 {
3114 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003115 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003116 }
3117#endif
3118
Bram Moolenaare1004402020-10-24 20:49:43 +02003119 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003120 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003121 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003122 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3123 OPT_FREE, SID_NONE);
3124 free_string_option(cmod->cmod_save_ei);
3125 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003126 }
3127
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003128 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003129
Bram Moolenaare1004402020-10-24 20:49:43 +02003130 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003131 {
3132 // messages could be enabled for a serious error, need to check if the
3133 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003134 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3135 msg_silent = cmod->cmod_save_msg_silent - 1;
3136 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003137 if (emsg_silent < 0)
3138 emsg_silent = 0;
3139 // Restore msg_scroll, it's set by file I/O commands, even when no
3140 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003141 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003142
3143 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3144 // somewhere in the line. Put it back in the first column.
3145 if (redirecting())
3146 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003147
Bram Moolenaare1004402020-10-24 20:49:43 +02003148 cmod->cmod_save_msg_silent = 0;
3149 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003150 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003151}
3152
3153/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003154 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003155 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003156 * Return FAIL and set "errormsg" or return OK.
3157 */
3158 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003159parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003160{
3161 int address_count = 1;
3162 linenr_T lnum;
3163
3164 // Repeat for all ',' or ';' separated addresses.
3165 for (;;)
3166 {
3167 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003168 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003169 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003170 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003171 eap->addr_count == 0, address_count++);
3172 if (eap->cmd == NULL) // error detected
3173 return FAIL;
3174 if (lnum == MAXLNUM)
3175 {
3176 if (*eap->cmd == '%') // '%' - all lines
3177 {
3178 ++eap->cmd;
3179 switch (eap->addr_type)
3180 {
3181 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003182 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003183 eap->line1 = 1;
3184 eap->line2 = curbuf->b_ml.ml_line_count;
3185 break;
3186 case ADDR_LOADED_BUFFERS:
3187 {
3188 buf_T *buf = firstbuf;
3189
3190 while (buf->b_next != NULL
3191 && buf->b_ml.ml_mfp == NULL)
3192 buf = buf->b_next;
3193 eap->line1 = buf->b_fnum;
3194 buf = lastbuf;
3195 while (buf->b_prev != NULL
3196 && buf->b_ml.ml_mfp == NULL)
3197 buf = buf->b_prev;
3198 eap->line2 = buf->b_fnum;
3199 break;
3200 }
3201 case ADDR_BUFFERS:
3202 eap->line1 = firstbuf->b_fnum;
3203 eap->line2 = lastbuf->b_fnum;
3204 break;
3205 case ADDR_WINDOWS:
3206 case ADDR_TABS:
3207 if (IS_USER_CMDIDX(eap->cmdidx))
3208 {
3209 eap->line1 = 1;
3210 eap->line2 = eap->addr_type == ADDR_WINDOWS
3211 ? LAST_WIN_NR : LAST_TAB_NR;
3212 }
3213 else
3214 {
3215 // there is no Vim command which uses '%' and
3216 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003217 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003218 return FAIL;
3219 }
3220 break;
3221 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003222 case ADDR_UNSIGNED:
3223 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003224 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003225 return FAIL;
3226 case ADDR_ARGUMENTS:
3227 if (ARGCOUNT == 0)
3228 eap->line1 = eap->line2 = 0;
3229 else
3230 {
3231 eap->line1 = 1;
3232 eap->line2 = ARGCOUNT;
3233 }
3234 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003235 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003236#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003237 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003238 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003239 if (eap->line2 == 0)
3240 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003241#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003242 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003243 case ADDR_NONE:
3244 // Will give an error later if a range is found.
3245 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003246 }
3247 ++eap->addr_count;
3248 }
3249 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3250 {
3251 pos_T *fp;
3252
3253 // '*' - visual area
3254 if (eap->addr_type != ADDR_LINES)
3255 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003256 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003257 return FAIL;
3258 }
3259
3260 ++eap->cmd;
3261 if (!eap->skip)
3262 {
3263 fp = getmark('<', FALSE);
3264 if (check_mark(fp) == FAIL)
3265 return FAIL;
3266 eap->line1 = fp->lnum;
3267 fp = getmark('>', FALSE);
3268 if (check_mark(fp) == FAIL)
3269 return FAIL;
3270 eap->line2 = fp->lnum;
3271 ++eap->addr_count;
3272 }
3273 }
3274 }
3275 else
3276 eap->line2 = lnum;
3277 eap->addr_count++;
3278
3279 if (*eap->cmd == ';')
3280 {
3281 if (!eap->skip)
3282 {
3283 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003284 // Don't leave the cursor on an illegal line or column, but do
3285 // accept zero as address, so 0;/PATTERN/ works correctly.
3286 if (eap->line2 > 0)
3287 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003288 }
3289 }
3290 else if (*eap->cmd != ',')
3291 break;
3292 ++eap->cmd;
3293 }
3294
3295 // One address given: set start and end lines.
3296 if (eap->addr_count == 1)
3297 {
3298 eap->line1 = eap->line2;
3299 // ... but only implicit: really no address given
3300 if (lnum == MAXLNUM)
3301 eap->addr_count = 0;
3302 }
3303 return OK;
3304}
3305
3306/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003307 * Append "cmd" to the error message in IObuff.
3308 * Takes care of limiting the length and handling 0xa0, which would be
3309 * invisible otherwise.
3310 */
3311 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003312append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003313{
3314 char_u *s = cmd;
3315 char_u *d;
3316
3317 STRCAT(IObuff, ": ");
3318 d = IObuff + STRLEN(IObuff);
3319 while (*s != NUL && d - IObuff < IOSIZE - 7)
3320 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003321 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003322 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003323 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003324 STRCPY(d, "<a0>");
3325 d += 4;
3326 }
3327 else
3328 MB_COPY_CHAR(s, d);
3329 }
3330 *d = NUL;
3331}
3332
3333/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003334 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3335 * the name. Otherwise just return "start".
3336 */
3337 char_u *
3338skip_option_env_lead(char_u *start)
3339{
3340 char_u *name = start;
3341
3342 if (*start == '&')
3343 {
3344 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3345 name += 3;
3346 else
3347 name += 1;
3348 }
3349 else if (*start == '$')
3350 name += 1;
3351 return name;
3352}
3353
3354/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003356 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003357 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003358 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 *
3360 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3361 * assignment without "let". Sets eap->cmdidx to the command while returning
3362 * "eap->cmd".
3363 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 * Returns NULL for an ambiguous user command.
3365 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003366 char_u *
3367find_ex_command(
3368 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003369 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003370 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003371 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372{
3373 int len;
3374 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003375 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377#ifdef FEAT_EVAL
3378 /*
3379 * Recognize a Vim9 script function/method call and assignment:
3380 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3381 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003382 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003383 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003385 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386
Bram Moolenaar83144542020-08-02 20:40:43 +02003387 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003388 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003390 int oplen;
3391 int heredoc;
3392 char_u *swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003394 if (
3395 // "(..." is an expression.
3396 // "funcname(" is always a function call.
3397 *p == '('
3398 || (p == eap->cmd
3399 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003400 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003401 *eap->cmd == '{'
3402 // "'string'->func()" is an expression.
3403 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003404 // '"string"->func()' is an expression.
3405 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003406 // "g:varname" is an expression.
3407 || eap->cmd[1] == ':'
3408 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003409 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003410 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003411 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003412 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3413 {
3414 // "{" by itself is the start of a block.
3415 eap->cmdidx = CMD_block;
3416 return eap->cmd + 1;
3417 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003418 eap->cmdidx = CMD_eval;
3419 return eap->cmd;
3420 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003421
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003422 if (p != eap->cmd && (
3423 // "varname[]" is an expression.
3424 *p == '['
3425 // "varname.key" is an expression.
3426 || (*p == '.' && ASCII_ISALPHA(p[1]))))
3427 {
3428 char_u *after = p;
3429
3430 // When followed by "=" or "+=" then it is an assignment.
3431 ++emsg_silent;
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01003432 if (*after == '.')
3433 after = skipwhite(after + 1);
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003434 if (skip_expr(&after, NULL) == OK)
3435 after = skipwhite(after);
3436 else
3437 after = (char_u *)"";
3438 if (*after == '=' || (*after != NUL && after[1] == '=')
3439 || (after[0] == '.' && after[1] == '.'
3440 && after[2] == '='))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003441 eap->cmdidx = CMD_var;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003442 else
3443 eap->cmdidx = CMD_eval;
3444 --emsg_silent;
3445 return eap->cmd;
3446 }
3447
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003448 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3449 // an assignment.
3450 // If there is no line break inside the "[...]" then "p" is
3451 // advanced to after the "]" by to_name_const_end(): check if a "="
3452 // follows.
3453 // If "[...]" has a line break "p" still points at the "[" and it
3454 // can't be an assignment.
3455 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003456 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003457 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003458 if (p == eap->cmd && *p == '[')
3459 {
3460 int count = 0;
3461 int semicolon = FALSE;
3462
3463 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3464 }
3465 if (p == NULL || p == eap->cmd || *skipwhite(p) != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003466 {
3467 eap->cmdidx = CMD_eval;
3468 return eap->cmd;
3469 }
3470 if (p > eap->cmd && *skipwhite(p) == '=')
3471 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003472 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003473 return eap->cmd;
3474 }
3475 }
3476
3477 // Recognize an assignment if we recognize the variable name:
3478 // "g:var = expr"
Bram Moolenaare0890d62021-02-17 14:52:14 +01003479 // "var = expr" where "var" is a variable name.
Bram Moolenaar83144542020-08-02 20:40:43 +02003480 if (*eap->cmd == '@')
3481 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003482 oplen = assignment_len(skipwhite(p), &heredoc);
3483 if (oplen > 0)
3484 {
3485 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3486 || *eap->cmd == '&'
3487 || *eap->cmd == '$'
3488 || *eap->cmd == '@'
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003489 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003490 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003491 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003492 return eap->cmd;
3493 }
3494 }
3495
3496 // Recognize using a type for a w:, b:, t: or g: variable:
3497 // "w:varname: number = 123".
3498 if (eap->cmd[1] == ':' && *p == ':')
3499 {
3500 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003501 return eap->cmd;
3502 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003503 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003504
3505 // If it is an ID it might be a variable with an operator on the next
3506 // line, if the variable exists it can't be an Ex command.
3507 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003508 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003509 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3510 {
3511 eap->cmdidx = CMD_eval;
3512 return eap->cmd;
3513 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 }
3515#endif
3516
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 /*
3518 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003519 * Exceptions:
Bram Moolenaar10b94212021-02-19 21:42:57 +01003520 * - The 'k' command can directly be followed by any character.
3521 * But it is not used in Vim9 script.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003523 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003525 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 */
3527 p = eap->cmd;
3528 if (*p == 'k')
3529 {
3530 eap->cmdidx = CMD_k;
3531 ++p;
3532 }
3533 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003534 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3535 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 || p[1] == 'g'
3537 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3538 || p[1] == 'I'
3539 || (p[1] == 'r' && p[2] != 'e')))
3540 {
3541 eap->cmdidx = CMD_substitute;
3542 ++p;
3543 }
3544 else
3545 {
3546 while (ASCII_ISALPHA(*p))
3547 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003548 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003549 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003551 while (ASCII_ISALNUM(*p))
3552 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 }
3554 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3555 {
3556 // include "9" for "vim9script"
3557 ++p;
3558 while (ASCII_ISALPHA(*p))
3559 ++p;
3560 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003561
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003562 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003563 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 ++p;
3565 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003566 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3567 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003568 // Check for ":dl", ":dell", etc. to ":deletel": that's
3569 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003570 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003571 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003572 break;
3573 if (i == len - 1)
3574 {
3575 --len;
3576 if (p[-1] == 'l')
3577 eap->flags |= EXFLAG_LIST;
3578 else
3579 eap->flags |= EXFLAG_PRINT;
3580 }
3581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003583 if (ASCII_ISLOWER(eap->cmd[0]))
3584 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003585 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003586 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003587
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003588 if (command_count != (int)CMD_SIZE)
3589 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003590 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003591 getout(1);
3592 }
3593
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003594 // Use a precomputed index for fast look-up in cmdnames[]
3595 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003596 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3597 if (ASCII_ISLOWER(c2))
3598 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3599 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003600 else if (ASCII_ISUPPER(eap->cmd[0]))
3601 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003603 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
3605 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3606 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3607 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3608 (size_t)len) == 0)
3609 {
3610#ifdef FEAT_EVAL
3611 if (full != NULL
3612 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3613 *full = TRUE;
3614#endif
3615 break;
3616 }
3617
Bram Moolenaar95388e32020-11-20 21:07:00 +01003618 // Not not recognize ":*" as the star command unless '*' is in
3619 // 'cpoptions'.
3620 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3621 p = eap->cmd;
3622
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003623 // Look for a user defined command as a last resort. Let ":Print" be
3624 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003625 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3626 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003628 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 while (ASCII_ISALNUM(*p))
3630 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003631 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003633 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 eap->cmdidx = CMD_SIZE;
3635 }
3636
Bram Moolenaar373863e2020-09-26 17:20:53 +02003637 // ":fina" means ":finally" for backwards compatibility.
3638 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3639 eap->cmdidx = CMD_finally;
3640
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003641#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003642 if (eap->cmdidx < CMD_SIZE
3643 && in_vim9script()
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003644 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003645 && (eap->cmdidx < 0 ||
3646 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003647 {
3648 semsg(_(e_command_not_followed_by_white_space_str), eap->cmd);
3649 eap->cmdidx = CMD_SIZE;
3650 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003651#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003652
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 return p;
3654}
3655
3656#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003657static struct cmdmod
3658{
3659 char *name;
3660 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003661 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003662} cmdmods[] = {
3663 {"aboveleft", 3, FALSE},
3664 {"belowright", 3, FALSE},
3665 {"botright", 2, FALSE},
3666 {"browse", 3, FALSE},
3667 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003668 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003669 {"hide", 3, FALSE},
3670 {"keepalt", 5, FALSE},
3671 {"keepjumps", 5, FALSE},
3672 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003673 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003674 {"leftabove", 5, FALSE},
3675 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003676 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003677 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003678 {"rightbelow", 6, FALSE},
3679 {"sandbox", 3, FALSE},
3680 {"silent", 3, FALSE},
3681 {"tab", 3, TRUE},
3682 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003683 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003684 {"verbose", 4, TRUE},
3685 {"vertical", 4, FALSE},
3686};
3687
3688/*
3689 * Return length of a command modifier (including optional count).
3690 * Return zero when it's not a modifier.
3691 */
3692 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003693modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003694{
3695 int i, j;
3696 char_u *p = cmd;
3697
3698 if (VIM_ISDIGIT(*cmd))
3699 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003700 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003701 {
3702 for (j = 0; p[j] != NUL; ++j)
3703 if (p[j] != cmdmods[i].name[j])
3704 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003705 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003706 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003707 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003708 }
3709 return 0;
3710}
3711
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712/*
3713 * Return > 0 if an Ex command "name" exists.
3714 * Return 2 if there is an exact match.
3715 * Return 3 if there is an ambiguous match.
3716 */
3717 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003718cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719{
3720 exarg_T ea;
3721 int full = FALSE;
3722 int i;
3723 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003724 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003726 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003727 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 {
3729 for (j = 0; name[j] != NUL; ++j)
3730 if (name[j] != cmdmods[i].name[j])
3731 break;
3732 if (name[j] == NUL && j >= cmdmods[i].minlen)
3733 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3734 }
3735
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003736 // Check built-in commands and user defined commands.
3737 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003738 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003741 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003743 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3744 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003745 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003746 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3748}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003749
3750/*
3751 * "fullcommand" function
3752 */
3753 void
3754f_fullcommand(typval_T *argvars, typval_T *rettv)
3755{
3756 exarg_T ea;
3757 char_u *name = argvars[0].vval.v_string;
3758 char_u *p;
3759
3760 while (name[0] != NUL && name[0] == ':')
3761 name++;
3762 name = skip_range(name, TRUE, NULL);
3763
3764 rettv->v_type = VAR_STRING;
3765
3766 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
3767 ea.cmdidx = (cmdidx_T)0;
3768 p = find_ex_command(&ea, NULL, NULL, NULL);
3769 if (p == NULL || ea.cmdidx == CMD_SIZE)
3770 return;
3771
3772 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
3773 ? get_user_commands(NULL, ea.useridx)
3774 : cmdnames[ea.cmdidx].cmd_name);
3775}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776#endif
3777
Bram Moolenaard0190392019-08-23 21:17:35 +02003778 cmdidx_T
3779excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780{
Bram Moolenaard0190392019-08-23 21:17:35 +02003781 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782
Bram Moolenaard0190392019-08-23 21:17:35 +02003783 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3784 idx = (cmdidx_T)((int)idx + 1))
3785 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 break;
3787
Bram Moolenaard0190392019-08-23 21:17:35 +02003788 return idx;
3789}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790
Bram Moolenaard0190392019-08-23 21:17:35 +02003791 long
3792excmd_get_argt(cmdidx_T idx)
3793{
3794 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795}
3796
3797/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003798 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 *
3800 * Backslashed delimiters after / or ? will be skipped, and commands will
3801 * not be expanded between /'s and ?'s or after "'".
3802 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003803 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 * Returns the "cmd" pointer advanced to beyond the range.
3805 */
3806 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003807skip_range(
3808 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003809 int skip_star, // skip "*" used for Visual range
3810 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003812 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003814 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003816 if (*cmd == '\\')
3817 {
3818 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3819 ++cmd;
3820 else
3821 break;
3822 }
3823 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 {
3825 if (*++cmd == NUL && ctx != NULL)
3826 *ctx = EXPAND_NOTHING;
3827 }
3828 else if (*cmd == '/' || *cmd == '?')
3829 {
3830 delim = *cmd++;
3831 while (*cmd != NUL && *cmd != delim)
3832 if (*cmd++ == '\\' && *cmd != NUL)
3833 ++cmd;
3834 if (*cmd == NUL && ctx != NULL)
3835 *ctx = EXPAND_NOTHING;
3836 }
3837 if (*cmd != NUL)
3838 ++cmd;
3839 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003840
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003841 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003842 while (*cmd == ':')
3843 cmd = skipwhite(cmd + 1);
3844
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003845 // Skip "*" used for Visual range.
3846 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3847 cmd = skipwhite(cmd + 1);
3848
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 return cmd;
3850}
3851
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003852 static void
3853addr_error(cmd_addr_T addr_type)
3854{
3855 if (addr_type == ADDR_NONE)
3856 emsg(_(e_norange));
3857 else
3858 emsg(_(e_invrange));
3859}
3860
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003862 * Return the default address for an address type.
3863 */
3864 static linenr_T
3865default_address(exarg_T *eap)
3866{
3867 linenr_T lnum = 0;
3868
3869 switch (eap->addr_type)
3870 {
3871 case ADDR_LINES:
3872 case ADDR_OTHER:
3873 // Default is the cursor line number. Avoid using an invalid
3874 // line number though.
3875 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3876 lnum = curbuf->b_ml.ml_line_count;
3877 else
3878 lnum = curwin->w_cursor.lnum;
3879 break;
3880 case ADDR_WINDOWS:
3881 lnum = CURRENT_WIN_NR;
3882 break;
3883 case ADDR_ARGUMENTS:
3884 lnum = curwin->w_arg_idx + 1;
3885 if (lnum > ARGCOUNT)
3886 lnum = ARGCOUNT;
3887 break;
3888 case ADDR_LOADED_BUFFERS:
3889 case ADDR_BUFFERS:
3890 lnum = curbuf->b_fnum;
3891 break;
3892 case ADDR_TABS:
3893 lnum = CURRENT_TAB_NR;
3894 break;
3895 case ADDR_TABS_RELATIVE:
3896 case ADDR_UNSIGNED:
3897 lnum = 1;
3898 break;
3899 case ADDR_QUICKFIX:
3900#ifdef FEAT_QUICKFIX
3901 lnum = qf_get_cur_idx(eap);
3902#endif
3903 break;
3904 case ADDR_QUICKFIX_VALID:
3905#ifdef FEAT_QUICKFIX
3906 lnum = qf_get_cur_valid_idx(eap);
3907#endif
3908 break;
3909 case ADDR_NONE:
3910 // Will give an error later if a range is found.
3911 break;
3912 }
3913 return lnum;
3914}
3915
3916/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003917 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 *
3919 * Set ptr to the next character after the part that was interpreted.
3920 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003921 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 *
3923 * Return MAXLNUM when no Ex address was found.
3924 */
3925 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003926get_address(
3927 exarg_T *eap UNUSED,
3928 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003929 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003930 int skip, // only skip the address, don't use it
3931 int silent, // no errors or side effects
3932 int to_other_file, // flag: may jump to other file
3933 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934{
3935 int c;
3936 int i;
3937 long n;
3938 char_u *cmd;
3939 pos_T pos;
3940 pos_T *fp;
3941 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003942 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943
3944 cmd = skipwhite(*ptr);
3945 lnum = MAXLNUM;
3946 do
3947 {
3948 switch (*cmd)
3949 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003950 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003951 ++cmd;
3952 switch (addr_type)
3953 {
3954 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003955 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 lnum = curwin->w_cursor.lnum;
3957 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003958 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003959 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003960 break;
3961 case ADDR_ARGUMENTS:
3962 lnum = curwin->w_arg_idx + 1;
3963 break;
3964 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003965 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003966 lnum = curbuf->b_fnum;
3967 break;
3968 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003969 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003970 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003971 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003972 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003973 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003974 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003975 cmd = NULL;
3976 goto error;
3977 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003978 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003979#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003980 lnum = qf_get_cur_idx(eap);
3981#endif
3982 break;
3983 case ADDR_QUICKFIX_VALID:
3984#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003985 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003986#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003987 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003988 }
3989 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003991 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003992 ++cmd;
3993 switch (addr_type)
3994 {
3995 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003996 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 lnum = curbuf->b_ml.ml_line_count;
3998 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003999 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004000 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004001 break;
4002 case ADDR_ARGUMENTS:
4003 lnum = ARGCOUNT;
4004 break;
4005 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004006 buf = lastbuf;
4007 while (buf->b_ml.ml_mfp == NULL)
4008 {
4009 if (buf->b_prev == NULL)
4010 break;
4011 buf = buf->b_prev;
4012 }
4013 lnum = buf->b_fnum;
4014 break;
4015 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004016 lnum = lastbuf->b_fnum;
4017 break;
4018 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004019 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004020 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004021 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004022 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004023 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004024 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004025 cmd = NULL;
4026 goto error;
4027 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004028 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004029#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004030 lnum = qf_get_size(eap);
4031 if (lnum == 0)
4032 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004033#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004034 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004035 case ADDR_QUICKFIX_VALID:
4036#ifdef FEAT_QUICKFIX
4037 lnum = qf_get_valid_size(eap);
4038 if (lnum == 0)
4039 lnum = 1;
4040#endif
4041 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004042 }
4043 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004045 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004046 if (*++cmd == NUL)
4047 {
4048 cmd = NULL;
4049 goto error;
4050 }
4051 if (addr_type != ADDR_LINES)
4052 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004053 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004054 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004055 goto error;
4056 }
4057 if (skip)
4058 ++cmd;
4059 else
4060 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004061 // Only accept a mark in another file when it is
4062 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004063 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4064 ++cmd;
4065 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004066 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004067 lnum = curwin->w_cursor.lnum;
4068 else
4069 {
4070 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 {
4072 cmd = NULL;
4073 goto error;
4074 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004075 lnum = fp->lnum;
4076 }
4077 }
4078 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079
4080 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004081 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004082 c = *cmd++;
4083 if (addr_type != ADDR_LINES)
4084 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004085 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004086 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004087 goto error;
4088 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004089 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004090 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004091 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004092 if (*cmd == c)
4093 ++cmd;
4094 }
4095 else
4096 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004097 int flags;
4098
4099 pos = curwin->w_cursor; // save curwin->w_cursor
4100
4101 // When '/' or '?' follows another address, start from
4102 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004103 if (lnum != MAXLNUM)
4104 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004105
4106 // Start a forward search at the end of the line (unless
4107 // before the first line).
4108 // Start a backward search at the start of the line.
4109 // This makes sure we never match in the current
4110 // line, and can match anywhere in the
4111 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004112 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004113 curwin->w_cursor.col = MAXCOL;
4114 else
4115 curwin->w_cursor.col = 0;
4116 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004117 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004118 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004119 {
4120 curwin->w_cursor = pos;
4121 cmd = NULL;
4122 goto error;
4123 }
4124 lnum = curwin->w_cursor.lnum;
4125 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004126 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004127 cmd += searchcmdlen;
4128 }
4129 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004131 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004132 ++cmd;
4133 if (addr_type != ADDR_LINES)
4134 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004135 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004136 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004137 goto error;
4138 }
4139 if (*cmd == '&')
4140 i = RE_SUBST;
4141 else if (*cmd == '?' || *cmd == '/')
4142 i = RE_SEARCH;
4143 else
4144 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004145 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004146 cmd = NULL;
4147 goto error;
4148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004150 if (!skip)
4151 {
4152 /*
4153 * When search follows another address, start from
4154 * there.
4155 */
4156 if (lnum != MAXLNUM)
4157 pos.lnum = lnum;
4158 else
4159 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004161 /*
4162 * Start the search just like for the above
4163 * do_search().
4164 */
4165 if (*cmd != '?')
4166 pos.col = MAXCOL;
4167 else
4168 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004169 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004170 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004171 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004172 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004173 lnum = pos.lnum;
4174 else
4175 {
4176 cmd = NULL;
4177 goto error;
4178 }
4179 }
4180 ++cmd;
4181 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182
4183 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004184 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004185 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187
4188 for (;;)
4189 {
4190 cmd = skipwhite(cmd);
4191 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4192 break;
4193
4194 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004195 {
4196 switch (addr_type)
4197 {
4198 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004199 case ADDR_OTHER:
4200 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004201 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004202 break;
4203 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004204 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004205 break;
4206 case ADDR_ARGUMENTS:
4207 lnum = curwin->w_arg_idx + 1;
4208 break;
4209 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004210 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004211 lnum = curbuf->b_fnum;
4212 break;
4213 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004214 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004215 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004216 case ADDR_TABS_RELATIVE:
4217 lnum = 1;
4218 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004219 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004220#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004221 lnum = qf_get_cur_idx(eap);
4222#endif
4223 break;
4224 case ADDR_QUICKFIX_VALID:
4225#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004226 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004227#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004228 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004229 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004230 case ADDR_UNSIGNED:
4231 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004232 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004233 }
4234 }
4235
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004237 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 else
4239 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004240 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 n = 1;
4242 else
4243 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004244
4245 if (addr_type == ADDR_TABS_RELATIVE)
4246 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004247 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004248 cmd = NULL;
4249 goto error;
4250 }
4251 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004252 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004253 lnum = compute_buffer_local_count(
4254 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004256 {
4257#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004258 // Relative line addressing, need to adjust for folded lines
4259 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004260 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4261 && address_count >= 2)
4262 (void)hasFolding(lnum, NULL, &lnum);
4263#endif
4264 if (i == '-')
4265 lnum -= n;
4266 else
4267 lnum += n;
4268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 }
4270 } while (*cmd == '/' || *cmd == '?');
4271
4272error:
4273 *ptr = cmd;
4274 return lnum;
4275}
4276
4277/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004278 * Set eap->line1 and eap->line2 to the whole range.
4279 * Used for commands with the EX_DFLALL flag and no range given.
4280 */
4281 static void
4282address_default_all(exarg_T *eap)
4283{
4284 eap->line1 = 1;
4285 switch (eap->addr_type)
4286 {
4287 case ADDR_LINES:
4288 case ADDR_OTHER:
4289 eap->line2 = curbuf->b_ml.ml_line_count;
4290 break;
4291 case ADDR_LOADED_BUFFERS:
4292 {
4293 buf_T *buf = firstbuf;
4294
4295 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4296 buf = buf->b_next;
4297 eap->line1 = buf->b_fnum;
4298 buf = lastbuf;
4299 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4300 buf = buf->b_prev;
4301 eap->line2 = buf->b_fnum;
4302 }
4303 break;
4304 case ADDR_BUFFERS:
4305 eap->line1 = firstbuf->b_fnum;
4306 eap->line2 = lastbuf->b_fnum;
4307 break;
4308 case ADDR_WINDOWS:
4309 eap->line2 = LAST_WIN_NR;
4310 break;
4311 case ADDR_TABS:
4312 eap->line2 = LAST_TAB_NR;
4313 break;
4314 case ADDR_TABS_RELATIVE:
4315 eap->line2 = 1;
4316 break;
4317 case ADDR_ARGUMENTS:
4318 if (ARGCOUNT == 0)
4319 eap->line1 = eap->line2 = 0;
4320 else
4321 eap->line2 = ARGCOUNT;
4322 break;
4323 case ADDR_QUICKFIX_VALID:
4324#ifdef FEAT_QUICKFIX
4325 eap->line2 = qf_get_valid_size(eap);
4326 if (eap->line2 == 0)
4327 eap->line2 = 1;
4328#endif
4329 break;
4330 case ADDR_NONE:
4331 case ADDR_UNSIGNED:
4332 case ADDR_QUICKFIX:
4333 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4334 break;
4335 }
4336}
4337
4338
4339/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004340 * Get flags from an Ex command argument.
4341 */
4342 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004343get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004344{
4345 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4346 {
4347 if (*eap->arg == 'l')
4348 eap->flags |= EXFLAG_LIST;
4349 else if (*eap->arg == 'p')
4350 eap->flags |= EXFLAG_PRINT;
4351 else
4352 eap->flags |= EXFLAG_NR;
4353 eap->arg = skipwhite(eap->arg + 1);
4354 }
4355}
4356
4357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 * Function called for command which is Not Implemented. NI!
4359 */
4360 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004361ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362{
4363 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004364 eap->errmsg =
4365 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366}
4367
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004368#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369/*
4370 * Function called for script command which is Not Implemented. NI!
4371 * Skips over ":perl <<EOF" constructs.
4372 */
4373 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004374ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375{
4376 if (!eap->skip)
4377 ex_ni(eap);
4378 else
4379 vim_free(script_get(eap, eap->arg));
4380}
4381#endif
4382
4383/*
4384 * Check range in Ex command for validity.
4385 * Return NULL when valid, error message when invalid.
4386 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004387 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004388invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004390 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004391
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 if ( eap->line1 < 0
4393 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004394 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004395 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004396
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004397 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004398 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004399 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004400 {
4401 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004402 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004403#ifdef FEAT_DIFF
4404 + (eap->cmdidx == CMD_diffget)
4405#endif
4406 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004407 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004408 break;
4409 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004410 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004411 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004412 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004413 break;
4414 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004415 // Only a boundary check, not whether the buffers actually
4416 // exist.
4417 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004418 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004419 break;
4420 case ADDR_LOADED_BUFFERS:
4421 buf = firstbuf;
4422 while (buf->b_ml.ml_mfp == NULL)
4423 {
4424 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004425 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004426 buf = buf->b_next;
4427 }
4428 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004429 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004430 buf = lastbuf;
4431 while (buf->b_ml.ml_mfp == NULL)
4432 {
4433 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004434 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004435 buf = buf->b_prev;
4436 }
4437 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004438 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004439 break;
4440 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004441 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004442 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004443 break;
4444 case ADDR_TABS:
4445 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004446 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004447 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004448 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004449 case ADDR_OTHER:
4450 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004451 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004452 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004453#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004454 // No error for value that is too big, will use the last entry.
4455 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004456 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004457#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004458 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004459 case ADDR_QUICKFIX_VALID:
4460#ifdef FEAT_QUICKFIX
4461 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4462 || eap->line2 < 0)
4463 return _(e_invrange);
4464#endif
4465 break;
4466 case ADDR_UNSIGNED:
4467 if (eap->line2 < 0)
4468 return _(e_invrange);
4469 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004470 case ADDR_NONE:
4471 // Will give an error elsewhere.
4472 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004473 }
4474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 return NULL;
4476}
4477
4478/*
4479 * Correct the range for zero line number, if required.
4480 */
4481 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004482correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004484 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 {
4486 if (eap->line1 == 0)
4487 eap->line1 = 1;
4488 if (eap->line2 == 0)
4489 eap->line2 = 1;
4490 }
4491}
4492
Bram Moolenaar748bf032005-02-02 23:04:36 +00004493#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004494/*
4495 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4496 * pattern. Otherwise return eap->arg.
4497 */
4498 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004499skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004500{
4501 char_u *p = eap->arg;
4502
Bram Moolenaara37420f2006-02-04 22:37:47 +00004503 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4504 || eap->cmdidx == CMD_vimgrepadd
4505 || eap->cmdidx == CMD_lvimgrepadd
4506 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004507 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004508 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004509 if (p == NULL)
4510 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004511 }
4512 return p;
4513}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004514
4515/*
4516 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4517 * in the command line, so that things like % get expanded.
4518 */
4519 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004520replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004521{
4522 char_u *new_cmdline;
4523 char_u *program;
4524 char_u *pos;
4525 char_u *ptr;
4526 int len;
4527 int i;
4528
4529 /*
4530 * Don't do it when ":vimgrep" is used for ":grep".
4531 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004532 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4533 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4534 || eap->cmdidx == CMD_grepadd
4535 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004536 && !grep_internal(eap->cmdidx))
4537 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004538 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4539 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004540 {
4541 if (*curbuf->b_p_gp == NUL)
4542 program = p_gp;
4543 else
4544 program = curbuf->b_p_gp;
4545 }
4546 else
4547 {
4548 if (*curbuf->b_p_mp == NUL)
4549 program = p_mp;
4550 else
4551 program = curbuf->b_p_mp;
4552 }
4553
4554 p = skipwhite(p);
4555
4556 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4557 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004558 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004559 i = 1;
4560 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4561 ++i;
4562 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004563 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004564 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004565 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004566 ptr = new_cmdline;
4567 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4568 {
4569 i = (int)(pos - program);
4570 STRNCPY(ptr, program, i);
4571 STRCPY(ptr += i, p);
4572 ptr += len;
4573 program = pos + 2;
4574 }
4575 STRCPY(ptr, program);
4576 }
4577 else
4578 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004579 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004580 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004581 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004582 STRCPY(new_cmdline, program);
4583 STRCAT(new_cmdline, " ");
4584 STRCAT(new_cmdline, p);
4585 }
4586 msg_make(p);
4587
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004588 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004589 vim_free(*cmdlinep);
4590 *cmdlinep = new_cmdline;
4591 p = new_cmdline;
4592 }
4593 return p;
4594}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004595#endif
4596
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597/*
4598 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004599 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 * Return FAIL for failure, OK otherwise.
4601 */
4602 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004603expand_filename(
4604 exarg_T *eap,
4605 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004606 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004608 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 char_u *repl;
4610 int srclen;
4611 char_u *p;
4612 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004613 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614
Bram Moolenaar748bf032005-02-02 23:04:36 +00004615#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004616 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004617 p = skip_grep_pat(eap);
4618#else
4619 p = eap->arg;
4620#endif
4621
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 /*
4623 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4624 * the file name contains a wildcard it should not cause expanding.
4625 * (it will be expanded anyway if there is a wildcard before replacing).
4626 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004627 has_wildcards = mch_has_wildcard(p);
4628 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004630#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004631 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004632 if (p[0] == '`' && p[1] == '=')
4633 {
4634 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004635 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004636 if (*p == '`')
4637 ++p;
4638 continue;
4639 }
4640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 /*
4642 * Quick check if this cannot be the start of a special string.
4643 * Also removes backslash before '%', '#' and '<'.
4644 */
4645 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4646 {
4647 ++p;
4648 continue;
4649 }
4650
4651 /*
4652 * Try to find a match at this position.
4653 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004654 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4655 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004656 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004658 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 {
4660 p += srclen;
4661 continue;
4662 }
4663
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004664 // Wildcards won't be expanded below, the replacement is taken
4665 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004666 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4667 {
4668 char_u *l = repl;
4669
4670 repl = expand_env_save(repl);
4671 vim_free(l);
4672 }
4673
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004674 // Need to escape white space et al. with a backslash.
4675 // Don't do this for:
4676 // - replacement that already has been escaped: "##"
4677 // - shell commands (may have to use quotes instead).
4678 // - non-unix systems when there is a single argument (spaces don't
4679 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004681 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 && eap->cmdidx != CMD_grep
4684 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004685 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004686 && eap->cmdidx != CMD_lgrep
4687 && eap->cmdidx != CMD_lgrepadd
4688 && eap->cmdidx != CMD_lmake
4689 && eap->cmdidx != CMD_make
4690 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004692 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693#endif
4694 )
4695 {
4696 char_u *l;
4697#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004698 // Don't escape a backslash here, because rem_backslash() doesn't
4699 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 static char_u *nobslash = (char_u *)" \t\"|";
4701# define ESCAPE_CHARS nobslash
4702#else
4703# define ESCAPE_CHARS escape_chars
4704#endif
4705
4706 for (l = repl; *l; ++l)
4707 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4708 {
4709 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4710 if (l != NULL)
4711 {
4712 vim_free(repl);
4713 repl = l;
4714 }
4715 break;
4716 }
4717 }
4718
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004719 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004720 if ((eap->usefilter || eap->cmdidx == CMD_bang
4721 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004722 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 {
4724 char_u *l;
4725
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004726 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 if (l != NULL)
4728 {
4729 vim_free(repl);
4730 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 }
4732 }
4733
4734 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4735 vim_free(repl);
4736 if (p == NULL)
4737 return FAIL;
4738 }
4739
4740 /*
4741 * One file argument: Expand wildcards.
4742 * Don't do this with ":r !command" or ":w !command".
4743 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004744 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 {
4746 /*
4747 * May do this twice:
4748 * 1. Replace environment variables.
4749 * 2. Replace any other wildcards, remove backslashes.
4750 */
4751 for (n = 1; n <= 2; ++n)
4752 {
4753 if (n == 2)
4754 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 /*
4756 * Halve the number of backslashes (this is Vi compatible).
4757 * For Unix and OS/2, when wildcards are expanded, this is
4758 * done by ExpandOne() below.
4759 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004760#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 if (!has_wildcards)
4762#endif
4763 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 }
4765
4766 if (has_wildcards)
4767 {
4768 if (n == 1)
4769 {
4770 /*
4771 * First loop: May expand environment variables. This
4772 * can be done much faster with expand_env() than with
4773 * something else (e.g., calling a shell).
4774 * After expanding environment variables, check again
4775 * if there are still wildcards present.
4776 */
4777 if (vim_strchr(eap->arg, '$') != NULL
4778 || vim_strchr(eap->arg, '~') != NULL)
4779 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004780 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004781 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 has_wildcards = mch_has_wildcard(NameBuff);
4783 p = NameBuff;
4784 }
4785 else
4786 p = NULL;
4787 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004788 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 {
4790 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004791 int options = WILD_LIST_NOTFOUND
4792 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793
4794 ExpandInit(&xpc);
4795 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004796 if (p_wic)
4797 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004799 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 if (p == NULL)
4801 return FAIL;
4802 }
4803 if (p != NULL)
4804 {
4805 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4806 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004807 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 vim_free(p);
4809 }
4810 }
4811 }
4812 }
4813 return OK;
4814}
4815
4816/*
4817 * Replace part of the command line, keeping eap->cmd, eap->arg and
4818 * eap->nextcmd correct.
4819 * "src" points to the part that is to be replaced, of length "srclen".
4820 * "repl" is the replacement string.
4821 * Returns a pointer to the character after the replaced string.
4822 * Returns NULL for failure.
4823 */
4824 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004825repl_cmdline(
4826 exarg_T *eap,
4827 char_u *src,
4828 int srclen,
4829 char_u *repl,
4830 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831{
4832 int len;
4833 int i;
4834 char_u *new_cmdline;
4835
4836 /*
4837 * The new command line is build in new_cmdline[].
4838 * First allocate it.
4839 * Careful: a "+cmd" argument may have been NUL terminated.
4840 */
4841 len = (int)STRLEN(repl);
4842 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004843 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004844 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004845 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004846 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847
4848 /*
4849 * Copy the stuff before the expanded part.
4850 * Copy the expanded stuff.
4851 * Copy what came after the expanded part.
4852 * Copy the next commands, if there are any.
4853 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004854 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004856
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004858 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004860 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004862 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 {
4864 i = (int)STRLEN(new_cmdline) + 1;
4865 STRCPY(new_cmdline + i, eap->nextcmd);
4866 eap->nextcmd = new_cmdline + i;
4867 }
4868 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4869 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4870 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4871 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4872 vim_free(*cmdlinep);
4873 *cmdlinep = new_cmdline;
4874
4875 return src;
4876}
4877
4878/*
4879 * Check for '|' to separate commands and '"' to start comments.
4880 */
4881 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004882separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883{
4884 char_u *p;
4885
Bram Moolenaar86b68352004-12-27 21:59:20 +00004886#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004887 p = skip_grep_pat(eap);
4888#else
4889 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004890#endif
4891
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004892 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 {
4894 if (*p == Ctrl_V)
4895 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004896 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004897 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004899 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004900 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004901 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 break;
4903 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004904
4905#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004906 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004907 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004908 {
4909 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004910 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01004911 if (*p == NUL) // stop at NUL after CTRL-V
4912 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004913 }
4914#endif
4915
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004916 // Check for '"': start of comment or '|': next command
4917 // :@" and :*" do not start a comment!
4918 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004919 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004920#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004921 && !in_vim9script()
4922#endif
4923 && !(eap->argt & EX_NOTRLCOM)
4924 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4925 || p != eap->arg)
4926 && (eap->cmdidx != CMD_redir
4927 || p != eap->arg + 1 || p[-1] != '@'))
4928#ifdef FEAT_EVAL
4929 || (*p == '#'
4930 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004931 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004932 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 || *p == '|' || *p == '\n')
4935 {
4936 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004937 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 * AND 'b' is present in 'cpoptions'.
4939 */
4940 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004941 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004943 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 --p;
4945 }
4946 else
4947 {
4948 eap->nextcmd = check_nextcmd(p);
4949 *p = NUL;
4950 break;
4951 }
4952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004954
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004955 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 del_trailing_spaces(eap->arg);
4957}
4958
4959/*
4960 * get + command from ex argument
4961 */
4962 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004963getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964{
4965 char_u *arg = *argp;
4966 char_u *command = NULL;
4967
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004968 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
4970 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004971 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 command = dollar_command;
4973 else
4974 {
4975 command = arg;
4976 arg = skip_cmd_arg(command, TRUE);
4977 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004978 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 }
4980
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004981 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 *argp = arg;
4983 }
4984 return command;
4985}
4986
4987/*
4988 * Find end of "+command" argument. Skip over "\ " and "\\".
4989 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004990 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004991skip_cmd_arg(
4992 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004993 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994{
4995 while (*p && !vim_isspace(*p))
4996 {
4997 if (*p == '\\' && p[1] != NUL)
4998 {
4999 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005000 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 else
5002 ++p;
5003 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005004 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
5006 return p;
5007}
5008
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005009 int
5010get_bad_opt(char_u *p, exarg_T *eap)
5011{
5012 if (STRICMP(p, "keep") == 0)
5013 eap->bad_char = BAD_KEEP;
5014 else if (STRICMP(p, "drop") == 0)
5015 eap->bad_char = BAD_DROP;
5016 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5017 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005018 else
5019 return FAIL;
5020 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005021}
5022
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023/*
5024 * Get "++opt=arg" argument.
5025 * Return FAIL or OK.
5026 */
5027 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005028getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029{
5030 char_u *arg = eap->arg + 2;
5031 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005032 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005035 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5037 {
5038 if (*arg == 'n')
5039 {
5040 arg += 2;
5041 eap->force_bin = FORCE_NOBIN;
5042 }
5043 else
5044 eap->force_bin = FORCE_BIN;
5045 if (!checkforcmd(&arg, "binary", 3))
5046 return FAIL;
5047 eap->arg = skipwhite(arg);
5048 return OK;
5049 }
5050
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005051 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005052 if (STRNCMP(arg, "edit", 4) == 0)
5053 {
5054 eap->read_edit = TRUE;
5055 eap->arg = skipwhite(arg + 4);
5056 return OK;
5057 }
5058
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 if (STRNCMP(arg, "ff", 2) == 0)
5060 {
5061 arg += 2;
5062 pp = &eap->force_ff;
5063 }
5064 else if (STRNCMP(arg, "fileformat", 10) == 0)
5065 {
5066 arg += 10;
5067 pp = &eap->force_ff;
5068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 else if (STRNCMP(arg, "enc", 3) == 0)
5070 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005071 if (STRNCMP(arg, "encoding", 8) == 0)
5072 arg += 8;
5073 else
5074 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 pp = &eap->force_enc;
5076 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005077 else if (STRNCMP(arg, "bad", 3) == 0)
5078 {
5079 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005080 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082
5083 if (pp == NULL || *arg != '=')
5084 return FAIL;
5085
5086 ++arg;
5087 *pp = (int)(arg - eap->cmd);
5088 arg = skip_cmd_arg(arg, FALSE);
5089 eap->arg = skipwhite(arg);
5090 *arg = NUL;
5091
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 if (pp == &eap->force_ff)
5093 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5095 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005096 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005098 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005100 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5102 *p = TOLOWER_ASC(*p);
5103 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005104 else
5105 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005106 // Check ++bad= argument. Must be a single-byte character, "keep" or
5107 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005108 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005109 return FAIL;
5110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111
5112 return OK;
5113}
5114
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005116ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117{
5118 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005119 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 * directory for security reasons.
5121 */
5122 if (secure)
5123 {
5124 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005125 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 }
5127 else if (eap->cmdidx == CMD_autocmd)
5128 do_autocmd(eap->arg, eap->forceit);
5129 else
5130 do_augroup(eap->arg, eap->forceit);
5131}
5132
5133/*
5134 * ":doautocmd": Apply the automatic commands to the current buffer.
5135 */
5136 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005137ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005139 char_u *arg = eap->arg;
5140 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005141 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005142
Bram Moolenaar1610d052016-06-09 22:53:01 +02005143 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005144 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005145 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005146 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149/*
5150 * :[N]bunload[!] [N] [bufname] unload buffer
5151 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5152 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5153 */
5154 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005155ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005157 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005158 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 eap->errmsg = do_bufdel(
5160 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5161 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5162 : DOBUF_UNLOAD, eap->arg,
5163 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5164}
5165
5166/*
5167 * :[N]buffer [N] to buffer N
5168 * :[N]sbuffer [N] to buffer N
5169 */
5170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005171ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005173 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005174 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005176 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 else
5178 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005179 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5181 else
5182 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005183 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005184 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
5186}
5187
5188/*
5189 * :[N]bmodified [N] to next mod. buffer
5190 * :[N]sbmodified [N] to next mod. buffer
5191 */
5192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005193ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194{
5195 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005196 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005197 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198}
5199
5200/*
5201 * :[N]bnext [N] to next buffer
5202 * :[N]sbnext [N] split and to next buffer
5203 */
5204 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005205ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005207 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005208 return;
5209
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005211 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005212 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213}
5214
5215/*
5216 * :[N]bNext [N] to previous buffer
5217 * :[N]bprevious [N] to previous buffer
5218 * :[N]sbNext [N] split and to previous buffer
5219 * :[N]sbprevious [N] split and to previous buffer
5220 */
5221 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005222ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005224 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005225 return;
5226
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005228 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005229 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230}
5231
5232/*
5233 * :brewind to first buffer
5234 * :bfirst to first buffer
5235 * :sbrewind split and to first buffer
5236 * :sbfirst split and to first buffer
5237 */
5238 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005239ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005241 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005242 return;
5243
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005245 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005246 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247}
5248
5249/*
5250 * :blast to last buffer
5251 * :sblast split and to last buffer
5252 */
5253 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005254ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005256 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005257 return;
5258
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005260 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005261 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263
Bram Moolenaar7a092242020-04-16 22:10:49 +02005264/*
5265 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005266 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005267 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005269ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005271 int comment_char = '"';
5272
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005273#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005274 if (in_vim9script())
5275 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005276#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005277 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005278}
5279
5280/*
5281 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5282 * to "cmd_start" or has a white space character before it.
5283 */
5284 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005285ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005286{
5287 int c = *cmd;
5288
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005289 if (c == NUL || c == '|' || c == '\n')
5290 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005291#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005292 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005293 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5294 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005295 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005296#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005297 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298}
5299
5300#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5301 || defined(PROTO)
5302/*
5303 * Return the next command, after the first '|' or '\n'.
5304 * Return NULL if not found.
5305 */
5306 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005307find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308{
5309 while (*p != '|' && *p != '\n')
5310 {
5311 if (*p == NUL)
5312 return NULL;
5313 ++p;
5314 }
5315 return (p + 1);
5316}
5317#endif
5318
5319/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005320 * Check if *p is a separator between Ex commands, skipping over white space.
5321 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 */
5323 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005324check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005326 char_u *s = skipwhite(p);
5327
5328 if (*s == '|' || *s == '\n')
5329 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 else
5331 return NULL;
5332}
5333
5334/*
5335 * - if there are more files to edit
5336 * - and this is the last window
5337 * - and forceit not used
5338 * - and not repeated twice on a row
5339 * return FAIL and give error message if 'message' TRUE
5340 * return OK otherwise
5341 */
5342 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005343check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005344 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005345 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346{
5347 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5348
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005349 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005350 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 {
5352 if (message)
5353 {
5354#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005355 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5356 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005358 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005360 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5361 NGETTEXT("%d more file to edit. Quit anyway?",
5362 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5364 return OK;
5365 return FAIL;
5366 }
5367#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005368 semsg(NGETTEXT("E173: %d more file to edit",
5369 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005370 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 }
5372 return FAIL;
5373 }
5374 return OK;
5375}
5376
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377/*
5378 * Function given to ExpandGeneric() to obtain the list of command names.
5379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005381get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382{
5383 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 return cmdnames[idx].cmd_name;
5386}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005389ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390{
Bram Moolenaare6850792010-05-14 15:28:44 +02005391 if (*eap->arg == NUL)
5392 {
5393#ifdef FEAT_EVAL
5394 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5395 char_u *p = NULL;
5396
5397 if (expr != NULL)
5398 {
5399 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005400 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005401 --emsg_off;
5402 vim_free(expr);
5403 }
5404 if (p != NULL)
5405 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005406 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005407 vim_free(p);
5408 }
5409 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005410 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005411#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005412 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005413#endif
5414 }
5415 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005416 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005417
5418#ifdef FEAT_VTP
5419 else if (has_vtp_working())
5420 {
5421 // background color change requires clear + redraw
5422 update_screen(CLEAR);
5423 redrawcmd();
5424 }
5425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426}
5427
5428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005429ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430{
5431 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005432 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 do_highlight(eap->arg, eap->forceit, FALSE);
5434}
5435
5436
5437/*
5438 * Call this function if we thought we were going to exit, but we won't
5439 * (because of an error). May need to restore the terminal mode.
5440 */
5441 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005442not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443{
5444 exiting = FALSE;
5445 settmode(TMODE_RAW);
5446}
5447
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005448 static int
5449before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5450{
5451 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5452
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005453 // Bail out when autocommands closed the window.
5454 // Refuse to quit when the buffer in the last window is being closed (can
5455 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005456 if (!win_valid(wp)
5457 || curbuf_locked()
5458 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5459 return TRUE;
5460
5461 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5462 {
5463 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005464 // Refuse to quit when locked or when the window was closed or the
5465 // buffer in the last window is being closed (can only happen in
5466 // autocommands).
5467 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005468 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5469 return TRUE;
5470 }
5471
5472 return FALSE;
5473}
5474
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005476 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005477 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005478 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005480 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005481ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005483 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005484
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485#ifdef FEAT_CMDWIN
5486 if (cmdwin_type != 0)
5487 {
5488 cmdwin_result = Ctrl_C;
5489 return;
5490 }
5491#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005492 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005493 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005494 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005495 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005496 return;
5497 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005498 if (eap->addr_count > 0)
5499 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005500 int wnr = eap->line2;
5501
5502 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5503 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005504 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005505 }
5506 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005507 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005508
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005509 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005510 if (curbuf_locked())
5511 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005512
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005513 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005514 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005515 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516
5517#ifdef FEAT_NETBEANS_INTG
5518 netbeansForcedQuit = eap->forceit;
5519#endif
5520
5521 /*
5522 * If there are more files or windows we won't exit.
5523 */
5524 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5525 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005526 if ((!buf_hide(wp->w_buffer)
5527 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005528 | (eap->forceit ? CCGD_FORCEIT : 0)
5529 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005531 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 {
5533 not_exiting();
5534 }
5535 else
5536 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005537 // quit last window
5538 // Note: only_one_window() returns true, even so a help window is
5539 // still open. In that case only quit, if no address has been
5540 // specified. Example:
5541 // :h|wincmd w|1q - don't quit
5542 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005543 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005545 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005546#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005548#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005549 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005550 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 }
5552}
5553
5554/*
5555 * ":cquit".
5556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005558ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005560 // this does not always pass on the exit code to the Manx compiler. why?
5561 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562}
5563
5564/*
5565 * ":qall": try to quit all windows
5566 */
5567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005568ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569{
5570# ifdef FEAT_CMDWIN
5571 if (cmdwin_type != 0)
5572 {
5573 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005574 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 else
5576 cmdwin_result = K_XF2;
5577 return;
5578 }
5579# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005580
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005581 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005582 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005583 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005584 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005585 return;
5586 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005587
5588 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005589 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005590
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005592 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 getout(0);
5594 not_exiting();
5595}
5596
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597/*
5598 * ":close": close current window, unless it is the last one
5599 */
5600 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005601ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005603 win_T *win;
5604 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005605#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005607 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005609#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005610 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005611 {
5612 if (eap->addr_count == 0)
5613 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005614 else
5615 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005616 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005617 {
5618 winnr++;
5619 if (winnr == eap->line2)
5620 break;
5621 }
5622 if (win == NULL)
5623 win = lastwin;
5624 ex_win_close(eap->forceit, win, NULL);
5625 }
5626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627}
5628
Bram Moolenaar4033c552017-09-16 20:54:51 +02005629#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005630/*
5631 * ":pclose": Close any preview window.
5632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005634ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005635{
5636 win_T *win;
5637
Bram Moolenaar79648732019-07-18 21:43:07 +02005638 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005639 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005640 if (win->w_p_pvw)
5641 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005642 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005643 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005644 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005645# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005646 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005647 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005648# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005649}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005650#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005651
Bram Moolenaarf740b292006-02-16 22:11:02 +00005652/*
5653 * Close window "win" and take care of handling closing the last window for a
5654 * modified buffer.
5655 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005656 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005657ex_win_close(
5658 int forceit,
5659 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005660 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661{
5662 int need_hide;
5663 buf_T *buf = win->w_buffer;
5664
Bram Moolenaarcf844172020-06-26 19:44:06 +02005665 // Never close the autocommand window.
5666 if (win == aucmd_win)
5667 {
5668 emsg(_(e_autocmd_close));
5669 return;
5670 }
5671
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005673 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005675#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005676 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005678 bufref_T bufref;
5679
5680 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005682 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 return;
5684 need_hide = FALSE;
5685 }
5686 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005689 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 return;
5691 }
5692 }
5693
Bram Moolenaar4033c552017-09-16 20:54:51 +02005694#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005696#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005697
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005698 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005699 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005700 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005701 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005702 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703}
5704
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005706 * Handle the argument for a tabpage related ex command.
5707 * Returns a tabpage number.
5708 * When an error is encountered then eap->errmsg is set.
5709 */
5710 static int
5711get_tabpage_arg(exarg_T *eap)
5712{
5713 int tab_number;
5714 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5715
5716 if (eap->arg && *eap->arg != NUL)
5717 {
5718 char_u *p = eap->arg;
5719 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005720 int relative = 0; // argument +N/-N means: go to N places to the
5721 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005722
5723 if (*p == '-')
5724 {
5725 relative = -1;
5726 p++;
5727 }
5728 else if (*p == '+')
5729 {
5730 relative = 1;
5731 p++;
5732 }
5733
5734 p_save = p;
5735 tab_number = getdigits(&p);
5736
5737 if (relative == 0)
5738 {
5739 if (STRCMP(p, "$") == 0)
5740 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005741 else if (STRCMP(p, "#") == 0)
5742 if (valid_tabpage(lastused_tabpage))
5743 tab_number = tabpage_index(lastused_tabpage);
5744 else
5745 {
5746 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5747 tab_number = 0;
5748 goto theend;
5749 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005750 else if (p == p_save || *p_save == '-' || *p != NUL
5751 || tab_number > LAST_TAB_NR)
5752 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005753 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005754 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005755 goto theend;
5756 }
5757 }
5758 else
5759 {
5760 if (*p_save == NUL)
5761 tab_number = 1;
5762 else if (p == p_save || *p_save == '-' || *p != NUL
5763 || tab_number == 0)
5764 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005765 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005766 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005767 goto theend;
5768 }
5769 tab_number = tab_number * relative + tabpage_index(curtab);
5770 if (!unaccept_arg0 && relative == -1)
5771 --tab_number;
5772 }
5773 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005774 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005775 }
5776 else if (eap->addr_count > 0)
5777 {
5778 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005779 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005780 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005781 tab_number = 0;
5782 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005783 else
5784 {
5785 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005786 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005787 {
5788 --tab_number;
5789 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005790 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005791 }
5792 }
5793 }
5794 else
5795 {
5796 switch (eap->cmdidx)
5797 {
5798 case CMD_tabnext:
5799 tab_number = tabpage_index(curtab) + 1;
5800 if (tab_number > LAST_TAB_NR)
5801 tab_number = 1;
5802 break;
5803 case CMD_tabmove:
5804 tab_number = LAST_TAB_NR;
5805 break;
5806 default:
5807 tab_number = tabpage_index(curtab);
5808 }
5809 }
5810
5811theend:
5812 return tab_number;
5813}
5814
5815/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005816 * ":tabclose": close current tab page, unless it is the last one.
5817 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 */
5819 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005820ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005822 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005823 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005824
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005825# ifdef FEAT_CMDWIN
5826 if (cmdwin_type != 0)
5827 cmdwin_result = K_IGNORE;
5828 else
5829# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005830 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005831 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005832 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005834 tab_number = get_tabpage_arg(eap);
5835 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005836 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005837 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005838 if (tp == NULL)
5839 {
5840 beep_flush();
5841 return;
5842 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005843 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005844 {
5845 tabpage_close_other(tp, eap->forceit);
5846 return;
5847 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005848 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005849 tabpage_close(eap->forceit);
5850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005852}
5853
5854/*
5855 * ":tabonly": close all tab pages except the current one
5856 */
5857 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005858ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005859{
5860 tabpage_T *tp;
5861 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005862 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005863
5864# ifdef FEAT_CMDWIN
5865 if (cmdwin_type != 0)
5866 cmdwin_result = K_IGNORE;
5867 else
5868# endif
5869 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005870 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005871 else
5872 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005873 tab_number = get_tabpage_arg(eap);
5874 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005875 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005876 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005877 // Repeat this up to a 1000 times, because autocommands may
5878 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005879 for (done = 0; done < 1000; ++done)
5880 {
5881 FOR_ALL_TABPAGES(tp)
5882 if (tp->tp_topframe != topframe)
5883 {
5884 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005885 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005886 if (valid_tabpage(tp))
5887 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005888 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005889 break;
5890 }
5891 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005892 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005893 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005894 }
5895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897
5898/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005899 * Close the current tab page.
5900 */
5901 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005902tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005903{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005904 // First close all the windows but the current one. If that worked then
5905 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005906 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005907 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005908 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005909 ex_win_close(forceit, curwin, NULL);
5910# ifdef FEAT_GUI
5911 need_mouse_correct = TRUE;
5912# endif
5913}
5914
5915/*
5916 * Close tab page "tp", which is not the current tab page.
5917 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005918 * Also takes care of the tab pages line disappearing when closing the
5919 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005920 */
5921 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005922tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005923{
5924 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005925 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005926 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005927
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005928 // Limit to 1000 windows, autocommands may add a window while we close
5929 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005930 while (++done < 1000)
5931 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005932 wp = tp->tp_firstwin;
5933 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005934
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005935 // Autocommands may delete the tab page under our fingers and we may
5936 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005937 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005938 break;
5939 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005940
Bram Moolenaar29323592016-07-24 22:04:11 +02005941 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005942
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005943 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005944 if (h != tabline_height())
5945 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005946}
5947
5948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 * ":only".
5950 */
5951 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005952ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005954 win_T *wp;
5955 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956# ifdef FEAT_GUI
5957 need_mouse_correct = TRUE;
5958# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005959 if (eap->addr_count > 0)
5960 {
5961 wnr = eap->line2;
5962 for (wp = firstwin; --wnr > 0; )
5963 {
5964 if (wp->w_next == NULL)
5965 break;
5966 else
5967 wp = wp->w_next;
5968 }
5969 win_goto(wp);
5970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 close_others(TRUE, eap->forceit);
5972}
5973
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005975ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005977 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005978 if (!eap->skip)
5979 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005980#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005981 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005982#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005983 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005984 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005985 else
5986 {
5987 int winnr = 0;
5988 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005989
Bram Moolenaar2256c992016-11-15 21:17:07 +01005990 FOR_ALL_WINDOWS(win)
5991 {
5992 winnr++;
5993 if (winnr == eap->line2)
5994 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005995 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005996 if (win == NULL)
5997 win = lastwin;
5998 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 }
6001}
6002
6003/*
6004 * ":stop" and ":suspend": Suspend Vim.
6005 */
6006 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006007ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008{
6009 /*
6010 * Disallow suspending for "rvim".
6011 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006012 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 {
6014 if (!eap->forceit)
6015 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01006016 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 windgoto((int)Rows - 1, 0);
6018 out_char('\n');
6019 out_flush();
6020 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006021 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006023 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006025 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026#ifdef FEAT_TITLE
6027 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006028 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029#endif
6030 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006031 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006033 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006034 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 }
6036}
6037
6038/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006039 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 */
6041 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006042ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006044#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006045 if (not_in_vim9(eap) == FAIL)
6046 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048#ifdef FEAT_CMDWIN
6049 if (cmdwin_type != 0)
6050 {
6051 cmdwin_result = Ctrl_C;
6052 return;
6053 }
6054#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006055 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006056 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006057 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006058 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006059 return;
6060 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006061
6062 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006063 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064
6065 /*
6066 * if more files or windows we won't exit
6067 */
6068 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6069 exiting = TRUE;
6070 if ( ((eap->cmdidx == CMD_wq
6071 || curbufIsChanged())
6072 && do_write(eap) == FAIL)
6073 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006074 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 {
6076 not_exiting();
6077 }
6078 else
6079 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006080 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006082 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083# ifdef FEAT_GUI
6084 need_mouse_correct = TRUE;
6085# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006086 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006087 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 }
6089}
6090
6091/*
6092 * ":print", ":list", ":number".
6093 */
6094 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006095ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006097 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006098 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006099 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006101 for ( ;!got_int; ui_breakcheck())
6102 {
6103 print_line(eap->line1,
6104 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6105 || (eap->flags & EXFLAG_NR)),
6106 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6107 if (++eap->line1 > eap->line2)
6108 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006109 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006110 }
6111 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006112 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006113 curwin->w_cursor.lnum = eap->line2;
6114 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 }
6116
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118}
6119
6120#ifdef FEAT_BYTEOFF
6121 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006122ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123{
6124 goto_byte(eap->line2);
6125}
6126#endif
6127
6128/*
6129 * ":shell".
6130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006132ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133{
6134 do_shell(NULL, 0);
6135}
6136
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006137#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006139static int drop_busy = FALSE;
6140static int drop_filec;
6141static char_u **drop_filev = NULL;
6142static int drop_split;
6143static void (*drop_callback)(void *);
6144static void *drop_cookie;
6145
6146 static void
6147handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148{
6149 exarg_T ea;
6150 int save_msg_scroll = msg_scroll;
6151
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006152 // Setting the argument list may cause screen updates and being called
6153 // recursively. Avoid that by setting drop_busy.
6154 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006156 // Check whether the current buffer is changed. If so, we will need
6157 // to split the current window or data could be lost.
6158 // We don't need to check if the 'hidden' option is set, as in this
6159 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006160 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 {
6162 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006163 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 --emsg_off;
6165 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006166 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 if (win_split(0, 0) == FAIL)
6169 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006170 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006172 // When splitting the window, create a new alist. Otherwise the
6173 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 alist_unlink(curwin->w_alist);
6175 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 }
6177
6178 /*
6179 * Set up the new argument list.
6180 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006181 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182
6183 /*
6184 * Move to the first file.
6185 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006186 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006187 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 ea.cmd = (char_u *)"next";
6189 do_argfile(&ea, 0);
6190
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006191 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6192 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 need_start_insertmode = FALSE;
6194
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006195 // Restore msg_scroll, otherwise a following command may cause scrolling
6196 // unexpectedly. The screen will be redrawn by the caller, thus
6197 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006199
6200 if (drop_callback != NULL)
6201 drop_callback(drop_cookie);
6202
6203 drop_filev = NULL;
6204 drop_busy = FALSE;
6205}
6206
6207/*
6208 * Handle a file drop. The code is here because a drop is *nearly* like an
6209 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006210 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006211 * code is very similar to :args and hence needs access to a lot of the static
6212 * functions in this file.
6213 *
6214 * The "filev" list must have been allocated using alloc(), as should each item
6215 * in the list. This function takes over responsibility for freeing the "filev"
6216 * list.
6217 */
6218 void
6219handle_drop(
6220 int filec, // the number of files dropped
6221 char_u **filev, // the list of files dropped
6222 int split, // force splitting the window
6223 void (*callback)(void *), // to be called after setting the argument
6224 // list
6225 void *cookie) // argument for "callback" (allocated)
6226{
6227 // Cannot handle recursive drops, finish the pending one.
6228 if (drop_busy)
6229 {
6230 FreeWild(filec, filev);
6231 vim_free(cookie);
6232 return;
6233 }
6234
6235 // When calling handle_drop() more than once in a row we only use the last
6236 // one.
6237 if (drop_filev != NULL)
6238 {
6239 FreeWild(drop_filec, drop_filev);
6240 vim_free(drop_cookie);
6241 }
6242
6243 drop_filec = filec;
6244 drop_filev = filev;
6245 drop_split = split;
6246 drop_callback = callback;
6247 drop_cookie = cookie;
6248
6249 // Postpone this when:
6250 // - editing the command line
6251 // - not possible to change the current buffer
6252 // - updating the screen
6253 // As it may change buffers and window structures that are in use and cause
6254 // freed memory to be used.
6255 if (text_locked() || curbuf_locked() || updating_screen)
6256 return;
6257
6258 handle_drop_internal();
6259}
6260
6261/*
6262 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6263 * reset: Handle a postponed drop.
6264 */
6265 void
6266handle_any_postponed_drop(void)
6267{
6268 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006269 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006270 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271}
6272#endif
6273
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 * ":preserve".
6276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006278ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006280 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 ml_preserve(curbuf, TRUE);
6282}
6283
6284/*
6285 * ":recover".
6286 */
6287 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006288ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006290 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006292 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6293 | CCGD_MULTWIN
6294 | (eap->forceit ? CCGD_FORCEIT : 0)
6295 | CCGD_EXCMD)
6296
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 && (*eap->arg == NUL
6298 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006299 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 recoverymode = FALSE;
6301}
6302
6303/*
6304 * Command modifier used in a wrong way.
6305 */
6306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006307ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006309 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310}
6311
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312/*
6313 * :sview [+command] file split window with new file, read-only
6314 * :split [[+command] file] split window with current or new file
6315 * :vsplit [[+command] file] split window vertically with current or new file
6316 * :new [[+command] file] split window with no or new file
6317 * :vnew [[+command] file] split vertically window with no or new file
6318 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006319 *
6320 * :tabedit open new Tab page with empty window
6321 * :tabedit [+command] file open new Tab page and edit "file"
6322 * :tabnew [[+command] file] just like :tabedit
6323 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 */
6325 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006326ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006328 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006329#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006331#endif
6332#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006333 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006334 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006335#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006336 int use_tab = eap->cmdidx == CMD_tabedit
6337 || eap->cmdidx == CMD_tabfind
6338 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006340 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006341 return;
6342
Bram Moolenaar4033c552017-09-16 20:54:51 +02006343#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346
Bram Moolenaar4033c552017-09-16 20:54:51 +02006347#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006348 // A ":split" in the quickfix window works like ":new". Don't want two
6349 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006350 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 {
6352 if (eap->cmdidx == CMD_split)
6353 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 if (eap->cmdidx == CMD_vsplit)
6355 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358
Bram Moolenaar4033c552017-09-16 20:54:51 +02006359#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006360 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 {
6362 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6363 FNAME_MESS, TRUE, curbuf->b_ffname);
6364 if (fname == NULL)
6365 goto theend;
6366 eap->arg = fname;
6367 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006368# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006369 else
6370# endif
6371#endif
6372#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006373 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 && eap->cmdidx != CMD_new)
6376 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006377 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006378# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006379 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006380# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006381 au_has_group((char_u *)"FileExplorer"))
6382 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006383 // No browsing supported but we do have the file explorer:
6384 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006385 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006386 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006387 }
6388 else
6389 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006390 fname = do_browse(0, (char_u *)(use_tab
6391 ? _("Edit File in new tab page")
6392 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006394 if (fname == NULL)
6395 goto theend;
6396 eap->arg = fname;
6397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006399 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006402 /*
6403 * Either open new tab page or split the window.
6404 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006405 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006406 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006407 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006408 : eap->addr_count == 0 ? 0
6409 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006410 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006411 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006412
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006413 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006414 if (curwin != old_curwin
6415 && win_valid(old_curwin)
6416 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006417 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006418 old_curwin->w_alt_fnum = curbuf->b_fnum;
6419 }
6420 }
6421 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6423 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006424 // Reset 'scrollbind' when editing another file, but keep it when
6425 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006426 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006427 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 else
6429 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 do_exedit(eap, old_curwin);
6431 }
6432
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006433# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006434 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006435# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006437# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438theend:
6439 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006440# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006442
6443/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006444 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006445 */
6446 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006447tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006448{
6449 exarg_T ea;
6450
Bram Moolenaara80faa82020-04-12 19:37:17 +02006451 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006452 ea.cmdidx = CMD_tabnew;
6453 ea.cmd = (char_u *)"tabn";
6454 ea.arg = (char_u *)"";
6455 ex_splitview(&ea);
6456}
6457
6458/*
6459 * :tabnext command
6460 */
6461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006462ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006463{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006464 int tab_number;
6465
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006466 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006467 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006468 switch (eap->cmdidx)
6469 {
6470 case CMD_tabfirst:
6471 case CMD_tabrewind:
6472 goto_tabpage(1);
6473 break;
6474 case CMD_tablast:
6475 goto_tabpage(9999);
6476 break;
6477 case CMD_tabprevious:
6478 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006479 if (eap->arg && *eap->arg != NUL)
6480 {
6481 char_u *p = eap->arg;
6482 char_u *p_save = p;
6483
6484 tab_number = getdigits(&p);
6485 if (p == p_save || *p_save == '-' || *p != NUL
6486 || tab_number == 0)
6487 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006488 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006489 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006490 return;
6491 }
6492 }
6493 else
6494 {
6495 if (eap->addr_count == 0)
6496 tab_number = 1;
6497 else
6498 {
6499 tab_number = eap->line2;
6500 if (tab_number < 1)
6501 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006502 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006503 return;
6504 }
6505 }
6506 }
6507 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006508 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006509 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006510 tab_number = get_tabpage_arg(eap);
6511 if (eap->errmsg == NULL)
6512 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006513 break;
6514 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006515}
6516
6517/*
6518 * :tabmove command
6519 */
6520 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006521ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006522{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006523 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006524
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006525 tab_number = get_tabpage_arg(eap);
6526 if (eap->errmsg == NULL)
6527 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006528}
6529
6530/*
6531 * :tabs command: List tabs and their contents.
6532 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006533 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006534ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006535{
6536 tabpage_T *tp;
6537 win_T *wp;
6538 int tabcount = 1;
6539
6540 msg_start();
6541 msg_scroll = TRUE;
6542 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6543 {
6544 msg_putchar('\n');
6545 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006546 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006547 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006548 ui_breakcheck();
6549
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006550 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006551 wp = firstwin;
6552 else
6553 wp = tp->tp_firstwin;
6554 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6555 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006556 msg_putchar('\n');
6557 msg_putchar(wp == curwin ? '>' : ' ');
6558 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006559 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6560 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006561 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006562 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006563 else
6564 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6565 IObuff, IOSIZE, TRUE);
6566 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006567 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006568 ui_breakcheck();
6569 }
6570 }
6571}
6572
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573/*
6574 * ":mode": Set screen mode.
6575 * If no argument given, just get the screen size and redraw.
6576 */
6577 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006578ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579{
6580 if (*eap->arg == NUL)
6581 shell_resized();
6582 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006583 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584}
6585
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586/*
6587 * ":resize".
6588 * set, increment or decrement current window height
6589 */
6590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006591ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592{
6593 int n;
6594 win_T *wp = curwin;
6595
6596 if (eap->addr_count > 0)
6597 {
6598 n = eap->line2;
6599 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6600 ;
6601 }
6602
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006603# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006605# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006607 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 {
6609 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006610 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006611 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 n = 9999;
6613 win_setwidth_win((int)n, wp);
6614 }
6615 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 {
6617 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006618 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006619 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 n = 9999;
6621 win_setheight_win((int)n, wp);
6622 }
6623}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624
6625/*
6626 * ":find [+command] <file>" command.
6627 */
6628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006629ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630{
6631#ifdef FEAT_SEARCHPATH
6632 char_u *fname;
6633 int count;
6634
6635 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6636 TRUE, curbuf->b_ffname);
6637 if (eap->addr_count > 0)
6638 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006639 // Repeat finding the file "count" times. This matters when it
6640 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 count = eap->line2;
6642 while (fname != NULL && --count > 0)
6643 {
6644 vim_free(fname);
6645 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6646 FALSE, curbuf->b_ffname);
6647 }
6648 }
6649
6650 if (fname != NULL)
6651 {
6652 eap->arg = fname;
6653#endif
6654 do_exedit(eap, NULL);
6655#ifdef FEAT_SEARCHPATH
6656 vim_free(fname);
6657 }
6658#endif
6659}
6660
6661/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006662 * ":open" simulation: for now just work like ":visual".
6663 */
6664 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006665ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006666{
6667 regmatch_T regmatch;
6668 char_u *p;
6669
Bram Moolenaar65088802021-03-13 21:07:21 +01006670#ifdef FEAT_EVAL
6671 if (not_in_vim9(eap) == FAIL)
6672 return;
6673#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006674 curwin->w_cursor.lnum = eap->line2;
6675 beginline(BL_SOL | BL_FIX);
6676 if (*eap->arg == '/')
6677 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006678 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006679 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006680 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00006681 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006682 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006683 if (regmatch.regprog != NULL)
6684 {
6685 regmatch.rm_ic = p_ic;
6686 p = ml_get_curline();
6687 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006688 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006689 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006690 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006691 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006692 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006693 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006694 eap->arg += STRLEN(eap->arg);
6695 }
6696 check_cursor();
6697
6698 eap->cmdidx = CMD_visual;
6699 do_exedit(eap, NULL);
6700}
6701
6702/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006703 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 */
6705 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006706ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707{
6708 do_exedit(eap, NULL);
6709}
6710
6711/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006712 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006715do_exedit(
6716 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006717 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718{
6719 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006721 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006723 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6724 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006725 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 /*
6727 * ":vi" command ends Ex mode.
6728 */
6729 if (exmode_active && (eap->cmdidx == CMD_visual
6730 || eap->cmdidx == CMD_view))
6731 {
6732 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006733 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006735 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006736 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006737 if (global_busy)
6738 {
6739 int rd = RedrawingDisabled;
6740 int nwr = no_wait_return;
6741 int ms = msg_scroll;
6742#ifdef FEAT_GUI
6743 int he = hold_gui_events;
6744#endif
6745
6746 if (eap->nextcmd != NULL)
6747 {
6748 stuffReadbuff(eap->nextcmd);
6749 eap->nextcmd = NULL;
6750 }
6751
6752 if (exmode_was != EXMODE_VIM)
6753 settmode(TMODE_RAW);
6754 RedrawingDisabled = 0;
6755 no_wait_return = 0;
6756 need_wait_return = FALSE;
6757 msg_scroll = 0;
6758#ifdef FEAT_GUI
6759 hold_gui_events = 0;
6760#endif
6761 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006762 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006763
6764 main_loop(FALSE, TRUE);
6765
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006766 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006767 RedrawingDisabled = rd;
6768 no_wait_return = nwr;
6769 msg_scroll = ms;
6770#ifdef FEAT_GUI
6771 hold_gui_events = he;
6772#endif
6773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 }
6777
6778 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006779 || eap->cmdidx == CMD_tabnew
6780 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006781 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006783 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 setpcmark();
6785 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006786 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6787 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006789 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 || *eap->arg != NUL
6791#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006792 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793#endif
6794 )
6795 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006796 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6797 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006798 if (*eap->arg != NUL && curbuf_locked())
6799 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006800
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 n = readonlymode;
6802 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6803 readonlymode = TRUE;
6804 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006805 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6806 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01006807 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
6808 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6810 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006811 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6813 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6814 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006815 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006817 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006818 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006819 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
6820 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006821 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006823 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 if (old_curwin != NULL)
6825 {
6826 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006827 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006829#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006830 cleanup_T cs;
6831
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006832 // Reset the error/interrupt/exception state here so that
6833 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006834 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006835#endif
6836#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006838#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006839 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006840
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006841#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006842 // Restore the error/interrupt/exception state if not
6843 // discarded by a new aborting error, interrupt, or
6844 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006845 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 }
6848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 }
6850 else if (readonlymode && curbuf->b_nwindows == 1)
6851 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006852 // When editing an already visited buffer, 'readonly' won't be set
6853 // but the previous value is kept. With ":view" and ":sview" we
6854 // want the file to be readonly, except when another window is
6855 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 curbuf->b_p_ro = TRUE;
6857 }
6858 readonlymode = n;
6859 }
6860 else
6861 {
6862 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01006863 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864#ifdef FEAT_TITLE
6865 n = curwin->w_arg_idx_invalid;
6866#endif
6867 check_arg_idx(curwin);
6868#ifdef FEAT_TITLE
6869 if (n != curwin->w_arg_idx_invalid)
6870 maketitle();
6871#endif
6872 }
6873
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 /*
6875 * if ":split file" worked, set alternate file name in old window to new
6876 * file
6877 */
6878 if (old_curwin != NULL
6879 && *eap->arg != NUL
6880 && curwin != old_curwin
6881 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006882 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006883 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885
6886 ex_no_reprint = TRUE;
6887}
6888
6889#ifndef FEAT_GUI
6890/*
6891 * ":gui" and ":gvim" when there is no GUI.
6892 */
6893 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006894ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006896 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897}
6898#endif
6899
Bram Moolenaar4f974752019-02-17 17:44:42 +01006900#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006902ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903{
6904 gui_make_tearoff(eap->arg);
6905}
6906#endif
6907
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006908#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6909 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006911ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006913# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6914 if (gui.in_use)
6915 gui_make_popup(eap->arg, eap->forceit);
6916# ifdef FEAT_TERM_POPUP_MENU
6917 else
6918# endif
6919# endif
6920# ifdef FEAT_TERM_POPUP_MENU
6921 pum_make_popup(eap->arg, eap->forceit);
6922# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923}
6924#endif
6925
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006927ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928{
6929 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006930 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006932 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933}
6934
6935/*
6936 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6937 * offset.
6938 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6939 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006941ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006944 win_T *save_curwin = curwin;
6945 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 long topline;
6947 long y;
6948 linenr_T old_linenr = curwin->w_cursor.lnum;
6949
6950 setpcmark();
6951
6952 /*
6953 * determine max topline
6954 */
6955 if (curwin->w_p_scb)
6956 {
6957 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006958 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 {
6960 if (wp->w_p_scb && wp->w_buffer)
6961 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006962 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 if (topline > y)
6964 topline = y;
6965 }
6966 }
6967 if (topline < 1)
6968 topline = 1;
6969 }
6970 else
6971 {
6972 topline = 1;
6973 }
6974
6975
6976 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006977 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006979 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 {
6981 if (curwin->w_p_scb)
6982 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006983 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 y = topline - curwin->w_topline;
6985 if (y > 0)
6986 scrollup(y, TRUE);
6987 else
6988 scrolldown(-y, TRUE);
6989 curwin->w_scbind_pos = topline;
6990 redraw_later(VALID);
6991 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 }
6994 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006995 curwin = save_curwin;
6996 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 if (curwin->w_p_scb)
6998 {
6999 did_syncbind = TRUE;
7000 checkpcmark();
7001 if (old_linenr != curwin->w_cursor.lnum)
7002 {
7003 char_u ctrl_o[2];
7004
7005 ctrl_o[0] = Ctrl_O;
7006 ctrl_o[1] = 0;
7007 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7008 }
7009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010}
7011
7012
7013 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007014ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007016 int i;
7017 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7018 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007020 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 do_bang(1, eap, FALSE, FALSE, TRUE);
7022 else
7023 {
7024 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7025 return;
7026
7027#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007028 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 {
7030 char_u *browseFile;
7031
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007032 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 NULL, NULL, NULL, curbuf);
7034 if (browseFile != NULL)
7035 {
7036 i = readfile(browseFile, NULL,
7037 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7038 vim_free(browseFile);
7039 }
7040 else
7041 i = OK;
7042 }
7043 else
7044#endif
7045 if (*eap->arg == NUL)
7046 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007047 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 return;
7049 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7050 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7051 }
7052 else
7053 {
7054 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7055 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7056 i = readfile(eap->arg, NULL,
7057 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7058
7059 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007060 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007062#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 if (!aborting())
7064#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007065 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 }
7067 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007068 {
7069 if (empty && exmode_active)
7070 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007071 // Delete the empty line that remains. Historically ex does
7072 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007073 if (eap->line2 == 0)
7074 lnum = curbuf->b_ml.ml_line_count;
7075 else
7076 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007077 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007078 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007079 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007080 if (curwin->w_cursor.lnum > 1
7081 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007082 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007083 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007084 }
7085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 }
7089}
7090
Bram Moolenaarea408852005-06-25 22:49:46 +00007091static char_u *prev_dir = NULL;
7092
7093#if defined(EXITFREE) || defined(PROTO)
7094 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007095free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007096{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007097 VIM_CLEAR(prev_dir);
7098 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007099}
7100#endif
7101
Bram Moolenaarf4258302013-06-02 18:20:17 +02007102/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007103 * Get the previous directory for the given chdir scope.
7104 */
7105 static char_u *
7106get_prevdir(cdscope_T scope)
7107{
7108 if (scope == CDSCOPE_WINDOW)
7109 return curwin->w_prevdir;
7110 else if (scope == CDSCOPE_TABPAGE)
7111 return curtab->tp_prevdir;
7112 return prev_dir;
7113}
7114
7115/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007116 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007117 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7118 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007119 */
7120 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007121post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007122{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007123 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007124 // Clear tab local directory for both :cd and :tcd
7125 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007126 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007127 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007128 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007129 char_u *pdir = get_prevdir(scope);
7130
7131 // If still in the global directory, need to remember current
7132 // directory as the global directory.
7133 if (globaldir == NULL && pdir != NULL)
7134 globaldir = vim_strsave(pdir);
7135
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007136 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007137 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007138 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007139 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007140 curtab->tp_localdir = vim_strsave(NameBuff);
7141 else
7142 curwin->w_localdir = vim_strsave(NameBuff);
7143 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007144 }
7145 else
7146 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007147 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007148 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007149 }
7150
7151 shorten_fnames(TRUE);
7152}
7153
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007154/*
7155 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007156 * chdir() function.
7157 * scope == CDSCOPE_WINDOW: changes the window-local directory
7158 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7159 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007160 * Returns TRUE if the directory is successfully changed.
7161 */
7162 int
7163changedir_func(
7164 char_u *new_dir,
7165 int forceit,
7166 cdscope_T scope)
7167{
7168 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02007169 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007170 int dir_differs;
7171 int retval = FALSE;
7172
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007173 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007174 return FALSE;
7175
7176 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7177 {
7178 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7179 return FALSE;
7180 }
7181
7182 // ":cd -": Change to previous directory
7183 if (STRCMP(new_dir, "-") == 0)
7184 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007185 pdir = get_prevdir(scope);
7186 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007187 {
7188 emsg(_("E186: No previous directory"));
7189 return FALSE;
7190 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007191 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007192 }
7193
Bram Moolenaar002bc792020-06-05 22:33:42 +02007194 // Free the previous directory
7195 tofree = get_prevdir(scope);
7196
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007197 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007198 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007199 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007200 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007201 pdir = NULL;
7202 if (scope == CDSCOPE_WINDOW)
7203 curwin->w_prevdir = pdir;
7204 else if (scope == CDSCOPE_TABPAGE)
7205 curtab->tp_prevdir = pdir;
7206 else
7207 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007208
7209#if defined(UNIX) || defined(VMS)
7210 // for UNIX ":cd" means: go to home directory
7211 if (*new_dir == NUL)
7212 {
7213 // use NameBuff for home directory name
7214# ifdef VMS
7215 char_u *p;
7216
7217 p = mch_getenv((char_u *)"SYS$LOGIN");
7218 if (p == NULL || *p == NUL) // empty is the same as not set
7219 NameBuff[0] = NUL;
7220 else
7221 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7222# else
7223 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7224# endif
7225 new_dir = NameBuff;
7226 }
7227#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02007228 dir_differs = new_dir == NULL || pdir == NULL
7229 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007230 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7231 emsg(_(e_failed));
7232 else
7233 {
7234 char_u *acmd_fname;
7235
7236 post_chdir(scope);
7237
7238 if (dir_differs)
7239 {
7240 if (scope == CDSCOPE_WINDOW)
7241 acmd_fname = (char_u *)"window";
7242 else if (scope == CDSCOPE_TABPAGE)
7243 acmd_fname = (char_u *)"tabpage";
7244 else
7245 acmd_fname = (char_u *)"global";
7246 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7247 curbuf);
7248 }
7249 retval = TRUE;
7250 }
7251 vim_free(tofree);
7252
7253 return retval;
7254}
Bram Moolenaarea408852005-06-25 22:49:46 +00007255
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007257 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007259 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007260ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007262 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263
7264 new_dir = eap->arg;
7265#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007266 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 if (*new_dir == NUL)
7268 ex_pwd(NULL);
7269 else
7270#endif
7271 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007272 cdscope_T scope = CDSCOPE_GLOBAL;
7273
7274 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7275 scope = CDSCOPE_WINDOW;
7276 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7277 scope = CDSCOPE_TABPAGE;
7278
7279 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007280 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007281 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007282 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 ex_pwd(eap);
7284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 }
7286}
7287
7288/*
7289 * ":pwd".
7290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007292ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293{
7294 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7295 {
7296#ifdef BACKSLASH_IN_FILENAME
7297 slash_adjust(NameBuff);
7298#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007299 if (p_verbose > 0)
7300 {
7301 char *context = "global";
7302
7303 if (curwin->w_localdir != NULL)
7304 context = "window";
7305 else if (curtab->tp_localdir != NULL)
7306 context = "tabpage";
7307 smsg("[%s] %s", context, (char *)NameBuff);
7308 }
7309 else
7310 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 }
7312 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007313 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314}
7315
7316/*
7317 * ":=".
7318 */
7319 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007320ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007322 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007323 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324}
7325
7326 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007327ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007329 int n;
7330 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331
7332 if (cursor_valid())
7333 {
7334 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7335 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007336 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007338
7339 len = eap->line2;
7340 switch (*eap->arg)
7341 {
7342 case 'm': break;
7343 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007344 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007345 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007346
7347 // Hide the cursor if invoked with !
7348 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349}
7350
7351/*
7352 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007353 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 */
7355 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007356do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357{
Bram Moolenaar52953192019-08-16 10:27:13 +02007358 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007359 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007360# ifdef ELAPSED_FUNC
7361 elapsed_T start_tv;
7362
7363 // Remember at what time we started, so that we know how much longer we
7364 // should wait after waiting for a bit.
7365 ELAPSED_INIT(start_tv);
7366# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007368 if (hide_cursor)
7369 cursor_off();
7370 else
7371 cursor_on();
7372
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007373 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007374 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007376 wait_now = msec - done > 1000L ? 1000L : msec - done;
7377#ifdef FEAT_TIMERS
7378 {
7379 long due_time = check_due_timer();
7380
7381 if (due_time > 0 && due_time < wait_now)
7382 wait_now = due_time;
7383 }
7384#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007385#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007386 if (has_any_channel() && wait_now > 20L)
7387 wait_now = 20L;
7388#endif
7389#ifdef FEAT_SOUND
7390 if (has_any_sound_callback() && wait_now > 20L)
7391 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007392#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007393 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007394
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007395#ifdef FEAT_JOB_CHANNEL
7396 if (has_any_channel())
7397 ui_breakcheck_force(TRUE);
7398 else
7399#endif
7400 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007401#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007402 // Process the netbeans and clientserver messages that may have been
7403 // received in the call to ui_breakcheck() when the GUI is in use. This
7404 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007405 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007406#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007407
7408# ifdef ELAPSED_FUNC
7409 // actual time passed
7410 done = ELAPSED_FUNC(start_tv);
7411# else
7412 // guestimate time passed (will actually be more)
7413 done += wait_now;
7414# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007416
7417 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7418 // input buffer, otherwise a following call to input() fails.
7419 if (got_int)
7420 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421}
7422
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423/*
7424 * ":winsize" command (obsolete).
7425 */
7426 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007427ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428{
7429 int w, h;
7430 char_u *arg = eap->arg;
7431 char_u *p;
7432
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007433 if (!isdigit(*arg))
7434 {
7435 semsg(_(e_invarg2), arg);
7436 return;
7437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 w = getdigits(&arg);
7439 arg = skipwhite(arg);
7440 p = arg;
7441 h = getdigits(&arg);
7442 if (*p != NUL && *arg == NUL)
7443 set_shellsize(w, h, TRUE);
7444 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007445 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446}
7447
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007449ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450{
7451 int xchar = NUL;
7452 char_u *p;
7453
7454 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7455 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007456 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 if (eap->arg[1] == NUL)
7458 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007459 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 return;
7461 }
7462 xchar = eap->arg[1];
7463 p = eap->arg + 2;
7464 }
7465 else
7466 p = eap->arg + 1;
7467
7468 eap->nextcmd = check_nextcmd(p);
7469 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007470 if (*p != NUL && *p != (
7471#ifdef FEAT_EVAL
7472 in_vim9script() ? '#' :
7473#endif
7474 '"')
7475 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007476 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007477 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007479 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007480 postponed_split_flags = cmdmod.cmod_split;
7481 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7483 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007484 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 }
7486}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487
Bram Moolenaar843ee412004-06-30 16:16:41 +00007488#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489/*
7490 * ":winpos".
7491 */
7492 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007493ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494{
7495 int x, y;
7496 char_u *arg = eap->arg;
7497 char_u *p;
7498
7499 if (*arg == NUL)
7500 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007501# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007502# ifdef VIMDLL
7503 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7504 mch_get_winpos(&x, &y) != FAIL)
7505# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007507# else
7508 if (mch_get_winpos(&x, &y) != FAIL)
7509# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 {
7511 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007512 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 }
7514 else
7515# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007516 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 }
7518 else
7519 {
7520 x = getdigits(&arg);
7521 arg = skipwhite(arg);
7522 p = arg;
7523 y = getdigits(&arg);
7524 if (*p == NUL || *arg != NUL)
7525 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007526 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 return;
7528 }
7529# ifdef FEAT_GUI
7530 if (gui.in_use)
7531 gui_mch_set_winpos(x, y);
7532 else if (gui.starting)
7533 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007534 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 gui_win_x = x;
7536 gui_win_y = y;
7537 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007538# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 else
7540# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007541# endif
7542# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007543 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544# endif
7545# ifdef HAVE_TGETENT
7546 if (*T_CWP)
7547 term_set_winpos(x, y);
7548# endif
7549 }
7550}
7551#endif
7552
7553/*
7554 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7555 */
7556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007557ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558{
7559 oparg_T oa;
7560
7561 clear_oparg(&oa);
7562 oa.regname = eap->regname;
7563 oa.start.lnum = eap->line1;
7564 oa.end.lnum = eap->line2;
7565 oa.line_count = eap->line2 - eap->line1 + 1;
7566 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007568 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 {
7570 setpcmark();
7571 curwin->w_cursor.lnum = eap->line1;
7572 beginline(BL_SOL | BL_FIX);
7573 }
7574
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007575 if (VIsual_active)
7576 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007577
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 switch (eap->cmdidx)
7579 {
7580 case CMD_delete:
7581 oa.op_type = OP_DELETE;
7582 op_delete(&oa);
7583 break;
7584
7585 case CMD_yank:
7586 oa.op_type = OP_YANK;
7587 (void)op_yank(&oa, FALSE, TRUE);
7588 break;
7589
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007590 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007591 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007593 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7594#else
7595 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007597 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 oa.op_type = OP_RSHIFT;
7599 else
7600 oa.op_type = OP_LSHIFT;
7601 op_shift(&oa, FALSE, eap->amount);
7602 break;
7603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007605 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606}
7607
7608/*
7609 * ":put".
7610 */
7611 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007612ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007614 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 if (eap->line2 == 0)
7616 {
7617 eap->line2 = 1;
7618 eap->forceit = TRUE;
7619 }
7620 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007621 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007622 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623}
7624
7625/*
7626 * Handle ":copy" and ":move".
7627 */
7628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007629ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630{
7631 long n;
7632
Bram Moolenaar491799b2020-08-01 19:23:43 +02007633#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007634 if (not_in_vim9(eap) == FAIL)
7635 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007636#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007637 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007638 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 {
7640 eap->nextcmd = NULL;
7641 return;
7642 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007643 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644
7645 /*
7646 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7647 */
7648 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7649 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007650 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 return;
7652 }
7653
7654 if (eap->cmdidx == CMD_move)
7655 {
7656 if (do_move(eap->line1, eap->line2, n) == FAIL)
7657 return;
7658 }
7659 else
7660 ex_copy(eap->line1, eap->line2, n);
7661 u_clearline();
7662 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007663 ex_may_print(eap);
7664}
7665
7666/*
7667 * Print the current line if flags were given to the Ex command.
7668 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007669 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007670ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007671{
7672 if (eap->flags != 0)
7673 {
7674 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7675 (eap->flags & EXFLAG_LIST));
7676 ex_no_reprint = TRUE;
7677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678}
7679
7680/*
7681 * ":smagic" and ":snomagic".
7682 */
7683 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007684ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007686 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007688 magic_overruled = eap->cmdidx == CMD_smagic
7689 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02007690 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007691 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692}
7693
7694/*
7695 * ":join".
7696 */
7697 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007698ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699{
7700 curwin->w_cursor.lnum = eap->line1;
7701 if (eap->line1 == eap->line2)
7702 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007703 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 return;
7705 if (eap->line2 == curbuf->b_ml.ml_line_count)
7706 {
7707 beep_flush();
7708 return;
7709 }
7710 ++eap->line2;
7711 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007712 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007714 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715}
7716
7717/*
7718 * ":[addr]@r" or ":[addr]*r": execute register
7719 */
7720 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007721ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722{
7723 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007724 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725
7726 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007727 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728
7729#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007730 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731#endif
7732
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007733 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 c = *eap->arg;
7735 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7736 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007737 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007738 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7739 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007740 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 else
7744 {
7745 int save_efr = exec_from_reg;
7746
7747 exec_from_reg = TRUE;
7748
7749 /*
7750 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007751 * Continue until the stuff buffer is empty and all added characters
7752 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007754 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7756
7757 exec_from_reg = save_efr;
7758 }
7759}
7760
7761/*
7762 * ":!".
7763 */
7764 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007765ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766{
7767 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7768}
7769
7770/*
7771 * ":undo".
7772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007774ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007776 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007777 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007778 else
7779 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780}
7781
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007782#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007784ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007785{
7786 char_u hash[UNDO_HASH_SIZE];
7787
7788 u_compute_hash(hash);
7789 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7790}
7791
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007792 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007793ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007794{
7795 char_u hash[UNDO_HASH_SIZE];
7796
7797 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007798 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007799}
7800#endif
7801
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802/*
7803 * ":redo".
7804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007806ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807{
7808 u_redo(1);
7809}
7810
7811/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007812 * ":earlier" and ":later".
7813 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007814 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007815ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007816{
7817 long count = 0;
7818 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007819 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007820 char_u *p = eap->arg;
7821
7822 if (*p == NUL)
7823 count = 1;
7824 else if (isdigit(*p))
7825 {
7826 count = getdigits(&p);
7827 switch (*p)
7828 {
7829 case 's': ++p; sec = TRUE; break;
7830 case 'm': ++p; sec = TRUE; count *= 60; break;
7831 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007832 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7833 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007834 }
7835 }
7836
7837 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007838 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007839 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007840 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7841 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007842}
7843
7844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 * ":redir": start/stop redirection.
7846 */
7847 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007848ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849{
7850 char *mode;
7851 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007852 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853
Bram Moolenaarba768492016-07-08 15:32:54 +02007854#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007855 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007856 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007857 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007858 return;
7859 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007860#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007861
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 if (STRICMP(eap->arg, "END") == 0)
7863 close_redir();
7864 else
7865 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007866 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007868 ++arg;
7869 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007871 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 mode = "a";
7873 }
7874 else
7875 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007876 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877
7878 close_redir();
7879
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007880 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007881 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 if (fname == NULL)
7883 return;
7884#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007885 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {
7887 char_u *browseFile;
7888
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007889 browseFile = do_browse(BROWSE_SAVE,
7890 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007891 fname, NULL, NULL,
7892 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007894 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 vim_free(fname);
7896 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007897 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 }
7899#endif
7900
7901 redir_fd = open_exfile(fname, eap->forceit, mode);
7902 vim_free(fname);
7903 }
7904#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007905 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007907 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007909 ++arg;
7910 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007912 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007913 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007915 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007917 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007918 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007919 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007920 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007922 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007923 if (*arg == '>')
7924 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007925 // Make register empty when not using @A-@Z and the
7926 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007927 if (*arg == NUL && !isupper(redir_reg))
7928 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007930 }
7931 if (*arg != NUL)
7932 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007933 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007934 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007935 }
7936 }
7937 else if (*arg == '=' && arg[1] == '>')
7938 {
7939 int append;
7940
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007941 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007942 close_redir();
7943 arg += 2;
7944
7945 if (*arg == '>')
7946 {
7947 ++arg;
7948 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 }
7950 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007951 append = FALSE;
7952
7953 if (var_redir_start(skipwhite(arg), append) == OK)
7954 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 }
7956#endif
7957
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007958 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007961 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007963
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007964 // Make sure redirection is not off. Can happen for cmdline completion
7965 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007966 if (redir_fd != NULL
7967#ifdef FEAT_EVAL
7968 || redir_reg || redir_vname
7969#endif
7970 )
7971 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972}
7973
7974/*
7975 * ":redraw": force redraw
7976 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007977 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007978ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979{
7980 int r = RedrawingDisabled;
7981 int p = p_lz;
7982
7983 RedrawingDisabled = 0;
7984 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007985 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007987 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988#ifdef FEAT_TITLE
7989 if (need_maketitle)
7990 maketitle();
7991#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007992#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7993# ifdef VIMDLL
7994 if (!gui.in_use)
7995# endif
7996 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 RedrawingDisabled = r;
7999 p_lz = p;
8000
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008001 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 msg_didout = FALSE;
8003 msg_col = 0;
8004
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008005 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02008006 need_wait_return = FALSE;
8007
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 out_flush();
8009}
8010
8011/*
8012 * ":redrawstatus": force redraw of status line(s)
8013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008015ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 int r = RedrawingDisabled;
8018 int p = p_lz;
8019
8020 RedrawingDisabled = 0;
8021 p_lz = FALSE;
8022 if (eap->forceit)
8023 status_redraw_all();
8024 else
8025 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008026 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 RedrawingDisabled = r;
8028 p_lz = p;
8029 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030}
8031
Bram Moolenaare12bab32019-01-08 22:02:56 +01008032/*
8033 * ":redrawtabline": force redraw of the tabline
8034 */
8035 static void
8036ex_redrawtabline(exarg_T *eap UNUSED)
8037{
8038 int r = RedrawingDisabled;
8039 int p = p_lz;
8040
8041 RedrawingDisabled = 0;
8042 p_lz = FALSE;
8043
8044 draw_tabline();
8045
8046 RedrawingDisabled = r;
8047 p_lz = p;
8048 out_flush();
8049}
8050
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008052close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053{
8054 if (redir_fd != NULL)
8055 {
8056 fclose(redir_fd);
8057 redir_fd = NULL;
8058 }
8059#ifdef FEAT_EVAL
8060 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008061 if (redir_vname)
8062 {
8063 var_redir_stop();
8064 redir_vname = 0;
8065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066#endif
8067}
8068
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008069#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008070 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008071vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008072{
8073 if (vim_mkdir(name, prot) != 0)
8074 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008075 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008076 return FAIL;
8077 }
8078 return OK;
8079}
8080#endif
8081
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082/*
8083 * Open a file for writing for an Ex command, with some checks.
8084 * Return file descriptor, or NULL on failure.
8085 */
8086 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008087open_exfile(
8088 char_u *fname,
8089 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008090 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091{
8092 FILE *fd;
8093
8094#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008095 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 if (mch_isdir(fname))
8097 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008098 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 return NULL;
8100 }
8101#endif
8102 if (!forceit && *mode != 'a' && vim_fexists(fname))
8103 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008104 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 return NULL;
8106 }
8107
8108 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008109 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110
8111 return fd;
8112}
8113
8114/*
8115 * ":mark" and ":k".
8116 */
8117 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008118ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119{
8120 pos_T pos;
8121
Bram Moolenaar10b94212021-02-19 21:42:57 +01008122#ifdef FEAT_EVAL
8123 if (not_in_vim9(eap) == FAIL)
8124 return;
8125#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008126 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008127 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008128 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02008129 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 else
8131 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008132 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 curwin->w_cursor.lnum = eap->line2;
8134 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008135 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008136 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008137 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 }
8139}
8140
8141/*
8142 * Update w_topline, w_leftcol and the cursor position.
8143 */
8144 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008145update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008147 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 update_topline();
8149 if (!curwin->w_p_wrap)
8150 validate_cursor();
8151 update_curswant();
8152}
8153
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008155 * Save the current State and go to Normal mode.
8156 * Return TRUE if the typeahead could be saved.
8157 */
8158 int
8159save_current_state(save_state_T *sst)
8160{
8161 sst->save_msg_scroll = msg_scroll;
8162 sst->save_restart_edit = restart_edit;
8163 sst->save_msg_didout = msg_didout;
8164 sst->save_State = State;
8165 sst->save_insertmode = p_im;
8166 sst->save_finish_op = finish_op;
8167 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008168 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008169
Bram Moolenaar4324d872020-12-01 20:12:24 +01008170 msg_scroll = FALSE; // no msg scrolling in Normal mode
8171 restart_edit = 0; // don't go to Insert mode
8172 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008173
Bram Moolenaara452b802020-12-01 21:47:59 +01008174 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008175 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008176
8177 /*
8178 * Save the current typeahead. This is required to allow using ":normal"
8179 * from an event handler and makes sure we don't hang when the argument
8180 * ends with half a command.
8181 */
8182 save_typeahead(&sst->tabuf);
8183 return sst->tabuf.typebuf_valid;
8184}
8185
8186 void
8187restore_current_state(save_state_T *sst)
8188{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008189 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008190 restore_typeahead(&sst->tabuf);
8191
8192 msg_scroll = sst->save_msg_scroll;
8193 restart_edit = sst->save_restart_edit;
8194 p_im = sst->save_insertmode;
8195 finish_op = sst->save_finish_op;
8196 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008197 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008198 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008199 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008200
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008201 // Restore the state (needed when called from a function executed for
8202 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008203 State = sst->save_State;
8204#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008205 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008206#endif
8207}
8208
8209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 * ":normal[!] {commands}": Execute normal mode commands.
8211 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008212 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008213ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008215 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 char_u *arg = NULL;
8217 int l;
8218 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008220 if (ex_normal_lock > 0)
8221 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008222 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008223 return;
8224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 if (ex_normal_busy >= p_mmd)
8226 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008227 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 return;
8229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 /*
8232 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8233 * this for the K_SPECIAL leading byte, otherwise special keys will not
8234 * work.
8235 */
8236 if (has_mbyte)
8237 {
8238 int len = 0;
8239
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008240 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 for (p = eap->arg; *p != NUL; ++p)
8242 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008243#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008244 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008246#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008247 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008248 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008249#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 )
8253 len += 2;
8254 }
8255 if (len > 0)
8256 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008257 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 if (arg != NULL)
8259 {
8260 len = 0;
8261 for (p = eap->arg; *p != NUL; ++p)
8262 {
8263 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008264#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 if (*p == CSI)
8266 {
8267 arg[len++] = KS_EXTRA;
8268 arg[len++] = (int)KE_CSI;
8269 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008270#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008271 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 {
8273 arg[len++] = *++p;
8274 if (*p == K_SPECIAL)
8275 {
8276 arg[len++] = KS_SPECIAL;
8277 arg[len++] = KE_FILLER;
8278 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008279#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 else if (*p == CSI)
8281 {
8282 arg[len++] = KS_EXTRA;
8283 arg[len++] = (int)KE_CSI;
8284 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 }
8287 arg[len] = NUL;
8288 }
8289 }
8290 }
8291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008293 ++ex_normal_busy;
8294 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 {
8296 /*
8297 * Repeat the :normal command for each line in the range. When no
8298 * range given, execute it just once, without positioning the cursor
8299 * first.
8300 */
8301 do
8302 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 if (eap->addr_count != 0)
8304 {
8305 curwin->w_cursor.lnum = eap->line1++;
8306 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008307 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 }
8309
Bram Moolenaar13505972019-01-24 15:04:48 +01008310 exec_normal_cmd(arg != NULL
8311 ? arg
8312 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 }
8314 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8315 }
8316
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008317 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 update_topline_cursor();
8319
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008320 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008322 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008323#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008324 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008325#endif
8326
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328}
8329
8330/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008331 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 */
8333 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008334ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008336 if (eap->forceit)
8337 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008338 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008339 if (!curwin->w_cursor.lnum)
8340 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008341 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008342 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008343#ifdef FEAT_TERMINAL
8344 // Ignore this when running in an active terminal.
8345 if (term_job_running(curbuf->b_term))
8346 return;
8347#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008348
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008349 // Ignore the command when already in Insert mode. Inserting an
8350 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008351 if (State & INSERT)
8352 return;
8353
Bram Moolenaar64969662005-12-14 21:59:55 +00008354 if (eap->cmdidx == CMD_startinsert)
8355 restart_edit = 'a';
8356 else if (eap->cmdidx == CMD_startreplace)
8357 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008359 restart_edit = 'V';
8360
8361 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008363 if (eap->cmdidx == CMD_startinsert)
8364 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008365 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008367
8368 if (VIsual_active)
8369 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370}
8371
8372/*
8373 * ":stopinsert"
8374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008376ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377{
8378 restart_edit = 0;
8379 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008380 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008383/*
8384 * Execute normal mode command "cmd".
8385 * "remap" can be REMAP_NONE or REMAP_YES.
8386 */
8387 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008388exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008389{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008390 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008391 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008392 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008393}
Bram Moolenaar25281632016-01-21 23:32:32 +01008394
Bram Moolenaar25281632016-01-21 23:32:32 +01008395/*
8396 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008397 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008398 */
8399 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008400exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008401{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008402 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008403 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008404
Bram Moolenaar905dd902019-04-07 14:21:47 +02008405 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8406 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008407 clear_oparg(&oa);
8408 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008409 while ((!stuff_empty()
8410 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008411 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008412 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008413 {
8414 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008415#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008416 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008417 && oa.op_type == OP_NOP && oa.regname == NUL
8418 && !VIsual_active)
8419 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008420 // If terminal_loop() returns OK we got a key that is handled
8421 // in Normal model. With FAIL we first need to position the
8422 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008423 if (terminal_loop(TRUE) == OK)
8424 normal_cmd(&oa, TRUE);
8425 }
8426 else
8427#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008428 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008429 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008430 }
8431}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008432
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433#ifdef FEAT_FIND_ID
8434 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008435ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436{
8437 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8438 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8439 (linenr_T)1, (linenr_T)MAXLNUM);
8440}
8441
Bram Moolenaar4033c552017-09-16 20:54:51 +02008442#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443/*
8444 * ":psearch"
8445 */
8446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008447ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448{
8449 g_do_tagpreview = p_pvh;
8450 ex_findpat(eap);
8451 g_do_tagpreview = 0;
8452}
8453#endif
8454
8455 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008456ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457{
8458 int whole = TRUE;
8459 long n;
8460 char_u *p;
8461 int action;
8462
8463 switch (cmdnames[eap->cmdidx].cmd_name[2])
8464 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008465 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8467 action = ACTION_GOTO;
8468 else
8469 action = ACTION_SHOW;
8470 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008471 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 action = ACTION_SHOW_ALL;
8473 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008474 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 action = ACTION_GOTO;
8476 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008477 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 action = ACTION_SPLIT;
8479 break;
8480 }
8481
8482 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008483 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 {
8485 n = getdigits(&eap->arg);
8486 eap->arg = skipwhite(eap->arg);
8487 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008488 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 {
8490 whole = FALSE;
8491 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008492 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 if (*p)
8494 {
8495 *p++ = NUL;
8496 p = skipwhite(p);
8497
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008498 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008499 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008500 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 else
8502 eap->nextcmd = check_nextcmd(p);
8503 }
8504 }
8505 if (!eap->skip)
8506 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8507 whole, !eap->forceit,
8508 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8509 n, action, eap->line1, eap->line2);
8510}
8511#endif
8512
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513
Bram Moolenaar4033c552017-09-16 20:54:51 +02008514#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515/*
8516 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8517 */
8518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008519ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008521 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8523}
8524
8525/*
8526 * ":pedit"
8527 */
8528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008529ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530{
8531 win_T *curwin_save = curwin;
8532
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008533 if (ERROR_IF_ANY_POPUP_WINDOW)
8534 return;
8535
Bram Moolenaar026587b2019-08-17 15:08:00 +02008536 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008538 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008539
Bram Moolenaar026587b2019-08-17 15:08:00 +02008540 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008542
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 if (curwin != curwin_save && win_valid(curwin_save))
8544 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008545 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 validate_cursor();
8547 redraw_later(VALID);
8548 win_enter(curwin_save, TRUE);
8549 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008550# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008551 else if (WIN_IS_POPUP(curwin))
8552 {
8553 // can't keep focus in popup window
8554 win_enter(firstwin, TRUE);
8555 }
8556# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 g_do_tagpreview = 0;
8558}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560
8561/*
8562 * ":stag", ":stselect" and ":stjump".
8563 */
8564 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008565ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566{
8567 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008568 postponed_split_flags = cmdmod.cmod_split;
8569 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8571 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008572 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574
8575/*
8576 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8577 */
8578 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008579ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580{
8581 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8582}
8583
8584 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008585ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586{
8587 int cmd;
8588
8589 switch (name[1])
8590 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008591 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008593 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008595 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008597 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008599 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008601 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008603 case 'f': // ":tfirst"
8604 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008606 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008608 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008610 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008612 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 return;
8614 }
8615#endif
8616 cmd = DT_TAG;
8617 break;
8618 }
8619
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008620 if (name[0] == 'l')
8621 {
8622#ifndef FEAT_QUICKFIX
8623 ex_ni(eap);
8624 return;
8625#else
8626 cmd = DT_LTAG;
8627#endif
8628 }
8629
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8631 eap->forceit, TRUE);
8632}
8633
8634/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008635 * Check "str" for starting with a special cmdline variable.
8636 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8637 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8638 */
8639 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008640find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008641{
8642 int len;
8643 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008644 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008645 "%",
8646#define SPEC_PERC 0
8647 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008648#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008649 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008650#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008651 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008652#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008653 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008654#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008655 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008656#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008657 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008658#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008659 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008660#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008661 "<stack>", // call stack
8662#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008663 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008664#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008665 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008666#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008667 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008668#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008669 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008670#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008671 "<SID>", // script ID: <SNR>123_
8672#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008673#ifdef FEAT_CLIENTSERVER
8674 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008675# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008676#endif
8677 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008678
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008679 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008680 {
8681 len = (int)STRLEN(spec_str[i]);
8682 if (STRNCMP(src, spec_str[i], len) == 0)
8683 {
8684 *usedlen = len;
8685 return i;
8686 }
8687 }
8688 return -1;
8689}
8690
8691/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 * Evaluate cmdline variables.
8693 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008694 * change "%" to curbuf->b_ffname
8695 * "#" to curwin->w_alt_fnum
8696 * "%%" to curwin->w_alt_fnum in Vim9 script
8697 * "<cword>" to word under the cursor
8698 * "<cWORD>" to WORD under the cursor
8699 * "<cexpr>" to C-expression under the cursor
8700 * "<cfile>" to path name under the cursor
8701 * "<sfile>" to sourced file name
8702 * "<stack>" to call stack
8703 * "<slnum>" to sourced file line number
8704 * "<afile>" to file name for autocommand
8705 * "<abuf>" to buffer number for autocommand
8706 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 *
8708 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8709 * "" for error without a message) and NULL is returned.
8710 * Returns an allocated string if a valid match was found.
8711 * Returns NULL if no match was found. "usedlen" then still contains the
8712 * number of characters to skip.
8713 */
8714 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008715eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008716 char_u *src, // pointer into commandline
8717 char_u *srcstart, // beginning of valid memory for src
8718 int *usedlen, // characters after src that are used
8719 linenr_T *lnump, // line number for :e command, or NULL
8720 char **errormsg, // pointer to error message
8721 int *escaped) // return value has escaped white space (can
8722 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723{
8724 int i;
8725 char_u *s;
8726 char_u *result;
8727 char_u *resultbuf = NULL;
8728 int resultlen;
8729 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008730 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008732 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735
8736 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008737 if (escaped != NULL)
8738 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739
8740 /*
8741 * Check if there is something to do.
8742 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008743 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008744 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 {
8746 *usedlen = 1;
8747 return NULL;
8748 }
8749
8750 /*
8751 * Skip when preceded with a backslash "\%" and "\#".
8752 * Note: In "\\%" the % is also not recognized!
8753 */
8754 if (src > srcstart && src[-1] == '\\')
8755 {
8756 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008757 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 return NULL;
8759 }
8760
8761 /*
8762 * word or WORD under cursor
8763 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008764 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8765 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008767 resultlen = find_ident_under_cursor(&result,
8768 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8769 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8770 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 if (resultlen == 0)
8772 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008773 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 return NULL;
8775 }
8776 }
8777
8778 /*
8779 * '#': Alternate file name
8780 * '%': Current file name
8781 * File name under the cursor
8782 * File name for autocommand
8783 * and following modifiers
8784 */
8785 else
8786 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008787 int off = 0;
8788
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 switch (spec_idx)
8790 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008791 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008792#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008793 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008796 // '%': current file
8797 if (curbuf->b_fname == NULL)
8798 {
8799 result = (char_u *)"";
8800 valid = 0; // Must have ":p:h" to be valid
8801 }
8802 else
8803 {
8804 result = curbuf->b_fname;
8805 tilde_file = STRCMP(result, "~") == 0;
8806 }
8807 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008809#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008810 // "%%" alternate file
8811 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008812#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01008813 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008814 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008815 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008817 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 result = arg_all();
8819 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008820 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008821 if (escaped != NULL)
8822 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 break;
8825 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008826 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008827 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008828 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008830 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008831 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008832 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008833 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008835 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008837 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00008838 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008839 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008840 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008841 return NULL;
8842 }
8843#ifdef FEAT_EVAL
8844 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8845 (long)i);
8846 if (result == NULL)
8847 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008848 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008849 return NULL;
8850 }
8851#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008852 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 }
8856 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008857 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008858 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
8859 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008860 buf = buflist_findnr(i);
8861 if (buf == NULL)
8862 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008863 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008864 return NULL;
8865 }
8866 if (lnump != NULL)
8867 *lnump = ECMD_LAST;
8868 if (buf->b_fname == NULL)
8869 {
8870 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008871 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008872 }
8873 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008874 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008875 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008876 tilde_file = STRCMP(result, "~") == 0;
8877 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 break;
8880
8881#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008882 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008883 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 if (result == NULL)
8885 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008886 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 return NULL;
8888 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008889 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 break;
8891#endif
8892
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008893 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008895 if (result != NULL && !autocmd_fname_full)
8896 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008897 // Still need to turn the fname into a full path. It is
8898 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008899 autocmd_fname_full = TRUE;
8900 result = FullName_save(autocmd_fname, FALSE);
8901 vim_free(autocmd_fname);
8902 autocmd_fname = result;
8903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 if (result == NULL)
8905 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008906 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 return NULL;
8908 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008909 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 break;
8911
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008912 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 if (autocmd_bufnr <= 0)
8914 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008915 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 return NULL;
8917 }
8918 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8919 result = strbuf;
8920 break;
8921
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008922 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 result = autocmd_match;
8924 if (result == NULL)
8925 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008926 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 return NULL;
8928 }
8929 break;
8930
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008931 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008932 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008933 result = estack_sfile(spec_idx == SPEC_SFILE
8934 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 if (result == NULL)
8936 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008937 *errormsg = spec_idx == SPEC_SFILE
8938 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8939 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 return NULL;
8941 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008942 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008944
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008945 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008946 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008947 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008948 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008949 return NULL;
8950 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008951 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008952 result = strbuf;
8953 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008954
8955#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008956 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008957 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008958 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008959 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008960 return NULL;
8961 }
8962 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008963 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008964 result = strbuf;
8965 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008966
Bram Moolenaar90944302020-08-01 20:45:11 +02008967 case SPEC_SID:
8968 if (current_sctx.sc_sid <= 0)
8969 {
8970 *errormsg = _(e_usingsid);
8971 return NULL;
8972 }
8973 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
8974 result = strbuf;
8975 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02008976#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02008977
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008978#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008979 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008980 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8981 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 result = strbuf;
8983 break;
8984#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008985
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008986 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008987 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008988 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 }
8990
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008991 resultlen = (int)STRLEN(result); // length of new string
8992 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 {
8994 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 resultlen = (int)(s - result);
8997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 else if (!skip_mod)
8999 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009000 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 &resultlen);
9002 if (result == NULL)
9003 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009004 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 return NULL;
9006 }
9007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 }
9009
9010 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9011 {
9012 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009013 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009014 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009016 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 result = NULL;
9018 }
9019 else
9020 result = vim_strnsave(result, resultlen);
9021 vim_free(resultbuf);
9022 return result;
9023}
9024
9025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 * Expand the <sfile> string in "arg".
9027 *
9028 * Returns an allocated string, or NULL for any error.
9029 */
9030 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009031expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009033 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 int len;
9035 char_u *result;
9036 char_u *newres;
9037 char_u *repl;
9038 int srclen;
9039 char_u *p;
9040
9041 result = vim_strsave(arg);
9042 if (result == NULL)
9043 return NULL;
9044
9045 for (p = result; *p; )
9046 {
9047 if (STRNCMP(p, "<sfile>", 7) != 0)
9048 ++p;
9049 else
9050 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009051 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00009052 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 if (errormsg != NULL)
9054 {
9055 if (*errormsg)
9056 emsg(errormsg);
9057 vim_free(result);
9058 return NULL;
9059 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009060 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 {
9062 p += srclen;
9063 continue;
9064 }
9065 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9066 newres = alloc(len);
9067 if (newres == NULL)
9068 {
9069 vim_free(repl);
9070 vim_free(result);
9071 return NULL;
9072 }
9073 mch_memmove(newres, result, (size_t)(p - result));
9074 STRCPY(newres + (p - result), repl);
9075 len = (int)STRLEN(newres);
9076 STRCAT(newres, p + srclen);
9077 vim_free(repl);
9078 vim_free(result);
9079 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009080 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 }
9082 }
9083
9084 return result;
9085}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009088/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009089 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009090 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009093dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 if (fname == NULL)
9096 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009097 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098}
9099#endif
9100
9101/*
9102 * ":behave {mswin,xterm}"
9103 */
9104 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009105ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106{
9107 if (STRCMP(eap->arg, "mswin") == 0)
9108 {
9109 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9110 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9111 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9112 set_option_value((char_u *)"keymodel", 0L,
9113 (char_u *)"startsel,stopsel", 0);
9114 }
9115 else if (STRCMP(eap->arg, "xterm") == 0)
9116 {
9117 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9118 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9119 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9120 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9121 }
9122 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009123 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124}
9125
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126static int filetype_detect = FALSE;
9127static int filetype_plugin = FALSE;
9128static int filetype_indent = FALSE;
9129
9130/*
9131 * ":filetype [plugin] [indent] {on,off,detect}"
9132 * on: Load the filetype.vim file to install autocommands for file types.
9133 * off: Load the ftoff.vim file to remove all autocommands for file types.
9134 * plugin on: load filetype.vim and ftplugin.vim
9135 * plugin off: load ftplugof.vim
9136 * indent on: load filetype.vim and indent.vim
9137 * indent off: load indoff.vim
9138 */
9139 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009140ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141{
9142 char_u *arg = eap->arg;
9143 int plugin = FALSE;
9144 int indent = FALSE;
9145
9146 if (*eap->arg == NUL)
9147 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009148 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009149 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 filetype_detect ? "ON" : "OFF",
9151 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9152 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9153 return;
9154 }
9155
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009156 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 for (;;)
9158 {
9159 if (STRNCMP(arg, "plugin", 6) == 0)
9160 {
9161 plugin = TRUE;
9162 arg = skipwhite(arg + 6);
9163 continue;
9164 }
9165 if (STRNCMP(arg, "indent", 6) == 0)
9166 {
9167 indent = TRUE;
9168 arg = skipwhite(arg + 6);
9169 continue;
9170 }
9171 break;
9172 }
9173 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9174 {
9175 if (*arg == 'o' || !filetype_detect)
9176 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009177 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 filetype_detect = TRUE;
9179 if (plugin)
9180 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009181 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 filetype_plugin = TRUE;
9183 }
9184 if (indent)
9185 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009186 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 filetype_indent = TRUE;
9188 }
9189 }
9190 if (*arg == 'd')
9191 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009192 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009193 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 }
9195 }
9196 else if (STRCMP(arg, "off") == 0)
9197 {
9198 if (plugin || indent)
9199 {
9200 if (plugin)
9201 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009202 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 filetype_plugin = FALSE;
9204 }
9205 if (indent)
9206 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009207 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 filetype_indent = FALSE;
9209 }
9210 }
9211 else
9212 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009213 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 filetype_detect = FALSE;
9215 }
9216 }
9217 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009218 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219}
9220
9221/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009222 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 */
9224 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009225ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226{
9227 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009228 {
9229 char_u *arg = eap->arg;
9230
9231 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9232 arg += 9;
9233
9234 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9235 if (arg != eap->arg)
9236 did_filetype = FALSE;
9237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009241ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242{
9243#ifdef FEAT_DIGRAPHS
9244 if (*eap->arg != NUL)
9245 putdigraph(eap->arg);
9246 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009247 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009249 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250#endif
9251}
9252
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009253#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9254 void
9255set_no_hlsearch(int flag)
9256{
9257 no_hlsearch = flag;
9258# ifdef FEAT_EVAL
9259 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9260# endif
9261}
9262
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263/*
9264 * ":nohlsearch"
9265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009267ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009269 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009270 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272#endif
9273
9274#ifdef FEAT_CRYPT
9275/*
9276 * ":X": Get crypt key
9277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009279ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009281 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009282 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283}
9284#endif
9285
9286#ifdef FEAT_FOLDING
9287 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009288ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289{
9290 if (foldManualAllowed(TRUE))
9291 foldCreate(eap->line1, eap->line2);
9292}
9293
9294 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009295ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296{
9297 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9298 eap->forceit, FALSE);
9299}
9300
9301 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009302ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303{
9304 linenr_T lnum;
9305
Bram Moolenaar4facea32019-10-12 20:17:40 +02009306# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009307 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009308# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009309
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009310 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9312 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9313 ml_setmarked(lnum);
9314
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009315 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009317 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009318# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009319 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009320# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321}
9322#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009323
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009324#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009325/*
9326 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9327 * instead of a quickfix command.
9328 */
9329 int
9330is_loclist_cmd(int cmdidx)
9331{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009332 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009333 return FALSE;
9334 return cmdnames[cmdidx].cmd_name[0] == 'l';
9335}
9336#endif
9337
Bram Moolenaar4facea32019-10-12 20:17:40 +02009338#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009339 int
9340get_pressedreturn(void)
9341{
9342 return ex_pressedreturn;
9343}
9344
9345 void
9346set_pressedreturn(int val)
9347{
9348 ex_pressedreturn = val;
9349}
9350#endif