blob: 6a4650130d9c69d7941a60603c5cf1d0fe86ea7c [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 Moolenaareffed932018-08-14 13:38:17 +02002688 * Parse and skip over command modifiers:
2689 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002690 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002691 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002692 * When "skip_only" is TRUE the global variables are not changed, except for
2693 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002694 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2695 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002696 * Call apply_cmdmod() to get the side effects of the modifiers:
2697 * - Increment "sandbox" for ":sandbox"
2698 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002699 * - set msg_silent for ":silent"
2700 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002701 * Return FAIL when the command is not to be executed.
2702 * May set "errormsg" to an error message.
2703 */
2704 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002705parse_command_modifiers(
2706 exarg_T *eap,
2707 char **errormsg,
2708 cmdmod_T *cmod,
2709 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002710{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002711 char_u *p;
2712 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002713
Bram Moolenaare1004402020-10-24 20:49:43 +02002714 CLEAR_POINTER(cmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002715
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002716 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002717 for (;;)
2718 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002719 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002720 {
2721 if (*eap->cmd == ':')
2722 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002723 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002724 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002725
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002726 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002727 if (*eap->cmd == NUL && exmode_active
2728 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2729 || getline_equal(eap->getline, eap->cookie, getexline))
2730 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2731 {
2732 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002733 if (!skip_only)
2734 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002735 }
2736
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002737 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002738 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002739 return FAIL;
2740 if (*eap->cmd == NUL)
2741 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002742 if (!skip_only)
2743 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002744 return FAIL;
2745 }
2746
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002747 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaar17126b12021-01-07 22:03:02 +01002748
2749 // In Vim9 script a variable can shadow a command modifier:
2750 // verbose = 123
2751 // verbose += 123
2752 // silent! verbose = func()
2753 // verbose.member = 2
2754 // verbose[expr] = 2
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002755 // But not:
2756 // verbose [a, b] = list
Bram Moolenaar17126b12021-01-07 22:03:02 +01002757 if (in_vim9script())
2758 {
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002759 char_u *s, *n;
Bram Moolenaar17126b12021-01-07 22:03:02 +01002760
2761 for (s = p; ASCII_ISALPHA(*s); ++s)
2762 ;
Bram Moolenaar9e0f8832021-01-09 12:09:22 +01002763 n = skipwhite(s);
2764 if (vim_strchr((char_u *)".=", *n) != NULL
2765 || *s == '['
2766 || (*n != NUL && n[1] == '='))
Bram Moolenaar17126b12021-01-07 22:03:02 +01002767 break;
2768 }
2769
Bram Moolenaareffed932018-08-14 13:38:17 +02002770 switch (*p)
2771 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002772 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002773 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2774 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002775 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002776 continue;
2777
2778 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2779 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002780 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002781 continue;
2782 }
2783 if (checkforcmd(&eap->cmd, "browse", 3))
2784 {
2785#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002786 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002787#endif
2788 continue;
2789 }
2790 if (!checkforcmd(&eap->cmd, "botright", 2))
2791 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002792 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002793 continue;
2794
2795 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2796 break;
2797#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002798 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002799#endif
2800 continue;
2801
2802 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2803 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002804 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002805 continue;
2806 }
2807 if (checkforcmd(&eap->cmd, "keepalt", 5))
2808 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002809 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002810 continue;
2811 }
2812 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2813 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002814 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002815 continue;
2816 }
2817 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2818 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002819 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002820 continue;
2821
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002822 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002823 {
2824 char_u *reg_pat;
2825
2826 if (!checkforcmd(&p, "filter", 4)
2827 || *p == NUL || ends_excmd(*p))
2828 break;
2829 if (*p == '!')
2830 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002831 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002832 p = skipwhite(p + 1);
2833 if (*p == NUL || ends_excmd(*p))
2834 break;
2835 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002836#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002837 // Avoid that "filter(arg)" is recognized.
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +01002838 if (in_vim9script() && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002839 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002840#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002841 if (skip_only)
2842 p = skip_vimgrep_pat(p, NULL, NULL);
2843 else
2844 // NOTE: This puts a NUL after the pattern.
2845 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002846 if (p == NULL || *p == NUL)
2847 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002848 if (!skip_only)
2849 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002850 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002851 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002852 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002853 break;
2854 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002855 eap->cmd = p;
2856 continue;
2857 }
2858
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002859 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002860 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2861 || *p == NUL || ends_excmd(*p))
2862 break;
2863 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002864 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002865 continue;
2866
2867 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2868 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002869 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002870 continue;
2871 }
2872
2873 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2874 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002875 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002876 continue;
2877
2878 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2879 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002880 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002881 continue;
2882 }
2883 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2884 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002885 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002886 continue;
2887
2888 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2889 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002890 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002891 continue;
2892
2893 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2894 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002895 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02002896 continue;
2897 }
2898 if (!checkforcmd(&eap->cmd, "silent", 3))
2899 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002900 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002901 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2902 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002903 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002904 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02002905 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002906 }
2907 continue;
2908
2909 case 't': if (checkforcmd(&p, "tab", 3))
2910 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002911 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002912 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002913 long tabnr = get_address(eap, &eap->cmd,
2914 ADDR_TABS, eap->skip,
2915 skip_only, FALSE, 1);
2916 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02002917 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01002918 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002919 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002920 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2921 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002922 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002923 return FAIL;
2924 }
Bram Moolenaare1004402020-10-24 20:49:43 +02002925 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002926 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002927 }
2928 eap->cmd = p;
2929 continue;
2930 }
2931 if (!checkforcmd(&eap->cmd, "topleft", 2))
2932 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002933 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02002934 continue;
2935
2936 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2937 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002938 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002939 continue;
2940
2941 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2942 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002943 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002944 continue;
2945 }
Bram Moolenaar39f3b142021-02-14 12:57:36 +01002946 if (checkforcmd(&eap->cmd, "vim9cmd", 4))
2947 {
2948 if (ends_excmd2(p, eap->cmd))
2949 {
2950 *errormsg =
2951 _(e_vim9cmd_must_be_followed_by_command);
2952 return FAIL;
2953 }
2954 cmod->cmod_flags |= CMOD_VIM9CMD;
2955 continue;
2956 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002957 if (!checkforcmd(&p, "verbose", 4))
2958 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002959 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02002960 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002961 else
Bram Moolenaare1004402020-10-24 20:49:43 +02002962 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002963 eap->cmd = p;
2964 continue;
2965 }
2966 break;
2967 }
2968
2969 return OK;
2970}
2971
2972/*
Bram Moolenaarfa984412021-03-25 22:15:28 +01002973 * Return TRUE if "cmod" has anything set.
2974 */
2975 int
2976has_cmdmod(cmdmod_T *cmod)
2977{
2978 return cmod->cmod_flags != 0
2979 || cmod->cmod_split != 0
2980 || cmod->cmod_verbose != 0
2981 || cmod->cmod_tab != 0
2982 || cmod->cmod_filter_regmatch.regprog != NULL;
2983}
2984
2985/*
2986 * If Vim9 script and "cmdmod" has anything set give an error and return TRUE.
2987 */
2988 int
2989cmdmod_error(void)
2990{
2991 if (in_vim9script() && has_cmdmod(&cmdmod))
2992 {
2993 emsg(_(e_misplaced_command_modifier));
2994 return TRUE;
2995 }
2996 return FALSE;
2997}
2998
2999/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003000 * Apply the command modifiers. Saves current state in "cmdmod", call
3001 * undo_cmdmod() later.
3002 */
3003 void
3004apply_cmdmod(cmdmod_T *cmod)
3005{
3006#ifdef HAVE_SANDBOX
3007 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
3008 {
3009 ++sandbox;
3010 cmod->cmod_did_sandbox = TRUE;
3011 }
3012#endif
3013 if (cmod->cmod_verbose > 0)
3014 {
3015 if (cmod->cmod_verbose_save == 0)
3016 cmod->cmod_verbose_save = p_verbose + 1;
3017 p_verbose = cmod->cmod_verbose;
3018 }
3019
3020 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
3021 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02003022 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003023 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02003024 cmod->cmod_save_msg_scroll = msg_scroll;
3025 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003026 if (cmod->cmod_flags & CMOD_SILENT)
3027 ++msg_silent;
3028 if (cmod->cmod_flags & CMOD_UNSILENT)
3029 msg_silent = 0;
3030
3031 if (cmod->cmod_flags & CMOD_ERRSILENT)
3032 {
3033 ++emsg_silent;
3034 ++cmod->cmod_did_esilent;
3035 }
3036
Bram Moolenaare1004402020-10-24 20:49:43 +02003037 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003038 {
3039 // Set 'eventignore' to "all".
3040 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02003041 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003042 set_string_option_direct((char_u *)"ei", -1,
3043 (char_u *)"all", OPT_FREE, SID_NONE);
3044 }
3045}
3046
3047/*
Bram Moolenaare1004402020-10-24 20:49:43 +02003048 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003049 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003050 void
Bram Moolenaare1004402020-10-24 20:49:43 +02003051undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003052{
Bram Moolenaare1004402020-10-24 20:49:43 +02003053 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003054 {
Bram Moolenaare1004402020-10-24 20:49:43 +02003055 p_verbose = cmod->cmod_verbose_save - 1;
3056 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003057 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003058
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003059#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02003060 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003061 {
3062 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02003063 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003064 }
3065#endif
3066
Bram Moolenaare1004402020-10-24 20:49:43 +02003067 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003068 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003069 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003070 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3071 OPT_FREE, SID_NONE);
3072 free_string_option(cmod->cmod_save_ei);
3073 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003074 }
3075
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003076 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003077
Bram Moolenaare1004402020-10-24 20:49:43 +02003078 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003079 {
3080 // messages could be enabled for a serious error, need to check if the
3081 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003082 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3083 msg_silent = cmod->cmod_save_msg_silent - 1;
3084 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003085 if (emsg_silent < 0)
3086 emsg_silent = 0;
3087 // Restore msg_scroll, it's set by file I/O commands, even when no
3088 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003089 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003090
3091 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3092 // somewhere in the line. Put it back in the first column.
3093 if (redirecting())
3094 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003095
Bram Moolenaare1004402020-10-24 20:49:43 +02003096 cmod->cmod_save_msg_silent = 0;
3097 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003098 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003099}
3100
3101/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003102 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003103 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003104 * Return FAIL and set "errormsg" or return OK.
3105 */
3106 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003107parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003108{
3109 int address_count = 1;
3110 linenr_T lnum;
3111
3112 // Repeat for all ',' or ';' separated addresses.
3113 for (;;)
3114 {
3115 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003116 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003117 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003118 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003119 eap->addr_count == 0, address_count++);
3120 if (eap->cmd == NULL) // error detected
3121 return FAIL;
3122 if (lnum == MAXLNUM)
3123 {
3124 if (*eap->cmd == '%') // '%' - all lines
3125 {
3126 ++eap->cmd;
3127 switch (eap->addr_type)
3128 {
3129 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003130 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003131 eap->line1 = 1;
3132 eap->line2 = curbuf->b_ml.ml_line_count;
3133 break;
3134 case ADDR_LOADED_BUFFERS:
3135 {
3136 buf_T *buf = firstbuf;
3137
3138 while (buf->b_next != NULL
3139 && buf->b_ml.ml_mfp == NULL)
3140 buf = buf->b_next;
3141 eap->line1 = buf->b_fnum;
3142 buf = lastbuf;
3143 while (buf->b_prev != NULL
3144 && buf->b_ml.ml_mfp == NULL)
3145 buf = buf->b_prev;
3146 eap->line2 = buf->b_fnum;
3147 break;
3148 }
3149 case ADDR_BUFFERS:
3150 eap->line1 = firstbuf->b_fnum;
3151 eap->line2 = lastbuf->b_fnum;
3152 break;
3153 case ADDR_WINDOWS:
3154 case ADDR_TABS:
3155 if (IS_USER_CMDIDX(eap->cmdidx))
3156 {
3157 eap->line1 = 1;
3158 eap->line2 = eap->addr_type == ADDR_WINDOWS
3159 ? LAST_WIN_NR : LAST_TAB_NR;
3160 }
3161 else
3162 {
3163 // there is no Vim command which uses '%' and
3164 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003165 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003166 return FAIL;
3167 }
3168 break;
3169 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003170 case ADDR_UNSIGNED:
3171 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003172 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003173 return FAIL;
3174 case ADDR_ARGUMENTS:
3175 if (ARGCOUNT == 0)
3176 eap->line1 = eap->line2 = 0;
3177 else
3178 {
3179 eap->line1 = 1;
3180 eap->line2 = ARGCOUNT;
3181 }
3182 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003183 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003184#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003185 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003186 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003187 if (eap->line2 == 0)
3188 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003189#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003190 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003191 case ADDR_NONE:
3192 // Will give an error later if a range is found.
3193 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003194 }
3195 ++eap->addr_count;
3196 }
3197 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3198 {
3199 pos_T *fp;
3200
3201 // '*' - visual area
3202 if (eap->addr_type != ADDR_LINES)
3203 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003204 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003205 return FAIL;
3206 }
3207
3208 ++eap->cmd;
3209 if (!eap->skip)
3210 {
3211 fp = getmark('<', FALSE);
3212 if (check_mark(fp) == FAIL)
3213 return FAIL;
3214 eap->line1 = fp->lnum;
3215 fp = getmark('>', FALSE);
3216 if (check_mark(fp) == FAIL)
3217 return FAIL;
3218 eap->line2 = fp->lnum;
3219 ++eap->addr_count;
3220 }
3221 }
3222 }
3223 else
3224 eap->line2 = lnum;
3225 eap->addr_count++;
3226
3227 if (*eap->cmd == ';')
3228 {
3229 if (!eap->skip)
3230 {
3231 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003232 // Don't leave the cursor on an illegal line or column, but do
3233 // accept zero as address, so 0;/PATTERN/ works correctly.
3234 if (eap->line2 > 0)
3235 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003236 }
3237 }
3238 else if (*eap->cmd != ',')
3239 break;
3240 ++eap->cmd;
3241 }
3242
3243 // One address given: set start and end lines.
3244 if (eap->addr_count == 1)
3245 {
3246 eap->line1 = eap->line2;
3247 // ... but only implicit: really no address given
3248 if (lnum == MAXLNUM)
3249 eap->addr_count = 0;
3250 }
3251 return OK;
3252}
3253
3254/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003255 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 * If there is a match advance "pp" to the argument and return TRUE.
3257 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003258 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003259checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003260 char_u **pp, // start of command
3261 char *cmd, // name of command
3262 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263{
3264 int i;
3265
3266 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003267 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 break;
Bram Moolenaar7a66a172020-10-16 19:56:12 +02003269 if (i >= len && !isalpha((*pp)[i]) && (*pp)[i] != '_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
3271 *pp = skipwhite(*pp + i);
3272 return TRUE;
3273 }
3274 return FALSE;
3275}
3276
3277/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003278 * Append "cmd" to the error message in IObuff.
3279 * Takes care of limiting the length and handling 0xa0, which would be
3280 * invisible otherwise.
3281 */
3282 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003283append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003284{
3285 char_u *s = cmd;
3286 char_u *d;
3287
3288 STRCAT(IObuff, ": ");
3289 d = IObuff + STRLEN(IObuff);
3290 while (*s != NUL && d - IObuff < IOSIZE - 7)
3291 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003292 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003293 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003294 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003295 STRCPY(d, "<a0>");
3296 d += 4;
3297 }
3298 else
3299 MB_COPY_CHAR(s, d);
3300 }
3301 *d = NUL;
3302}
3303
3304/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003305 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3306 * the name. Otherwise just return "start".
3307 */
3308 char_u *
3309skip_option_env_lead(char_u *start)
3310{
3311 char_u *name = start;
3312
3313 if (*start == '&')
3314 {
3315 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3316 name += 3;
3317 else
3318 name += 1;
3319 }
3320 else if (*start == '$')
3321 name += 1;
3322 return name;
3323}
3324
3325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003327 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003328 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003329 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330 *
3331 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3332 * assignment without "let". Sets eap->cmdidx to the command while returning
3333 * "eap->cmd".
3334 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 * Returns NULL for an ambiguous user command.
3336 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003337 char_u *
3338find_ex_command(
3339 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003340 int *full UNUSED,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003341 int (*lookup)(char_u *, size_t, int cmd, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003342 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
3344 int len;
3345 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003346 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348#ifdef FEAT_EVAL
3349 /*
3350 * Recognize a Vim9 script function/method call and assignment:
3351 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3352 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003353 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003354 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003356 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357
Bram Moolenaar83144542020-08-02 20:40:43 +02003358 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003359 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003361 int oplen;
3362 int heredoc;
3363 char_u *swp = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003365 if (
3366 // "(..." is an expression.
3367 // "funcname(" is always a function call.
3368 *p == '('
3369 || (p == eap->cmd
3370 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003371 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003372 *eap->cmd == '{'
3373 // "'string'->func()" is an expression.
3374 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003375 // '"string"->func()' is an expression.
3376 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003377 // "g:varname" is an expression.
3378 || eap->cmd[1] == ':'
3379 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003380 // "varname->func()" is an expression.
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003381 : (*swp == '-' && swp[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003382 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003383 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3384 {
3385 // "{" by itself is the start of a block.
3386 eap->cmdidx = CMD_block;
3387 return eap->cmd + 1;
3388 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003389 eap->cmdidx = CMD_eval;
3390 return eap->cmd;
3391 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003392
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003393 if (p != eap->cmd && (
3394 // "varname[]" is an expression.
3395 *p == '['
3396 // "varname.key" is an expression.
3397 || (*p == '.' && ASCII_ISALPHA(p[1]))))
3398 {
3399 char_u *after = p;
3400
3401 // When followed by "=" or "+=" then it is an assignment.
3402 ++emsg_silent;
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01003403 if (*after == '.')
3404 after = skipwhite(after + 1);
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003405 if (skip_expr(&after, NULL) == OK)
3406 after = skipwhite(after);
3407 else
3408 after = (char_u *)"";
3409 if (*after == '=' || (*after != NUL && after[1] == '=')
3410 || (after[0] == '.' && after[1] == '.'
3411 && after[2] == '='))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003412 eap->cmdidx = CMD_var;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003413 else
3414 eap->cmdidx = CMD_eval;
3415 --emsg_silent;
3416 return eap->cmd;
3417 }
3418
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003419 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3420 // an assignment.
3421 // If there is no line break inside the "[...]" then "p" is
3422 // advanced to after the "]" by to_name_const_end(): check if a "="
3423 // follows.
3424 // If "[...]" has a line break "p" still points at the "[" and it
3425 // can't be an assignment.
3426 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003427 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003428 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003429 if (p == eap->cmd && *p == '[')
3430 {
3431 int count = 0;
3432 int semicolon = FALSE;
3433
3434 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3435 }
3436 if (p == NULL || p == eap->cmd || *skipwhite(p) != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003437 {
3438 eap->cmdidx = CMD_eval;
3439 return eap->cmd;
3440 }
3441 if (p > eap->cmd && *skipwhite(p) == '=')
3442 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003443 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003444 return eap->cmd;
3445 }
3446 }
3447
3448 // Recognize an assignment if we recognize the variable name:
3449 // "g:var = expr"
Bram Moolenaare0890d62021-02-17 14:52:14 +01003450 // "var = expr" where "var" is a variable name.
Bram Moolenaar83144542020-08-02 20:40:43 +02003451 if (*eap->cmd == '@')
3452 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003453 oplen = assignment_len(skipwhite(p), &heredoc);
3454 if (oplen > 0)
3455 {
3456 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3457 || *eap->cmd == '&'
3458 || *eap->cmd == '$'
3459 || *eap->cmd == '@'
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003460 || lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003461 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003462 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003463 return eap->cmd;
3464 }
3465 }
3466
3467 // Recognize using a type for a w:, b:, t: or g: variable:
3468 // "w:varname: number = 123".
3469 if (eap->cmd[1] == ':' && *p == ':')
3470 {
3471 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003472 return eap->cmd;
3473 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003474 }
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003475
3476 // If it is an ID it might be a variable with an operator on the next
3477 // line, if the variable exists it can't be an Ex command.
3478 if (p > eap->cmd && ends_excmd(*skipwhite(p))
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003479 && (lookup(eap->cmd, p - eap->cmd, TRUE, cctx) == OK
Bram Moolenaar148be9b2021-02-02 21:33:52 +01003480 || (ASCII_ISALPHA(eap->cmd[0]) && eap->cmd[1] == ':')))
3481 {
3482 eap->cmdidx = CMD_eval;
3483 return eap->cmd;
3484 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 }
3486#endif
3487
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 /*
3489 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003490 * Exceptions:
Bram Moolenaar10b94212021-02-19 21:42:57 +01003491 * - The 'k' command can directly be followed by any character.
3492 * But it is not used in Vim9 script.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003494 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003496 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 */
3498 p = eap->cmd;
3499 if (*p == 'k')
3500 {
3501 eap->cmdidx = CMD_k;
3502 ++p;
3503 }
3504 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003505 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3506 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 || p[1] == 'g'
3508 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3509 || p[1] == 'I'
3510 || (p[1] == 'r' && p[2] != 'e')))
3511 {
3512 eap->cmdidx = CMD_substitute;
3513 ++p;
3514 }
3515 else
3516 {
3517 while (ASCII_ISALPHA(*p))
3518 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003519 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003520 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003522 while (ASCII_ISALNUM(*p))
3523 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 }
3525 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3526 {
3527 // include "9" for "vim9script"
3528 ++p;
3529 while (ASCII_ISALPHA(*p))
3530 ++p;
3531 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003532
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003533 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003534 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 ++p;
3536 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003537 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3538 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003539 // Check for ":dl", ":dell", etc. to ":deletel": that's
3540 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003541 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003542 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003543 break;
3544 if (i == len - 1)
3545 {
3546 --len;
3547 if (p[-1] == 'l')
3548 eap->flags |= EXFLAG_LIST;
3549 else
3550 eap->flags |= EXFLAG_PRINT;
3551 }
3552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003554 if (ASCII_ISLOWER(eap->cmd[0]))
3555 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003556 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003557 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003558
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003559 if (command_count != (int)CMD_SIZE)
3560 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003561 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003562 getout(1);
3563 }
3564
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003565 // Use a precomputed index for fast look-up in cmdnames[]
3566 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003567 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3568 if (ASCII_ISLOWER(c2))
3569 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3570 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003571 else if (ASCII_ISUPPER(eap->cmd[0]))
3572 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003574 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575
3576 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3577 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3578 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3579 (size_t)len) == 0)
3580 {
3581#ifdef FEAT_EVAL
3582 if (full != NULL
3583 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3584 *full = TRUE;
3585#endif
3586 break;
3587 }
3588
Bram Moolenaar95388e32020-11-20 21:07:00 +01003589 // Not not recognize ":*" as the star command unless '*' is in
3590 // 'cpoptions'.
3591 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3592 p = eap->cmd;
3593
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003594 // Look for a user defined command as a last resort. Let ":Print" be
3595 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003596 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3597 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003599 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 while (ASCII_ISALNUM(*p))
3601 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003602 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003604 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 eap->cmdidx = CMD_SIZE;
3606 }
3607
Bram Moolenaar373863e2020-09-26 17:20:53 +02003608 // ":fina" means ":finally" for backwards compatibility.
3609 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3610 eap->cmdidx = CMD_finally;
3611
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003612#ifdef FEAT_EVAL
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003613 if (eap->cmdidx < CMD_SIZE
3614 && in_vim9script()
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003615 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!'
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003616 && (eap->cmdidx < 0 ||
3617 (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0))
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003618 {
3619 semsg(_(e_command_not_followed_by_white_space_str), eap->cmd);
3620 eap->cmdidx = CMD_SIZE;
3621 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003622#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003623
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 return p;
3625}
3626
3627#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003628static struct cmdmod
3629{
3630 char *name;
3631 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003632 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003633} cmdmods[] = {
3634 {"aboveleft", 3, FALSE},
3635 {"belowright", 3, FALSE},
3636 {"botright", 2, FALSE},
3637 {"browse", 3, FALSE},
3638 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003639 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003640 {"hide", 3, FALSE},
3641 {"keepalt", 5, FALSE},
3642 {"keepjumps", 5, FALSE},
3643 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003644 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003645 {"leftabove", 5, FALSE},
3646 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003647 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003648 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003649 {"rightbelow", 6, FALSE},
3650 {"sandbox", 3, FALSE},
3651 {"silent", 3, FALSE},
3652 {"tab", 3, TRUE},
3653 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003654 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003655 {"verbose", 4, TRUE},
3656 {"vertical", 4, FALSE},
3657};
3658
3659/*
3660 * Return length of a command modifier (including optional count).
3661 * Return zero when it's not a modifier.
3662 */
3663 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003664modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003665{
3666 int i, j;
3667 char_u *p = cmd;
3668
3669 if (VIM_ISDIGIT(*cmd))
3670 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003671 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003672 {
3673 for (j = 0; p[j] != NUL; ++j)
3674 if (p[j] != cmdmods[i].name[j])
3675 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003676 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003677 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003678 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003679 }
3680 return 0;
3681}
3682
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683/*
3684 * Return > 0 if an Ex command "name" exists.
3685 * Return 2 if there is an exact match.
3686 * Return 3 if there is an ambiguous match.
3687 */
3688 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003689cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690{
3691 exarg_T ea;
3692 int full = FALSE;
3693 int i;
3694 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003695 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003697 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003698 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 {
3700 for (j = 0; name[j] != NUL; ++j)
3701 if (name[j] != cmdmods[i].name[j])
3702 break;
3703 if (name[j] == NUL && j >= cmdmods[i].minlen)
3704 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3705 }
3706
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003707 // Check built-in commands and user defined commands.
3708 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003709 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003712 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003714 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3715 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003716 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003717 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3719}
Bram Moolenaar038e09e2021-02-06 12:38:51 +01003720
3721/*
3722 * "fullcommand" function
3723 */
3724 void
3725f_fullcommand(typval_T *argvars, typval_T *rettv)
3726{
3727 exarg_T ea;
3728 char_u *name = argvars[0].vval.v_string;
3729 char_u *p;
3730
3731 while (name[0] != NUL && name[0] == ':')
3732 name++;
3733 name = skip_range(name, TRUE, NULL);
3734
3735 rettv->v_type = VAR_STRING;
3736
3737 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
3738 ea.cmdidx = (cmdidx_T)0;
3739 p = find_ex_command(&ea, NULL, NULL, NULL);
3740 if (p == NULL || ea.cmdidx == CMD_SIZE)
3741 return;
3742
3743 rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx)
3744 ? get_user_commands(NULL, ea.useridx)
3745 : cmdnames[ea.cmdidx].cmd_name);
3746}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747#endif
3748
Bram Moolenaard0190392019-08-23 21:17:35 +02003749 cmdidx_T
3750excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751{
Bram Moolenaard0190392019-08-23 21:17:35 +02003752 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753
Bram Moolenaard0190392019-08-23 21:17:35 +02003754 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3755 idx = (cmdidx_T)((int)idx + 1))
3756 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 break;
3758
Bram Moolenaard0190392019-08-23 21:17:35 +02003759 return idx;
3760}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761
Bram Moolenaard0190392019-08-23 21:17:35 +02003762 long
3763excmd_get_argt(cmdidx_T idx)
3764{
3765 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766}
3767
3768/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003769 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 *
3771 * Backslashed delimiters after / or ? will be skipped, and commands will
3772 * not be expanded between /'s and ?'s or after "'".
3773 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003774 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 * Returns the "cmd" pointer advanced to beyond the range.
3776 */
3777 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003778skip_range(
3779 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003780 int skip_star, // skip "*" used for Visual range
3781 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003783 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003785 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003787 if (*cmd == '\\')
3788 {
3789 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3790 ++cmd;
3791 else
3792 break;
3793 }
3794 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 {
3796 if (*++cmd == NUL && ctx != NULL)
3797 *ctx = EXPAND_NOTHING;
3798 }
3799 else if (*cmd == '/' || *cmd == '?')
3800 {
3801 delim = *cmd++;
3802 while (*cmd != NUL && *cmd != delim)
3803 if (*cmd++ == '\\' && *cmd != NUL)
3804 ++cmd;
3805 if (*cmd == NUL && ctx != NULL)
3806 *ctx = EXPAND_NOTHING;
3807 }
3808 if (*cmd != NUL)
3809 ++cmd;
3810 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003811
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003812 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003813 while (*cmd == ':')
3814 cmd = skipwhite(cmd + 1);
3815
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003816 // Skip "*" used for Visual range.
3817 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3818 cmd = skipwhite(cmd + 1);
3819
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 return cmd;
3821}
3822
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003823 static void
3824addr_error(cmd_addr_T addr_type)
3825{
3826 if (addr_type == ADDR_NONE)
3827 emsg(_(e_norange));
3828 else
3829 emsg(_(e_invrange));
3830}
3831
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003833 * Return the default address for an address type.
3834 */
3835 static linenr_T
3836default_address(exarg_T *eap)
3837{
3838 linenr_T lnum = 0;
3839
3840 switch (eap->addr_type)
3841 {
3842 case ADDR_LINES:
3843 case ADDR_OTHER:
3844 // Default is the cursor line number. Avoid using an invalid
3845 // line number though.
3846 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3847 lnum = curbuf->b_ml.ml_line_count;
3848 else
3849 lnum = curwin->w_cursor.lnum;
3850 break;
3851 case ADDR_WINDOWS:
3852 lnum = CURRENT_WIN_NR;
3853 break;
3854 case ADDR_ARGUMENTS:
3855 lnum = curwin->w_arg_idx + 1;
3856 if (lnum > ARGCOUNT)
3857 lnum = ARGCOUNT;
3858 break;
3859 case ADDR_LOADED_BUFFERS:
3860 case ADDR_BUFFERS:
3861 lnum = curbuf->b_fnum;
3862 break;
3863 case ADDR_TABS:
3864 lnum = CURRENT_TAB_NR;
3865 break;
3866 case ADDR_TABS_RELATIVE:
3867 case ADDR_UNSIGNED:
3868 lnum = 1;
3869 break;
3870 case ADDR_QUICKFIX:
3871#ifdef FEAT_QUICKFIX
3872 lnum = qf_get_cur_idx(eap);
3873#endif
3874 break;
3875 case ADDR_QUICKFIX_VALID:
3876#ifdef FEAT_QUICKFIX
3877 lnum = qf_get_cur_valid_idx(eap);
3878#endif
3879 break;
3880 case ADDR_NONE:
3881 // Will give an error later if a range is found.
3882 break;
3883 }
3884 return lnum;
3885}
3886
3887/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003888 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 *
3890 * Set ptr to the next character after the part that was interpreted.
3891 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003892 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 *
3894 * Return MAXLNUM when no Ex address was found.
3895 */
3896 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003897get_address(
3898 exarg_T *eap UNUSED,
3899 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003900 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003901 int skip, // only skip the address, don't use it
3902 int silent, // no errors or side effects
3903 int to_other_file, // flag: may jump to other file
3904 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905{
3906 int c;
3907 int i;
3908 long n;
3909 char_u *cmd;
3910 pos_T pos;
3911 pos_T *fp;
3912 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003913 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914
3915 cmd = skipwhite(*ptr);
3916 lnum = MAXLNUM;
3917 do
3918 {
3919 switch (*cmd)
3920 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003921 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003922 ++cmd;
3923 switch (addr_type)
3924 {
3925 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003926 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 lnum = curwin->w_cursor.lnum;
3928 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003929 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003930 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003931 break;
3932 case ADDR_ARGUMENTS:
3933 lnum = curwin->w_arg_idx + 1;
3934 break;
3935 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003936 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003937 lnum = curbuf->b_fnum;
3938 break;
3939 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003940 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003941 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003942 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003943 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003944 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003945 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003946 cmd = NULL;
3947 goto error;
3948 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003949 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003950#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003951 lnum = qf_get_cur_idx(eap);
3952#endif
3953 break;
3954 case ADDR_QUICKFIX_VALID:
3955#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003956 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003957#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003958 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003959 }
3960 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003962 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003963 ++cmd;
3964 switch (addr_type)
3965 {
3966 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003967 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 lnum = curbuf->b_ml.ml_line_count;
3969 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003970 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003971 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003972 break;
3973 case ADDR_ARGUMENTS:
3974 lnum = ARGCOUNT;
3975 break;
3976 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003977 buf = lastbuf;
3978 while (buf->b_ml.ml_mfp == NULL)
3979 {
3980 if (buf->b_prev == NULL)
3981 break;
3982 buf = buf->b_prev;
3983 }
3984 lnum = buf->b_fnum;
3985 break;
3986 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003987 lnum = lastbuf->b_fnum;
3988 break;
3989 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003990 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003991 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003992 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003993 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003994 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003995 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003996 cmd = NULL;
3997 goto error;
3998 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003999 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004000#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004001 lnum = qf_get_size(eap);
4002 if (lnum == 0)
4003 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004004#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004005 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004006 case ADDR_QUICKFIX_VALID:
4007#ifdef FEAT_QUICKFIX
4008 lnum = qf_get_valid_size(eap);
4009 if (lnum == 0)
4010 lnum = 1;
4011#endif
4012 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004013 }
4014 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004016 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004017 if (*++cmd == NUL)
4018 {
4019 cmd = NULL;
4020 goto error;
4021 }
4022 if (addr_type != ADDR_LINES)
4023 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004024 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004025 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004026 goto error;
4027 }
4028 if (skip)
4029 ++cmd;
4030 else
4031 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004032 // Only accept a mark in another file when it is
4033 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004034 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4035 ++cmd;
4036 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004037 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004038 lnum = curwin->w_cursor.lnum;
4039 else
4040 {
4041 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
4043 cmd = NULL;
4044 goto error;
4045 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004046 lnum = fp->lnum;
4047 }
4048 }
4049 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050
4051 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004052 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004053 c = *cmd++;
4054 if (addr_type != ADDR_LINES)
4055 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004056 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004057 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004058 goto error;
4059 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004060 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004061 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01004062 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004063 if (*cmd == c)
4064 ++cmd;
4065 }
4066 else
4067 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004068 int flags;
4069
4070 pos = curwin->w_cursor; // save curwin->w_cursor
4071
4072 // When '/' or '?' follows another address, start from
4073 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004074 if (lnum != MAXLNUM)
4075 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004076
4077 // Start a forward search at the end of the line (unless
4078 // before the first line).
4079 // Start a backward search at the start of the line.
4080 // This makes sure we never match in the current
4081 // line, and can match anywhere in the
4082 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004083 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004084 curwin->w_cursor.col = MAXCOL;
4085 else
4086 curwin->w_cursor.col = 0;
4087 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004088 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01004089 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004090 {
4091 curwin->w_cursor = pos;
4092 cmd = NULL;
4093 goto error;
4094 }
4095 lnum = curwin->w_cursor.lnum;
4096 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004097 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004098 cmd += searchcmdlen;
4099 }
4100 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004102 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004103 ++cmd;
4104 if (addr_type != ADDR_LINES)
4105 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004106 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004107 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004108 goto error;
4109 }
4110 if (*cmd == '&')
4111 i = RE_SUBST;
4112 else if (*cmd == '?' || *cmd == '/')
4113 i = RE_SEARCH;
4114 else
4115 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004116 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004117 cmd = NULL;
4118 goto error;
4119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004121 if (!skip)
4122 {
4123 /*
4124 * When search follows another address, start from
4125 * there.
4126 */
4127 if (lnum != MAXLNUM)
4128 pos.lnum = lnum;
4129 else
4130 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004132 /*
4133 * Start the search just like for the above
4134 * do_search().
4135 */
4136 if (*cmd != '?')
4137 pos.col = MAXCOL;
4138 else
4139 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004140 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004141 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004142 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004143 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004144 lnum = pos.lnum;
4145 else
4146 {
4147 cmd = NULL;
4148 goto error;
4149 }
4150 }
4151 ++cmd;
4152 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153
4154 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004155 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004156 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 }
4158
4159 for (;;)
4160 {
4161 cmd = skipwhite(cmd);
4162 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4163 break;
4164
4165 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004166 {
4167 switch (addr_type)
4168 {
4169 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004170 case ADDR_OTHER:
4171 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004172 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004173 break;
4174 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004175 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004176 break;
4177 case ADDR_ARGUMENTS:
4178 lnum = curwin->w_arg_idx + 1;
4179 break;
4180 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004181 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004182 lnum = curbuf->b_fnum;
4183 break;
4184 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004185 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004186 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004187 case ADDR_TABS_RELATIVE:
4188 lnum = 1;
4189 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004190 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004191#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004192 lnum = qf_get_cur_idx(eap);
4193#endif
4194 break;
4195 case ADDR_QUICKFIX_VALID:
4196#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004197 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004198#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004199 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004200 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004201 case ADDR_UNSIGNED:
4202 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004203 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004204 }
4205 }
4206
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004208 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 else
4210 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004211 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 n = 1;
4213 else
4214 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004215
4216 if (addr_type == ADDR_TABS_RELATIVE)
4217 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004218 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004219 cmd = NULL;
4220 goto error;
4221 }
4222 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004223 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004224 lnum = compute_buffer_local_count(
4225 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004227 {
4228#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004229 // Relative line addressing, need to adjust for folded lines
4230 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004231 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4232 && address_count >= 2)
4233 (void)hasFolding(lnum, NULL, &lnum);
4234#endif
4235 if (i == '-')
4236 lnum -= n;
4237 else
4238 lnum += n;
4239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 }
4241 } while (*cmd == '/' || *cmd == '?');
4242
4243error:
4244 *ptr = cmd;
4245 return lnum;
4246}
4247
4248/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004249 * Set eap->line1 and eap->line2 to the whole range.
4250 * Used for commands with the EX_DFLALL flag and no range given.
4251 */
4252 static void
4253address_default_all(exarg_T *eap)
4254{
4255 eap->line1 = 1;
4256 switch (eap->addr_type)
4257 {
4258 case ADDR_LINES:
4259 case ADDR_OTHER:
4260 eap->line2 = curbuf->b_ml.ml_line_count;
4261 break;
4262 case ADDR_LOADED_BUFFERS:
4263 {
4264 buf_T *buf = firstbuf;
4265
4266 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4267 buf = buf->b_next;
4268 eap->line1 = buf->b_fnum;
4269 buf = lastbuf;
4270 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4271 buf = buf->b_prev;
4272 eap->line2 = buf->b_fnum;
4273 }
4274 break;
4275 case ADDR_BUFFERS:
4276 eap->line1 = firstbuf->b_fnum;
4277 eap->line2 = lastbuf->b_fnum;
4278 break;
4279 case ADDR_WINDOWS:
4280 eap->line2 = LAST_WIN_NR;
4281 break;
4282 case ADDR_TABS:
4283 eap->line2 = LAST_TAB_NR;
4284 break;
4285 case ADDR_TABS_RELATIVE:
4286 eap->line2 = 1;
4287 break;
4288 case ADDR_ARGUMENTS:
4289 if (ARGCOUNT == 0)
4290 eap->line1 = eap->line2 = 0;
4291 else
4292 eap->line2 = ARGCOUNT;
4293 break;
4294 case ADDR_QUICKFIX_VALID:
4295#ifdef FEAT_QUICKFIX
4296 eap->line2 = qf_get_valid_size(eap);
4297 if (eap->line2 == 0)
4298 eap->line2 = 1;
4299#endif
4300 break;
4301 case ADDR_NONE:
4302 case ADDR_UNSIGNED:
4303 case ADDR_QUICKFIX:
4304 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4305 break;
4306 }
4307}
4308
4309
4310/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004311 * Get flags from an Ex command argument.
4312 */
4313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004314get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004315{
4316 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4317 {
4318 if (*eap->arg == 'l')
4319 eap->flags |= EXFLAG_LIST;
4320 else if (*eap->arg == 'p')
4321 eap->flags |= EXFLAG_PRINT;
4322 else
4323 eap->flags |= EXFLAG_NR;
4324 eap->arg = skipwhite(eap->arg + 1);
4325 }
4326}
4327
4328/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 * Function called for command which is Not Implemented. NI!
4330 */
4331 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004332ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333{
4334 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004335 eap->errmsg =
4336 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337}
4338
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004339#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340/*
4341 * Function called for script command which is Not Implemented. NI!
4342 * Skips over ":perl <<EOF" constructs.
4343 */
4344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004345ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346{
4347 if (!eap->skip)
4348 ex_ni(eap);
4349 else
4350 vim_free(script_get(eap, eap->arg));
4351}
4352#endif
4353
4354/*
4355 * Check range in Ex command for validity.
4356 * Return NULL when valid, error message when invalid.
4357 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004358 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004359invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004361 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004362
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 if ( eap->line1 < 0
4364 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004365 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004366 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004367
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004368 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004369 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004370 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004371 {
4372 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004373 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004374#ifdef FEAT_DIFF
4375 + (eap->cmdidx == CMD_diffget)
4376#endif
4377 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004378 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004379 break;
4380 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004381 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004382 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004383 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004384 break;
4385 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004386 // Only a boundary check, not whether the buffers actually
4387 // exist.
4388 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004389 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004390 break;
4391 case ADDR_LOADED_BUFFERS:
4392 buf = firstbuf;
4393 while (buf->b_ml.ml_mfp == NULL)
4394 {
4395 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004396 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004397 buf = buf->b_next;
4398 }
4399 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004400 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004401 buf = lastbuf;
4402 while (buf->b_ml.ml_mfp == NULL)
4403 {
4404 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004405 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004406 buf = buf->b_prev;
4407 }
4408 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004409 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004410 break;
4411 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004412 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004413 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004414 break;
4415 case ADDR_TABS:
4416 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004417 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004418 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004419 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004420 case ADDR_OTHER:
4421 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004422 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004423 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004424#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004425 // No error for value that is too big, will use the last entry.
4426 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004427 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004428#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004429 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004430 case ADDR_QUICKFIX_VALID:
4431#ifdef FEAT_QUICKFIX
4432 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4433 || eap->line2 < 0)
4434 return _(e_invrange);
4435#endif
4436 break;
4437 case ADDR_UNSIGNED:
4438 if (eap->line2 < 0)
4439 return _(e_invrange);
4440 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004441 case ADDR_NONE:
4442 // Will give an error elsewhere.
4443 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004444 }
4445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 return NULL;
4447}
4448
4449/*
4450 * Correct the range for zero line number, if required.
4451 */
4452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004453correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004455 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 {
4457 if (eap->line1 == 0)
4458 eap->line1 = 1;
4459 if (eap->line2 == 0)
4460 eap->line2 = 1;
4461 }
4462}
4463
Bram Moolenaar748bf032005-02-02 23:04:36 +00004464#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004465/*
4466 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4467 * pattern. Otherwise return eap->arg.
4468 */
4469 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004470skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004471{
4472 char_u *p = eap->arg;
4473
Bram Moolenaara37420f2006-02-04 22:37:47 +00004474 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4475 || eap->cmdidx == CMD_vimgrepadd
4476 || eap->cmdidx == CMD_lvimgrepadd
4477 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004478 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004479 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004480 if (p == NULL)
4481 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004482 }
4483 return p;
4484}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004485
4486/*
4487 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4488 * in the command line, so that things like % get expanded.
4489 */
4490 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004491replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004492{
4493 char_u *new_cmdline;
4494 char_u *program;
4495 char_u *pos;
4496 char_u *ptr;
4497 int len;
4498 int i;
4499
4500 /*
4501 * Don't do it when ":vimgrep" is used for ":grep".
4502 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004503 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4504 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4505 || eap->cmdidx == CMD_grepadd
4506 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004507 && !grep_internal(eap->cmdidx))
4508 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004509 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4510 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004511 {
4512 if (*curbuf->b_p_gp == NUL)
4513 program = p_gp;
4514 else
4515 program = curbuf->b_p_gp;
4516 }
4517 else
4518 {
4519 if (*curbuf->b_p_mp == NUL)
4520 program = p_mp;
4521 else
4522 program = curbuf->b_p_mp;
4523 }
4524
4525 p = skipwhite(p);
4526
4527 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4528 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004529 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004530 i = 1;
4531 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4532 ++i;
4533 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004534 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004535 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004536 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004537 ptr = new_cmdline;
4538 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4539 {
4540 i = (int)(pos - program);
4541 STRNCPY(ptr, program, i);
4542 STRCPY(ptr += i, p);
4543 ptr += len;
4544 program = pos + 2;
4545 }
4546 STRCPY(ptr, program);
4547 }
4548 else
4549 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004550 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004551 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004552 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004553 STRCPY(new_cmdline, program);
4554 STRCAT(new_cmdline, " ");
4555 STRCAT(new_cmdline, p);
4556 }
4557 msg_make(p);
4558
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004559 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004560 vim_free(*cmdlinep);
4561 *cmdlinep = new_cmdline;
4562 p = new_cmdline;
4563 }
4564 return p;
4565}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004566#endif
4567
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568/*
4569 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004570 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 * Return FAIL for failure, OK otherwise.
4572 */
4573 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004574expand_filename(
4575 exarg_T *eap,
4576 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004577 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004579 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 char_u *repl;
4581 int srclen;
4582 char_u *p;
4583 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004584 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585
Bram Moolenaar748bf032005-02-02 23:04:36 +00004586#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004587 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004588 p = skip_grep_pat(eap);
4589#else
4590 p = eap->arg;
4591#endif
4592
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 /*
4594 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4595 * the file name contains a wildcard it should not cause expanding.
4596 * (it will be expanded anyway if there is a wildcard before replacing).
4597 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004598 has_wildcards = mch_has_wildcard(p);
4599 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004601#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004602 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004603 if (p[0] == '`' && p[1] == '=')
4604 {
4605 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004606 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004607 if (*p == '`')
4608 ++p;
4609 continue;
4610 }
4611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 /*
4613 * Quick check if this cannot be the start of a special string.
4614 * Also removes backslash before '%', '#' and '<'.
4615 */
4616 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4617 {
4618 ++p;
4619 continue;
4620 }
4621
4622 /*
4623 * Try to find a match at this position.
4624 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004625 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4626 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004627 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004629 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 {
4631 p += srclen;
4632 continue;
4633 }
4634
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004635 // Wildcards won't be expanded below, the replacement is taken
4636 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004637 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4638 {
4639 char_u *l = repl;
4640
4641 repl = expand_env_save(repl);
4642 vim_free(l);
4643 }
4644
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004645 // Need to escape white space et al. with a backslash.
4646 // Don't do this for:
4647 // - replacement that already has been escaped: "##"
4648 // - shell commands (may have to use quotes instead).
4649 // - non-unix systems when there is a single argument (spaces don't
4650 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004652 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 && eap->cmdidx != CMD_grep
4655 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004656 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004657 && eap->cmdidx != CMD_lgrep
4658 && eap->cmdidx != CMD_lgrepadd
4659 && eap->cmdidx != CMD_lmake
4660 && eap->cmdidx != CMD_make
4661 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004663 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664#endif
4665 )
4666 {
4667 char_u *l;
4668#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004669 // Don't escape a backslash here, because rem_backslash() doesn't
4670 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 static char_u *nobslash = (char_u *)" \t\"|";
4672# define ESCAPE_CHARS nobslash
4673#else
4674# define ESCAPE_CHARS escape_chars
4675#endif
4676
4677 for (l = repl; *l; ++l)
4678 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4679 {
4680 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4681 if (l != NULL)
4682 {
4683 vim_free(repl);
4684 repl = l;
4685 }
4686 break;
4687 }
4688 }
4689
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004690 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004691 if ((eap->usefilter || eap->cmdidx == CMD_bang
4692 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004693 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 {
4695 char_u *l;
4696
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004697 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 if (l != NULL)
4699 {
4700 vim_free(repl);
4701 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 }
4703 }
4704
4705 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4706 vim_free(repl);
4707 if (p == NULL)
4708 return FAIL;
4709 }
4710
4711 /*
4712 * One file argument: Expand wildcards.
4713 * Don't do this with ":r !command" or ":w !command".
4714 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004715 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 {
4717 /*
4718 * May do this twice:
4719 * 1. Replace environment variables.
4720 * 2. Replace any other wildcards, remove backslashes.
4721 */
4722 for (n = 1; n <= 2; ++n)
4723 {
4724 if (n == 2)
4725 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 /*
4727 * Halve the number of backslashes (this is Vi compatible).
4728 * For Unix and OS/2, when wildcards are expanded, this is
4729 * done by ExpandOne() below.
4730 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004731#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 if (!has_wildcards)
4733#endif
4734 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 }
4736
4737 if (has_wildcards)
4738 {
4739 if (n == 1)
4740 {
4741 /*
4742 * First loop: May expand environment variables. This
4743 * can be done much faster with expand_env() than with
4744 * something else (e.g., calling a shell).
4745 * After expanding environment variables, check again
4746 * if there are still wildcards present.
4747 */
4748 if (vim_strchr(eap->arg, '$') != NULL
4749 || vim_strchr(eap->arg, '~') != NULL)
4750 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004751 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004752 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 has_wildcards = mch_has_wildcard(NameBuff);
4754 p = NameBuff;
4755 }
4756 else
4757 p = NULL;
4758 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004759 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 {
4761 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004762 int options = WILD_LIST_NOTFOUND
4763 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764
4765 ExpandInit(&xpc);
4766 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004767 if (p_wic)
4768 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004770 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 if (p == NULL)
4772 return FAIL;
4773 }
4774 if (p != NULL)
4775 {
4776 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4777 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004778 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 vim_free(p);
4780 }
4781 }
4782 }
4783 }
4784 return OK;
4785}
4786
4787/*
4788 * Replace part of the command line, keeping eap->cmd, eap->arg and
4789 * eap->nextcmd correct.
4790 * "src" points to the part that is to be replaced, of length "srclen".
4791 * "repl" is the replacement string.
4792 * Returns a pointer to the character after the replaced string.
4793 * Returns NULL for failure.
4794 */
4795 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004796repl_cmdline(
4797 exarg_T *eap,
4798 char_u *src,
4799 int srclen,
4800 char_u *repl,
4801 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802{
4803 int len;
4804 int i;
4805 char_u *new_cmdline;
4806
4807 /*
4808 * The new command line is build in new_cmdline[].
4809 * First allocate it.
4810 * Careful: a "+cmd" argument may have been NUL terminated.
4811 */
4812 len = (int)STRLEN(repl);
4813 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004814 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004815 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004816 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004817 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818
4819 /*
4820 * Copy the stuff before the expanded part.
4821 * Copy the expanded stuff.
4822 * Copy what came after the expanded part.
4823 * Copy the next commands, if there are any.
4824 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004825 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004827
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004829 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004831 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004833 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 {
4835 i = (int)STRLEN(new_cmdline) + 1;
4836 STRCPY(new_cmdline + i, eap->nextcmd);
4837 eap->nextcmd = new_cmdline + i;
4838 }
4839 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4840 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4841 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4842 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4843 vim_free(*cmdlinep);
4844 *cmdlinep = new_cmdline;
4845
4846 return src;
4847}
4848
4849/*
4850 * Check for '|' to separate commands and '"' to start comments.
4851 */
4852 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004853separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854{
4855 char_u *p;
4856
Bram Moolenaar86b68352004-12-27 21:59:20 +00004857#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004858 p = skip_grep_pat(eap);
4859#else
4860 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004861#endif
4862
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004863 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 {
4865 if (*p == Ctrl_V)
4866 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004867 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004868 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004870 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004871 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004872 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 break;
4874 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004875
4876#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004877 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004878 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004879 {
4880 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004881 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01004882 if (*p == NUL) // stop at NUL after CTRL-V
4883 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004884 }
4885#endif
4886
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004887 // Check for '"': start of comment or '|': next command
4888 // :@" and :*" do not start a comment!
4889 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004890 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004891#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004892 && !in_vim9script()
4893#endif
4894 && !(eap->argt & EX_NOTRLCOM)
4895 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4896 || p != eap->arg)
4897 && (eap->cmdidx != CMD_redir
4898 || p != eap->arg + 1 || p[-1] != '@'))
4899#ifdef FEAT_EVAL
4900 || (*p == '#'
4901 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004902 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004903 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 || *p == '|' || *p == '\n')
4906 {
4907 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004908 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 * AND 'b' is present in 'cpoptions'.
4910 */
4911 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004912 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004914 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 --p;
4916 }
4917 else
4918 {
4919 eap->nextcmd = check_nextcmd(p);
4920 *p = NUL;
4921 break;
4922 }
4923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004925
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004926 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 del_trailing_spaces(eap->arg);
4928}
4929
4930/*
4931 * get + command from ex argument
4932 */
4933 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004934getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935{
4936 char_u *arg = *argp;
4937 char_u *command = NULL;
4938
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004939 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 {
4941 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004942 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 command = dollar_command;
4944 else
4945 {
4946 command = arg;
4947 arg = skip_cmd_arg(command, TRUE);
4948 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004949 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 }
4951
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004952 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 *argp = arg;
4954 }
4955 return command;
4956}
4957
4958/*
4959 * Find end of "+command" argument. Skip over "\ " and "\\".
4960 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004961 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004962skip_cmd_arg(
4963 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004964 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965{
4966 while (*p && !vim_isspace(*p))
4967 {
4968 if (*p == '\\' && p[1] != NUL)
4969 {
4970 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004971 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 else
4973 ++p;
4974 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004975 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 }
4977 return p;
4978}
4979
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004980 int
4981get_bad_opt(char_u *p, exarg_T *eap)
4982{
4983 if (STRICMP(p, "keep") == 0)
4984 eap->bad_char = BAD_KEEP;
4985 else if (STRICMP(p, "drop") == 0)
4986 eap->bad_char = BAD_DROP;
4987 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4988 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004989 else
4990 return FAIL;
4991 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004992}
4993
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994/*
4995 * Get "++opt=arg" argument.
4996 * Return FAIL or OK.
4997 */
4998 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004999getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000{
5001 char_u *arg = eap->arg + 2;
5002 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005003 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005006 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5008 {
5009 if (*arg == 'n')
5010 {
5011 arg += 2;
5012 eap->force_bin = FORCE_NOBIN;
5013 }
5014 else
5015 eap->force_bin = FORCE_BIN;
5016 if (!checkforcmd(&arg, "binary", 3))
5017 return FAIL;
5018 eap->arg = skipwhite(arg);
5019 return OK;
5020 }
5021
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005022 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005023 if (STRNCMP(arg, "edit", 4) == 0)
5024 {
5025 eap->read_edit = TRUE;
5026 eap->arg = skipwhite(arg + 4);
5027 return OK;
5028 }
5029
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 if (STRNCMP(arg, "ff", 2) == 0)
5031 {
5032 arg += 2;
5033 pp = &eap->force_ff;
5034 }
5035 else if (STRNCMP(arg, "fileformat", 10) == 0)
5036 {
5037 arg += 10;
5038 pp = &eap->force_ff;
5039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 else if (STRNCMP(arg, "enc", 3) == 0)
5041 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005042 if (STRNCMP(arg, "encoding", 8) == 0)
5043 arg += 8;
5044 else
5045 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 pp = &eap->force_enc;
5047 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005048 else if (STRNCMP(arg, "bad", 3) == 0)
5049 {
5050 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005051 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053
5054 if (pp == NULL || *arg != '=')
5055 return FAIL;
5056
5057 ++arg;
5058 *pp = (int)(arg - eap->cmd);
5059 arg = skip_cmd_arg(arg, FALSE);
5060 eap->arg = skipwhite(arg);
5061 *arg = NUL;
5062
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 if (pp == &eap->force_ff)
5064 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5066 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005067 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005069 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005071 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5073 *p = TOLOWER_ASC(*p);
5074 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005075 else
5076 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005077 // Check ++bad= argument. Must be a single-byte character, "keep" or
5078 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005079 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005080 return FAIL;
5081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082
5083 return OK;
5084}
5085
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005087ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088{
5089 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005090 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 * directory for security reasons.
5092 */
5093 if (secure)
5094 {
5095 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005096 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 }
5098 else if (eap->cmdidx == CMD_autocmd)
5099 do_autocmd(eap->arg, eap->forceit);
5100 else
5101 do_augroup(eap->arg, eap->forceit);
5102}
5103
5104/*
5105 * ":doautocmd": Apply the automatic commands to the current buffer.
5106 */
5107 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005108ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005110 char_u *arg = eap->arg;
5111 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005112 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005113
Bram Moolenaar1610d052016-06-09 22:53:01 +02005114 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005115 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005116 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005117 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120/*
5121 * :[N]bunload[!] [N] [bufname] unload buffer
5122 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5123 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5124 */
5125 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005126ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005128 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005129 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 eap->errmsg = do_bufdel(
5131 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5132 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5133 : DOBUF_UNLOAD, eap->arg,
5134 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5135}
5136
5137/*
5138 * :[N]buffer [N] to buffer N
5139 * :[N]sbuffer [N] to buffer N
5140 */
5141 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005142ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005144 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005145 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005147 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 else
5149 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005150 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5152 else
5153 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005154 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005155 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 }
5157}
5158
5159/*
5160 * :[N]bmodified [N] to next mod. buffer
5161 * :[N]sbmodified [N] to next mod. buffer
5162 */
5163 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005164ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165{
5166 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005167 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005168 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169}
5170
5171/*
5172 * :[N]bnext [N] to next buffer
5173 * :[N]sbnext [N] split and to next buffer
5174 */
5175 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005176ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005178 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005179 return;
5180
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005182 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005183 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184}
5185
5186/*
5187 * :[N]bNext [N] to previous buffer
5188 * :[N]bprevious [N] to previous buffer
5189 * :[N]sbNext [N] split and to previous buffer
5190 * :[N]sbprevious [N] split and to previous buffer
5191 */
5192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005193ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005195 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005196 return;
5197
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005199 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005200 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201}
5202
5203/*
5204 * :brewind to first buffer
5205 * :bfirst to first buffer
5206 * :sbrewind split and to first buffer
5207 * :sbfirst split and to first buffer
5208 */
5209 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005210ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005212 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005213 return;
5214
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005216 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005217 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218}
5219
5220/*
5221 * :blast to last buffer
5222 * :sblast split and to last buffer
5223 */
5224 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005225ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005227 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005228 return;
5229
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005231 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005232 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234
Bram Moolenaar7a092242020-04-16 22:10:49 +02005235/*
5236 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005237 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005238 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005240ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005242 int comment_char = '"';
5243
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005244#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005245 if (in_vim9script())
5246 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005247#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005248 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005249}
5250
5251/*
5252 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5253 * to "cmd_start" or has a white space character before it.
5254 */
5255 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005256ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005257{
5258 int c = *cmd;
5259
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005260 if (c == NUL || c == '|' || c == '\n')
5261 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005262#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005263 if (in_vim9script())
Bram Moolenaara0399ef2021-03-20 15:00:01 +01005264 // # starts a comment, #{ might be a mistake, #{{ can start a fold
5265 return c == '#' && (cmd[1] != '{' || cmd[2] == '{')
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01005266 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005267#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005268 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269}
5270
5271#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5272 || defined(PROTO)
5273/*
5274 * Return the next command, after the first '|' or '\n'.
5275 * Return NULL if not found.
5276 */
5277 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005278find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279{
5280 while (*p != '|' && *p != '\n')
5281 {
5282 if (*p == NUL)
5283 return NULL;
5284 ++p;
5285 }
5286 return (p + 1);
5287}
5288#endif
5289
5290/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005291 * Check if *p is a separator between Ex commands, skipping over white space.
5292 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 */
5294 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005295check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005297 char_u *s = skipwhite(p);
5298
5299 if (*s == '|' || *s == '\n')
5300 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 else
5302 return NULL;
5303}
5304
5305/*
5306 * - if there are more files to edit
5307 * - and this is the last window
5308 * - and forceit not used
5309 * - and not repeated twice on a row
5310 * return FAIL and give error message if 'message' TRUE
5311 * return OK otherwise
5312 */
5313 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005314check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005315 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005316 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
5318 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5319
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005320 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005321 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 {
5323 if (message)
5324 {
5325#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005326 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5327 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005329 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005331 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5332 NGETTEXT("%d more file to edit. Quit anyway?",
5333 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5335 return OK;
5336 return FAIL;
5337 }
5338#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005339 semsg(NGETTEXT("E173: %d more file to edit",
5340 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005341 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 }
5343 return FAIL;
5344 }
5345 return OK;
5346}
5347
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348/*
5349 * Function given to ExpandGeneric() to obtain the list of command names.
5350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005352get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353{
5354 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 return cmdnames[idx].cmd_name;
5357}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005360ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361{
Bram Moolenaare6850792010-05-14 15:28:44 +02005362 if (*eap->arg == NUL)
5363 {
5364#ifdef FEAT_EVAL
5365 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5366 char_u *p = NULL;
5367
5368 if (expr != NULL)
5369 {
5370 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005371 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005372 --emsg_off;
5373 vim_free(expr);
5374 }
5375 if (p != NULL)
5376 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005377 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005378 vim_free(p);
5379 }
5380 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005381 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005382#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005383 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005384#endif
5385 }
5386 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005387 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005388
5389#ifdef FEAT_VTP
5390 else if (has_vtp_working())
5391 {
5392 // background color change requires clear + redraw
5393 update_screen(CLEAR);
5394 redrawcmd();
5395 }
5396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397}
5398
5399 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005400ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401{
5402 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005403 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 do_highlight(eap->arg, eap->forceit, FALSE);
5405}
5406
5407
5408/*
5409 * Call this function if we thought we were going to exit, but we won't
5410 * (because of an error). May need to restore the terminal mode.
5411 */
5412 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005413not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
5415 exiting = FALSE;
5416 settmode(TMODE_RAW);
5417}
5418
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005419 static int
5420before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5421{
5422 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5423
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005424 // Bail out when autocommands closed the window.
5425 // Refuse to quit when the buffer in the last window is being closed (can
5426 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005427 if (!win_valid(wp)
5428 || curbuf_locked()
5429 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5430 return TRUE;
5431
5432 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5433 {
5434 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005435 // Refuse to quit when locked or when the window was closed or the
5436 // buffer in the last window is being closed (can only happen in
5437 // autocommands).
5438 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005439 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5440 return TRUE;
5441 }
5442
5443 return FALSE;
5444}
5445
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005447 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005448 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005449 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005451 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005452ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005454 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005455
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456#ifdef FEAT_CMDWIN
5457 if (cmdwin_type != 0)
5458 {
5459 cmdwin_result = Ctrl_C;
5460 return;
5461 }
5462#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005463 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005464 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005465 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005466 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005467 return;
5468 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005469 if (eap->addr_count > 0)
5470 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005471 int wnr = eap->line2;
5472
5473 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5474 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005475 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005476 }
5477 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005478 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005479
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005480 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005481 if (curbuf_locked())
5482 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005483
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005484 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005485 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005486 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487
5488#ifdef FEAT_NETBEANS_INTG
5489 netbeansForcedQuit = eap->forceit;
5490#endif
5491
5492 /*
5493 * If there are more files or windows we won't exit.
5494 */
5495 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5496 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005497 if ((!buf_hide(wp->w_buffer)
5498 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005499 | (eap->forceit ? CCGD_FORCEIT : 0)
5500 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005502 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 {
5504 not_exiting();
5505 }
5506 else
5507 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005508 // quit last window
5509 // Note: only_one_window() returns true, even so a help window is
5510 // still open. In that case only quit, if no address has been
5511 // specified. Example:
5512 // :h|wincmd w|1q - don't quit
5513 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005514 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005516 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005517#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005519#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005520 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005521 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 }
5523}
5524
5525/*
5526 * ":cquit".
5527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005529ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005531 // this does not always pass on the exit code to the Manx compiler. why?
5532 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533}
5534
5535/*
5536 * ":qall": try to quit all windows
5537 */
5538 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005539ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540{
5541# ifdef FEAT_CMDWIN
5542 if (cmdwin_type != 0)
5543 {
5544 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005545 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 else
5547 cmdwin_result = K_XF2;
5548 return;
5549 }
5550# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005551
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005552 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005553 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005554 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005555 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005556 return;
5557 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005558
5559 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005560 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005561
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005563 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 getout(0);
5565 not_exiting();
5566}
5567
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568/*
5569 * ":close": close current window, unless it is the last one
5570 */
5571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005572ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005574 win_T *win;
5575 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005576#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005578 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005580#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005581 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005582 {
5583 if (eap->addr_count == 0)
5584 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005585 else
5586 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005587 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005588 {
5589 winnr++;
5590 if (winnr == eap->line2)
5591 break;
5592 }
5593 if (win == NULL)
5594 win = lastwin;
5595 ex_win_close(eap->forceit, win, NULL);
5596 }
5597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598}
5599
Bram Moolenaar4033c552017-09-16 20:54:51 +02005600#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005601/*
5602 * ":pclose": Close any preview window.
5603 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005605ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005606{
5607 win_T *win;
5608
Bram Moolenaar79648732019-07-18 21:43:07 +02005609 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005610 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005611 if (win->w_p_pvw)
5612 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005613 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005614 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005615 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005616# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005617 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005618 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005619# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005620}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005621#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005622
Bram Moolenaarf740b292006-02-16 22:11:02 +00005623/*
5624 * Close window "win" and take care of handling closing the last window for a
5625 * modified buffer.
5626 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005628ex_win_close(
5629 int forceit,
5630 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005631 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632{
5633 int need_hide;
5634 buf_T *buf = win->w_buffer;
5635
Bram Moolenaarcf844172020-06-26 19:44:06 +02005636 // Never close the autocommand window.
5637 if (win == aucmd_win)
5638 {
5639 emsg(_(e_autocmd_close));
5640 return;
5641 }
5642
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005644 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005646#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005647 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005649 bufref_T bufref;
5650
5651 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005653 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 return;
5655 need_hide = FALSE;
5656 }
5657 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005660 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 return;
5662 }
5663 }
5664
Bram Moolenaar4033c552017-09-16 20:54:51 +02005665#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005667#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005668
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005669 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005670 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005671 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005672 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005673 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674}
5675
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005677 * Handle the argument for a tabpage related ex command.
5678 * Returns a tabpage number.
5679 * When an error is encountered then eap->errmsg is set.
5680 */
5681 static int
5682get_tabpage_arg(exarg_T *eap)
5683{
5684 int tab_number;
5685 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5686
5687 if (eap->arg && *eap->arg != NUL)
5688 {
5689 char_u *p = eap->arg;
5690 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005691 int relative = 0; // argument +N/-N means: go to N places to the
5692 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005693
5694 if (*p == '-')
5695 {
5696 relative = -1;
5697 p++;
5698 }
5699 else if (*p == '+')
5700 {
5701 relative = 1;
5702 p++;
5703 }
5704
5705 p_save = p;
5706 tab_number = getdigits(&p);
5707
5708 if (relative == 0)
5709 {
5710 if (STRCMP(p, "$") == 0)
5711 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005712 else if (STRCMP(p, "#") == 0)
5713 if (valid_tabpage(lastused_tabpage))
5714 tab_number = tabpage_index(lastused_tabpage);
5715 else
5716 {
5717 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5718 tab_number = 0;
5719 goto theend;
5720 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005721 else if (p == p_save || *p_save == '-' || *p != NUL
5722 || tab_number > LAST_TAB_NR)
5723 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005724 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005725 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005726 goto theend;
5727 }
5728 }
5729 else
5730 {
5731 if (*p_save == NUL)
5732 tab_number = 1;
5733 else if (p == p_save || *p_save == '-' || *p != NUL
5734 || tab_number == 0)
5735 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005736 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005737 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005738 goto theend;
5739 }
5740 tab_number = tab_number * relative + tabpage_index(curtab);
5741 if (!unaccept_arg0 && relative == -1)
5742 --tab_number;
5743 }
5744 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005745 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005746 }
5747 else if (eap->addr_count > 0)
5748 {
5749 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005750 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005751 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005752 tab_number = 0;
5753 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005754 else
5755 {
5756 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005757 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005758 {
5759 --tab_number;
5760 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005761 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005762 }
5763 }
5764 }
5765 else
5766 {
5767 switch (eap->cmdidx)
5768 {
5769 case CMD_tabnext:
5770 tab_number = tabpage_index(curtab) + 1;
5771 if (tab_number > LAST_TAB_NR)
5772 tab_number = 1;
5773 break;
5774 case CMD_tabmove:
5775 tab_number = LAST_TAB_NR;
5776 break;
5777 default:
5778 tab_number = tabpage_index(curtab);
5779 }
5780 }
5781
5782theend:
5783 return tab_number;
5784}
5785
5786/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005787 * ":tabclose": close current tab page, unless it is the last one.
5788 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 */
5790 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005791ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005793 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005794 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005795
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005796# ifdef FEAT_CMDWIN
5797 if (cmdwin_type != 0)
5798 cmdwin_result = K_IGNORE;
5799 else
5800# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005801 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005802 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005803 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005805 tab_number = get_tabpage_arg(eap);
5806 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005807 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005808 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005809 if (tp == NULL)
5810 {
5811 beep_flush();
5812 return;
5813 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005814 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005815 {
5816 tabpage_close_other(tp, eap->forceit);
5817 return;
5818 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005819 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005820 tabpage_close(eap->forceit);
5821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005823}
5824
5825/*
5826 * ":tabonly": close all tab pages except the current one
5827 */
5828 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005829ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005830{
5831 tabpage_T *tp;
5832 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005833 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005834
5835# ifdef FEAT_CMDWIN
5836 if (cmdwin_type != 0)
5837 cmdwin_result = K_IGNORE;
5838 else
5839# endif
5840 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005841 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005842 else
5843 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005844 tab_number = get_tabpage_arg(eap);
5845 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005846 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005847 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005848 // Repeat this up to a 1000 times, because autocommands may
5849 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005850 for (done = 0; done < 1000; ++done)
5851 {
5852 FOR_ALL_TABPAGES(tp)
5853 if (tp->tp_topframe != topframe)
5854 {
5855 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005856 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005857 if (valid_tabpage(tp))
5858 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005859 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005860 break;
5861 }
5862 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005863 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005864 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005865 }
5866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868
5869/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005870 * Close the current tab page.
5871 */
5872 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005873tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005874{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005875 // First close all the windows but the current one. If that worked then
5876 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005877 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005878 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005879 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005880 ex_win_close(forceit, curwin, NULL);
5881# ifdef FEAT_GUI
5882 need_mouse_correct = TRUE;
5883# endif
5884}
5885
5886/*
5887 * Close tab page "tp", which is not the current tab page.
5888 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005889 * Also takes care of the tab pages line disappearing when closing the
5890 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005891 */
5892 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005893tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005894{
5895 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005896 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005897 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005898
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005899 // Limit to 1000 windows, autocommands may add a window while we close
5900 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005901 while (++done < 1000)
5902 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005903 wp = tp->tp_firstwin;
5904 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005905
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005906 // Autocommands may delete the tab page under our fingers and we may
5907 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005908 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005909 break;
5910 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005911
Bram Moolenaar29323592016-07-24 22:04:11 +02005912 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005913
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005914 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005915 if (h != tabline_height())
5916 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005917}
5918
5919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 * ":only".
5921 */
5922 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005923ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005925 win_T *wp;
5926 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927# ifdef FEAT_GUI
5928 need_mouse_correct = TRUE;
5929# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005930 if (eap->addr_count > 0)
5931 {
5932 wnr = eap->line2;
5933 for (wp = firstwin; --wnr > 0; )
5934 {
5935 if (wp->w_next == NULL)
5936 break;
5937 else
5938 wp = wp->w_next;
5939 }
5940 win_goto(wp);
5941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 close_others(TRUE, eap->forceit);
5943}
5944
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005946ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005948 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005949 if (!eap->skip)
5950 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005951#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005952 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005953#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005954 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005955 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005956 else
5957 {
5958 int winnr = 0;
5959 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005960
Bram Moolenaar2256c992016-11-15 21:17:07 +01005961 FOR_ALL_WINDOWS(win)
5962 {
5963 winnr++;
5964 if (winnr == eap->line2)
5965 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005966 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005967 if (win == NULL)
5968 win = lastwin;
5969 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 }
5972}
5973
5974/*
5975 * ":stop" and ":suspend": Suspend Vim.
5976 */
5977 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005978ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979{
5980 /*
5981 * Disallow suspending for "rvim".
5982 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005983 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 {
5985 if (!eap->forceit)
5986 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01005987 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 windgoto((int)Rows - 1, 0);
5989 out_char('\n');
5990 out_flush();
5991 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005992 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005994 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005996 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997#ifdef FEAT_TITLE
5998 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005999 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000#endif
6001 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006002 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006004 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01006005 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 }
6007}
6008
6009/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006010 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 */
6012 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006013ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014{
Bram Moolenaar461f2122020-07-28 20:25:47 +02006015#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02006016 if (not_in_vim9(eap) == FAIL)
6017 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02006018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019#ifdef FEAT_CMDWIN
6020 if (cmdwin_type != 0)
6021 {
6022 cmdwin_result = Ctrl_C;
6023 return;
6024 }
6025#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006026 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006027 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006028 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006029 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006030 return;
6031 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006032
6033 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006034 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035
6036 /*
6037 * if more files or windows we won't exit
6038 */
6039 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6040 exiting = TRUE;
6041 if ( ((eap->cmdidx == CMD_wq
6042 || curbufIsChanged())
6043 && do_write(eap) == FAIL)
6044 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006045 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 {
6047 not_exiting();
6048 }
6049 else
6050 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006051 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006053 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054# ifdef FEAT_GUI
6055 need_mouse_correct = TRUE;
6056# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006057 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02006058 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 }
6060}
6061
6062/*
6063 * ":print", ":list", ":number".
6064 */
6065 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006066ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006068 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006069 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006070 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006072 for ( ;!got_int; ui_breakcheck())
6073 {
6074 print_line(eap->line1,
6075 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6076 || (eap->flags & EXFLAG_NR)),
6077 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6078 if (++eap->line1 > eap->line2)
6079 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006080 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00006081 }
6082 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006083 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00006084 curwin->w_cursor.lnum = eap->line2;
6085 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 }
6087
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089}
6090
6091#ifdef FEAT_BYTEOFF
6092 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006093ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094{
6095 goto_byte(eap->line2);
6096}
6097#endif
6098
6099/*
6100 * ":shell".
6101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006103ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104{
6105 do_shell(NULL, 0);
6106}
6107
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006108#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006110static int drop_busy = FALSE;
6111static int drop_filec;
6112static char_u **drop_filev = NULL;
6113static int drop_split;
6114static void (*drop_callback)(void *);
6115static void *drop_cookie;
6116
6117 static void
6118handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119{
6120 exarg_T ea;
6121 int save_msg_scroll = msg_scroll;
6122
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006123 // Setting the argument list may cause screen updates and being called
6124 // recursively. Avoid that by setting drop_busy.
6125 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006127 // Check whether the current buffer is changed. If so, we will need
6128 // to split the current window or data could be lost.
6129 // We don't need to check if the 'hidden' option is set, as in this
6130 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006131 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 {
6133 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006134 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 --emsg_off;
6136 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006137 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 if (win_split(0, 0) == FAIL)
6140 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006141 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006143 // When splitting the window, create a new alist. Otherwise the
6144 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 alist_unlink(curwin->w_alist);
6146 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 }
6148
6149 /*
6150 * Set up the new argument list.
6151 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006152 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153
6154 /*
6155 * Move to the first file.
6156 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006157 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006158 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 ea.cmd = (char_u *)"next";
6160 do_argfile(&ea, 0);
6161
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006162 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6163 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 need_start_insertmode = FALSE;
6165
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006166 // Restore msg_scroll, otherwise a following command may cause scrolling
6167 // unexpectedly. The screen will be redrawn by the caller, thus
6168 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006170
6171 if (drop_callback != NULL)
6172 drop_callback(drop_cookie);
6173
6174 drop_filev = NULL;
6175 drop_busy = FALSE;
6176}
6177
6178/*
6179 * Handle a file drop. The code is here because a drop is *nearly* like an
6180 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006181 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006182 * code is very similar to :args and hence needs access to a lot of the static
6183 * functions in this file.
6184 *
6185 * The "filev" list must have been allocated using alloc(), as should each item
6186 * in the list. This function takes over responsibility for freeing the "filev"
6187 * list.
6188 */
6189 void
6190handle_drop(
6191 int filec, // the number of files dropped
6192 char_u **filev, // the list of files dropped
6193 int split, // force splitting the window
6194 void (*callback)(void *), // to be called after setting the argument
6195 // list
6196 void *cookie) // argument for "callback" (allocated)
6197{
6198 // Cannot handle recursive drops, finish the pending one.
6199 if (drop_busy)
6200 {
6201 FreeWild(filec, filev);
6202 vim_free(cookie);
6203 return;
6204 }
6205
6206 // When calling handle_drop() more than once in a row we only use the last
6207 // one.
6208 if (drop_filev != NULL)
6209 {
6210 FreeWild(drop_filec, drop_filev);
6211 vim_free(drop_cookie);
6212 }
6213
6214 drop_filec = filec;
6215 drop_filev = filev;
6216 drop_split = split;
6217 drop_callback = callback;
6218 drop_cookie = cookie;
6219
6220 // Postpone this when:
6221 // - editing the command line
6222 // - not possible to change the current buffer
6223 // - updating the screen
6224 // As it may change buffers and window structures that are in use and cause
6225 // freed memory to be used.
6226 if (text_locked() || curbuf_locked() || updating_screen)
6227 return;
6228
6229 handle_drop_internal();
6230}
6231
6232/*
6233 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6234 * reset: Handle a postponed drop.
6235 */
6236 void
6237handle_any_postponed_drop(void)
6238{
6239 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006240 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006241 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242}
6243#endif
6244
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 * ":preserve".
6247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006249ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006251 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 ml_preserve(curbuf, TRUE);
6253}
6254
6255/*
6256 * ":recover".
6257 */
6258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006259ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006261 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006263 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6264 | CCGD_MULTWIN
6265 | (eap->forceit ? CCGD_FORCEIT : 0)
6266 | CCGD_EXCMD)
6267
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 && (*eap->arg == NUL
6269 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006270 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 recoverymode = FALSE;
6272}
6273
6274/*
6275 * Command modifier used in a wrong way.
6276 */
6277 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006278ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006280 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281}
6282
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283/*
6284 * :sview [+command] file split window with new file, read-only
6285 * :split [[+command] file] split window with current or new file
6286 * :vsplit [[+command] file] split window vertically with current or new file
6287 * :new [[+command] file] split window with no or new file
6288 * :vnew [[+command] file] split vertically window with no or new file
6289 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006290 *
6291 * :tabedit open new Tab page with empty window
6292 * :tabedit [+command] file open new Tab page and edit "file"
6293 * :tabnew [[+command] file] just like :tabedit
6294 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 */
6296 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006297ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006299 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006300#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006302#endif
6303#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006304 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006305 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006306#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006307 int use_tab = eap->cmdidx == CMD_tabedit
6308 || eap->cmdidx == CMD_tabfind
6309 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006311 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006312 return;
6313
Bram Moolenaar4033c552017-09-16 20:54:51 +02006314#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317
Bram Moolenaar4033c552017-09-16 20:54:51 +02006318#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006319 // A ":split" in the quickfix window works like ":new". Don't want two
6320 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006321 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 {
6323 if (eap->cmdidx == CMD_split)
6324 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 if (eap->cmdidx == CMD_vsplit)
6326 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329
Bram Moolenaar4033c552017-09-16 20:54:51 +02006330#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006331 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 {
6333 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6334 FNAME_MESS, TRUE, curbuf->b_ffname);
6335 if (fname == NULL)
6336 goto theend;
6337 eap->arg = fname;
6338 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006339# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006340 else
6341# endif
6342#endif
6343#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006344 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 && eap->cmdidx != CMD_new)
6347 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006348 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006349# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006350 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006351# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006352 au_has_group((char_u *)"FileExplorer"))
6353 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006354 // No browsing supported but we do have the file explorer:
6355 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006356 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006357 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006358 }
6359 else
6360 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006361 fname = do_browse(0, (char_u *)(use_tab
6362 ? _("Edit File in new tab page")
6363 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006365 if (fname == NULL)
6366 goto theend;
6367 eap->arg = fname;
6368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006370 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006373 /*
6374 * Either open new tab page or split the window.
6375 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006376 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006377 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006378 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006379 : eap->addr_count == 0 ? 0
6380 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006381 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006382 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006383
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006384 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006385 if (curwin != old_curwin
6386 && win_valid(old_curwin)
6387 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006388 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006389 old_curwin->w_alt_fnum = curbuf->b_fnum;
6390 }
6391 }
6392 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6394 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006395 // Reset 'scrollbind' when editing another file, but keep it when
6396 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006397 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006398 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 else
6400 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 do_exedit(eap, old_curwin);
6402 }
6403
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006404# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006405 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006406# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006408# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409theend:
6410 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006411# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006413
6414/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006415 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006416 */
6417 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006418tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006419{
6420 exarg_T ea;
6421
Bram Moolenaara80faa82020-04-12 19:37:17 +02006422 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006423 ea.cmdidx = CMD_tabnew;
6424 ea.cmd = (char_u *)"tabn";
6425 ea.arg = (char_u *)"";
6426 ex_splitview(&ea);
6427}
6428
6429/*
6430 * :tabnext command
6431 */
6432 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006433ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006434{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006435 int tab_number;
6436
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006437 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006438 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006439 switch (eap->cmdidx)
6440 {
6441 case CMD_tabfirst:
6442 case CMD_tabrewind:
6443 goto_tabpage(1);
6444 break;
6445 case CMD_tablast:
6446 goto_tabpage(9999);
6447 break;
6448 case CMD_tabprevious:
6449 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006450 if (eap->arg && *eap->arg != NUL)
6451 {
6452 char_u *p = eap->arg;
6453 char_u *p_save = p;
6454
6455 tab_number = getdigits(&p);
6456 if (p == p_save || *p_save == '-' || *p != NUL
6457 || tab_number == 0)
6458 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006459 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006460 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006461 return;
6462 }
6463 }
6464 else
6465 {
6466 if (eap->addr_count == 0)
6467 tab_number = 1;
6468 else
6469 {
6470 tab_number = eap->line2;
6471 if (tab_number < 1)
6472 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006473 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006474 return;
6475 }
6476 }
6477 }
6478 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006479 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006480 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006481 tab_number = get_tabpage_arg(eap);
6482 if (eap->errmsg == NULL)
6483 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006484 break;
6485 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006486}
6487
6488/*
6489 * :tabmove command
6490 */
6491 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006492ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006493{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006494 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006495
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006496 tab_number = get_tabpage_arg(eap);
6497 if (eap->errmsg == NULL)
6498 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006499}
6500
6501/*
6502 * :tabs command: List tabs and their contents.
6503 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006505ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006506{
6507 tabpage_T *tp;
6508 win_T *wp;
6509 int tabcount = 1;
6510
6511 msg_start();
6512 msg_scroll = TRUE;
6513 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6514 {
6515 msg_putchar('\n');
6516 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006517 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006518 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006519 ui_breakcheck();
6520
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006521 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006522 wp = firstwin;
6523 else
6524 wp = tp->tp_firstwin;
6525 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6526 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006527 msg_putchar('\n');
6528 msg_putchar(wp == curwin ? '>' : ' ');
6529 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006530 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6531 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006532 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006533 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006534 else
6535 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6536 IObuff, IOSIZE, TRUE);
6537 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006538 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006539 ui_breakcheck();
6540 }
6541 }
6542}
6543
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544/*
6545 * ":mode": Set screen mode.
6546 * If no argument given, just get the screen size and redraw.
6547 */
6548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006549ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550{
6551 if (*eap->arg == NUL)
6552 shell_resized();
6553 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006554 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555}
6556
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557/*
6558 * ":resize".
6559 * set, increment or decrement current window height
6560 */
6561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006562ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563{
6564 int n;
6565 win_T *wp = curwin;
6566
6567 if (eap->addr_count > 0)
6568 {
6569 n = eap->line2;
6570 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6571 ;
6572 }
6573
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006574# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006576# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006578 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 {
6580 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006581 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006582 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 n = 9999;
6584 win_setwidth_win((int)n, wp);
6585 }
6586 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 {
6588 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006589 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006590 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 n = 9999;
6592 win_setheight_win((int)n, wp);
6593 }
6594}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595
6596/*
6597 * ":find [+command] <file>" command.
6598 */
6599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006600ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601{
6602#ifdef FEAT_SEARCHPATH
6603 char_u *fname;
6604 int count;
6605
6606 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6607 TRUE, curbuf->b_ffname);
6608 if (eap->addr_count > 0)
6609 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006610 // Repeat finding the file "count" times. This matters when it
6611 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 count = eap->line2;
6613 while (fname != NULL && --count > 0)
6614 {
6615 vim_free(fname);
6616 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6617 FALSE, curbuf->b_ffname);
6618 }
6619 }
6620
6621 if (fname != NULL)
6622 {
6623 eap->arg = fname;
6624#endif
6625 do_exedit(eap, NULL);
6626#ifdef FEAT_SEARCHPATH
6627 vim_free(fname);
6628 }
6629#endif
6630}
6631
6632/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006633 * ":open" simulation: for now just work like ":visual".
6634 */
6635 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006636ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006637{
6638 regmatch_T regmatch;
6639 char_u *p;
6640
Bram Moolenaar65088802021-03-13 21:07:21 +01006641#ifdef FEAT_EVAL
6642 if (not_in_vim9(eap) == FAIL)
6643 return;
6644#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00006645 curwin->w_cursor.lnum = eap->line2;
6646 beginline(BL_SOL | BL_FIX);
6647 if (*eap->arg == '/')
6648 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006649 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006650 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006651 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00006652 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006653 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006654 if (regmatch.regprog != NULL)
6655 {
6656 regmatch.rm_ic = p_ic;
6657 p = ml_get_curline();
6658 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006659 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006660 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006661 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006662 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006663 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006664 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006665 eap->arg += STRLEN(eap->arg);
6666 }
6667 check_cursor();
6668
6669 eap->cmdidx = CMD_visual;
6670 do_exedit(eap, NULL);
6671}
6672
6673/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006674 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 */
6676 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006677ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678{
6679 do_exedit(eap, NULL);
6680}
6681
6682/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006683 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006686do_exedit(
6687 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006688 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689{
6690 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006692 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006694 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6695 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006696 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 /*
6698 * ":vi" command ends Ex mode.
6699 */
6700 if (exmode_active && (eap->cmdidx == CMD_visual
6701 || eap->cmdidx == CMD_view))
6702 {
6703 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006704 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006706 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006707 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006708 if (global_busy)
6709 {
6710 int rd = RedrawingDisabled;
6711 int nwr = no_wait_return;
6712 int ms = msg_scroll;
6713#ifdef FEAT_GUI
6714 int he = hold_gui_events;
6715#endif
6716
6717 if (eap->nextcmd != NULL)
6718 {
6719 stuffReadbuff(eap->nextcmd);
6720 eap->nextcmd = NULL;
6721 }
6722
6723 if (exmode_was != EXMODE_VIM)
6724 settmode(TMODE_RAW);
6725 RedrawingDisabled = 0;
6726 no_wait_return = 0;
6727 need_wait_return = FALSE;
6728 msg_scroll = 0;
6729#ifdef FEAT_GUI
6730 hold_gui_events = 0;
6731#endif
6732 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006733 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006734
6735 main_loop(FALSE, TRUE);
6736
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006737 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006738 RedrawingDisabled = rd;
6739 no_wait_return = nwr;
6740 msg_scroll = ms;
6741#ifdef FEAT_GUI
6742 hold_gui_events = he;
6743#endif
6744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 }
6748
6749 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006750 || eap->cmdidx == CMD_tabnew
6751 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006752 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006754 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 setpcmark();
6756 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006757 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6758 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006760 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 || *eap->arg != NUL
6762#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006763 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764#endif
6765 )
6766 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006767 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6768 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006769 if (*eap->arg != NUL && curbuf_locked())
6770 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006771
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 n = readonlymode;
6773 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6774 readonlymode = TRUE;
6775 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006776 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6777 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01006778 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
6779 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6781 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006782 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6784 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6785 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006786 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006788 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006789 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006790 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
6791 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006792 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006794 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 if (old_curwin != NULL)
6796 {
6797 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006798 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006800#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006801 cleanup_T cs;
6802
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006803 // Reset the error/interrupt/exception state here so that
6804 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006805 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006806#endif
6807#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006809#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006810 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006811
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006812#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006813 // Restore the error/interrupt/exception state if not
6814 // discarded by a new aborting error, interrupt, or
6815 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006816 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 }
6819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 }
6821 else if (readonlymode && curbuf->b_nwindows == 1)
6822 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006823 // When editing an already visited buffer, 'readonly' won't be set
6824 // but the previous value is kept. With ":view" and ":sview" we
6825 // want the file to be readonly, except when another window is
6826 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 curbuf->b_p_ro = TRUE;
6828 }
6829 readonlymode = n;
6830 }
6831 else
6832 {
6833 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01006834 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835#ifdef FEAT_TITLE
6836 n = curwin->w_arg_idx_invalid;
6837#endif
6838 check_arg_idx(curwin);
6839#ifdef FEAT_TITLE
6840 if (n != curwin->w_arg_idx_invalid)
6841 maketitle();
6842#endif
6843 }
6844
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 /*
6846 * if ":split file" worked, set alternate file name in old window to new
6847 * file
6848 */
6849 if (old_curwin != NULL
6850 && *eap->arg != NUL
6851 && curwin != old_curwin
6852 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006853 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006854 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856
6857 ex_no_reprint = TRUE;
6858}
6859
6860#ifndef FEAT_GUI
6861/*
6862 * ":gui" and ":gvim" when there is no GUI.
6863 */
6864 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006865ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006867 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868}
6869#endif
6870
Bram Moolenaar4f974752019-02-17 17:44:42 +01006871#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006873ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874{
6875 gui_make_tearoff(eap->arg);
6876}
6877#endif
6878
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006879#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6880 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006882ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006884# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6885 if (gui.in_use)
6886 gui_make_popup(eap->arg, eap->forceit);
6887# ifdef FEAT_TERM_POPUP_MENU
6888 else
6889# endif
6890# endif
6891# ifdef FEAT_TERM_POPUP_MENU
6892 pum_make_popup(eap->arg, eap->forceit);
6893# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894}
6895#endif
6896
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006898ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899{
6900 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006901 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006903 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904}
6905
6906/*
6907 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6908 * offset.
6909 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006912ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006915 win_T *save_curwin = curwin;
6916 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 long topline;
6918 long y;
6919 linenr_T old_linenr = curwin->w_cursor.lnum;
6920
6921 setpcmark();
6922
6923 /*
6924 * determine max topline
6925 */
6926 if (curwin->w_p_scb)
6927 {
6928 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006929 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 {
6931 if (wp->w_p_scb && wp->w_buffer)
6932 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006933 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 if (topline > y)
6935 topline = y;
6936 }
6937 }
6938 if (topline < 1)
6939 topline = 1;
6940 }
6941 else
6942 {
6943 topline = 1;
6944 }
6945
6946
6947 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006948 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006950 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 {
6952 if (curwin->w_p_scb)
6953 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006954 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 y = topline - curwin->w_topline;
6956 if (y > 0)
6957 scrollup(y, TRUE);
6958 else
6959 scrolldown(-y, TRUE);
6960 curwin->w_scbind_pos = topline;
6961 redraw_later(VALID);
6962 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 }
6965 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006966 curwin = save_curwin;
6967 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 if (curwin->w_p_scb)
6969 {
6970 did_syncbind = TRUE;
6971 checkpcmark();
6972 if (old_linenr != curwin->w_cursor.lnum)
6973 {
6974 char_u ctrl_o[2];
6975
6976 ctrl_o[0] = Ctrl_O;
6977 ctrl_o[1] = 0;
6978 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6979 }
6980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981}
6982
6983
6984 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006985ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006987 int i;
6988 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6989 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006991 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 do_bang(1, eap, FALSE, FALSE, TRUE);
6993 else
6994 {
6995 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6996 return;
6997
6998#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006999 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 {
7001 char_u *browseFile;
7002
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007003 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 NULL, NULL, NULL, curbuf);
7005 if (browseFile != NULL)
7006 {
7007 i = readfile(browseFile, NULL,
7008 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7009 vim_free(browseFile);
7010 }
7011 else
7012 i = OK;
7013 }
7014 else
7015#endif
7016 if (*eap->arg == NUL)
7017 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007018 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 return;
7020 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7021 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7022 }
7023 else
7024 {
7025 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7026 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7027 i = readfile(eap->arg, NULL,
7028 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7029
7030 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007031 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007033#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 if (!aborting())
7035#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007036 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 }
7038 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007039 {
7040 if (empty && exmode_active)
7041 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007042 // Delete the empty line that remains. Historically ex does
7043 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007044 if (eap->line2 == 0)
7045 lnum = curbuf->b_ml.ml_line_count;
7046 else
7047 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007048 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007049 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007050 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007051 if (curwin->w_cursor.lnum > 1
7052 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007053 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007054 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007055 }
7056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 }
7060}
7061
Bram Moolenaarea408852005-06-25 22:49:46 +00007062static char_u *prev_dir = NULL;
7063
7064#if defined(EXITFREE) || defined(PROTO)
7065 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007066free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007067{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007068 VIM_CLEAR(prev_dir);
7069 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007070}
7071#endif
7072
Bram Moolenaarf4258302013-06-02 18:20:17 +02007073/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02007074 * Get the previous directory for the given chdir scope.
7075 */
7076 static char_u *
7077get_prevdir(cdscope_T scope)
7078{
7079 if (scope == CDSCOPE_WINDOW)
7080 return curwin->w_prevdir;
7081 else if (scope == CDSCOPE_TABPAGE)
7082 return curtab->tp_prevdir;
7083 return prev_dir;
7084}
7085
7086/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02007087 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007088 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7089 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007090 */
7091 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007092post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007093{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007094 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007095 // Clear tab local directory for both :cd and :tcd
7096 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007097 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007098 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007099 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007100 char_u *pdir = get_prevdir(scope);
7101
7102 // If still in the global directory, need to remember current
7103 // directory as the global directory.
7104 if (globaldir == NULL && pdir != NULL)
7105 globaldir = vim_strsave(pdir);
7106
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007107 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007108 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007109 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007110 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007111 curtab->tp_localdir = vim_strsave(NameBuff);
7112 else
7113 curwin->w_localdir = vim_strsave(NameBuff);
7114 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007115 }
7116 else
7117 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007118 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007119 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007120 }
7121
7122 shorten_fnames(TRUE);
7123}
7124
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007125/*
7126 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007127 * chdir() function.
7128 * scope == CDSCOPE_WINDOW: changes the window-local directory
7129 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7130 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007131 * Returns TRUE if the directory is successfully changed.
7132 */
7133 int
7134changedir_func(
7135 char_u *new_dir,
7136 int forceit,
7137 cdscope_T scope)
7138{
7139 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02007140 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007141 int dir_differs;
7142 int retval = FALSE;
7143
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007144 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007145 return FALSE;
7146
7147 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7148 {
7149 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7150 return FALSE;
7151 }
7152
7153 // ":cd -": Change to previous directory
7154 if (STRCMP(new_dir, "-") == 0)
7155 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007156 pdir = get_prevdir(scope);
7157 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007158 {
7159 emsg(_("E186: No previous directory"));
7160 return FALSE;
7161 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007162 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007163 }
7164
Bram Moolenaar002bc792020-06-05 22:33:42 +02007165 // Free the previous directory
7166 tofree = get_prevdir(scope);
7167
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007168 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007169 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007170 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007171 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007172 pdir = NULL;
7173 if (scope == CDSCOPE_WINDOW)
7174 curwin->w_prevdir = pdir;
7175 else if (scope == CDSCOPE_TABPAGE)
7176 curtab->tp_prevdir = pdir;
7177 else
7178 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007179
7180#if defined(UNIX) || defined(VMS)
7181 // for UNIX ":cd" means: go to home directory
7182 if (*new_dir == NUL)
7183 {
7184 // use NameBuff for home directory name
7185# ifdef VMS
7186 char_u *p;
7187
7188 p = mch_getenv((char_u *)"SYS$LOGIN");
7189 if (p == NULL || *p == NUL) // empty is the same as not set
7190 NameBuff[0] = NUL;
7191 else
7192 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7193# else
7194 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7195# endif
7196 new_dir = NameBuff;
7197 }
7198#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02007199 dir_differs = new_dir == NULL || pdir == NULL
7200 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007201 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7202 emsg(_(e_failed));
7203 else
7204 {
7205 char_u *acmd_fname;
7206
7207 post_chdir(scope);
7208
7209 if (dir_differs)
7210 {
7211 if (scope == CDSCOPE_WINDOW)
7212 acmd_fname = (char_u *)"window";
7213 else if (scope == CDSCOPE_TABPAGE)
7214 acmd_fname = (char_u *)"tabpage";
7215 else
7216 acmd_fname = (char_u *)"global";
7217 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7218 curbuf);
7219 }
7220 retval = TRUE;
7221 }
7222 vim_free(tofree);
7223
7224 return retval;
7225}
Bram Moolenaarea408852005-06-25 22:49:46 +00007226
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007228 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007230 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007231ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007233 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234
7235 new_dir = eap->arg;
7236#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007237 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 if (*new_dir == NUL)
7239 ex_pwd(NULL);
7240 else
7241#endif
7242 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007243 cdscope_T scope = CDSCOPE_GLOBAL;
7244
7245 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7246 scope = CDSCOPE_WINDOW;
7247 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7248 scope = CDSCOPE_TABPAGE;
7249
7250 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007251 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007252 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007253 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 ex_pwd(eap);
7255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 }
7257}
7258
7259/*
7260 * ":pwd".
7261 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007263ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264{
7265 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7266 {
7267#ifdef BACKSLASH_IN_FILENAME
7268 slash_adjust(NameBuff);
7269#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007270 if (p_verbose > 0)
7271 {
7272 char *context = "global";
7273
7274 if (curwin->w_localdir != NULL)
7275 context = "window";
7276 else if (curtab->tp_localdir != NULL)
7277 context = "tabpage";
7278 smsg("[%s] %s", context, (char *)NameBuff);
7279 }
7280 else
7281 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 }
7283 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007284 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285}
7286
7287/*
7288 * ":=".
7289 */
7290 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007291ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007293 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007294 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295}
7296
7297 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007298ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007300 int n;
7301 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302
7303 if (cursor_valid())
7304 {
7305 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7306 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007307 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007309
7310 len = eap->line2;
7311 switch (*eap->arg)
7312 {
7313 case 'm': break;
7314 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007315 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007316 }
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007317
7318 // Hide the cursor if invoked with !
7319 do_sleep(len, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320}
7321
7322/*
7323 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007324 * Hide the cursor if "hide_cursor" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 */
7326 void
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007327do_sleep(long msec, int hide_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328{
Bram Moolenaar52953192019-08-16 10:27:13 +02007329 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007330 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007331# ifdef ELAPSED_FUNC
7332 elapsed_T start_tv;
7333
7334 // Remember at what time we started, so that we know how much longer we
7335 // should wait after waiting for a bit.
7336 ELAPSED_INIT(start_tv);
7337# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01007339 if (hide_cursor)
7340 cursor_off();
7341 else
7342 cursor_on();
7343
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007344 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007345 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007347 wait_now = msec - done > 1000L ? 1000L : msec - done;
7348#ifdef FEAT_TIMERS
7349 {
7350 long due_time = check_due_timer();
7351
7352 if (due_time > 0 && due_time < wait_now)
7353 wait_now = due_time;
7354 }
7355#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007356#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007357 if (has_any_channel() && wait_now > 20L)
7358 wait_now = 20L;
7359#endif
7360#ifdef FEAT_SOUND
7361 if (has_any_sound_callback() && wait_now > 20L)
7362 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007363#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007364 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007365
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007366#ifdef FEAT_JOB_CHANNEL
7367 if (has_any_channel())
7368 ui_breakcheck_force(TRUE);
7369 else
7370#endif
7371 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007372#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007373 // Process the netbeans and clientserver messages that may have been
7374 // received in the call to ui_breakcheck() when the GUI is in use. This
7375 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007376 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007377#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007378
7379# ifdef ELAPSED_FUNC
7380 // actual time passed
7381 done = ELAPSED_FUNC(start_tv);
7382# else
7383 // guestimate time passed (will actually be more)
7384 done += wait_now;
7385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007387
7388 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7389 // input buffer, otherwise a following call to input() fails.
7390 if (got_int)
7391 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392}
7393
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394/*
7395 * ":winsize" command (obsolete).
7396 */
7397 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007398ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399{
7400 int w, h;
7401 char_u *arg = eap->arg;
7402 char_u *p;
7403
Bram Moolenaarf5a51162021-02-06 12:58:18 +01007404 if (!isdigit(*arg))
7405 {
7406 semsg(_(e_invarg2), arg);
7407 return;
7408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 w = getdigits(&arg);
7410 arg = skipwhite(arg);
7411 p = arg;
7412 h = getdigits(&arg);
7413 if (*p != NUL && *arg == NUL)
7414 set_shellsize(w, h, TRUE);
7415 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007416 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417}
7418
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007420ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421{
7422 int xchar = NUL;
7423 char_u *p;
7424
7425 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7426 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007427 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 if (eap->arg[1] == NUL)
7429 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007430 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 return;
7432 }
7433 xchar = eap->arg[1];
7434 p = eap->arg + 2;
7435 }
7436 else
7437 p = eap->arg + 1;
7438
7439 eap->nextcmd = check_nextcmd(p);
7440 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007441 if (*p != NUL && *p != (
7442#ifdef FEAT_EVAL
7443 in_vim9script() ? '#' :
7444#endif
7445 '"')
7446 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007447 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007448 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007450 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007451 postponed_split_flags = cmdmod.cmod_split;
7452 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7454 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007455 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 }
7457}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458
Bram Moolenaar843ee412004-06-30 16:16:41 +00007459#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460/*
7461 * ":winpos".
7462 */
7463 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007464ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465{
7466 int x, y;
7467 char_u *arg = eap->arg;
7468 char_u *p;
7469
7470 if (*arg == NUL)
7471 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007472# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007473# ifdef VIMDLL
7474 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7475 mch_get_winpos(&x, &y) != FAIL)
7476# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007478# else
7479 if (mch_get_winpos(&x, &y) != FAIL)
7480# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 {
7482 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007483 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 }
7485 else
7486# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007487 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 }
7489 else
7490 {
7491 x = getdigits(&arg);
7492 arg = skipwhite(arg);
7493 p = arg;
7494 y = getdigits(&arg);
7495 if (*p == NUL || *arg != NUL)
7496 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007497 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 return;
7499 }
7500# ifdef FEAT_GUI
7501 if (gui.in_use)
7502 gui_mch_set_winpos(x, y);
7503 else if (gui.starting)
7504 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007505 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 gui_win_x = x;
7507 gui_win_y = y;
7508 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007509# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 else
7511# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007512# endif
7513# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007514 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515# endif
7516# ifdef HAVE_TGETENT
7517 if (*T_CWP)
7518 term_set_winpos(x, y);
7519# endif
7520 }
7521}
7522#endif
7523
7524/*
7525 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7526 */
7527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007528ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529{
7530 oparg_T oa;
7531
7532 clear_oparg(&oa);
7533 oa.regname = eap->regname;
7534 oa.start.lnum = eap->line1;
7535 oa.end.lnum = eap->line2;
7536 oa.line_count = eap->line2 - eap->line1 + 1;
7537 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007539 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 {
7541 setpcmark();
7542 curwin->w_cursor.lnum = eap->line1;
7543 beginline(BL_SOL | BL_FIX);
7544 }
7545
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007546 if (VIsual_active)
7547 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007548
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 switch (eap->cmdidx)
7550 {
7551 case CMD_delete:
7552 oa.op_type = OP_DELETE;
7553 op_delete(&oa);
7554 break;
7555
7556 case CMD_yank:
7557 oa.op_type = OP_YANK;
7558 (void)op_yank(&oa, FALSE, TRUE);
7559 break;
7560
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007561 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007562 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007564 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7565#else
7566 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007568 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 oa.op_type = OP_RSHIFT;
7570 else
7571 oa.op_type = OP_LSHIFT;
7572 op_shift(&oa, FALSE, eap->amount);
7573 break;
7574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007576 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577}
7578
7579/*
7580 * ":put".
7581 */
7582 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007583ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007585 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 if (eap->line2 == 0)
7587 {
7588 eap->line2 = 1;
7589 eap->forceit = TRUE;
7590 }
7591 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007592 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007593 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594}
7595
7596/*
7597 * Handle ":copy" and ":move".
7598 */
7599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007600ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601{
7602 long n;
7603
Bram Moolenaar491799b2020-08-01 19:23:43 +02007604#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007605 if (not_in_vim9(eap) == FAIL)
7606 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007607#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007608 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007609 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 {
7611 eap->nextcmd = NULL;
7612 return;
7613 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007614 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615
7616 /*
7617 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7618 */
7619 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7620 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007621 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 return;
7623 }
7624
7625 if (eap->cmdidx == CMD_move)
7626 {
7627 if (do_move(eap->line1, eap->line2, n) == FAIL)
7628 return;
7629 }
7630 else
7631 ex_copy(eap->line1, eap->line2, n);
7632 u_clearline();
7633 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007634 ex_may_print(eap);
7635}
7636
7637/*
7638 * Print the current line if flags were given to the Ex command.
7639 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007640 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007641ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007642{
7643 if (eap->flags != 0)
7644 {
7645 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7646 (eap->flags & EXFLAG_LIST));
7647 ex_no_reprint = TRUE;
7648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649}
7650
7651/*
7652 * ":smagic" and ":snomagic".
7653 */
7654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007655ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007657 optmagic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007659 magic_overruled = eap->cmdidx == CMD_smagic
7660 ? OPTION_MAGIC_ON : OPTION_MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02007661 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007662 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663}
7664
7665/*
7666 * ":join".
7667 */
7668 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007669ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670{
7671 curwin->w_cursor.lnum = eap->line1;
7672 if (eap->line1 == eap->line2)
7673 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007674 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 return;
7676 if (eap->line2 == curbuf->b_ml.ml_line_count)
7677 {
7678 beep_flush();
7679 return;
7680 }
7681 ++eap->line2;
7682 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007683 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007685 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686}
7687
7688/*
7689 * ":[addr]@r" or ":[addr]*r": execute register
7690 */
7691 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007692ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693{
7694 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007695 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696
7697 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007698 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699
7700#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007701 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702#endif
7703
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007704 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 c = *eap->arg;
7706 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7707 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007708 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007709 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7710 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007711 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 else
7715 {
7716 int save_efr = exec_from_reg;
7717
7718 exec_from_reg = TRUE;
7719
7720 /*
7721 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007722 * Continue until the stuff buffer is empty and all added characters
7723 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007725 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7727
7728 exec_from_reg = save_efr;
7729 }
7730}
7731
7732/*
7733 * ":!".
7734 */
7735 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007736ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737{
7738 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7739}
7740
7741/*
7742 * ":undo".
7743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007745ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007747 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007748 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007749 else
7750 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751}
7752
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007753#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007754 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007755ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007756{
7757 char_u hash[UNDO_HASH_SIZE];
7758
7759 u_compute_hash(hash);
7760 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7761}
7762
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007763 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007764ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007765{
7766 char_u hash[UNDO_HASH_SIZE];
7767
7768 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007769 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007770}
7771#endif
7772
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773/*
7774 * ":redo".
7775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007777ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778{
7779 u_redo(1);
7780}
7781
7782/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007783 * ":earlier" and ":later".
7784 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007785 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007786ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007787{
7788 long count = 0;
7789 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007790 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007791 char_u *p = eap->arg;
7792
7793 if (*p == NUL)
7794 count = 1;
7795 else if (isdigit(*p))
7796 {
7797 count = getdigits(&p);
7798 switch (*p)
7799 {
7800 case 's': ++p; sec = TRUE; break;
7801 case 'm': ++p; sec = TRUE; count *= 60; break;
7802 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007803 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7804 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007805 }
7806 }
7807
7808 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007809 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007810 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007811 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7812 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007813}
7814
7815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 * ":redir": start/stop redirection.
7817 */
7818 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007819ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820{
7821 char *mode;
7822 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007823 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824
Bram Moolenaarba768492016-07-08 15:32:54 +02007825#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007826 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007827 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007828 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007829 return;
7830 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007831#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007832
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 if (STRICMP(eap->arg, "END") == 0)
7834 close_redir();
7835 else
7836 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007837 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007839 ++arg;
7840 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007842 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 mode = "a";
7844 }
7845 else
7846 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007847 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848
7849 close_redir();
7850
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007851 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007852 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 if (fname == NULL)
7854 return;
7855#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007856 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 {
7858 char_u *browseFile;
7859
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007860 browseFile = do_browse(BROWSE_SAVE,
7861 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007862 fname, NULL, NULL,
7863 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007865 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 vim_free(fname);
7867 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007868 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 }
7870#endif
7871
7872 redir_fd = open_exfile(fname, eap->forceit, mode);
7873 vim_free(fname);
7874 }
7875#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007876 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007878 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007880 ++arg;
7881 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007883 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007884 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007886 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007888 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007889 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007890 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007891 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007893 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007894 if (*arg == '>')
7895 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007896 // Make register empty when not using @A-@Z and the
7897 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007898 if (*arg == NUL && !isupper(redir_reg))
7899 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007901 }
7902 if (*arg != NUL)
7903 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007904 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007905 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007906 }
7907 }
7908 else if (*arg == '=' && arg[1] == '>')
7909 {
7910 int append;
7911
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007912 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007913 close_redir();
7914 arg += 2;
7915
7916 if (*arg == '>')
7917 {
7918 ++arg;
7919 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 }
7921 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007922 append = FALSE;
7923
7924 if (var_redir_start(skipwhite(arg), append) == OK)
7925 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 }
7927#endif
7928
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007929 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007932 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007934
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007935 // Make sure redirection is not off. Can happen for cmdline completion
7936 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007937 if (redir_fd != NULL
7938#ifdef FEAT_EVAL
7939 || redir_reg || redir_vname
7940#endif
7941 )
7942 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943}
7944
7945/*
7946 * ":redraw": force redraw
7947 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007948 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007949ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950{
7951 int r = RedrawingDisabled;
7952 int p = p_lz;
7953
7954 RedrawingDisabled = 0;
7955 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007956 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007958 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959#ifdef FEAT_TITLE
7960 if (need_maketitle)
7961 maketitle();
7962#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007963#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7964# ifdef VIMDLL
7965 if (!gui.in_use)
7966# endif
7967 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 RedrawingDisabled = r;
7970 p_lz = p;
7971
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007972 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 msg_didout = FALSE;
7974 msg_col = 0;
7975
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007976 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007977 need_wait_return = FALSE;
7978
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 out_flush();
7980}
7981
7982/*
7983 * ":redrawstatus": force redraw of status line(s)
7984 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007986ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 int r = RedrawingDisabled;
7989 int p = p_lz;
7990
7991 RedrawingDisabled = 0;
7992 p_lz = FALSE;
7993 if (eap->forceit)
7994 status_redraw_all();
7995 else
7996 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007997 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 RedrawingDisabled = r;
7999 p_lz = p;
8000 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001}
8002
Bram Moolenaare12bab32019-01-08 22:02:56 +01008003/*
8004 * ":redrawtabline": force redraw of the tabline
8005 */
8006 static void
8007ex_redrawtabline(exarg_T *eap UNUSED)
8008{
8009 int r = RedrawingDisabled;
8010 int p = p_lz;
8011
8012 RedrawingDisabled = 0;
8013 p_lz = FALSE;
8014
8015 draw_tabline();
8016
8017 RedrawingDisabled = r;
8018 p_lz = p;
8019 out_flush();
8020}
8021
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008023close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024{
8025 if (redir_fd != NULL)
8026 {
8027 fclose(redir_fd);
8028 redir_fd = NULL;
8029 }
8030#ifdef FEAT_EVAL
8031 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008032 if (redir_vname)
8033 {
8034 var_redir_stop();
8035 redir_vname = 0;
8036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037#endif
8038}
8039
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008040#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008041 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008042vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008043{
8044 if (vim_mkdir(name, prot) != 0)
8045 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008046 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008047 return FAIL;
8048 }
8049 return OK;
8050}
8051#endif
8052
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053/*
8054 * Open a file for writing for an Ex command, with some checks.
8055 * Return file descriptor, or NULL on failure.
8056 */
8057 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008058open_exfile(
8059 char_u *fname,
8060 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008061 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062{
8063 FILE *fd;
8064
8065#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008066 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 if (mch_isdir(fname))
8068 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008069 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 return NULL;
8071 }
8072#endif
8073 if (!forceit && *mode != 'a' && vim_fexists(fname))
8074 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008075 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 return NULL;
8077 }
8078
8079 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008080 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081
8082 return fd;
8083}
8084
8085/*
8086 * ":mark" and ":k".
8087 */
8088 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008089ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090{
8091 pos_T pos;
8092
Bram Moolenaar10b94212021-02-19 21:42:57 +01008093#ifdef FEAT_EVAL
8094 if (not_in_vim9(eap) == FAIL)
8095 return;
8096#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008097 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008098 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008099 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02008100 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 else
8102 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008103 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 curwin->w_cursor.lnum = eap->line2;
8105 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008106 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008107 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008108 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 }
8110}
8111
8112/*
8113 * Update w_topline, w_leftcol and the cursor position.
8114 */
8115 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008116update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008118 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 update_topline();
8120 if (!curwin->w_p_wrap)
8121 validate_cursor();
8122 update_curswant();
8123}
8124
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008126 * Save the current State and go to Normal mode.
8127 * Return TRUE if the typeahead could be saved.
8128 */
8129 int
8130save_current_state(save_state_T *sst)
8131{
8132 sst->save_msg_scroll = msg_scroll;
8133 sst->save_restart_edit = restart_edit;
8134 sst->save_msg_didout = msg_didout;
8135 sst->save_State = State;
8136 sst->save_insertmode = p_im;
8137 sst->save_finish_op = finish_op;
8138 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008139 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008140
Bram Moolenaar4324d872020-12-01 20:12:24 +01008141 msg_scroll = FALSE; // no msg scrolling in Normal mode
8142 restart_edit = 0; // don't go to Insert mode
8143 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008144
Bram Moolenaara452b802020-12-01 21:47:59 +01008145 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008146 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008147
8148 /*
8149 * Save the current typeahead. This is required to allow using ":normal"
8150 * from an event handler and makes sure we don't hang when the argument
8151 * ends with half a command.
8152 */
8153 save_typeahead(&sst->tabuf);
8154 return sst->tabuf.typebuf_valid;
8155}
8156
8157 void
8158restore_current_state(save_state_T *sst)
8159{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008160 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008161 restore_typeahead(&sst->tabuf);
8162
8163 msg_scroll = sst->save_msg_scroll;
8164 restart_edit = sst->save_restart_edit;
8165 p_im = sst->save_insertmode;
8166 finish_op = sst->save_finish_op;
8167 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008168 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008169 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008170 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008171
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008172 // Restore the state (needed when called from a function executed for
8173 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008174 State = sst->save_State;
8175#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008176 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008177#endif
8178}
8179
8180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 * ":normal[!] {commands}": Execute normal mode commands.
8182 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008183 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008184ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008186 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 char_u *arg = NULL;
8188 int l;
8189 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008191 if (ex_normal_lock > 0)
8192 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008193 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008194 return;
8195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 if (ex_normal_busy >= p_mmd)
8197 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008198 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 return;
8200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 /*
8203 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8204 * this for the K_SPECIAL leading byte, otherwise special keys will not
8205 * work.
8206 */
8207 if (has_mbyte)
8208 {
8209 int len = 0;
8210
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008211 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 for (p = eap->arg; *p != NUL; ++p)
8213 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008214#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008215 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008217#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008218 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008219 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008220#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 )
8224 len += 2;
8225 }
8226 if (len > 0)
8227 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008228 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 if (arg != NULL)
8230 {
8231 len = 0;
8232 for (p = eap->arg; *p != NUL; ++p)
8233 {
8234 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008235#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 if (*p == CSI)
8237 {
8238 arg[len++] = KS_EXTRA;
8239 arg[len++] = (int)KE_CSI;
8240 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008241#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008242 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {
8244 arg[len++] = *++p;
8245 if (*p == K_SPECIAL)
8246 {
8247 arg[len++] = KS_SPECIAL;
8248 arg[len++] = KE_FILLER;
8249 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008250#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 else if (*p == CSI)
8252 {
8253 arg[len++] = KS_EXTRA;
8254 arg[len++] = (int)KE_CSI;
8255 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 }
8258 arg[len] = NUL;
8259 }
8260 }
8261 }
8262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008264 ++ex_normal_busy;
8265 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 {
8267 /*
8268 * Repeat the :normal command for each line in the range. When no
8269 * range given, execute it just once, without positioning the cursor
8270 * first.
8271 */
8272 do
8273 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 if (eap->addr_count != 0)
8275 {
8276 curwin->w_cursor.lnum = eap->line1++;
8277 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008278 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 }
8280
Bram Moolenaar13505972019-01-24 15:04:48 +01008281 exec_normal_cmd(arg != NULL
8282 ? arg
8283 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 }
8285 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8286 }
8287
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008288 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 update_topline_cursor();
8290
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008291 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008293 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008294#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008295 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008296#endif
8297
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299}
8300
8301/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008302 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 */
8304 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008305ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008307 if (eap->forceit)
8308 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008309 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008310 if (!curwin->w_cursor.lnum)
8311 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008312 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008313 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008314#ifdef FEAT_TERMINAL
8315 // Ignore this when running in an active terminal.
8316 if (term_job_running(curbuf->b_term))
8317 return;
8318#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008319
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008320 // Ignore the command when already in Insert mode. Inserting an
8321 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008322 if (State & INSERT)
8323 return;
8324
Bram Moolenaar64969662005-12-14 21:59:55 +00008325 if (eap->cmdidx == CMD_startinsert)
8326 restart_edit = 'a';
8327 else if (eap->cmdidx == CMD_startreplace)
8328 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008330 restart_edit = 'V';
8331
8332 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008334 if (eap->cmdidx == CMD_startinsert)
8335 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008336 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008338
8339 if (VIsual_active)
8340 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341}
8342
8343/*
8344 * ":stopinsert"
8345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008347ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348{
8349 restart_edit = 0;
8350 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008351 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008354/*
8355 * Execute normal mode command "cmd".
8356 * "remap" can be REMAP_NONE or REMAP_YES.
8357 */
8358 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008359exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008360{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008361 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008362 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008363 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008364}
Bram Moolenaar25281632016-01-21 23:32:32 +01008365
Bram Moolenaar25281632016-01-21 23:32:32 +01008366/*
8367 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008368 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008369 */
8370 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008371exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008372{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008373 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008374 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008375
Bram Moolenaar905dd902019-04-07 14:21:47 +02008376 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8377 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008378 clear_oparg(&oa);
8379 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008380 while ((!stuff_empty()
8381 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008382 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008383 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008384 {
8385 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008386#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008387 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008388 && oa.op_type == OP_NOP && oa.regname == NUL
8389 && !VIsual_active)
8390 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008391 // If terminal_loop() returns OK we got a key that is handled
8392 // in Normal model. With FAIL we first need to position the
8393 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008394 if (terminal_loop(TRUE) == OK)
8395 normal_cmd(&oa, TRUE);
8396 }
8397 else
8398#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008399 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008400 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008401 }
8402}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008403
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404#ifdef FEAT_FIND_ID
8405 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008406ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407{
8408 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8409 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8410 (linenr_T)1, (linenr_T)MAXLNUM);
8411}
8412
Bram Moolenaar4033c552017-09-16 20:54:51 +02008413#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414/*
8415 * ":psearch"
8416 */
8417 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008418ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419{
8420 g_do_tagpreview = p_pvh;
8421 ex_findpat(eap);
8422 g_do_tagpreview = 0;
8423}
8424#endif
8425
8426 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008427ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428{
8429 int whole = TRUE;
8430 long n;
8431 char_u *p;
8432 int action;
8433
8434 switch (cmdnames[eap->cmdidx].cmd_name[2])
8435 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008436 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8438 action = ACTION_GOTO;
8439 else
8440 action = ACTION_SHOW;
8441 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008442 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 action = ACTION_SHOW_ALL;
8444 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008445 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 action = ACTION_GOTO;
8447 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008448 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 action = ACTION_SPLIT;
8450 break;
8451 }
8452
8453 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008454 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 {
8456 n = getdigits(&eap->arg);
8457 eap->arg = skipwhite(eap->arg);
8458 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008459 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 {
8461 whole = FALSE;
8462 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008463 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 if (*p)
8465 {
8466 *p++ = NUL;
8467 p = skipwhite(p);
8468
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008469 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008470 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008471 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 else
8473 eap->nextcmd = check_nextcmd(p);
8474 }
8475 }
8476 if (!eap->skip)
8477 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8478 whole, !eap->forceit,
8479 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8480 n, action, eap->line1, eap->line2);
8481}
8482#endif
8483
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484
Bram Moolenaar4033c552017-09-16 20:54:51 +02008485#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486/*
8487 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8488 */
8489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008490ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008492 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8494}
8495
8496/*
8497 * ":pedit"
8498 */
8499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008500ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501{
8502 win_T *curwin_save = curwin;
8503
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008504 if (ERROR_IF_ANY_POPUP_WINDOW)
8505 return;
8506
Bram Moolenaar026587b2019-08-17 15:08:00 +02008507 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008509 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008510
Bram Moolenaar026587b2019-08-17 15:08:00 +02008511 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008513
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 if (curwin != curwin_save && win_valid(curwin_save))
8515 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008516 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 validate_cursor();
8518 redraw_later(VALID);
8519 win_enter(curwin_save, TRUE);
8520 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008521# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008522 else if (WIN_IS_POPUP(curwin))
8523 {
8524 // can't keep focus in popup window
8525 win_enter(firstwin, TRUE);
8526 }
8527# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 g_do_tagpreview = 0;
8529}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531
8532/*
8533 * ":stag", ":stselect" and ":stjump".
8534 */
8535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008536ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537{
8538 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008539 postponed_split_flags = cmdmod.cmod_split;
8540 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8542 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008543 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545
8546/*
8547 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8548 */
8549 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008550ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551{
8552 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8553}
8554
8555 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008556ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557{
8558 int cmd;
8559
8560 switch (name[1])
8561 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008562 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008564 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008566 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008568 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008570 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008572 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008574 case 'f': // ":tfirst"
8575 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008577 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008579 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008581 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008583 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 return;
8585 }
8586#endif
8587 cmd = DT_TAG;
8588 break;
8589 }
8590
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008591 if (name[0] == 'l')
8592 {
8593#ifndef FEAT_QUICKFIX
8594 ex_ni(eap);
8595 return;
8596#else
8597 cmd = DT_LTAG;
8598#endif
8599 }
8600
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8602 eap->forceit, TRUE);
8603}
8604
8605/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008606 * Check "str" for starting with a special cmdline variable.
8607 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8608 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8609 */
8610 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008611find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008612{
8613 int len;
8614 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008615 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008616 "%",
8617#define SPEC_PERC 0
8618 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008619#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008620 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008621#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008622 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008623#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008624 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008625#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008626 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008627#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008628 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008629#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008630 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008631#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008632 "<stack>", // call stack
8633#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008634 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008635#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008636 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008637#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008638 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008639#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008640 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008641#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008642 "<SID>", // script ID: <SNR>123_
8643#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008644#ifdef FEAT_CLIENTSERVER
8645 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008646# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008647#endif
8648 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008649
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008650 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008651 {
8652 len = (int)STRLEN(spec_str[i]);
8653 if (STRNCMP(src, spec_str[i], len) == 0)
8654 {
8655 *usedlen = len;
8656 return i;
8657 }
8658 }
8659 return -1;
8660}
8661
8662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 * Evaluate cmdline variables.
8664 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008665 * change "%" to curbuf->b_ffname
8666 * "#" to curwin->w_alt_fnum
8667 * "%%" to curwin->w_alt_fnum in Vim9 script
8668 * "<cword>" to word under the cursor
8669 * "<cWORD>" to WORD under the cursor
8670 * "<cexpr>" to C-expression under the cursor
8671 * "<cfile>" to path name under the cursor
8672 * "<sfile>" to sourced file name
8673 * "<stack>" to call stack
8674 * "<slnum>" to sourced file line number
8675 * "<afile>" to file name for autocommand
8676 * "<abuf>" to buffer number for autocommand
8677 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 *
8679 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8680 * "" for error without a message) and NULL is returned.
8681 * Returns an allocated string if a valid match was found.
8682 * Returns NULL if no match was found. "usedlen" then still contains the
8683 * number of characters to skip.
8684 */
8685 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008686eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008687 char_u *src, // pointer into commandline
8688 char_u *srcstart, // beginning of valid memory for src
8689 int *usedlen, // characters after src that are used
8690 linenr_T *lnump, // line number for :e command, or NULL
8691 char **errormsg, // pointer to error message
8692 int *escaped) // return value has escaped white space (can
8693 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694{
8695 int i;
8696 char_u *s;
8697 char_u *result;
8698 char_u *resultbuf = NULL;
8699 int resultlen;
8700 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008701 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008703 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706
8707 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008708 if (escaped != NULL)
8709 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710
8711 /*
8712 * Check if there is something to do.
8713 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008714 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008715 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 {
8717 *usedlen = 1;
8718 return NULL;
8719 }
8720
8721 /*
8722 * Skip when preceded with a backslash "\%" and "\#".
8723 * Note: In "\\%" the % is also not recognized!
8724 */
8725 if (src > srcstart && src[-1] == '\\')
8726 {
8727 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008728 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 return NULL;
8730 }
8731
8732 /*
8733 * word or WORD under cursor
8734 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008735 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8736 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008738 resultlen = find_ident_under_cursor(&result,
8739 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8740 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8741 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 if (resultlen == 0)
8743 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008744 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 return NULL;
8746 }
8747 }
8748
8749 /*
8750 * '#': Alternate file name
8751 * '%': Current file name
8752 * File name under the cursor
8753 * File name for autocommand
8754 * and following modifiers
8755 */
8756 else
8757 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008758 int off = 0;
8759
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 switch (spec_idx)
8761 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008762 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008763#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008764 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008767 // '%': current file
8768 if (curbuf->b_fname == NULL)
8769 {
8770 result = (char_u *)"";
8771 valid = 0; // Must have ":p:h" to be valid
8772 }
8773 else
8774 {
8775 result = curbuf->b_fname;
8776 tilde_file = STRCMP(result, "~") == 0;
8777 }
8778 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008780#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008781 // "%%" alternate file
8782 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008783#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01008784 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008785 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008786 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008788 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 result = arg_all();
8790 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008791 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008792 if (escaped != NULL)
8793 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 break;
8796 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008797 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008798 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008799 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008801 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008802 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008803 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008804 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008806 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008808 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00008809 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008810 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008811 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008812 return NULL;
8813 }
8814#ifdef FEAT_EVAL
8815 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8816 (long)i);
8817 if (result == NULL)
8818 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008819 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008820 return NULL;
8821 }
8822#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008823 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 }
8827 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008828 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008829 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
8830 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008831 buf = buflist_findnr(i);
8832 if (buf == NULL)
8833 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008834 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008835 return NULL;
8836 }
8837 if (lnump != NULL)
8838 *lnump = ECMD_LAST;
8839 if (buf->b_fname == NULL)
8840 {
8841 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008842 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008843 }
8844 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008845 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008846 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008847 tilde_file = STRCMP(result, "~") == 0;
8848 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 break;
8851
8852#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008853 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008854 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 if (result == NULL)
8856 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008857 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 return NULL;
8859 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008860 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 break;
8862#endif
8863
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008864 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008866 if (result != NULL && !autocmd_fname_full)
8867 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008868 // Still need to turn the fname into a full path. It is
8869 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008870 autocmd_fname_full = TRUE;
8871 result = FullName_save(autocmd_fname, FALSE);
8872 vim_free(autocmd_fname);
8873 autocmd_fname = result;
8874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 if (result == NULL)
8876 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008877 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 return NULL;
8879 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008880 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 break;
8882
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008883 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 if (autocmd_bufnr <= 0)
8885 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008886 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 return NULL;
8888 }
8889 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8890 result = strbuf;
8891 break;
8892
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008893 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 result = autocmd_match;
8895 if (result == NULL)
8896 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008897 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 return NULL;
8899 }
8900 break;
8901
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008902 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008903 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008904 result = estack_sfile(spec_idx == SPEC_SFILE
8905 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 if (result == NULL)
8907 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008908 *errormsg = spec_idx == SPEC_SFILE
8909 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8910 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 return NULL;
8912 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008913 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008915
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008916 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008917 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008918 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008919 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008920 return NULL;
8921 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008922 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008923 result = strbuf;
8924 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008925
8926#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008927 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008928 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008929 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008930 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008931 return NULL;
8932 }
8933 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008934 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008935 result = strbuf;
8936 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008937
Bram Moolenaar90944302020-08-01 20:45:11 +02008938 case SPEC_SID:
8939 if (current_sctx.sc_sid <= 0)
8940 {
8941 *errormsg = _(e_usingsid);
8942 return NULL;
8943 }
8944 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
8945 result = strbuf;
8946 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02008947#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02008948
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008949#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008950 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008951 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8952 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 result = strbuf;
8954 break;
8955#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008956
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008957 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008958 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008959 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 }
8961
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008962 resultlen = (int)STRLEN(result); // length of new string
8963 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 {
8965 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 resultlen = (int)(s - result);
8968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 else if (!skip_mod)
8970 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008971 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 &resultlen);
8973 if (result == NULL)
8974 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008975 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 return NULL;
8977 }
8978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 }
8980
8981 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8982 {
8983 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008984 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008985 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008987 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 result = NULL;
8989 }
8990 else
8991 result = vim_strnsave(result, resultlen);
8992 vim_free(resultbuf);
8993 return result;
8994}
8995
8996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 * Expand the <sfile> string in "arg".
8998 *
8999 * Returns an allocated string, or NULL for any error.
9000 */
9001 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009002expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009004 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 int len;
9006 char_u *result;
9007 char_u *newres;
9008 char_u *repl;
9009 int srclen;
9010 char_u *p;
9011
9012 result = vim_strsave(arg);
9013 if (result == NULL)
9014 return NULL;
9015
9016 for (p = result; *p; )
9017 {
9018 if (STRNCMP(p, "<sfile>", 7) != 0)
9019 ++p;
9020 else
9021 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009022 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00009023 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 if (errormsg != NULL)
9025 {
9026 if (*errormsg)
9027 emsg(errormsg);
9028 vim_free(result);
9029 return NULL;
9030 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009031 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 {
9033 p += srclen;
9034 continue;
9035 }
9036 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9037 newres = alloc(len);
9038 if (newres == NULL)
9039 {
9040 vim_free(repl);
9041 vim_free(result);
9042 return NULL;
9043 }
9044 mch_memmove(newres, result, (size_t)(p - result));
9045 STRCPY(newres + (p - result), repl);
9046 len = (int)STRLEN(newres);
9047 STRCAT(newres, p + srclen);
9048 vim_free(repl);
9049 vim_free(result);
9050 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009051 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 }
9053 }
9054
9055 return result;
9056}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009059/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009060 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009061 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009062 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009064dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 if (fname == NULL)
9067 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009068 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069}
9070#endif
9071
9072/*
9073 * ":behave {mswin,xterm}"
9074 */
9075 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009076ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077{
9078 if (STRCMP(eap->arg, "mswin") == 0)
9079 {
9080 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9081 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9082 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9083 set_option_value((char_u *)"keymodel", 0L,
9084 (char_u *)"startsel,stopsel", 0);
9085 }
9086 else if (STRCMP(eap->arg, "xterm") == 0)
9087 {
9088 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9089 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9090 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9091 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9092 }
9093 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009094 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095}
9096
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097static int filetype_detect = FALSE;
9098static int filetype_plugin = FALSE;
9099static int filetype_indent = FALSE;
9100
9101/*
9102 * ":filetype [plugin] [indent] {on,off,detect}"
9103 * on: Load the filetype.vim file to install autocommands for file types.
9104 * off: Load the ftoff.vim file to remove all autocommands for file types.
9105 * plugin on: load filetype.vim and ftplugin.vim
9106 * plugin off: load ftplugof.vim
9107 * indent on: load filetype.vim and indent.vim
9108 * indent off: load indoff.vim
9109 */
9110 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009111ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112{
9113 char_u *arg = eap->arg;
9114 int plugin = FALSE;
9115 int indent = FALSE;
9116
9117 if (*eap->arg == NUL)
9118 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009119 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009120 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 filetype_detect ? "ON" : "OFF",
9122 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9123 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9124 return;
9125 }
9126
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009127 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 for (;;)
9129 {
9130 if (STRNCMP(arg, "plugin", 6) == 0)
9131 {
9132 plugin = TRUE;
9133 arg = skipwhite(arg + 6);
9134 continue;
9135 }
9136 if (STRNCMP(arg, "indent", 6) == 0)
9137 {
9138 indent = TRUE;
9139 arg = skipwhite(arg + 6);
9140 continue;
9141 }
9142 break;
9143 }
9144 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9145 {
9146 if (*arg == 'o' || !filetype_detect)
9147 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009148 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 filetype_detect = TRUE;
9150 if (plugin)
9151 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009152 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 filetype_plugin = TRUE;
9154 }
9155 if (indent)
9156 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009157 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 filetype_indent = TRUE;
9159 }
9160 }
9161 if (*arg == 'd')
9162 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009163 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009164 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 }
9166 }
9167 else if (STRCMP(arg, "off") == 0)
9168 {
9169 if (plugin || indent)
9170 {
9171 if (plugin)
9172 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009173 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 filetype_plugin = FALSE;
9175 }
9176 if (indent)
9177 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009178 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 filetype_indent = FALSE;
9180 }
9181 }
9182 else
9183 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009184 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 filetype_detect = FALSE;
9186 }
9187 }
9188 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009189 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190}
9191
9192/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009193 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 */
9195 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009196ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197{
9198 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009199 {
9200 char_u *arg = eap->arg;
9201
9202 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9203 arg += 9;
9204
9205 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9206 if (arg != eap->arg)
9207 did_filetype = FALSE;
9208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009212ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213{
9214#ifdef FEAT_DIGRAPHS
9215 if (*eap->arg != NUL)
9216 putdigraph(eap->arg);
9217 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009218 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009220 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221#endif
9222}
9223
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009224#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9225 void
9226set_no_hlsearch(int flag)
9227{
9228 no_hlsearch = flag;
9229# ifdef FEAT_EVAL
9230 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9231# endif
9232}
9233
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234/*
9235 * ":nohlsearch"
9236 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009238ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009240 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009241 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243#endif
9244
9245#ifdef FEAT_CRYPT
9246/*
9247 * ":X": Get crypt key
9248 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009250ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009252 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009253 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254}
9255#endif
9256
9257#ifdef FEAT_FOLDING
9258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009259ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260{
9261 if (foldManualAllowed(TRUE))
9262 foldCreate(eap->line1, eap->line2);
9263}
9264
9265 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009266ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267{
9268 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9269 eap->forceit, FALSE);
9270}
9271
9272 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009273ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274{
9275 linenr_T lnum;
9276
Bram Moolenaar4facea32019-10-12 20:17:40 +02009277# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009278 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009279# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009280
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009281 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9283 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9284 ml_setmarked(lnum);
9285
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009286 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009288 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009289# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009290 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009291# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292}
9293#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009294
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009295#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009296/*
9297 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9298 * instead of a quickfix command.
9299 */
9300 int
9301is_loclist_cmd(int cmdidx)
9302{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009303 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009304 return FALSE;
9305 return cmdnames[cmdidx].cmd_name[0] == 'l';
9306}
9307#endif
9308
Bram Moolenaar4facea32019-10-12 20:17:40 +02009309#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009310 int
9311get_pressedreturn(void)
9312{
9313 return ex_pressedreturn;
9314}
9315
9316 void
9317set_pressedreturn(int val)
9318{
9319 ex_pressedreturn = val;
9320}
9321#endif